package com.sckw.payment.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.page.PageRes;
import com.sckw.payment.dao.KwpSettlementLogisticsMapper;
import com.sckw.payment.model.KwpSettlementLogistics;
import com.sckw.payment.model.constant.SettlementEnum;
import com.sckw.payment.model.dto.SettlementLogisticsDto;
import com.sckw.payment.model.vo.req.SettlementReq;
import com.sckw.payment.model.vo.res.SettlementLogisticsStatusCountVo;
import com.sckw.payment.model.vo.res.SettlementLogisticsSumVo;
import com.sckw.payment.utils.CommonValidator;
import com.sckw.payment.utils.PageMoreRes;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Aick Spt
 * @date 2023-07-10 16:38:36
 */
@AllArgsConstructor
@Service
@Slf4j
public class KwpSettlementLogisticsService {
    private final KwpSettlementLogisticsMapper settlementLogisticsMapper;

    public void save(KwpSettlementLogistics settlementLogistics) {
        settlementLogisticsMapper.insert(settlementLogistics);
    }
//    public SettlementLogisticsStatusCountVo getCountList() {
//        SettlementLogisticsStatusCountVo settlementLogisticsStatusCountVos = new SettlementLogisticsStatusCountVo();
//        LambdaQueryWrapper<KwpSettlementLogistics> wrapper = new LambdaQueryWrapper<>();
//
//        settlementLogisticsStatusCountVos.setAllCount(settlementLogisticsMapper.selectCount(wrapper));
//
//        wrapper.eq(KwpSettlementLogistics::getStatus, SettlementEnum.WAITING_PAYMENT.getStatus());//待结算
//        settlementLogisticsStatusCountVos.setWaitingPaymentCount(settlementLogisticsMapper.selectCount(wrapper));
//
//        wrapper.clear();
//        wrapper.eq(KwpSettlementLogistics::getStatus, SettlementEnum.PARTIAL_PAYMENT.getStatus());//部分结算
//        settlementLogisticsStatusCountVos.setPartialPaymentCount(settlementLogisticsMapper.selectCount(wrapper));
//
//        wrapper.clear();
//        wrapper.eq(KwpSettlementLogistics::getStatus, SettlementEnum.ALL_PAYMENT.getStatus());//全部结算
//        settlementLogisticsStatusCountVos.setAllPaymentCount(settlementLogisticsMapper.selectCount(wrapper));
//
//        return settlementLogisticsStatusCountVos;
//    }

    /**
     * 运费收款-物流订单各状态列表总数
     *
     * @author Aick Spt
     * @date 2023-07-24 16:21
     */
    public SettlementLogisticsStatusCountVo getCountListCollection(SettlementReq settlementReq) {
        SettlementLogisticsStatusCountVo settlementLogisticsStatusCountVos = new SettlementLogisticsStatusCountVo();
        //总
        settlementReq.setStatus(null);
        settlementLogisticsStatusCountVos.setAllCount(settlementLogisticsMapper.getCountListCollection(settlementReq));

        //待结算
        settlementReq.setStatus(SettlementEnum.WAITING_PAYMENT.getStatus());
        settlementLogisticsStatusCountVos.setWaitingPaymentCount(settlementLogisticsMapper.getCountListCollection(settlementReq));

        //部分结算
        settlementReq.setStatus(SettlementEnum.PARTIAL_PAYMENT.getStatus());
        settlementLogisticsStatusCountVos.setPartialPaymentCount(settlementLogisticsMapper.getCountListCollection(settlementReq));

        //全部结算
        settlementReq.setStatus(SettlementEnum.ALL_PAYMENT.getStatus());
        settlementLogisticsStatusCountVos.setAllPaymentCount(settlementLogisticsMapper.getCountListCollection(settlementReq));

        return settlementLogisticsStatusCountVos;
    }

    /**
     * 运费付款-物流订单各状态列表总数
     *
     * @author Aick Spt
     * @date 2023-07-24 16:23
     */
    public SettlementLogisticsStatusCountVo getCountListPayment(SettlementReq settlementReq) {
        SettlementLogisticsStatusCountVo settlementLogisticsStatusCountVos = new SettlementLogisticsStatusCountVo();
        //总
        settlementReq.setStatus(null);
        settlementLogisticsStatusCountVos.setAllCount(settlementLogisticsMapper.getCountListPayment(settlementReq));

        //待结算
        settlementReq.setStatus(SettlementEnum.WAITING_PAYMENT.getStatus());
        settlementLogisticsStatusCountVos.setWaitingPaymentCount(settlementLogisticsMapper.getCountListPayment(settlementReq));

        //部分结算
        settlementReq.setStatus(SettlementEnum.PARTIAL_PAYMENT.getStatus());
        settlementLogisticsStatusCountVos.setPartialPaymentCount(settlementLogisticsMapper.getCountListPayment(settlementReq));

        //全部结算
        settlementReq.setStatus(SettlementEnum.ALL_PAYMENT.getStatus());
        settlementLogisticsStatusCountVos.setAllPaymentCount(settlementLogisticsMapper.getCountListPayment(settlementReq));

        return settlementLogisticsStatusCountVos;
    }

    /**
     * 运费收款-物流订单列表
     *
     * @author Aick Spt
     * @date 2023-07-24 16:21
     */
    public PageMoreRes<SettlementLogisticsDto> pageSelectCollection(SettlementReq settlementReq) {
        CommonValidator.isValidCoexisting(settlementReq.getStartCreateTime(), settlementReq.getEndCreateTime(), "");
        CommonValidator.isValidCoexisting(settlementReq.getStartReceiptTime(), settlementReq.getEndReceiptTime(), "预计收款开始和结束时间");

        PageHelper.startPage(settlementReq.getPage(), settlementReq.getPageSize());
        List<SettlementLogisticsDto> kwpLedgerLogisticsList = settlementLogisticsMapper.pageSelectCollection(settlementReq);
        SettlementLogisticsSumVo settlementLogisticsSum = settlementLogisticsMapper.selectCollectionSum(settlementReq);

        // 对查询结果进行处理，转换 status 到 desc
        return new PageMoreRes<>(getSettlementLogisticsDtoPageRes(kwpLedgerLogisticsList), settlementLogisticsSum);
    }


    /**
     * 运费付款-物流订单列表
     *
     * @author Aick Spt
     * @date 2023-07-24 16:23
     */
    public PageRes<SettlementLogisticsDto> pageSelectPayment(SettlementReq settlementReq) {
        CommonValidator.isValidCoexisting(settlementReq.getStartCreateTime(), settlementReq.getEndCreateTime(), "");
        CommonValidator.isValidCoexisting(settlementReq.getStartReceiptTime(), settlementReq.getEndReceiptTime(), "预计付款开始和结束时间");

        PageHelper.startPage(settlementReq.getPage(), settlementReq.getPageSize());
        List<SettlementLogisticsDto> kwpLedgerLogisticsList = settlementLogisticsMapper.pageSelectPayment(settlementReq);

        // 对查询结果进行处理，转换 status 到 desc
        return new PageRes<>(getSettlementLogisticsDtoPageRes(kwpLedgerLogisticsList));
    }

    @NotNull
    private PageInfo<SettlementLogisticsDto> getSettlementLogisticsDtoPageRes(List<SettlementLogisticsDto> kwpLedgerLogisticsList) {
        for (SettlementLogisticsDto entity : kwpLedgerLogisticsList) {
            entity.setStatusText(SettlementEnum.getStatusDesc(entity.getStatus()));
            entity.setResiduePrice(entity.getLedgerSettlePrice().subtract(entity.getActualPrice()));
            entity.setCreateByText("创建人名称");
            entity.setUpdateByText("更新人名称");
        }
        return new PageInfo<>(kwpLedgerLogisticsList);
    }


    /**
     * 运费收款-物流订单详情
     *
     * @author Aick Spt
     * @date 2023-07-24 16:22
     */
    public SettlementLogisticsDto detailCollection(Long id) {
        return settlementLogisticsMapper.detailCollection(id);
    }

    /**
     * 运费付款-物流订单详情
     *
     * @author Aick Spt
     * @date 2023-07-24 16:24
     */
    public SettlementLogisticsDto detailPayment(Long id) {
        return settlementLogisticsMapper.detailPayment(id);
    }


    /**
     * 根据id查物流结算订单
     *
     * @param ids 物流结算订单集合List<Long>
     * @author Aick Spt
     * @date 2023-07-21 16:16
     */
    public List<SettlementLogisticsDto> selectList(List<Long> ids) {
        return settlementLogisticsMapper.selectIds(ids);
    }

    public List<SettlementLogisticsDto> selectList(Integer unitType) {
        return settlementLogisticsMapper.selectList(unitType);
    }

}
