package com.sckw.payment.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.exception.BusinessException;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.utils.IdWorker;
import com.sckw.payment.dao.KwpSettlementWalletMapper;
import com.sckw.payment.model.KwpSettlementWallet;
import com.sckw.payment.model.constant.SettlementEnum;
import com.sckw.payment.model.constant.SettlementWalletPayTypeEnum;
import com.sckw.payment.model.dto.SettlementLogisticsDto;
import com.sckw.payment.model.dto.SettlementWalletDto;
import com.sckw.payment.model.vo.req.SettlementWalletReq;
import com.sckw.payment.model.vo.res.SettlementWalletVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Aick Spt
 * @date 2023-07-20 14:55
 */
@AllArgsConstructor
@Service
@Slf4j
public class KwpSettlementWalletService {

    private final KwpSettlementWalletMapper settlementWalletMapper;

    private final KwpSettlementLogisticsService kwpSettlementLogisticsService;

    /**
     * 物流-电子钱包付款（货到付款）结算记录-列表
     *
     * @author Aick Spt
     * @date 2023-07-26 16:43
     */
    public PageRes<SettlementWalletVo> pageListLogisticsPayment(SettlementWalletReq settlementWalletReq) {
//        //todo 查询缓存，获取客户企业id
//        String keywords = settlementReq.getKeywords();
//        if (StringUtils.isNotBlank(keywords)) {
//            System.out.println("关键之：" + keywords);
//        }
        PageHelper.startPage(settlementWalletReq.getPage(), settlementWalletReq.getPageSize());
        settlementWalletReq.setPayType(SettlementWalletPayTypeEnum.CASH_ON_DELIVERY.getStatus());//只筛选 货到付款
        List<SettlementWalletDto> settlementWalletLogisticsList = settlementWalletMapper.pageListLogisticsPayment(settlementWalletReq);

        if (CollectionUtils.isEmpty(settlementWalletLogisticsList)) {
            return new PageRes<>(new PageInfo<>());
        }

        for (SettlementWalletDto entity : settlementWalletLogisticsList) {
            entity.setCreateByText("创建人名称");
            entity.setUpdateByText("更新人名称");
        }

        //指定返回值
        List<SettlementWalletVo> collect = settlementWalletLogisticsList.stream().map(a -> {
            SettlementWalletVo settlementWalletVo = new SettlementWalletVo();
            BeanUtils.copyProperties(a, settlementWalletVo);
            return settlementWalletVo;
        }).collect(Collectors.toList());

        return new PageRes<>(new PageInfo<>(collect));
    }


    /**
     * 结算-物流订单详情
     *
     * @param id 结算物流订单id
     * @author Aick Spt
     * @date 2023-07-19 09:11
     */
    public SettlementWalletDto detailLogisticsPayment(Long id) {
        return settlementWalletMapper.detailLogisticsPayment(id);
    }

    /**
     * 物流-电子钱包付款（货到付款）结算记录-新增
     *
     * @param id    结算单id
     * @param price 付款金额
     * @return InsertId
     * @author Aick Spt
     * @date 2023-07-20 14:23
     */
    public Integer confirmLogisticsPayment(Long id, Float price) {
        log.info("接收到数据，开始物流-电子钱包付款（货到付款）结算记录-新增，待完善");
        //todo 待完善
        //先查询出结算单情况
        SettlementLogisticsDto settlementLogistics = kwpSettlementLogisticsService.detailPayment(id);
        log.info(String.valueOf(settlementLogistics));
        //检查结算单状态和所差金额
        if (settlementLogistics == null) {
            log.error("非法请求，查无数据：" + id);
            throw new BusinessException("非法请求运费结算参数ID");
        }
        if (settlementLogistics.getStatus() == SettlementEnum.ALL_PAYMENT.getStatus()) {
            log.error("非法请求，该订单已结算完成：" + id);
            throw new BusinessException("该订单已结算完成，无法操作");
        }

        //对比金额

        //累加入库结算单

        //扣费电子钱包

        //新增电子钱包结算记录
        KwpSettlementWallet kwpSettlementWallet = new KwpSettlementWallet();
        kwpSettlementWallet.setId(new IdWorker(1).nextId());
        //todo 待完善
        settlementWalletMapper.insert(kwpSettlementWallet);
        return settlementWalletMapper.confirmLogisticsPayment(id, price);
    }

    public PageRes<SettlementWalletVo> pageListTradePayment(SettlementWalletReq settlementWalletReq) {
//        //todo 查询缓存，获取客户企业id
//        String keywords = settlementReq.getKeywords();
//        if (StringUtils.isNotBlank(keywords)) {
//            System.out.println("关键之：" + keywords);
//        }
        PageHelper.startPage(settlementWalletReq.getPage(), settlementWalletReq.getPageSize());

        List<SettlementWalletDto> settlementWalletLogisticsList = settlementWalletMapper.pageListLogisticsPayment(settlementWalletReq);
        for (SettlementWalletDto entity : settlementWalletLogisticsList) {
            entity.setCreateByText("创建人名称");
            entity.setUpdateByText("更新人名称");
        }

        if (CollectionUtils.isEmpty(settlementWalletLogisticsList)) {
            return new PageRes<>(new PageInfo<>());
        }

        //指定返回值
        List<SettlementWalletVo> collect = settlementWalletLogisticsList.stream().map(a -> {
            SettlementWalletVo settlementWalletVo = new SettlementWalletVo();
            BeanUtils.copyProperties(a, settlementWalletVo);
            return settlementWalletVo;
        }).collect(Collectors.toList());

        return new PageRes<>(new PageInfo<>(collect));
    }


    public Integer confirmTradePayment(Long id, Float price) {
        log.info("接收到数据，开始物流-销售-付款确认-(新增电子钱包记录-采购货到付款)-新增，待完善");
        //todo 待完善
        //先查询出结算单情况

        //检查结算单状态和所差金额

        //对比金额

        //累加入库结算单

        //扣费电子钱包

        //新增电子钱包结算记录
        KwpSettlementWallet kwpSettlementWallet = new KwpSettlementWallet();
        kwpSettlementWallet.setId(new IdWorker(1).nextId());
        //todo 待完善
        settlementWalletMapper.insert(kwpSettlementWallet);
        return settlementWalletMapper.confirmLogisticsPayment(id, price);
    }

    public PageRes<SettlementWalletVo> pageListTradeCollection(SettlementWalletReq settlementWalletReq) {
//        //todo 查询缓存，获取客户企业id
//        String keywords = settlementReq.getKeywords();
//        if (StringUtils.isNotBlank(keywords)) {
//            System.out.println("关键之：" + keywords);
//        }
        PageHelper.startPage(settlementWalletReq.getPage(), settlementWalletReq.getPageSize());

        List<SettlementWalletDto> settlementWalletLogisticsList = settlementWalletMapper.pageListLogisticsPayment(settlementWalletReq);
        for (SettlementWalletDto entity : settlementWalletLogisticsList) {
            entity.setCreateByText("创建人名称");
            entity.setUpdateByText("更新人名称");
        }

        if (CollectionUtils.isEmpty(settlementWalletLogisticsList)) {
            return new PageRes<>(new PageInfo<>());
        }

        //指定返回值
        List<SettlementWalletVo> collect = settlementWalletLogisticsList.stream().map(a -> {
            SettlementWalletVo settlementWalletVo = new SettlementWalletVo();
            BeanUtils.copyProperties(a, settlementWalletVo);
            return settlementWalletVo;
        }).collect(Collectors.toList());

        return new PageRes<>(new PageInfo<>(collect));
    }


    public Integer confirmTradeCollection(Long id, Float price) {
        log.info("接收到数据，开始物流-销售-收款确认-预付款(电子钱包)-新增，待完善");
        //todo 待完善
        //先查询出结算单情况

        //检查结算单状态和所差金额

        //对比金额

        //累加入库结算单

        //扣费电子钱包

        //新增电子钱包结算记录
        KwpSettlementWallet kwpSettlementWallet = new KwpSettlementWallet();
        kwpSettlementWallet.setId(new IdWorker(1).nextId());
        //todo 待完善
        settlementWalletMapper.insert(kwpSettlementWallet);
        return settlementWalletMapper.confirmLogisticsPayment(id, price);
    }
}

