package com.pusong.business.service.approver.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.pusong.business.domain.PsApproverRecord;
import com.pusong.business.domain.PsContractInfo;
import com.pusong.business.domain.PsContractPay;
import com.pusong.business.domain.PsTaskMain;
import com.pusong.business.domain.bo.PsContractPayBo;
import com.pusong.business.domain.vo.PsCompanyInfoVo;
import com.pusong.business.domain.vo.PsContractInfoVo;
import com.pusong.business.domain.vo.PsContractPayVo;
import com.pusong.business.enums.*;
import com.pusong.business.service.IPsContractInfoService;
import com.pusong.business.service.IPsContractPayService;
import com.pusong.business.service.IPsTaskService;
import com.pusong.common.core.utils.MapstructUtils;
import com.pusong.common.core.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 合同回款审批处理类
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class PayContractApproverServiceImpl extends ApproverAbstractServiceImpl {

    private final IPsContractInfoService psContractInfoService;
    private final IPsContractPayService payService;
    private final IPsTaskService taskMainService;
    @Override
    public String approverType() {
        return ApproverTypeEnum.PAY.getCode();
    }


    /**
     * 1 审批成功修改回款记录状态，增加主任务
     * @param psApproverRecord 审批类
     */
    @Override
    public void success(PsApproverRecord psApproverRecord) {
        //查询合同信息
        PsContractInfoVo info = psContractInfoService.queryContractByCode(psApproverRecord.getContractCode());
        //修改回款记录状态
        PsContractPay update = new PsContractPay();
//        if(StringUtils.isNotBlank(psApproverRecord.getUpdateData())){
//            PsContractPayBo upda = JSON.parseObject(psApproverRecord.getUpdateData(), PsContractPayBo.class);
//            MapstructUtils.convert(upda,update);
//        }
        update.setId(Long.valueOf(psApproverRecord.getBusinessId()));
        update.setPayStatus(PayStatusEnum.SUCCESS.getCode());
        payService.updateById(update);

        PsContractInfo updateInfo = new PsContractInfo();
        updateInfo.setContractCode(info.getContractCode());
        if("1".equals(info.getIsDue())){
            if(StringUtils.equals(ContractStatusEnum.CREATE.getCode(), info.getContractStatus())){
                updateInfo.setContractStatus(ContractStatusEnum.EXECUTION.getCode());
                psContractInfoService.updateByCode(updateInfo);
            }
            return;//续费合同不生成任务直接return
        }else{
            if(StringUtils.equals(ContractStatusEnum.CREATE.getCode(), info.getContractStatus())){
                updateInfo.setContractStatus(ContractStatusEnum.RETURN.getCode());
                psContractInfoService.updateByCode(updateInfo);
            }
        }

//        List<PsContractPayVo> list = payService.queryListByContractCode(info.getContractCode(), PayBuinessStatusEnum.PAY.getCode(), PayStatusEnum.SUCCESS);
        //如果没有过回款记录初始化任务主表,并修改合同状态
//        log.info("查询回款记录数量{}任务状态{}", list.size(),info.getContractStatus());

        List<PsTaskMain> tasks = taskMainService.queryByContractCode(psApproverRecord.getContractCode());
        if(tasks.isEmpty() || tasks.get(0).getId() == null){
            log.info("任务未初始化，初始化任务主表");
            List<PsTaskMain> psTaskMainList = new ArrayList<>();
            for (PsCompanyInfoVo psCompanyInfoVo : info.getPsCompanySerivceVo()) {
                PsTaskMain psTaskMain = new PsTaskMain();
                psTaskMain.setContractCode(psApproverRecord.getContractCode());
                psTaskMain.setTaskType(TaskTypeEnum.CONTRACT.getCode());
                psTaskMain.setTaskStatus(TaskStatusEnum.INIT.getCode());
                psTaskMain.setCompanyId(info.getCompanyId());
                psTaskMain.setCustomId(info.getCustomId());
                psTaskMain.setServiceCompanyId(psCompanyInfoVo.getId());
                psTaskMainList.add(psTaskMain);
            }
            taskMainService.insertBatch(psTaskMainList);
        }

    }

    @Override
    public void fail(PsApproverRecord psApproverRecord) {
        //如果是修改不做处理
        if(StringUtils.isNotBlank(psApproverRecord.getUpdateData())){

        }else {//如果是新增则修改状态
            //修改回款记录状态
            PsContractPay update = new PsContractPay();
            update.setId(Long.valueOf(psApproverRecord.getBusinessId()));
            update.setPayStatus(PayStatusEnum.FILE.getCode());
            payService.updateById(update);
        }
    }
}
