package me.zhengjie.modules.dm.activiti;
import me.zhengjie.modules.dm.information.domain.DmInformation;
import me.zhengjie.modules.dm.information.service.DmInformationService;
import me.zhengjie.modules.dm.information.service.dto.DmInformationDto;
import me.zhengjie.modules.dm.informationApproval.service.DmInformationApprovalService;
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 info_id = (Long) delegateTask.getVariable("op_id");
        Integer is_auto = (Integer) delegateTask.getVariable("is_auto");
        System.out.println("taskId : "+taskId);
        System.out.println("param.info_id : "+info_id);
        DmInformationService informationService = SpringContextHolder.getBean(DmInformationService.class);
        DmInformationApprovalService informationApprovalService = SpringContextHolder.getBean(DmInformationApprovalService.class);
        DmInformationDto informationDto = informationService.findById(info_id);
        informationDto.setTaskId(taskId);//任务id
        informationDto.setProcInstId(processInstanceId);//流程实例id
        DmInformation information = informationService.toEntity(informationDto);
        //自动执行
        if(is_auto == 0){
            informationService.update(information);
        }else{
            //不自动执行,更新关联任务id
            informationService.updateTask(information);
        }

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