##查询项目人中重复人员
select sp.id,sp.projectName,spu.dept_name,sdd.dict_label,su.nick_name,su.phonenumber from sur_project sp
left join sur_project_userinfo spu on spu.project_id=sp.id
left join sys_user su on su.user_id = spu.user_id
left join sys_dict_data sdd on sdd.dict_type='sys_job_type' and sdd.dict_value = spu.job_type
where su.user_id in (
select user_id from (select user_id,count(1) total from sur_project_userinfo
where 1=1 group by user_id) t where t.total>1 ORDER by t.total desc)


##隐患排查视图
DROP VIEW vw_smz_ssp_problemmodify_audit;
CREATE VIEW vw_smz_ssp_problemmodify_audit AS (
	SELECT
		a.*, a.updateTime AS audittime,
		b.deptId,
		b.projectName as prjName,
		u.nick_name AS createBy
	FROM
		smz_ssp_problemmodify a
	LEFT OUTER JOIN sur_project b ON a.projectId = b.id
	LEFT OUTER JOIN sys_user u ON a.createUser = u.phonenumber
);

##

##全部，根据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,
	sp.id AS businessKey,
	sp.deptId as businessDeptId,
	sp.projectName AS businessKeyName,
	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 sur_project sp ON RES.BUSINESS_KEY_ = sp.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,
	sp.id AS businessKey,
	sp.deptId AS businessDeptId,
	sp.projectName AS businessKeyName,
	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 sur_project sp ON re.BUSINESS_KEY_ = sp.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)
##下面是当前登录人代办查询条件
--  AND (
-- 	RES.ASSIGNEE_ = 1
-- 	OR (
-- 		RES.ASSIGNEE_ IS NULL
-- 		AND EXISTS (
-- 			SELECT
-- 				LINK.ID_
-- 			FROM
-- 				ACT_RU_IDENTITYLINK LINK
-- 			WHERE
-- 				LINK.TASK_ID_ = RES.ID_
-- 			AND LINK.TYPE_ = 'candidate'
-- 			AND (
-- 				LINK.USER_ID_ = 1
-- 				OR (
-- 					LINK.GROUP_ID_ IN (1, 42, 45, 46)
-- 				)
-- 			)
-- 		)
-- 	)
-- )

##审批意见
DROP view vw_flow_comment;
CREATE VIEW vw_flow_comment AS (
SELECT
	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.REV_ != 2 THEN
	'通过'
WHEN hc.TYPE_ != 1 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_
LEFT JOIN act_hi_identitylink hi on hi.TASK_ID_ = ht.ID_ and hi.TYPE_ = 'candidate'
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'
)

##审批意见V2
DROP view vw_flow_comment;
CREATE VIEW vw_flow_comment AS (
SELECT
	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.TASK_ID_ = ht.ID_ and hi.TYPE_ = 'candidate'
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')


##已办
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,
	sp.id AS businessKey,
	sp.deptId AS businessDeptId,
	sp.projectName AS businessKeyName,
	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 sur_project sp ON DEF.BUSINESS_KEY_ = sp.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)

##君哥其它sql
CREATE VIEW vw_smz_ssp_problemmodify_audit
AS

SELECT p.*,a.createtime AS audittime FROM (
SELECT a.*,b.deptId,b.projectName prjName,u.nick_name AS createBy
FROM smz_ssp_problemmodify a,sur_project b ,sys_user u  WHERE a.projectId=b.id AND a.isDel!=1 AND b.isDel!=1 AND a.createUser=u.phonenumber
) p
LEFT JOIN 
(
SELECT * FROM smz_ssp_auditinfo WHERE id IN(SELECT MAX(id) FROM smz_ssp_auditinfo WHERE processState=4 AND isDel=0
GROUP BY mainid)
) a ON p.id=a.mainid


CREATE VIEW vw_sur_project_build_node_data_all
AS
SELECT a.id, node_id, project_id,p.projectName,start_date, end_date,plan_start_date,plan_end_date, STATUS, a.remark, files, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time ,node_text,node_lvl,node_type
                          FROM sur_project_build_node_data a,base_build_node b,sur_project p WHERE a.node_id=b.id AND a.project_id=p.id AND a.is_del!=1 AND b.is_del!=1 AND p.isdel!=1



CREATE VIEW vw_sur_project_build_node_data_current
AS                     
SELECT * FROM vw_sur_project_build_node_data_all WHERE LENGTH(node_lvl)=2 AND plan_start_date IS NOT NULL AND start_date IS NOT NULL AND end_date IS NULL
UNION
SELECT * FROM vw_sur_project_build_node_data_all WHERE node_type=0 AND LENGTH(node_lvl)=6 AND plan_start_date IS NOT NULL
UNION
SELECT * FROM vw_sur_project_build_node_data_all WHERE node_type=1 AND LENGTH(node_lvl)=4 AND plan_start_date IS NOT NULL








CREATE VIEW vw_sur_project_schedule
AS
SELECT a.*,b.projectName,b.projectSort,b.projectSchedule FROM sur_project_schedule a,sur_project b WHERE a.project_id=b.id AND IF(b.isdel IS NULL ,0,b.isdel)=0 AND IF(a.is_del IS NULL ,0,a.is_del)=0





CREATE VIEW vw_sur_project
AS
SELECT 
  id,
  deptId,
  projectName,
  projectCode,
  paretProjectName,
  paretProjectCode,
  simpleName,
  projectType,
  projiectLevel,
  projectRegional,
  projectAddress,
  projectNature,
  licenceNumber,
  projectApproval,
  projectPerson,
  projectPhone,
  longitude,
  latitude,
  projectTimeLimit,
  IF(o.totalInvestment,o.totalInvestment,0) totalInvestment,
  floorArea,
  totalOutputValue,
  plannedCompletionTime,
  scheduledStartTime,
  actualOperatingTime,
  actualCompletionTime,
  IF(o.contractAmount,o.contractAmount,0) contractAmount,
  IF(o.paidAmount,o.paidAmount,0) paidAmount,
  IF(o.onAccountAmount,o.onAccountAmount,0) onAccountAmount,
  projectSchedule,
  projectSummarize,
  isDel,
  projectStatus,
  servicePersonnel,
  supervisorPersonnel,
  generalContractor,
  create_by,
  create_time,
  update_by,
  update_time,
  remark,
  weightType,
  projectSort,
  progressVisible
FROM  sur_project  p LEFT JOIN 
(
SELECT a.*,b.contractAmount,c.paidAmount,d.onAccountAmount
FROM 
(SELECT project_id,money AS totalInvestment  FROM sur_project_cost_output WHERE cost_type=1 ) a,
(SELECT project_id,money AS contractAmount   FROM sur_project_cost_output WHERE cost_type=3) b,
(SELECT project_id,money AS paidAmount   FROM sur_project_cost_output WHERE cost_type=4) c,
(SELECT project_id,money AS onAccountAmount   FROM sur_project_cost_output WHERE cost_type=5) d
WHERE a.project_id=b.project_id AND a.project_id=c.project_id AND a.project_id=d.project_id) o ON p.id=o.project_id


广联达分组信息中有重复的companyId,companyTypeId因此写view来解决
CREATE VIEW vw_sur_project_attendance_group
AS
SELECT * FROM sur_project_attendance_group WHERE id IN (
SELECT MIN(id) FROM sur_project_attendance_group GROUP BY companyId,companyTypeId)
 



create view view_sur_project_attendance_group
as
select * from vw_sur_project_attendance_group where  cfgid  not  in (select id from sur_project_attendance_cfg where is_del=0 and vendors_code='huazhu')
union
SELECT  id,  cfgid,  app_id,  serverid,  bizLicense,  companyCode,  companyId,  companyName, 1 companyTypeId,  vendorId,  name,  leaderName,  leaderPhone,  teamId,  teamName,  type,  leaderId,  deleted,  createTimestamp,  platformGroupId,  platformTeamId,  enterDate,  exitDate,  remark,  is_del,  create_by,  create_time,  update_by,  update_time
FROM vw_sur_project_attendance_group where  cfgid  in (select id from sur_project_attendance_cfg where is_del=0 and vendors_code='huazhu')
and companyName in (select unitName from sur_project_unit_info where unittype=2)
union
SELECT  id,  cfgid,  app_id,  serverid,  bizLicense,  companyCode,  companyId,  companyName, 8 companyTypeId,  vendorId,  name,  leaderName,  leaderPhone,  teamId,  teamName,  type,  leaderId,  deleted,  createTimestamp,  platformGroupId,  platformTeamId,  enterDate,  exitDate,  remark,  is_del,  create_by,  create_time,  update_by,  update_time
FROM vw_sur_project_attendance_group where cfgid  in (select id from sur_project_attendance_cfg where is_del=0 and vendors_code='huazhu')
and companyName in (select unitName from sur_project_unit_info where unittype=4)
union
SELECT  id,  cfgid,  app_id,  serverid,  bizLicense,  companyCode,  companyId,  companyName, 2 companyTypeId,  vendorId,  name,  leaderName,  leaderPhone,  teamId,  teamName,  type,  leaderId,  deleted,  createTimestamp,  platformGroupId,  platformTeamId,  enterDate,  exitDate,  remark,  is_del,  create_by,  create_time,  update_by,  update_time
FROM vw_sur_project_attendance_group where cfgid  in (select id from sur_project_attendance_cfg where is_del=0 and vendors_code='huazhu')
and companyName not in (select unitName from sur_project_unit_info where unittype=4 or unittype=2)

