package co.yixiang.yshop.module.distributor.service.distributororder;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.distributor.controller.admin.distributororder.vo.DistributorOrderDetailsRespVO;
import co.yixiang.yshop.module.distributor.controller.admin.distributororder.vo.DistributorOrderExportReqVO;
import co.yixiang.yshop.module.distributor.controller.admin.distributororder.vo.DistributorOrderPageReqVO;
import co.yixiang.yshop.module.distributor.controller.app.distributor.vo.AppDistributorOrderPageReqVO;
import co.yixiang.yshop.module.distributor.controller.app.distributororder.vo.AppDistributorOrderWagesRespVO;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributor.DistributorDO;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributororder.DistributorOrderDO;
import co.yixiang.yshop.module.distributor.enums.DistributorOrderStatusEnum;
import co.yixiang.yshop.module.distributor.service.distributor.DistributorService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Service
@Validated
public class DistributorOrderFacadeImpl implements DistributorOrderFacade {

    @Resource
    private DistributorOrderService distributorOrderService;

    @Resource
    private DistributorService distributorService;

    @Override
    public AppDistributorOrderWagesRespVO getAppWages(LocalDateTime[] createTime) {
        AppDistributorOrderWagesRespVO respVO = new AppDistributorOrderWagesRespVO();
        // 获取分销商
        Long userId = getLoginUserId();
        DistributorDO distributorDO = distributorService.getDistributorByUserId(userId);

        // 已入账p1
        DistributorOrderDO sumP1 = distributorOrderService.sumFirstAmount(distributorDO.getId(), createTime, DistributorOrderStatusEnum.COMPLETE.getStatus());
        // 已入账p2
        DistributorOrderDO sumP2 = distributorOrderService.sumSecondAmount(distributorDO.getId(), createTime, DistributorOrderStatusEnum.COMPLETE.getStatus());
        if (sumP1 == null) {
            sumP1 = new DistributorOrderDO();
            sumP1.setFirstAmount(BigDecimal.ZERO);
        }
        if (sumP2 == null) {
            sumP2 = new DistributorOrderDO();
            sumP2.setSecondAmount(BigDecimal.ZERO);
        }

        // 未入账p1
        DistributorOrderDO waitSumP1 = distributorOrderService.sumFirstAmount(distributorDO.getId(), createTime, DistributorOrderStatusEnum.WAIT.getStatus());
        // 未入账p2
        DistributorOrderDO waitSumP2 = distributorOrderService.sumSecondAmount(distributorDO.getId(), createTime, DistributorOrderStatusEnum.WAIT.getStatus());
        if (waitSumP1 == null) {
            waitSumP1 = new DistributorOrderDO();
            waitSumP1.setFirstAmount(BigDecimal.ZERO);
        }
        if (waitSumP2 == null) {
            waitSumP2 = new DistributorOrderDO();
            waitSumP2.setSecondAmount(BigDecimal.ZERO);
        }

        DistributorOrderExportReqVO listReqVO = new DistributorOrderExportReqVO();
        listReqVO.setSuperiorIdP1(distributorDO.getId());
        listReqVO.setSuperiorIdP2(distributorDO.getId());
        listReqVO.setCreateTime(createTime);
        List<DistributorOrderDO> orderList = distributorOrderService.getOrderList(listReqVO);

        respVO.setAmount(sumP1.getFirstAmount().add(sumP2.getSecondAmount()));
        respVO.setRefuseAmount(waitSumP1.getFirstAmount().add(waitSumP2.getSecondAmount()));
        respVO.setOrderCount(orderList.size());
        return respVO;
    }

    @Override
    public PageResult<DistributorOrderDO> getAppPage(AppDistributorOrderPageReqVO reqVO) {
        Long userId = getLoginUserId();
        DistributorDO distributorDO = distributorService.getDistributorByUserId(userId);
        DistributorOrderPageReqVO pageReqVO = new DistributorOrderPageReqVO();
        pageReqVO.setSuperiorIdP1(distributorDO.getId());
        pageReqVO.setSuperiorIdP2(distributorDO.getId());
        pageReqVO.setPageNo(reqVO.getPageNo());
        pageReqVO.setPageSize(reqVO.getPageSize());
        pageReqVO.setCreateTime(reqVO.getCreateTime());
        PageResult<DistributorOrderDO> orderPage = distributorOrderService.getOrderPage(pageReqVO);
        orderPage.getList().forEach(x -> {
            if (distributorDO.getId().equals(x.getSuperiorIdP1())) {
                x.setWages(x.getFirstAmount());
            } else {
                x.setWages(x.getSecondAmount());
            }
        });
        return orderPage;
    }

}
