package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.entity.UserOrder;
import com.yonge.cooleshow.biz.dal.enums.OrderStatusEnum;
import com.yonge.cooleshow.biz.dal.service.UserOrderService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import com.yonge.cooleshow.biz.dal.entity.TenantAlbumPurchase;
import com.yonge.cooleshow.biz.dal.wrapper.TenantAlbumPurchaseWrapper;
import com.yonge.cooleshow.biz.dal.mapper.TenantAlbumPurchaseMapper;
import com.yonge.cooleshow.biz.dal.service.TenantAlbumPurchaseService;

import java.util.List;
import java.util.Map;

/**
 * 机构专辑采购
 * 2023-07-21 17:32:49
 */
@Slf4j
@Service
public class TenantAlbumPurchaseServiceImpl extends ServiceImpl<TenantAlbumPurchaseMapper, TenantAlbumPurchase> implements TenantAlbumPurchaseService {

    @Autowired
    private UserOrderService userOrderService;

    /**
     * 查询详情
     *
     * @param id 详情ID
     * @return TenantAlbumPurchase
     */
    @Override
    public TenantAlbumPurchase detail(Long id) {

        return baseMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param page  IPage<TenantAlbumPurchase>
     * @param query TenantAlbumPurchaseWrapper.TenantAlbumPurchaseQuery
     * @return IPage<TenantAlbumPurchase>
     */
    @Override
    public IPage<TenantAlbumPurchaseWrapper.TenantAlbumPurchase> selectPage(IPage<TenantAlbumPurchaseWrapper.TenantAlbumPurchase> page,
                                                                            TenantAlbumPurchaseWrapper.TenantAlbumPurchaseQuery query) {

        List<TenantAlbumPurchaseWrapper.TenantAlbumPurchase> selectPage = baseMapper.selectPage(page, query);

        if (CollectionUtils.isEmpty(selectPage)) {
            return page.setRecords(selectPage);
        }

        // 订单号集合
        List<String> orderNoList = selectPage.stream().map(TenantAlbumPurchaseWrapper.TenantAlbumPurchase::getOrderNo).collect(java.util.stream.Collectors.toList());

        Map<String, UserOrder> mapByOrderNos = userOrderService.getMapByOrderNos(orderNoList);

        // 设置状态
        selectPage.forEach(tenantAlbumPurchase -> {
            UserOrder userOrder = mapByOrderNos.get(tenantAlbumPurchase.getOrderNo());
            if (userOrder != null) {
                tenantAlbumPurchase.setPurchaseStatus(getPurchaseStatus(userOrder.getStatus()));
            } else {
                tenantAlbumPurchase.setPurchaseStatus(OrderStatusEnum.CLOSE.getCode());
            }
        });

        return page.setRecords(selectPage);
    }

    private String getPurchaseStatus(OrderStatusEnum status) {
        switch (status) {
            case WAIT_PAY:
            case PAYING:
                return OrderStatusEnum.WAIT_PAY.getCode();
            case PAID:
                return OrderStatusEnum.PAID.getCode();
            case CLOSE:
            case FAIL:
                return OrderStatusEnum.CLOSE.getCode();
        }
        return OrderStatusEnum.CLOSE.getCode();
    }

    /**
     * 添加
     *
     * @param tenantAlbumPurchase TenantAlbumPurchaseWrapper.TenantAlbumPurchase
     * @return Boolean
     */
    @Override
    public Boolean add(TenantAlbumPurchaseWrapper.TenantAlbumPurchase tenantAlbumPurchase) {

        return this.save(JSON.parseObject(tenantAlbumPurchase.jsonString(), TenantAlbumPurchase.class));
    }

    /**
     * 更新
     *
     * @param tenantAlbumPurchase TenantAlbumPurchaseWrapper.TenantAlbumPurchase
     * @return Boolean
     */
    @Override
    public Boolean update(TenantAlbumPurchaseWrapper.TenantAlbumPurchase tenantAlbumPurchase) {

        return this.updateById(JSON.parseObject(tenantAlbumPurchase.jsonString(), TenantAlbumPurchase.class));
    }

    @Override
    public TenantAlbumPurchase getByOrderNo(String orderNo) {
        return this.lambdaQuery()
                .eq(TenantAlbumPurchase::getOrderNo, orderNo)
                .last("limit 1")
                .one();

    }
}