package com.sckw.contract.dubbo;
import com.google.common.collect.Lists;

import com.sckw.contract.api.RemoteContractService;
import com.sckw.contract.api.model.dto.res.ContractCommonInfoResDto;
import com.sckw.contract.api.model.dto.res.EntInfo;
import com.sckw.contract.dao.KwcContractLogisticsMapper;
import com.sckw.contract.dao.KwcContractLogisticsUnitMapper;
import com.sckw.contract.dao.KwcContractTradeMapper;
import com.sckw.contract.model.entity.KwcContractLogistics;
import com.sckw.contract.model.entity.KwcContractLogisticsUnit;
import com.sckw.contract.model.entity.KwcContractTrade;
import com.sckw.contract.service.KwcContractLogisticsUnitService;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.enums.ContractStatusEnum;
import com.sckw.core.model.enums.SigningWayEnum;
import com.sckw.core.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@DubboService(group = "design", version = "2.0.0")
@Service
public class RemoteContractServiceImpl implements RemoteContractService {

    @Autowired
    private KwcContractLogisticsMapper kwcContractLogisticsMapper;

    @Autowired
    private KwcContractLogisticsUnitService kwcContractLogisticsUnitService;

    @Autowired
    private KwcContractTradeMapper kwcContractTradeMapper;

    @Override
    public Map<Long, ContractCommonInfoResDto> queryContractBaseInfo(List<Long> contractIds) {
        HashMap<Long, ContractCommonInfoResDto> map = new HashMap<>();
        for (Long contractId : contractIds) {
            KwcContractLogistics kwcContractLogistics = kwcContractLogisticsMapper.selectById(contractId);
            if (Objects.nonNull(kwcContractLogistics)) {
                Integer status = kwcContractLogistics.getStatus();
                Integer delFlag = kwcContractLogistics.getDelFlag();
                if (Objects.equals(status, ContractStatusEnum.SAVE.getCode()) || Objects.equals(status, ContractStatusEnum.COMPLETE.getCode()) || delFlag.equals(Global.YES)) {
                    continue;
                }
                map.put(contractId, fillContractLogistics(kwcContractLogistics));
                continue;
            }

            KwcContractTrade kwcContractTrade = kwcContractTradeMapper.selectById(contractId);
            if (Objects.nonNull(kwcContractTrade)) {
                Integer status = kwcContractTrade.getStatus();
                Integer delFlag = kwcContractTrade.getDelFlag();
                if (Objects.equals(status, ContractStatusEnum.SAVE.getCode()) || Objects.equals(status, ContractStatusEnum.COMPLETE.getCode()) || delFlag.equals(Global.YES)) {
                    continue;
                }
                map.put(contractId, fillContractTrade(kwcContractTrade));
            }
        }
        return map;
    }

    private ContractCommonInfoResDto fillContractLogistics(KwcContractLogistics kwcContractLogistics) {
        ContractCommonInfoResDto contractCommonInfoResDto = new ContractCommonInfoResDto();
        List<EntInfo> entList = new ArrayList<>();
        contractCommonInfoResDto.setId(kwcContractLogistics.getId());
        contractCommonInfoResDto.setContactName(kwcContractLogistics.getName());
        contractCommonInfoResDto.setContractCode(kwcContractLogistics.getContractNo());
        contractCommonInfoResDto.setSigningWay(kwcContractLogistics.getSigningWay());
        contractCommonInfoResDto.setSigningWayName(SigningWayEnum.getName(kwcContractLogistics.getSigningWay()).getName());
        contractCommonInfoResDto.setEntInfoList(entList);
        contractCommonInfoResDto.setStatus(kwcContractLogistics.getStatus());
        contractCommonInfoResDto.setStatusName(ContractStatusEnum.getName(kwcContractLogistics.getStatus()).getName());
        List<KwcContractLogisticsUnit> kwcContractLogisticsUnits = kwcContractLogisticsUnitService.queryByContractId(kwcContractLogistics.getId());
        if (CollectionUtils.isNotEmpty(kwcContractLogisticsUnits)) {
            for (KwcContractLogisticsUnit kwcContractLogisticsUnit : kwcContractLogisticsUnits) {
                EntInfo entInfo = new EntInfo();
                entInfo.setEntId(kwcContractLogisticsUnit.getEntId());
                entInfo.setEntName(kwcContractLogisticsUnit.getFirmName());
                entList.add(entInfo);
            }
        }
        return contractCommonInfoResDto;
    }


    private ContractCommonInfoResDto fillContractTrade(KwcContractTrade kwcContractTrade) {
        List<EntInfo> entList = new ArrayList<>();
        ContractCommonInfoResDto contractCommonInfoResDto = new ContractCommonInfoResDto();
        contractCommonInfoResDto.setId(kwcContractTrade.getId());
        contractCommonInfoResDto.setContactName(kwcContractTrade.getName());
        contractCommonInfoResDto.setContractCode(kwcContractTrade.getContractNo());
        contractCommonInfoResDto.setSigningWay(kwcContractTrade.getSigningWay());
        contractCommonInfoResDto.setSigningWayName(SigningWayEnum.getName(kwcContractTrade.getSigningWay()).getName());
        contractCommonInfoResDto.setEntInfoList(entList);
        contractCommonInfoResDto.setStatus(kwcContractTrade.getStatus());
        contractCommonInfoResDto.setStatusName(ContractStatusEnum.getName(kwcContractTrade.getStatus()).getName());
        List<KwcContractLogisticsUnit> kwcContractLogisticsUnits = kwcContractLogisticsUnitService.queryByContractId(kwcContractTrade.getId());
        if (CollectionUtils.isNotEmpty(kwcContractLogisticsUnits)) {
            for (KwcContractLogisticsUnit kwcContractLogisticsUnit : kwcContractLogisticsUnits) {
                EntInfo entInfo = new EntInfo();
                entInfo.setEntId(kwcContractLogisticsUnit.getEntId());
                entInfo.setEntName(kwcContractLogisticsUnit.getFirmName());
                entList.add(entInfo);
            }
        }
        return contractCommonInfoResDto;
    }
}
