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.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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


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

    @Autowired
    private KwcContractTradeGoodsMapper kwcContractLogisticsGoodsMapper;


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

        Long userId = LoginUserHolder.getUserId();
        Date date = new Date();
        for (TradeGoodsInfoReqVo tradeGoodsInfoReqVo : goodsInfoList) {
            KwcContractTradeGoods kwcContractTradeGoods = new KwcContractTradeGoods();
            BeanUtils.copyProperties(tradeGoodsInfoReqVo, kwcContractTradeGoods);
            kwcContractTradeGoods.setId(new IdWorker(1L).nextId());
            kwcContractTradeGoods.setContractId(contractId);
            kwcContractTradeGoods.setSkuId(0L);
            kwcContractTradeGoods.setPerformedAmount(new BigDecimal("0"));
            kwcContractTradeGoods.setStatus(Global.NO);
            kwcContractTradeGoods.setCreateBy(userId);
            kwcContractTradeGoods.setCreateTime(date);
            kwcContractTradeGoods.setUpdateBy(userId);
            kwcContractTradeGoods.setUpdateTime(date);
            kwcContractTradeGoods.setDelFlag(Global.NO);
            if (kwcContractLogisticsGoodsMapper.insert(kwcContractTradeGoods) <= 0) {
                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
            }
        }
    }

    /**
     * @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;
    }
}




