package com.ho.flow.order1;

import cn.hutool.core.date.DateUtil;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

/**
 * @author fancl
 * @desc: 工单处理流程
 * @date 2023/1/2
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProcInstOrder1Demo {
    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;

    /**
     * 发布流程 流程部署
     */
    @Test
    public void initDeployment() {
        String fileName = "processes/workOrder.bpmn20.xml";
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource(fileName)
                .name("1租户的流程")
                .tenantId("1")
                //.category("workOrder")
                .deploy();
        System.out.println("流程部署名称：" + deployment.getName() + " key: " + deployment.getKey() + " id:" + deployment.getId());
    }

    /**
     * 创建流程实例
     */
    @Test
    public void initProcessInstance() {
        // 流程定义KEY
        String processDefinitionKey = "workOrder";
        // 业务表KEY（用于把业务数据与Activiti7流程数据相关联） 实际可以为工单号
        String businessKey = "4208169753200945";

        // 参数
        Map<String, Object> variables = new HashMap<>(16);
        //可以查到这个工单的组织机构列表
        variables.put("stationId","251");
        //类型
        variables.put("assignType","workOrder");
        //设置候选人,因为是多个,使用集合
        //List<String> toUsers = Arrays.asList("fan","lisi");
        variables.put("startUser", "fan");
        //租户id,使用集团id
        String tenantId = "1";
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKeyAndTenantId(processDefinitionKey,businessKey, variables,tenantId);
        System.out.println("流程实例ID：" + processInstance.getProcessInstanceId());
        System.out.println("业务businesKey：" + processInstance.getBusinessKey());
    }

    //查询历史记录
    @Test
    public void queryTaskHistoryByProcInst(){
        String procInstId = "285001";
        List<HistoricTaskInstance> procInstList = historyService.createHistoricTaskInstanceQuery().processInstanceId(procInstId)
                .orderByHistoricTaskInstanceStartTime().asc().list();
        for (HistoricTaskInstance hti : procInstList) {
            System.out.println("历史任务ID：" + hti.getId());
            System.out.println("流程定义ID：" + hti.getProcessDefinitionId());
            System.out.println("流程执行实例ID：" + hti.getExecutionId());
            System.out.println("流程结束时间：" + DateUtil.format(hti.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
            System.out.println("流程的处理人是：" + hti.getAssignee());
            System.out.println("################################");
        }
    }

    /**
     * 查询流程部署列表
     */
    @Test
    public void listDeployments() {
        List<Deployment> deployments = repositoryService.createDeploymentQuery()
                .deploymentKey("order2").deploymentTenantId("1").list();
        if (!deployments.isEmpty()) {
            deployments.forEach(deployment -> {
                System.out.println("Id：" + deployment.getId());
                System.out.println("Name：" + deployment.getName());
                System.out.println("DeploymentTime：" + deployment.getDeploymentTime());
                System.out.println("Key：" + deployment.getKey());
            });
        }
    }

    /**
     * 删除对流程实例、历史流程实例和作业的给定部署和级联删除
     */
    @Test
    public void deleteDeployment() {
        String deploymentId = "685001";
        this.repositoryService.deleteDeployment(deploymentId, true);
    }



    //删除部署的任务
    @Test
    public void deleteDeployedProcess() {
        List<String> list = Arrays.asList("147501", "150001", "155001", "172501");
        String deployId1 = "142501";
        for (String s : list) {
            repositoryService.deleteDeployment(s, true);
        }
        repositoryService.deleteDeployment(deployId1, true);
    }


}
