##全部，根据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)

##审批意见
DROP view vw_flow_comment;
CREATE VIEW vw_flow_comment AS (
SELECT
  hi.ID_ as cc,
	hc.ID_ AS commentId,
	hc.TYPE_ AS commentType,
	ht.ID_ AS taskId,
	ht.NAME_ AS taskName,
	ht.REV_ AS rev,
	CASE
WHEN hc.TYPE_ = 1 and ht.NAME_ !='提交申请' THEN
	'通过'
WHEN hc.TYPE_ = 2 THEN
	'退回'
WHEN hc.TYPE_ = 3 THEN
	'驳回'
WHEN hc.TYPE_ = 4 THEN
	'委派'
WHEN hc.TYPE_ = 5 THEN
	'转办'
WHEN hc.TYPE_ = 6 THEN
	'终止'
WHEN hc.TYPE_ = 7 THEN
	'撤回'
END AS commentResult,
 ht.PROC_INST_ID_ AS procInstId,
 ht.TASK_DEF_KEY_ AS taskDefKey,
 ht.EXECUTION_ID_ AS executionId,
 ht.DELETE_REASON_ AS deleteReason,
 DATE_FORMAT(
	ht.START_TIME_,
	'%Y-%m-%d %H:%i:%S'
) AS startTime,
 DATE_FORMAT(
	ht.END_TIME_,
	'%Y-%m-%d %H:%i:%S'
) AS endTime,
 ht.DURATION_ AS duration,
 hc.MESSAGE_ AS message,
 ru.nick_name AS assigneeName,
 rd.dept_name AS deptName,
 r.role_name as candidate
FROM
	act_hi_taskinst ht
LEFT JOIN act_hi_comment hc ON hc.TASK_ID_ = ht.ID_ or (hc.TASK_ID_ is null and hc.PROC_INST_ID_ = ht.PROC_INST_ID_)
LEFT JOIN act_hi_identitylink hi on hi.TYPE_ = 'candidate' and hi.TASK_ID_ = ht.ID_ and hi.USER_ID_ IS NULL
left JOIN sys_role r on r.role_id = hi.group_Id_
LEFT JOIN sys_user ru ON ru.user_id = ht.ASSIGNEE_
LEFT JOIN sys_dept rd ON rd.dept_id = ru.dept_id
where hc.TYPE_ is null or hc.TYPE_!='event')