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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pusong.business.domain.PsApproverRecord;
import com.pusong.business.domain.PsContractInfo;
import com.pusong.business.domain.PsTaskAppoint;
import com.pusong.business.domain.PsTaskMain;
import com.pusong.business.domain.bo.PsContractBusinessBo;
import com.pusong.business.domain.bo.PsContractInfoBo;
import com.pusong.business.domain.vo.PsContractInfoVo;
import com.pusong.business.enums.*;
import com.pusong.business.mapper.PsTaskAppointMapper;
import com.pusong.business.mapper.PsTaskMainMapper;
import com.pusong.business.service.IPsCompanyInfoService;
import com.pusong.business.service.IPsContractBusinessService;
import com.pusong.business.service.IPsContractInfoService;
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.List;
import java.util.stream.Collectors;

/**
 * 修改合同审批处理类
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class UpdateContractApproverServiceImpl extends ApproverAbstractServiceImpl {
    @Override
    public String approverType() {
        return ApproverTypeEnum.UPDATE.getCode();
    }
    private final IPsContractBusinessService businessService;
    private final IPsContractInfoService contractInfoService;
    private final IPsTaskService taskService;
    private final PsTaskMainMapper taskMainMapper;
    private final PsTaskAppointMapper appointMapper;
    private final IPsCompanyInfoService companyInfoService;

    /**
     * 1 审批成功 合同状态变为待派单  合同已派单：任务重置
     *
     * @param psApproverRecord 神瀑类
     */
    @Override
    public void success(PsApproverRecord psApproverRecord) {
        //1查询源数据
        PsContractInfoVo src = contractInfoService.queryContractByCode(psApproverRecord.getContractCode());
        //json化修改的数据
        PsContractInfoBo bo = JSON.parseObject(psApproverRecord.getUpdateData(),PsContractInfoBo.class);
//        List<PsContractBusinessBo> businessBoslist = bo.getServiceCompanyInfoList().stream().flatMap(com -> com.getBusinessList().stream()).toList();
        //2.删除所有服务类型和服务项目,.插入服务类型
//        businessService.saveList(businessBoslist,bo.getContractCode());
//        //保存公司合同关联表
//        companyInfoService.saveContractCompany(bo.getServiceCompanyInfoList(),src.getContractCode());

        contractInfoService.update(src, bo);


        //3.修改后操作
//        afterUpdateOperate(bo, src);
        //插入合同
//        PsContractInfo add = new PsContractInfo();
//        add.setCompanyId(bo.getCompanyInfoBo().getId());//公司id
//        MapstructUtils.convert(bo, add);
//        //合同状态已汇款
//        add.setContractStatus(ContractStatusEnum.RETURN.getCode());
//        contractInfoService.updateByCode(add);
//        //4.生成合同
//        contractInfoService.makePdf(add,bo,true);
        //5.任务状态变为待派单，子任务删除
//        List<PsTaskMain> task = taskMainMapper.selectList(Wrappers.<PsTaskMain>lambdaQuery().eq(PsTaskMain::getContractCode, psApproverRecord.getContractCode()).eq(PsTaskMain::getDelFlag, 0));
//        List<Long> list = task.stream().map(PsTaskMain::getId).collect(Collectors.toList());
//        if(CollectionUtils.isEmpty(list)){
//            return;
//        }
//        //任务变为待派单
//        taskMainMapper.update(Wrappers.<PsTaskMain>lambdaUpdate()
//                .set(PsTaskMain::getTaskStatus, TaskStatusEnum.INIT.getCode())
//                .in(PsTaskMain::getDelFlag,0)
//                .in(PsTaskMain::getId,list));
//        //删除子任务
//        appointMapper.update(Wrappers.<PsTaskAppoint>lambdaUpdate().set(PsTaskAppoint::getAppointStatus, TaskStatusEnum.AppointStatusEnum.CANCEL.getCode()).in(PsTaskAppoint::getTaskId,list));
    }
    /**
     * 修改合同后操作
     *  1.已回款之后的合同修改后走审批
     *  2.已回款合同：新的合同金额<已付金额：展示申请退款
     *  3.合同签字盖章成功:修改合同成功，合同签字盖章变回未签章状态，需重新进行签章申请
     */
    private void afterUpdateOperate(PsContractInfoBo updateBo, PsContractInfoVo srcVo){

        //3.签章：合同签字盖章成功:修改合同成功，合同签字盖章变回未签章状态，需重新进行签章申请
        if(StringUtils.equals(srcVo.getSignStatus(), CommonStatusEnum.SUCCESS.getCode()) ){
            updateBo.setSignStatus(CommonStatusEnum.INIT.getCode());
        }

    }

    /**
     * 审批失败合同状态变为原状态
     * @param psApproverRecord
     */
    @Override
    public void fail(PsApproverRecord psApproverRecord) {
        PsContractInfoBo bo = JSON.parseObject(psApproverRecord.getUpdateData(),PsContractInfoBo.class);
        PsContractInfo add = new PsContractInfo();
        add.setContractCode(psApproverRecord.getContractCode());
        add.setContractStatus(bo.getContractStatusCode());
        contractInfoService.updateByCode(add);
    }
}
