package com.sckw.contract.service;

import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.contract.dao.KwcContractLogisticsMapper;
import com.sckw.contract.dao.KwcContractLogisticsTrackMapper;
import com.sckw.contract.dao.KwcContractLogisticsUnitMapper;
import com.sckw.contract.model.entity.KwcContractLogistics;
import com.sckw.contract.model.entity.KwcContractLogisticsTrack;
import com.sckw.contract.model.entity.KwcContractLogisticsUnit;
import com.sckw.contract.model.vo.res.ContractTrackResVo;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.enums.ContractTrackEnum;
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.RemoteSystemService;
import com.sckw.system.api.model.dto.res.UserCacheResDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author PC
 * @description 针对表【kwc_contract_logistics_track(物流合同（承运合同/托运合同）-物流订单)】的数据库操作Service实现
 * @createDate 2023-07-13 13:36:19
 */
@Service
public class KwcContractLogisticsTrackService {

    @Autowired
    private KwcContractLogisticsTrackMapper kwcContractLogisticsTrackMapper;

    @Autowired
    private KwcContractLogisticsMapper kwcContractLogisticsMapper;

    @Autowired
    private KwcContractLogisticsUnitMapper kwcContractLogisticsUnitMapper;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    /**
     * @param contractId 合同id, status签约状态
     * @desc: 存审批记录
     * @author: czh
     * @date: 2023/7/14
     */
    public void saveContractLogisticsTrack(Long contractId, Integer status) {
        KwcContractLogisticsTrack kwcContractLogisticsTrack = new KwcContractLogisticsTrack();
        Date date = new Date();
        date = DateUtil.offsetSecond(date, Global.NUMERICAL_TWO);
        kwcContractLogisticsTrack.setId(new IdWorker(1L).nextId());
        kwcContractLogisticsTrack.setContractId(contractId);
        kwcContractLogisticsTrack.setType(status);
        kwcContractLogisticsTrack.setRemark("");
        kwcContractLogisticsTrack.setStatus(Global.NO);
        kwcContractLogisticsTrack.setCreateBy(LoginUserHolder.getUserId());
        kwcContractLogisticsTrack.setCreateTime(date);
        kwcContractLogisticsTrack.setUpdateBy(LoginUserHolder.getUserId());
        kwcContractLogisticsTrack.setUpdateTime(date);
        kwcContractLogisticsTrack.setDelFlag(Global.NO);
        if (kwcContractLogisticsTrackMapper.insert(kwcContractLogisticsTrack) <= 0) {
            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
        }
    }

    /**
     * @param id 合同id
     * @return List<ContractTrackResVo>
     * @desc: 根据合同id查流程
     * @author: czh
     * @date: 2023/7/31
     */
    public List<ContractTrackResVo> queryContractTrackByContractId(Long id) {
        LambdaQueryWrapper<KwcContractLogisticsTrack> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwcContractLogisticsTrack::getContractId, id).
                eq(KwcContractLogisticsTrack::getDelFlag, Global.NO).
                orderByDesc(KwcContractLogisticsTrack::getCreateTime);
        List<KwcContractLogisticsTrack> kwcContractLogisticsTracks = kwcContractLogisticsTrackMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(kwcContractLogisticsTracks)) {
            return Collections.emptyList();
        }

        KwcContractLogistics kwcContractLogistics = kwcContractLogisticsMapper.selectById(id);
        if (Objects.isNull(kwcContractLogistics)) {
            return Collections.emptyList();
        }

        Long entId = kwcContractLogistics.getEntId();
        LambdaQueryWrapper<KwcContractLogisticsUnit> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(KwcContractLogisticsUnit::getContractId, id).
                 eq(KwcContractLogisticsUnit::getDelFlag, Global.NO);
        List<KwcContractLogisticsUnit> kwcContractLogisticsUnits = kwcContractLogisticsUnitMapper.selectList(wrapper2);
        if (CollectionUtils.isEmpty(kwcContractLogisticsTracks)) {
            return Collections.emptyList();
        }

        String initiateEntName = "";
        String targetEntName  = "";
        for (KwcContractLogisticsUnit kwcContractLogisticsUnit : kwcContractLogisticsUnits) {
            if (kwcContractLogisticsUnit.getEntId().equals(entId)) {
                initiateEntName = kwcContractLogisticsUnit.getFirmName();
            } else {
                targetEntName = kwcContractLogisticsUnit.getFirmName();
            }
        }

        List<ContractTrackResVo> list = new ArrayList<>();
        for (KwcContractLogisticsTrack item : kwcContractLogisticsTracks) {
            ContractTrackResVo contractTrackResVo = new ContractTrackResVo();
            contractTrackResVo.setCreateTime(item.getCreateTime());
            contractTrackResVo.setTrackName(ContractTrackEnum.getName(item.getType()).getName());
            if (item.getType().equals(ContractTrackEnum.SIGNING.getCode())) {
                contractTrackResVo.setEntName(initiateEntName);
            } else if (item.getType().equals(ContractTrackEnum.SIGNED.getCode())) {
                contractTrackResVo.setEntName(targetEntName);
            } else {
                UserCacheResDto userCacheResDto = remoteSystemService.queryUserCacheById(item.getCreateBy());
                contractTrackResVo.setEntName(userCacheResDto.getEntInfo().getFirmName());
            }
            list.add(contractTrackResVo);
        }
        return list;
    }

}




