package cc.smtweb.system.bpm.web.engine.flow;

import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.EntityHelper;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.framework.core.mvc.service.AbstractCompProvider;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.system.bpm.web.design.flow.FlowConst;
import cc.smtweb.system.bpm.web.engine.flow.entity.Comment;
import cc.smtweb.system.bpm.web.engine.flow.entity.ProcInst;
import cc.smtweb.system.bpm.web.engine.flow.entity.Task;
import cc.smtweb.system.bpm.web.engine.flow.entity.TaskRel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Akmm at 2022/5/26 18:40
 * 工作流数据提供者
 */
public class FlowProvider extends AbstractCompProvider {

    //根据id，获取流程实例
    public ProcInst findProcInst(long billId) {
        return doGetData("p_" + billId, () -> {
            EntityDao<ProcInst> bdao = DbEngine.getInstance().findDao(ProcInst.class);
            ProcInst bean = bdao.queryEntity(billId);
            if (bean == null) throw new BizException("没有找到指定流程实例（id=" + billId + "）！");
            EntityHelper.loadBeanText(bean.getTableName(), bean.getData(), null);
            return bean;
        });
    }

    //根据id，获取流程实例
    public Task findTask(long taskId) {
        return doGetData("a_" + taskId, () -> {
            EntityDao<Task> bdao = DbEngine.getInstance().findDao(Task.class);
            Task bean = bdao.queryEntity(taskId);
            if (bean == null) return null;
            EntityHelper.loadBeanText(bean.getTableName(), bean.getData(), null);
            return bean;
        });
    }

    //获取指定任务的前置任务
    public List<Task> findBeforeTasks(long taskId) {
        return doGetData("ab_" + taskId, () -> {
            EntityDao<Task> bdao = DbEngine.getInstance().findDao(Task.class);
            List<Task> list = bdao.queryEx(" t," + EntityHelper.getSchemaTableName(TaskRel.ENTITY_NAME) + " r " +
                    "where t.tsk_id=r.trl_src_task_id and r.trl_dst_task_id  = ?", taskId);
            if (CommUtil.isEmpty(list)) throw new BizException("没有找到指定流程前置任务（id=" + taskId + "）！");
            return list;
        });
    }

    //获取指定任务的前置任务
    public List<Task> findBeforeTasks(String taskIds) {
        EntityDao<Task> bdao = DbEngine.getInstance().findDao(Task.class);
        List<Task> list = bdao.queryEx(" t," + EntityHelper.getSchemaTableName(TaskRel.ENTITY_NAME) + " r " +
                "where t.tsk_id=r.trl_src_task_id and r.trl_dst_task_id in (" + taskIds + ")");
        if (CommUtil.isEmpty(list)) return new ArrayList<>();
        return list;
    }

    //获取指定任务的前置任务
    public List<Task> findAfterTasks(long taskId) {
        return doGetData("ab_" + taskId, () -> {
            EntityDao<Task> bdao = DbEngine.getInstance().findDao(Task.class);
            List<Task> list = bdao.queryEx(" t," + EntityHelper.getSchemaTableName(TaskRel.ENTITY_NAME) + " r " +
                    "where t.tsk_id=r.trl_dst_task_id and r.trl_src_task_id  = ?", taskId);
            if (CommUtil.isEmpty(list)) throw new BizException("没有找到指定流程前置任务（id=" + taskId + "）！");
            return list;
        });
    }

    //获取指定任务的前置任务
    public List<Task> findAfterTasks(String taskIds) {
        EntityDao<Task> bdao = DbEngine.getInstance().findDao(Task.class);
        List<Task> list = bdao.queryEx(" t," + EntityHelper.getSchemaTableName(TaskRel.ENTITY_NAME) + " r " +
                "where t.task_id=r.trl_src_task_id and r.trl_dst_task_id in (" + taskIds + ")");
        if (CommUtil.isEmpty(list)) return new ArrayList<>();
        return list;
    }

    //根据id，获取流程实例
    public List<Task> findCurTask(long billId) {
        return doGetData("ac_" + billId, () -> {
            EntityDao<Task> bdao = DbEngine.getInstance().findDao(Task.class);
            List<Task> list = bdao.queryWhere(" tsk_pri_id = ? and tsk_statu<?", billId, FlowConst.ActivityStatu.SUBMIT.value);
            if (list == null) return new ArrayList<>();
            return list;
        });
    }

    //获取流程所有任务
    public List<Task> findTasks(long billId) {
        return doGetData("aa_" + billId, () -> {
            EntityDao<Task> bdao = DbEngine.getInstance().findDao(Task.class);
            List<Task> list = bdao.queryWhere(" tsk_pri_id = ?", billId, FlowConst.ActivityStatu.SUBMIT.value);
            if (list == null) return new ArrayList<>();
            return list;
        });
    }

    //寻找流程的制单任务
    public Task findMakeTask(long bill_id) {
        return DbEngine.getInstance().findDao(Task.class).queryEntityWhere(" tsk_pri_id = ? and is_make = 1 order by tsk_end_time desc", bill_id);
    }

    public List<Comment> findComment(long bill_id) {
        return doGetData("cp_" + bill_id, () -> DbEngine.getInstance().findDao(Comment.class).queryWhere(" cmt_pri_id=? and cmt_statu>? order by cmt_end_time", bill_id, FlowConst.ActivityStatu.WAIT.value));

    }

    public Comment findTaskComment(long task_id) {
        return doGetData("ct_" + task_id, () -> DbEngine.getInstance().findDao(Comment.class).queryEntityWhere(" cmt_tsk_id=?", task_id));
    }
}
