package me.zhengjie.modules.dm.activiti;

import me.zhengjie.modules.dm.printingApproval.service.DmPrintingApprovalService;
import me.zhengjie.modules.dm.printingRequest.domain.DmPrintingRequest;
import me.zhengjie.modules.dm.printingRequest.service.DmPrintingRequestService;
import me.zhengjie.modules.dm.printingRequest.service.dto.DmPrintingRequestDto;
import me.zhengjie.utils.SpringContextHolder;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

/**
 * 审批节点开始前的监听
 */
public class ApproveStartListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        System.out.println("审批任务开始监听  ApproveStartListener || 审批开始");
        String taskId = delegateTask.getId();
        Long requestId = (Long) delegateTask.getVariable("op_id");
        Integer is_auto = (Integer) delegateTask.getVariable("is_auto");
        System.out.println("taskId : "+taskId);
        System.out.println("param.requestId : "+requestId);
        DmPrintingRequestService printingRequestService = SpringContextHolder.getBean(DmPrintingRequestService.class);
        DmPrintingApprovalService printingApprovalService = SpringContextHolder.getBean(DmPrintingApprovalService.class);
        DmPrintingRequestDto requestDto = printingRequestService.findById(requestId);
        DmPrintingRequest printingRequest = printingRequestService.toEntity(requestDto);
        if("00".equals(printingRequest.getStatus()) || "02".equals(printingRequest.getStatus())){
            //修改审批列表对应事务
            printingApprovalService.resetTask(requestId,delegateTask.getAssignee(),taskId,delegateTask.getProcessInstanceId());
        }
    }
}
