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.core.utils.IdWorker;
import com.sckw.payment.dao.KwpSettlementOfflineMapper;
import com.sckw.payment.model.KwpSettlementOffline;
import com.sckw.payment.model.constant.SettlementWalletPayTypeEnum;
import com.sckw.payment.model.dto.SettlementOfflineDto;
import com.sckw.payment.model.vo.req.SettlementOfflineReq;
import com.sckw.payment.model.vo.req.SettlementReq;
import com.sckw.payment.model.vo.res.SettlementOfflineVo;
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.stream.Collectors;

/**
 * @author xucaiqin
 * @date 2023-07-10 16:38:36
 */
@AllArgsConstructor
@Service
@Slf4j
public class KwpSettlementOfflineService {

    private final KwpSettlementOfflineMapper settlementOfflineMapper;

    /**
     * 物流-线下付款(运费收款)记录-列表
     * @author Aick Spt
     * @date 2023-07-26 17:15
     */
    public PageRes<SettlementOfflineVo> pageListLogisticsCollection(SettlementOfflineReq settlementOfflineReq) {
//        //todo 查询缓存，获取客户企业id
//        String keywords = settlementReq.getKeywords();
//        if (StringUtils.isNotBlank(keywords)) {
//            System.out.println("关键之：" + keywords);
//        }
        PageHelper.startPage(settlementOfflineReq.getPage(), settlementOfflineReq.getPageSize());

        List<SettlementOfflineDto> settlementOfflineLogisticsList = settlementOfflineMapper.pageListLogisticsCollection(settlementOfflineReq);

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

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

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

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


    /**
     * 结算-物流-线下付款(运费收款)记录-详情
     *
     * @param id 结算物流订单id
     * @author Aick Spt
     * @date 2023-07-19 09:11
     */
    public SettlementOfflineDto detailLogisticsCollection(Long id) {
        return settlementOfflineMapper.detailLogisticsCollection(id);
    }

    /**
     * 物流-线下付款(运费收款)记录-新增
     *
     * @param id    结算单id
     * @param price 付款金额
     * @return InsertId
     * @author Aick Spt
     * @date 2023-07-20 14:23
     */
    public Integer confirmLogisticsCollection(Long id, Float price) {
        log.info("接收到数据，开始物流-物流-线下付款(运费收款)记录-新增，待完善");
        //todo 待完善
        //先查询出结算单情况

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

        //对比金额

        //累加入库结算单

        //新增电子钱包结算记录
        KwpSettlementOffline kwpSettlementOffline = new KwpSettlementOffline();
        kwpSettlementOffline.setId(new IdWorker(1).nextId());

        //todo 待完善
        settlementOfflineMapper.insert(kwpSettlementOffline);
        return settlementOfflineMapper.confirmLogisticsCollection(id, price);
    }

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

        List<SettlementOfflineDto> settlementOfflineLogisticsList = settlementOfflineMapper.pageListLogisticsCollection(settlementOfflineReq);
        for (SettlementOfflineDto entity : settlementOfflineLogisticsList) {
            entity.setCreateByText("创建人名称");
            entity.setUpdateByText("更新人名称");
        }

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

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

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

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

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

        //对比金额

        //累加入库结算单

        //新增电子钱包结算记录
        KwpSettlementOffline kwpSettlementOffline = new KwpSettlementOffline();
        kwpSettlementOffline.setId(new IdWorker(1).nextId());

        //todo 待完善
        settlementOfflineMapper.insert(kwpSettlementOffline);
        return settlementOfflineMapper.confirmLogisticsCollection(id, price);
    }
}
