package com.pusong.business.service.impl;

import com.pusong.business.domain.PsContractInfo;
import com.pusong.business.domain.PsContractPay;
import com.pusong.business.domain.bo.PsContractPayBo;
import com.pusong.business.domain.vo.PsCompanyInfoVo;
import com.pusong.business.domain.vo.PsContractBusinessVo;
import com.pusong.business.domain.vo.PsContractInfoVo;
import com.pusong.business.enums.ApproverTypeEnum;
import com.pusong.business.enums.CommonStatusEnum;
import com.pusong.business.enums.PayBuinessStatusEnum;
import com.pusong.business.mapper.PsContractPayMapper;
import com.pusong.business.service.IPsContractInfoService;
import com.pusong.business.service.IPsContractOperateService;
import com.pusong.business.service.approver.ApproverContainer;
import com.pusong.business.service.approver.ApproverService;
import com.pusong.common.core.utils.MapstructUtils;
import com.pusong.common.core.utils.StringUtils;
import com.pusong.system.domain.vo.SysOssVo;
import com.pusong.system.service.ISysOssService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 合同操作Service接口
 *
 * @author wls
 * @date 2024-07-29
 */
@RequiredArgsConstructor
@Service
public class IPsContractOperateServiceImpl implements IPsContractOperateService {

    private static final Logger log = LoggerFactory.getLogger(IPsContractOperateServiceImpl.class);
    private final IPsContractInfoService contractInfoService;
    @Resource
    private final ApproverContainer container;
    @Resource
    private final ISysOssService ossService;

    /**
     * 合同签章
     *
     * @param contractCode 合同编码
     * @return 合同基本信息
     */
    public void sign(String contractCode){
//        PsContractInfoVo info = contractInfoService.queryContractByCode(contractCode);
        //后续添加校验可再次添加
        //合同签章：
        ApproverService service = container.getService(ApproverTypeEnum.SIGN.getCode());
        service.apply(contractCode, contractCode,null, null);

        PsContractInfo psContractInfo = new PsContractInfo();
        psContractInfo.setContractCode(contractCode);
        psContractInfo.setSignStatus(CommonStatusEnum.ING.getCode());
        contractInfoService.updateByCode(psContractInfo);
    }


    /**
     * 回传合同
     * @param file 回传合同文件
     * @param contractCode 合同编码
     */
    @Transactional
    public String rollbackContract( MultipartFile file, String contractCode){
        PsContractInfoVo info = contractInfoService.queryContractByCode(contractCode);

        String checkStr = checkRollbackContract(info);
        if (checkStr != null)
            return checkStr;

        //后续添加校验可再次添加
        SysOssVo vo = ossService.upload(file);
        //发起回传合同审批
        ApproverService service = container.getService(ApproverTypeEnum.ROLBACK.getCode());
        service.apply(contractCode,contractCode,null, vo.getOssId()+"");
        //修改合同信息
        PsContractInfo psContractInfo = new PsContractInfo();
        psContractInfo.setContractCode(contractCode);
        psContractInfo.setRollBackStatus(CommonStatusEnum.ING.getCode());
        contractInfoService.updateByCode(psContractInfo);
        //若已存在回传的合同，则删除
        if(info.getRollBackPdf() != null){
            try{
                ossService.deleteWithValidByIds(List.of(info.getRollBackPdf()),null);
            }catch (Exception e){
                log.error("删除回传合同信息失败",e);
            }
        }

        return null;

    }

    private static String checkRollbackContract(PsContractInfoVo info) {
        if (!info.getSignStatus().equals(CommonStatusEnum.SUCCESS.getCode())){//没盖电子章
                return "未盖电子章，无法回传合同";
        }
        return null;
    }


    /**
     * 确认单
     * @param file 确认单
     * @param contractCode 合同编码
     */
    @Transactional
    public void confirm( MultipartFile file, String contractCode){
        PsContractInfoVo info = contractInfoService.queryContractByCode(contractCode);
        //后续添加校验可再次添加
        SysOssVo vo = ossService.upload(file);
        //修改合同信息
        PsContractInfo psContractInfo = new PsContractInfo();
        psContractInfo.setContractCode(contractCode);
        psContractInfo.setConfirmOssid(vo.getOssId());
        contractInfoService.updateByCode(psContractInfo);
        //若已存在确认单，则删除
        if(info.getRollBackPdf() != null){
            try{
                ossService.deleteWithValidByIds(List.of(info.getConfirmOssid()),null);
            }catch (Exception e){
                log.error("删除确认单失败",e);
            }
        }



    }
}
