package com.sckw.contract.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.contract.dao.KwcContractTradeGoodsMapper;
import com.sckw.contract.model.entity.KwcContractTradeGoods;
import com.sckw.contract.model.vo.req.TradeGoodsInfoReqVo;
import com.sckw.contract.repository.KwcContractTradeGoodsRepository;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.context.LoginUserHolder;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


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

    private final KwcContractTradeGoodsMapper kwcContractLogisticsGoodsMapper;
    private final KwcContractTradeGoodsRepository kwcContractTradeGoodsRepository;


    public void saveContractTradeGoods(long contractId, List<TradeGoodsInfoReqVo> goodsInfoList,Date startTime,
                                       Date endTime) {
        //todo 调dubbo接口返回商品详情 填充sku
        //List<Long> goodsIdList = goodsInfoList.stream().map(TradeGoodsInfoReqVo::getGoodsId).toList();

        Long userId = LoginUserHolder.getUserId();

        if (CollectionUtils.isEmpty(goodsInfoList)){
            return;
        }
        Set<KwcContractTradeGoods> tradeGoods = goodsInfoList.stream()
                .map(g -> getKwcContractTradeGoods(contractId, g, userId, startTime, endTime))
                .collect(Collectors.toSet());
        if (CollectionUtils.isNotEmpty(tradeGoods)){
            kwcContractTradeGoodsRepository.saveBatch(tradeGoods);

        }
    }

    @NotNull
    private static KwcContractTradeGoods getKwcContractTradeGoods(long contractId, TradeGoodsInfoReqVo g, Long userId
            ,Date startTime, Date endTime) {
        Date date = new Date();
        KwcContractTradeGoods kwcContractTradeGoods = new KwcContractTradeGoods();
        kwcContractTradeGoods.setId(new IdWorker(1L).nextId());
        kwcContractTradeGoods.setContractId(contractId);
        kwcContractTradeGoods.setGoodsId(g.getGoodsId());
        kwcContractTradeGoods.setSkuId(0L);
        kwcContractTradeGoods.setPerformedAmount(new BigDecimal("0"));
        kwcContractTradeGoods.setStatus(Global.NO);
        kwcContractTradeGoods.setUnit(g.getUnit());
        kwcContractTradeGoods.setPrice(g.getPrice());
        kwcContractTradeGoods.setAmount(g.getAmount());
        kwcContractTradeGoods.setCreateBy(userId);
        kwcContractTradeGoods.setCreateTime(date);
        kwcContractTradeGoods.setUpdateBy(userId);
        kwcContractTradeGoods.setUpdateTime(date);
        kwcContractTradeGoods.setDelFlag(Global.NO);
        kwcContractTradeGoods.setStartTime(startTime);
        kwcContractTradeGoods.setEndTime(endTime);
        return kwcContractTradeGoods;
    }

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

    public int updateBatch(List<KwcContractTradeGoods> list) {
        int count = 0;
        for (KwcContractTradeGoods kwcContractTradeGoods : list) {
            count += kwcContractLogisticsGoodsMapper.updateById(kwcContractTradeGoods);
        }
        return count;
    }
}




