package com.sckw.contract.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.contract.dao.KwcContractTradeUnitMapper;
import com.sckw.contract.model.entity.KwcContractLogisticsUnit;
import com.sckw.contract.model.entity.KwcContractTradeUnit;
import com.sckw.contract.model.vo.req.TradeBaseInfoReqVo;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.enums.CooperateTypeEnum;
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 com.sckw.system.api.model.dto.res.EntCacheResDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author PC
* @description 针对表【kwc_contract_trade_unit(交易合同采购/销售企业信息)】的数据库操作Service实现
* @createDate 2023-07-13 13:36:19
*/
@Service
public class KwcContractTradeUnitService {

    @Autowired
    private KwcContractTradeUnitMapper kwcContractTradeUnitMapper;

    @Autowired
    private CommonBusinessService commonBusinessService;

    /**
     * @desc: 保存合同单位
     * @param: contractId 合同id， baseInfo基础信息
     * @author: czh
     * @date 2023/7/16
     */
    public void saveContractTradeUnit(long contractId, TradeBaseInfoReqVo baseInfo) {
        List<Long> entIdList = new ArrayList<>();
        if (Objects.nonNull(baseInfo.getProvideEntId())) {
            entIdList.add(baseInfo.getProvideEntId());
        }

        if (Objects.nonNull(baseInfo.getPurchaseEntId())) {
            entIdList.add(baseInfo.getPurchaseEntId());
        }

        if (CollectionUtils.isEmpty(entIdList)) {
            return;
        }

        Map<Long, EntCacheResDto> entCacheResDtoMap = commonBusinessService.queryEntCacheMapByIds(entIdList);

        Date date = new Date();
        saveProvideAndPurchase(contractId, entCacheResDtoMap, date, baseInfo);
    }

    private void saveProvideAndPurchase(long contractId, Map<Long, EntCacheResDto> entCacheResDtoMap, Date date, TradeBaseInfoReqVo baseInfo) {
        //存供应商
        KwcContractTradeUnit kwcContractTradeUnit = new KwcContractTradeUnit();
        if(Objects.nonNull(baseInfo.getProvideEntId())) {
            kwcContractTradeUnit.setId(new IdWorker(1L).nextId());
            kwcContractTradeUnit.setContractId(contractId);
            kwcContractTradeUnit.setUnitType(CooperateTypeEnum.SUPPLIER.getCode());
            kwcContractTradeUnit.setEntId(baseInfo.getProvideEntId());
            saveEnt(entCacheResDtoMap, kwcContractTradeUnit);
            kwcContractTradeUnit.setSignPhone(baseInfo.getProvidePhone());
            kwcContractTradeUnit.setStatus(Global.NO);
            kwcContractTradeUnit.setCreateBy(LoginUserHolder.getUserId());
            kwcContractTradeUnit.setCreateTime(date);
            kwcContractTradeUnit.setUpdateBy(LoginUserHolder.getUserId());
            kwcContractTradeUnit.setUpdateTime(date);
            kwcContractTradeUnit.setDelFlag(Global.NO);
            if (kwcContractTradeUnitMapper.insert(kwcContractTradeUnit) <= 0) {
                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
            }
        }

        //存采购商
        if (Objects.nonNull(baseInfo.getPurchaseEntId())) {
            kwcContractTradeUnit.setId(new IdWorker(1L).nextId());
            kwcContractTradeUnit.setUnitType(CooperateTypeEnum.PURCHASER.getCode());
            kwcContractTradeUnit.setEntId(baseInfo.getPurchaseEntId());
            kwcContractTradeUnit.setContractId(contractId);
            saveEnt(entCacheResDtoMap, kwcContractTradeUnit);
            kwcContractTradeUnit.setSignPhone(baseInfo.getPurchasePhone());
            kwcContractTradeUnit.setStatus(Global.NO);
            kwcContractTradeUnit.setCreateBy(LoginUserHolder.getUserId());
            kwcContractTradeUnit.setCreateTime(date);
            kwcContractTradeUnit.setUpdateBy(LoginUserHolder.getUserId());
            kwcContractTradeUnit.setUpdateTime(date);
            kwcContractTradeUnit.setDelFlag(Global.NO);
            if (kwcContractTradeUnitMapper.insert(kwcContractTradeUnit) <= 0) {
                throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
            }
        }

    }

    /**
     * @desc: 保存合同企业
     * @author: czh
     * @date 2023/7/16
     */
    private void saveEnt(Map<Long, EntCacheResDto> entCacheResDtoMap, KwcContractTradeUnit kwcContractTradeUnit) {
        EntCacheResDto entCacheResDto = entCacheResDtoMap.get(kwcContractTradeUnit.getEntId());
        if (Objects.nonNull(entCacheResDto)) {
            kwcContractTradeUnit.setFirmName(entCacheResDto.getFirmName());
        }
        kwcContractTradeUnit.setContacts(entCacheResDto.getContacts());
        kwcContractTradeUnit.setPhone(entCacheResDto.getPhone());
    }

    /**
     * @param id 合同id
     * @return KwcContractTradeUnit
     * @desc: 根据合同id查企业记录
     * @author: czh
     * @date: 2023/7/20
     */
    public List<KwcContractTradeUnit> queryByContractId(Long id) {
        LambdaQueryWrapper<KwcContractTradeUnit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwcContractTradeUnit::getContractId, id);
        wrapper.eq(KwcContractTradeUnit::getDelFlag, Global.NO);
        wrapper.eq(KwcContractTradeUnit::getStatus, Global.NO);
        return kwcContractTradeUnitMapper.selectList(wrapper);
    }

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

}




