package com.casic.missiles.service.listeners.processor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONObject;
import com.casic.missiles.dto.flowable.NotifyEventStatusDTO;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.notifyevent.ActionStatusBeanEnum;
import com.casic.missiles.enums.notifyevent.PrintFileDataBeanNameAliasEnum;
import com.casic.missiles.enums.system.ApprovalStatusEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.mapper.business.BusinessSiteExecutiveInfoMapper;
import com.casic.missiles.model.business.BusinessSiteExecutiveInfo;
import com.casic.missiles.service.listeners.NotifyEventPostProcessor;
import com.casic.missiles.service.listeners.register.PrintFileRegister;
import com.casic.missiles.utils.DictCodeUtils;
import com.casic.missiles.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @author zt
 * <p>
 * 外场检测审批通过状态回填
 */
@Slf4j
@Service(ActionStatusBeanEnum.SITE_EXECUTIVE_INFO_BEAN_NAME)
public class SiteExecutiveInfoEventPostProcessor implements NotifyEventPostProcessor {

    @Override
    public void doHandleNotifyEvent(NotifyEventStatusDTO notifyEventStatusDTO) {
        Long id = Long.valueOf(notifyEventStatusDTO.getId());
        Assert.isFalse(Objects.isNull(id), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        BusinessSiteExecutiveInfoMapper siteExecutiveInfoMapper = SpringContextUtil.getBean(BusinessSiteExecutiveInfoMapper.class);
        BusinessSiteExecutiveInfo siteExecutive = new BusinessSiteExecutiveInfo();
        siteExecutive.setId(id);
        //设置审批状态为通过
        siteExecutive.setApprovalStatus(ApprovalStatusEnum.PASSED);
        siteExecutive.setUpdateTime(DateUtil.formatDateTime(new Date()));
        siteExecutiveInfoMapper.updateById(siteExecutive);
//        //审批通过 根据模板生成审批单打印样例
//        //获取证书报告信息
//        siteExecutive = siteExecutiveInfoMapper.selectById(id);
//        //进行证书报告生成，并在远程注册服务器注册
//        PrintFileRegister printFileRegister = SpringContextUtil.getBean(PrintFileRegister.class);
//        try {
//            DictCodeUtils.convertDictCodeToName(siteExecutive);
//            String printFileName = printFileRegister.registerPrintFile(siteExecutive, PrintFileDataBeanNameAliasEnum.EXTERNAL_CALIBRATION_FILE_BEAN);
//            setSitePrintFileName(siteExecutive.getId(), printFileName, siteExecutiveInfoMapper);
//        } catch (Exception ex) {
//            log.error("打印异常，证书报告信息为{},异常信息为{}", JSONObject.toJSON(siteExecutive), ex);
//        }
    }

//    private void setSitePrintFileName(Long id, String printFileName, BusinessSiteExecutiveInfoMapper siteExecutiveInfoMapper) {
//        //生成证书打印信息
//        BusinessSiteExecutiveInfo sitePrintFileName = new BusinessSiteExecutiveInfo();
//        sitePrintFileName.setId(id);
//        sitePrintFileName.setPrintFileName(printFileName);
//        siteExecutiveInfoMapper.updateById(sitePrintFileName);
//    }

}
