package com.sckw.contract.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.contract.dao.KwcContractLogisticsGoodsMapper;
import com.sckw.contract.model.entity.KwcContractLogisticsGoods;
import com.sckw.contract.model.vo.req.LogisticsGoodsInfoReqVo;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.context.LoginUserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author PC
 * @description 针对表【kwc_contract_logistics_goods(物流合同商品信息)】的数据库操作Service实现
 * @createDate 2023-07-13 13:36:19
 */
@Service
public class KwcContractLogisticsGoodsService {


    @Autowired
    private KwcContractLogisticsGoodsMapper kwcContractLogisticsGoodsMapper;



    /**
     * @param contractId 合同id， goodsInfoList标的信息
     * @desc: 保存交易合同商品信息
     * @author: czh
     * @date: 2023/7/14
     */
    public void saveContractLogisticsGoods(Long contractId, List<LogisticsGoodsInfoReqVo> goodsInfoList) {
        //todo 调dubbo接口返回商品详情 填充sku
//        List<Long> goodsIdList = goodsInfoList.stream().map(LogisticsGoodsInfoReqVo::getGoodsId).toList();
//        Map<Long, KwpGoods> goodsMap = goodsInfoService.getGoodsByIds(goodsIdList);
        Long userId = LoginUserHolder.getUserId();
        Date date = new Date();
        for (LogisticsGoodsInfoReqVo logisticsGoodsInfoReqVo : goodsInfoList) {
            KwcContractLogisticsGoods kwcContractLogisticsGoods = new KwcContractLogisticsGoods();
            BeanUtils.copyProperties(logisticsGoodsInfoReqVo, kwcContractLogisticsGoods);
            kwcContractLogisticsGoods.setId(new IdWorker(1L).nextId());
            kwcContractLogisticsGoods.setContractId(contractId);
            kwcContractLogisticsGoods.setSkuId(0L);
            kwcContractLogisticsGoods.setPerformedAmount(new BigDecimal("0"));
            kwcContractLogisticsGoods.setStatus(Global.NO);
            kwcContractLogisticsGoods.setCreateBy(userId);
            kwcContractLogisticsGoods.setCreateTime(date);
            kwcContractLogisticsGoods.setUpdateBy(userId);
            kwcContractLogisticsGoods.setUpdateTime(date);
            kwcContractLogisticsGoods.setDelFlag(Global.NO);
            if (kwcContractLogisticsGoodsMapper.insert(kwcContractLogisticsGoods) <= 0) {
                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
            }
        }
    }

    /**
     * @return KwcContractLogisticsGoods
     * @desc: 根据合同id查标的信息
     * @param: contractId 合同id
     * @author: czh
     * @date 2023/7/16
     */
    public List<KwcContractLogisticsGoods> queryGoodsInfoByContractId(Long contractId) {
        LambdaQueryWrapper<KwcContractLogisticsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwcContractLogisticsGoods::getContractId, contractId);
        wrapper.eq(KwcContractLogisticsGoods::getDelFlag, Global.NO);
        wrapper.eq(KwcContractLogisticsGoods::getStatus, Global.NO);
        return kwcContractLogisticsGoodsMapper.selectList(wrapper);
    }

    /**
     * @return 修改个数
     * @desc: 修改
     * @param: list 实体类
     * @author: czh
     * @date 2023/7/17
     */
    public int updateBatch(List<KwcContractLogisticsGoods> list) {
        int count = 0;
        for (KwcContractLogisticsGoods kwcContractLogisticsGoods : list) {
            count += kwcContractLogisticsGoodsMapper.updateById(kwcContractLogisticsGoods);
        }
        return count;
    }

    /**
     * @param idList 合同id
     * @return   Map<Long, BigDecimal>
     * @desc: 根据合同id查总量
     * @author: czh
     * @date: 2023/7/25
     */
    public Map<Long, BigDecimal> getAmountByIdList(List<Long> idList) {
        LambdaQueryWrapper<KwcContractLogisticsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(KwcContractLogisticsGoods::getContractId, idList);
        wrapper.eq(KwcContractLogisticsGoods::getDelFlag, Global.NO);
        List<KwcContractLogisticsGoods> kwcContractLogisticsGoods = kwcContractLogisticsGoodsMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(kwcContractLogisticsGoods)) {
            return new HashMap<>(1);
        }
       return kwcContractLogisticsGoods.stream().collect(Collectors.groupingBy(KwcContractLogisticsGoods::getContractId, Collectors.reducing(BigDecimal.ZERO, KwcContractLogisticsGoods::getAmount, BigDecimal::add)));
    }

}




