package com.sifang.modules.flowable.tasks;

import com.sifang.modules.config.SfCacheService;
import com.sifang.modules.flowable.service.ApplicationDetailService;
import com.sifang.modules.flowpath.vo.TaskVo;
import com.sifang.modules.sys.common.utils.StringUtil;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.impl.context.Context;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.api.Task;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//创建任务的时候
@Component("oneTaskCompleteListener")
public class OneTaskCompleteListener implements TaskListener {
    private static Logger logger = LoggerFactory.getLogger(OneTaskCompleteListener.class);

    @Autowired
    public TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private ApplicationDetailService detailService;

    @Autowired
    SfCacheService cacheService;

    @Override
    public void notify(DelegateTask delegateTask) {
        logger.info("notify exec of oneTaskCompleteListener");
        String processInstanceId=delegateTask.getProcessInstanceId();
        logger.info(">>>创建任务的时候 oneTaskCompleteListener:{}",processInstanceId);
        String name=delegateTask.getName();
        String formId=delegateTask.getFormKey();
        String ccUserId=delegateTask.getOwner();
        String assignee=delegateTask.getAssignee();
        logger.info(">>>>>>>>>>>>processInstanceId:{},name:{},formId:{},{}",processInstanceId,name,formId,delegateTask.getId());
        String applicationId = (String)runtimeService.getVariable(processInstanceId, "applicationId");
        String userId=assignee;
        if(StringUtil.isNotBlank(ccUserId)){
            userId+=","+ccUserId;
        }
        logger.info(">>>>>>>>>>>>>>>>>userId :{}",userId);
        detailService.createTaskChangeTaskName(applicationId,formId,name,userId,processInstanceId);
        logger.info(">>>>>>>>>>>>ccuserId:{}",ccUserId);
        //判断上一级任务，有没有抄送任务
        if(StringUtil.isNotBlank(ccUserId)){
            String ifCC = cacheService.get(processInstanceId+"_"+name);
            if(!"1".equals(ifCC)){
                logger.info(">>>>>>>>>>>>>>>>OneTaskCompleteListener  ccUserId:{}",ccUserId);
                String[] userids = ccUserId.split(",");
                for(String id:userids){
                    logger.info("----oneTaskCompleteListener 抄送id:{}",id);
                    Task ta = taskService.newTask();
                    ta.setName("抄送");
                    ta.setCategory(TaskVo.TYPE_THREE);
                    ta.setAssignee(id);
                    ta.setFormKey(formId);
                    ta.setParentTaskId(processInstanceId);
                    ta.setTenantId(applicationId);
                    logger.info("除了提交时抄送的parentTaskId:"+delegateTask.getId());
                    taskService.saveTask(ta);
                    Map<String,String> mapc=new HashMap<>();
                    mapc.put("applicationId",applicationId);
               /* mapc.put("formId",formId);
                mapc.put("processInstanceId",processInstanceId);*/
                    taskService.setVariables(ta.getId(),mapc);
                    cacheService.setex(processInstanceId+"_"+name,60,"1");
                }
            }

        }
    }
}
