##全部，根据finishTime判断代办，已办
DROP view vw_flow_all;
CREATE VIEW vw_flow_all AS (
	SELECT
		RES.ID_ AS procInsId,
		DEF.DEPLOYMENT_ID_ AS deployId,
		RES.START_TIME_ AS createTime,
		RES.END_TIME_ AS finishTime,
		CASE
	WHEN RES.END_TIME_ IS NULL THEN
		TIMESTAMPDIFF(
			MINUTE,
			RES.START_TIME_,
			DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S')
		)
	ELSE
		TIMESTAMPDIFF(
			MINUTE,
			RES.START_TIME_,
			RES.END_TIME_
		)
	END AS duration,
	DEF.NAME_ AS procDefName,
	DEF.VERSION_ AS procDefVersion,
	DEF.CATEGORY_ AS category,
	DEF.KEY_ AS procDefKey,
	ppa.id AS businessKey,
	ppa.dept_id as businessDeptId,
	ppa.proj_name AS businessKeyName,
  ppa.par_proj_name as businessKeyParName,
	ru.user_id AS startUserId,
	ru.nick_name AS startUserName,
	rd.dept_name AS startDeptName,
	ht.*
FROM
	ACT_HI_PROCINST RES
LEFT OUTER JOIN ACT_RE_PROCDEF DEF ON RES.PROC_DEF_ID_ = DEF.ID_
LEFT OUTER JOIN pro_project_apply ppa ON RES.BUSINESS_KEY_ = ppa.id
LEFT JOIN sys_user ru ON ru.user_id = RES.START_USER_ID_
LEFT JOIN sys_dept rd ON rd.dept_id = ru.dept_id
LEFT JOIN (
	SELECT
		ht.ID_ AS taskId,
		ht.NAME_ AS taskName,
		tu.user_id AS assigneeId,
		tu.nick_name AS assigneeName,
		td.dept_name AS assigneeDeptName,
		ht.PROC_INST_ID_
	FROM
		act_hi_taskinst ht
	LEFT JOIN sys_user tu ON tu.user_id = ht.ASSIGNEE_
	LEFT JOIN sys_dept td ON td.dept_id = tu.dept_id
	WHERE
		ht.ID_ IN (
			SELECT
				MAX(ID_)
			FROM
				act_hi_taskinst
			GROUP BY
				PROC_INST_ID_
		)
	GROUP BY ht.PROC_INST_ID_
) ht ON ht.PROC_INST_ID_ = RES.PROC_INST_ID_
);

##待办
DROP view vw_flow_await;
CREATE VIEW vw_flow_await AS (SELECT
	RES.ID_ AS taskId,
	RES.NAME_ AS taskName,
	re.ID_ AS procInsId,
	DEF.DEPLOYMENT_ID_ AS deployId,
	DATE_FORMAT(re.START_TIME_, '%Y-%m-%d %H:%i:%S') AS createTime,
	TIMESTAMPDIFF(
		MINUTE,
		re.START_TIME_,
		DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%S')
	) AS duration,
	DEF.NAME_ AS procDefName,
	DEF.VERSION_ AS procDefVersion,
	DEF.CATEGORY_ AS category,
	DEF.KEY_ AS procDefKey,
	ppa.id AS businessKey,
	ppa.dept_id AS businessDeptId,
	ppa.proj_name AS businessKeyName,
  ppa.par_proj_name as businessKeyParName,
	ru.user_id AS startUserId,
	ru.nick_name AS startUserName,
	rd.dept_name AS startDeptName,
	RES.SUSPENSION_STATE_,
	RES.ASSIGNEE_,
	LINK.USER_ID_,
	LINK.GROUP_ID_
FROM
	ACT_RU_TASK RES
LEFT JOIN act_ru_execution re ON re.PARENT_ID_ IS NULL
AND re.PROC_INST_ID_ = RES.PROC_INST_ID_
LEFT OUTER JOIN ACT_RE_PROCDEF DEF ON re.PROC_DEF_ID_ = DEF.ID_
LEFT OUTER JOIN pro_project_apply ppa ON re.BUSINESS_KEY_ = ppa.id
LEFT JOIN sys_user ru ON ru.user_id = re.START_USER_ID_
LEFT JOIN sys_dept rd ON rd.dept_id = ru.dept_id
left JOIN ACT_RU_IDENTITYLINK LINK on LINK.TASK_ID_ = RES.ID_ AND LINK.TYPE_ = 'candidate'
WHERE
	RES.SUSPENSION_STATE_ = 1)


##已办
DROP view vw_flow_finished;
CREATE VIEW vw_flow_finished AS (SELECT
	RES.ID_ AS taskId,
	RES.NAME_ AS taskName,
	RES.PROC_INST_ID_ AS procInsId,
	pd.DEPLOYMENT_ID_ as deployId,
	DATE_FORMAT(RES.START_TIME_, '%Y-%m-%d %H:%i:%S') AS createTime,
	DATE_FORMAT(RES.END_TIME_, '%Y-%m-%d %H:%i:%S') AS endTime,
	TIMESTAMPDIFF(
		MINUTE,
		res.START_TIME_,
		RES.END_TIME_
	) AS duration,
DEF.END_TIME_ as finishTime,
	pd.NAME_ AS procDefName,
	pd.CATEGORY_ AS category,
	pd.KEY_ AS procDefKey,
	ppa.id AS businessKey,
	ppa.dept_id AS businessDeptId,
	ppa.proj_name AS businessKeyName,
  ppa.par_proj_name as businessKeyParName,
	ru.user_id AS startUserId,
	ru.nick_name AS startUserName,
	rd.dept_name AS startDeptName,
	RES.ASSIGNEE_
FROM
	act_hi_taskinst RES
LEFT OUTER JOIN act_hi_procinst DEF ON RES.PROC_INST_ID_ = DEF.PROC_INST_ID_
LEFT OUTER join act_re_procdef pd on RES.PROC_DEF_ID_ = pd.ID_
LEFT OUTER JOIN pro_project_apply ppa ON DEF.BUSINESS_KEY_ = ppa.id
LEFT JOIN sys_user ru ON ru.user_id = RES.ASSIGNEE_
LEFT JOIN sys_dept rd ON rd.dept_id = ru.dept_id
WHERE
	RES.ASSIGNEE_ is not null and RES.END_TIME_ is not null)