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

import cn.hutool.core.lang.Assert;
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.system.ApprovalStatusEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.service.listeners.NotifyEventPostProcessor;
import com.casic.missiles.mapper.business.BusinessCertificatePrintMapper;
import com.casic.missiles.model.business.BusinessCertificatePrint;
import com.casic.missiles.utils.SpringContextUtil;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author cz
 *
 * 证书打印审批通过状态回填
 */
@Service(ActionStatusBeanEnum.CERTIFICATE_BEAN_NAME)
public class CertificatePrintEventPostProcessor 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);
        });
        BusinessCertificatePrintMapper certificateReportMapper = SpringContextUtil.getBean(BusinessCertificatePrintMapper.class);
        BusinessCertificatePrint certificatePrint =BusinessCertificatePrint.builder()
                .id(id)
                .approvalStatus(ApprovalStatusEnum.PASSED)
                .printStatus("1")
                .build();
        int row = certificateReportMapper.updateById(certificatePrint);
        if (row <= 0) {
            throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
        }
    }


}
