package net.ferrum.flowable.utils;

import net.ferrum.common.utils.DateUtils;
import net.ferrum.common.utils.StringUtils;
import net.ferrum.flowable.common.constant.ProcessConstants;
import net.ferrum.flowable.core.domain.ProcessQuery;
import org.flowable.common.engine.api.query.Query;
import org.flowable.common.engine.impl.db.SuspensionState;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.flowable.task.api.TaskQuery;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;
import org.flowable.variable.api.history.HistoricVariableInstanceQuery;

import java.util.Map;

/**
 * 流程工具类
 *
 * @author konbai
 * @since 2022/12/11 03:35
 */
public class ProcessUtils {

    public static void buildProcessSearch(Query<?, ?> query, ProcessQuery process) {
        if (query instanceof ProcessDefinitionQuery) {
            buildProcessDefinitionSearch((ProcessDefinitionQuery) query, process);
        } else if (query instanceof TaskQuery) {
            buildTaskSearch((TaskQuery) query, process);
        } else if (query instanceof HistoricTaskInstanceQuery) {
            buildHistoricTaskInstanceSearch((HistoricTaskInstanceQuery) query, process);
        } else if (query instanceof HistoricProcessInstanceQuery) {
            buildHistoricProcessInstanceSearch((HistoricProcessInstanceQuery) query, process);
        }

        else if (query instanceof HistoricVariableInstanceQuery) {
            buildHistoricVariableInstanceSearch((HistoricVariableInstanceQuery) query, process);
        }
    }

    /**
     * 构建流程定义搜索
     */
    public static void buildProcessDefinitionSearch(ProcessDefinitionQuery query, ProcessQuery process) {
        // 流程标识
        if (StringUtils.isNotBlank(process.getProcessKey())) {
            query.processDefinitionKeyLike("%" + process.getProcessKey() + "%");
        }
        // 流程名称
        if (StringUtils.isNotBlank(process.getProcessName())) {
            query.processDefinitionNameLike("%" + process.getProcessName() + "%");
        }
        // 流程分类
        if (StringUtils.isNotBlank(process.getCategory())) {
            query.processDefinitionCategory(process.getCategory());
        }
        // 流程状态
        if (StringUtils.isNotBlank(process.getState())) {
            if (SuspensionState.ACTIVE.toString().equals(process.getState())) {
                query.active();
            } else if (SuspensionState.SUSPENDED.toString().equals(process.getState())) {
                query.suspended();
            }
        }
    }

    /**
     * 构建任务搜索
     */
    public static void buildTaskSearch(TaskQuery query, ProcessQuery process) {
        Map<String, Object> params = process.getParams();
        if (StringUtils.isNotBlank(process.getProcessKey())) {
            query.processDefinitionKeyLike("%" + process.getProcessKey() + "%");
        }
        if (StringUtils.isNotBlank(process.getProcessName())) {
            query.processDefinitionNameLike("%" + process.getProcessName() + "%");
        }
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            query.taskCreatedAfter(DateUtils.parseDate(params.get("beginTime")));
            query.taskCreatedBefore(DateUtils.parseDate(params.get("endTime")));
        }

        String sKey = getBusynessKey(process);
        if(sKey != null) {
            query.processInstanceBusinessKeyLike(sKey);
        }



    }

    private static String getBusynessKey(ProcessQuery process){
        String sKey = null;

        if (StringUtils.isNotBlank(process.getSpecBatch()) || StringUtils.isNotBlank(process.getSpecName())) {
            //批次
            if (StringUtils.isNotBlank(process.getSpecBatch())){
                sKey = "%" + process.getSpecBatch();
                //规格名称
                if (StringUtils.isNotBlank(process.getSpecName())) {
                    sKey += "%" + ProcessConstants.PROCESS_BUSYNESSKEY_ConnString + "%" + process.getSpecName() + "%";
                }else{
                    sKey += "%" + ProcessConstants.PROCESS_BUSYNESSKEY_ConnString + "%";
                }
            }
            else{
                if (StringUtils.isNotBlank(process.getSpecName())) {
                    sKey = "%" + ProcessConstants.PROCESS_BUSYNESSKEY_ConnString + "%" + process.getSpecName() + "%";
                }else{
                    sKey = "%" + ProcessConstants.PROCESS_BUSYNESSKEY_ConnString + "%";
                }
            }

        }
        return sKey;
    }

    private static void buildHistoricTaskInstanceSearch(HistoricTaskInstanceQuery query, ProcessQuery process) {
        Map<String, Object> params = process.getParams();
        if (StringUtils.isNotBlank(process.getProcessKey())) {
            query.processDefinitionKeyLike("%" + process.getProcessKey() + "%");
        }
        if (StringUtils.isNotBlank(process.getProcessName())) {
            query.processDefinitionNameLike("%" + process.getProcessName() + "%");
        }
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            query.taskCompletedAfter(DateUtils.parseDate(params.get("beginTime")));
            query.taskCompletedBefore(DateUtils.parseDate(params.get("endTime")));
        }
        String sKey = getBusynessKey(process);
        if(sKey != null) {
            query.processInstanceBusinessKeyLike(sKey);
        }
    }

    /**
     * 构建历史流程实例搜索
     */
    public static void buildHistoricProcessInstanceSearch(HistoricProcessInstanceQuery query, ProcessQuery process) {
        Map<String, Object> params = process.getParams();

        /*
        // 流程标识
        if (StringUtils.isNotBlank(process.getProcessKey())) {
            query.processDefinitionKey(process.getProcessKey());
        }
        // 流程名称
        if (StringUtils.isNotBlank(process.getProcessName())) {
            query.processDefinitionName(process.getProcessName());
        }

         */
        String sKey = getBusynessKey(process);
        if(sKey != null) {
            query.processInstanceBusinessKeyLike(sKey);
        }

        // 流程类别
        if (StringUtils.isNotBlank(process.getCategory())) {
            query.processDefinitionCategory(process.getCategory());
        }
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            query.startedAfter(DateUtils.parseDate(params.get("beginTime")));
            query.startedBefore(DateUtils.parseDate(params.get("endTime")));
        }
    }


    public static void buildHistoricVariableInstanceSearch(HistoricVariableInstanceQuery query, ProcessQuery process) {
         // 规格批次
        if (StringUtils.isNotBlank(process.getSpecBatch())) {
            query.variableValueLike("lbl_Batch","%" + process.getSpecBatch() + "%");
        }
        // 规格名称
        if (StringUtils.isNotBlank(process.getSpecName())) {
            query.variableValueLike("lbl_name","%" + process.getSpecBatch() + "%");
        }
    }


}
