package me.zhengjie.modules.dm.activiti;
import me.zhengjie.modules.dm.printingApproval.domain.DmPrintingApproval;
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 ReserveSubmitStartListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        System.out.println("提交预约监听  ReserveSubmitStartListener || 用户准备开始提交预约");
        String taskId = delegateTask.getId();
        String processInstanceId = delegateTask.getProcessInstanceId();
        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);
        requestDto.setTaskId(taskId);//任务id
        requestDto.setProcInstId(processInstanceId);//流程实例id
        DmPrintingRequest printingRequest = printingRequestService.toEntity(requestDto);
        //自动执行
        if(is_auto == 0){
           printingRequestService.update(printingRequest);
        }else{
            //不自动执行,更新关联任务id
            printingRequestService.updatePringTask(printingRequest);
        }

//        if("00".equals(printingRequest.getStatus())){
//            //修改审批列表对应事务
//            printingApprovalService.resetTask(requestId,delegateTask.getAssignee(),taskId,delegateTask.getProcessInstanceId());
//        }
   }
}
