<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zcloud.mapper.datasource.eightwork.EightWorkMapper">
	<update id="invalid">
		update ${table} set STEP_ID = -1,STEP_REASON = #{STEP_REASON},OPERATOR =#{OPERATOR},OPERATTIME=#{OPERATTIME} where ${PRIMARY_KEY} = #{WORK_ID}
	</update>

	<select id="getEightWorks" resultType="pd">
		SELECT
			*,
			DATE_FORMAT(tmp.ACT_TIME,'%Y-%m') WORK_MONTH
		FROM
			( SELECT * FROM (SELECT
		*
	FROM
		(
		WITH RankedData AS (
			SELECT
			tmp.*,
			DATE_FORMAT( tmp.ACT_TIME, '%Y-%m' ) WORK_MONTH,
			ROW_NUMBER() OVER ( PARTITION BY tmp.WORK_ID, tmp.CORPINFO_ID ORDER BY tmp.NEXT_STEP_ID DESC ) AS rn
		FROM
		`vi_eightwork_flow_log` AS tmp )
		SELECT * FROM RankedData WHERE rn =1) log) log
			           where log.CORPINFO_ID=#{CORPINFO_ID} and log.WORK_ID IN (
						   SELECT
							   f.BLINDBOARD_ID id
						   FROM
							   BUS_BLINDBOARD f
						   WHERE
							   ISDELETE = 0 and f.STEP_ID != -1
							 AND CORPINFO_ID = #{CORPINFO_ID} UNION ALL
						   SELECT
							   f.BREAKGROUND_ID id
						   FROM
							   BUS_BREAKGROUND f
						   WHERE
							   ISDELETE = 0 and f.STEP_ID != -1
							 AND CORPINFO_ID = #{CORPINFO_ID} UNION ALL
						   SELECT
							   f.CONFINEDSPACE_ID id
						   FROM
							   BUS_CONFINEDSPACE f
						   WHERE
							   ISDELETE = 0 and f.STEP_ID != -1
							 AND CORPINFO_ID = #{CORPINFO_ID} UNION ALL
						   SELECT
							   f.ELECTRICITY_ID id
						   FROM
							   BUS_ELECTRICITY f
						   WHERE
							   ISDELETE = 0 and f.STEP_ID != -1
							 AND CORPINFO_ID = #{CORPINFO_ID} UNION ALL
						   SELECT
							   f.CUTROAD_ID id
						   FROM
							   BUS_CUTROAD f
						   WHERE
							   ISDELETE = 0 and f.STEP_ID != -1
							 AND CORPINFO_ID = #{CORPINFO_ID} UNION ALL
						   SELECT
							   f.HIGHWORK_ID id
						   FROM
							   BUS_HIGHWORK f
						   WHERE
							   ISDELETE = 0 and f.STEP_ID != -1
							 AND CORPINFO_ID = #{CORPINFO_ID} UNION ALL
						   SELECT
							   f.HOISTING_ID id
						   FROM
							   BUS_HOISTING f
						   WHERE
							   ISDELETE = 0 and f.STEP_ID != -1
							 AND CORPINFO_ID = #{CORPINFO_ID} UNION ALL
						   SELECT
							   f.HOTWORK_ID id
						   FROM
							   BUS_HOTWORK f
						   WHERE
							   ISDELETE = 0 and f.STEP_ID != -1
							 AND CORPINFO_ID = #{CORPINFO_ID})  HAVING 1 ORDER BY log.ACT_TIME DESC ) tmp
		GROUP BY
			tmp.WORK_ID
		order by tmp.ACT_TIME DESC
	</select>

	<select id="listAllForDiagnosis" parameterType="pd" resultType="pd">
		select count(1) as count,sum(case when f.STEP_ID=15 then 1 else 0 end) as pass_count from BUS_BLINDBOARD f  where DATE_FORMAT(f.CREATTIME,'%Y-%m')=#{MONTH} and f.CORPINFO_ID = #{CORPINFO_ID} and ISDELETE = 0
		union all
		select count(1) as count,sum(case when f.STEP_ID=15 then 1 else 0 end) as pass_count from BUS_BREAKGROUND f  where DATE_FORMAT(f.CREATTIME,'%Y-%m')=#{MONTH} and f.CORPINFO_ID = #{CORPINFO_ID} and ISDELETE = 0
		union all
		select count(1) as count,sum(case when f.STEP_ID=15 then 1 else 0 end) as pass_count from BUS_CONFINEDSPACE f  where DATE_FORMAT(f.CREATTIME,'%Y-%m')=#{MONTH} and f.CORPINFO_ID = #{CORPINFO_ID} and ISDELETE = 0
		union all
		select count(1) as count,sum(case when f.STEP_ID=15 then 1 else 0 end) as pass_count from BUS_ELECTRICITY f  where DATE_FORMAT(f.CREATTIME,'%Y-%m')=#{MONTH} and f.CORPINFO_ID = #{CORPINFO_ID} and ISDELETE = 0
		union all
		select count(1) as count,sum(case when f.STEP_ID=15 then 1 else 0 end) as pass_count from BUS_CUTROAD f  where DATE_FORMAT(f.CREATTIME,'%Y-%m')=#{MONTH} and f.CORPINFO_ID = #{CORPINFO_ID} and ISDELETE = 0
		union all
		select count(1) as count,sum(case when f.STEP_ID=15 then 1 else 0 end) as pass_count from BUS_HIGHWORK f  where DATE_FORMAT(f.CREATTIME,'%Y-%m')=#{MONTH} and f.CORPINFO_ID = #{CORPINFO_ID} and ISDELETE = 0
		union all
		select count(1) as count,sum(case when f.STEP_ID=15 then 1 else 0 end) as pass_count from BUS_HOISTING f  where DATE_FORMAT(f.CREATTIME,'%Y-%m')=#{MONTH} and f.CORPINFO_ID = #{CORPINFO_ID} and ISDELETE = 0
		union all
		select count(1) as count,sum(case when f.STEP_ID=15 then 1 else 0 end) as pass_count from BUS_HOTWORK f  where DATE_FORMAT(f.CREATTIME,'%Y-%m')=#{MONTH} and f.CORPINFO_ID = #{CORPINFO_ID} and ISDELETE = 0
	</select>
	<select id="getEightWorksByType" resultType="com.zcloud.entity.PageData">
		SELECT
			vsw.WORK_ID ID,
			vsw.CHECK_NO,
			vsw.WORK_POSITION
		FROM
			vi_starting_work vsw
		WHERE
			vsw.CORPINFO_ID = #{CORPINFO_ID}
		  AND vsw.WORK_TYPE = #{TYPE}
		  AND vsw.WORK_POSITION IS NOT NULL
	</select>
	<select id="startingList" resultType="com.zcloud.entity.PageData">
		select vsw.*,u.`NAME` CONFIRM_USER_NAME
		from vi_starting_work vsw
		LEFT JOIN sys_user u ON vsw.CONFIRM_USER_ID = u.USER_ID
		         where vsw.CORPINFO_ID = #{CORPINFO_ID}
		<if test="WORK_TYPE != null and WORK_TYPE != ''">
			and vsw.WORK_TYPE = #{WORK_TYPE}
		</if>
		<if test="KEYWORDS != null and KEYWORDS != ''">
			and vsw.CREATTIME like '%${KEYWORDS}%'
		</if>
	</select>
	<select id="eightWorkIndex" resultType="com.zcloud.entity.PageData">
		SELECT
			c.CORPINFO_ID,-- 动火
			IFNULL( hot.HOTWORK_ALL, 0 ) HOTWORK_ALL,
			IFNULL( hot.HOTWORK_ARCHIVE_COUNT, 0 ) HOTWORK_ARCHIVE_COUNT,
		(SELECT count(1) from bus_hotwork h WHERE h.STEP_ID &lt;0  and h.ISDELETE = 0 and h.CORPINFO_ID = #{CORPINFO_ID}) HOTWORK_REPULSE_COUNT,
		(SELECT count(1) from BUS_HIGHWORK h WHERE h.STEP_ID &lt;0  and h.ISDELETE = 0 and h.CORPINFO_ID = #{CORPINFO_ID})HIGHWORK_REPULSE_COUNT,
		(SELECT count(1) from bus_breakground h WHERE h.STEP_ID &lt;0  and h.ISDELETE = 0 and h.CORPINFO_ID = #{CORPINFO_ID}) BREAKGROUND_REPULSE_COUNT,
		(SELECT count(1) from bus_hoisting h WHERE h.STEP_ID &lt;0  and h.ISDELETE = 0 and h.CORPINFO_ID = #{CORPINFO_ID}) HOISTING_REPULSE_COUNT,
		(SELECT count(1) from bus_electricity h WHERE h.STEP_ID &lt;0  and h.ISDELETE = 0 and h.CORPINFO_ID = #{CORPINFO_ID}) ELECTRICITY_REPULSE_COUNT,
		(SELECT count(1) from bus_cutroad h WHERE h.STEP_ID &lt;0  and h.ISDELETE = 0 and h.CORPINFO_ID = #{CORPINFO_ID}) CUTROAD_REPULSE_COUNT,
		(SELECT count(1) from bus_blindboard h WHERE h.STEP_ID &lt;0  and h.ISDELETE = 0 and h.CORPINFO_ID = #{CORPINFO_ID}) BLINDBOARD_REPULSE_COUNT,
		(SELECT count(1) from bus_confinedspace h WHERE h.STEP_ID &lt;0  and h.ISDELETE = 0 and h.CORPINFO_ID = #{CORPINFO_ID}) CONFINEDSPACE_REPULSE_COUNT,
			-- 高处
			IFNULL( hig.HIGHWORK_ALL, 0 ) HIGHWORK_ALL,
			IFNULL( hig.HIGHWORK_ARCHIVE_COUNT, 0 ) HIGHWORK_ARCHIVE_COUNT,-- 受限空间
			IFNULL( con.CONFINEDSPACE_ALL, 0 ) CONFINEDSPACE_ALL,
			IFNULL( con.CONFINEDSPACE_ARCHIVE_COUNT, 0 ) CONFINEDSPACE_ARCHIVE_COUNT,-- 吊装
			IFNULL( hoi.HOISTING_ALL, 0 ) HOISTING_ALL,
			IFNULL( hoi.HOISTING_ARCHIVE_COUNT, 0 ) HOISTING_ARCHIVE_COUNT,-- 动土
			IFNULL( bre.BREAKGROUND_ALL, 0 ) BREAKGROUND_ALL,
			IFNULL( bre.BREAKGROUND_ARCHIVE_COUNT, 0 ) BREAKGROUND_ARCHIVE_COUNT,-- 用电
			IFNULL( ele.ELECTRICITY_ALL, 0 ) ELECTRICITY_ALL,
			IFNULL( ele.ELECTRICITY_ARCHIVE_COUNT, 0 ) ELECTRICITY_ARCHIVE_COUNT,-- 断路
			IFNULL( cut.CUTROAD_ALL, 0 ) CUTROAD_ALL,
			IFNULL( cut.CUTROAD_ARCHIVE_COUNT, 0 ) CUTROAD_ARCHIVE_COUNT,-- 盲板
			IFNULL( bli.BLINDBOARD_ALL, 0 ) BLINDBOARD_ALL,
			IFNULL( bli.BLINDBOARD_ARCHIVE_COUNT, 0 ) BLINDBOARD_ARCHIVE_COUNT
		FROM
			bus_corp_info c
				LEFT JOIN (
				SELECT
					f.CORPINFO_ID,
					tmp.HIGHWORK_ALL,
					COUNT( log.log_id ) HIGHWORK_ARCHIVE_COUNT
				FROM
					BUS_HIGHWORK f
						LEFT JOIN (
						SELECT
							*
						FROM
							( SELECT *, row_number ( ) over ( PARTITION BY WORK_ID ORDER BY ACT_TIME DESC ) AS rn FROM vi_eightwork_flow_log ) pca
						WHERE
							pca.rn = 1
					) log ON log.WORK_ID = f.HIGHWORK_ID AND log.NEXT_STEP_ID = '99' AND f.STEP_ID != - 1
						LEFT JOIN ( SELECT CORPINFO_ID, count( 1 ) HIGHWORK_ALL FROM BUS_HIGHWORK h WHERE h.ISDELETE = 0 and h.STEP_ID >= 1 GROUP BY h.CORPINFO_ID ) tmp ON f.CORPINFO_ID = tmp.CORPINFO_ID
				WHERE
					f.ISDELETE = '0'

				GROUP BY
					f.CORPINFO_ID
			) hig ON hig.CORPINFO_ID = c.CORPINFO_ID
				LEFT JOIN (
				SELECT
					f.CORPINFO_ID,
					tmp.HOTWORK_ALL,
					COUNT( log.log_id ) HOTWORK_ARCHIVE_COUNT
				FROM
					bus_hotwork f
						LEFT JOIN (
						SELECT
							*
						FROM
							( SELECT *, row_number ( ) over ( PARTITION BY WORK_ID ORDER BY ACT_TIME DESC ) AS rn FROM vi_eightwork_flow_log ) pca
						WHERE
							pca.rn = 1
					) log ON log.WORK_ID = f.HOTWORK_ID AND log.NEXT_STEP_ID = '99' AND f.STEP_ID != - 1
						LEFT JOIN ( SELECT CORPINFO_ID, count( 1 ) HOTWORK_ALL FROM bus_hotwork h WHERE h.ISDELETE = 0 and h.STEP_ID >= 1 GROUP BY h.CORPINFO_ID ) tmp ON f.CORPINFO_ID = tmp.CORPINFO_ID
				WHERE
					f.ISDELETE = '0'
				  AND f.STEP_ID != - 1
				GROUP BY
					f.CORPINFO_ID
			) hot ON hot.CORPINFO_ID = c.CORPINFO_ID
				LEFT JOIN (
				SELECT
					f.CORPINFO_ID,
					tmp.BREAKGROUND_ALL,
					COUNT( log.log_id ) BREAKGROUND_ARCHIVE_COUNT
				FROM
					bus_breakground f
						LEFT JOIN (
						SELECT
							*
						FROM
							( SELECT *, row_number ( ) over ( PARTITION BY WORK_ID ORDER BY ACT_TIME DESC ) AS rn FROM vi_eightwork_flow_log ) pca
						WHERE
							pca.rn = 1
					) log ON log.WORK_ID = f.BREAKGROUND_ID AND log.NEXT_STEP_ID = '99' AND f.STEP_ID != - 1
						LEFT JOIN ( SELECT CORPINFO_ID, count( 1 ) BREAKGROUND_ALL FROM bus_breakground h WHERE h.ISDELETE = 0 and h.STEP_ID >= 1 GROUP BY h.CORPINFO_ID ) tmp ON f.CORPINFO_ID = tmp.CORPINFO_ID
				WHERE
					f.ISDELETE = '0'

				  AND f.STEP_ID != - 1
				GROUP BY
					f.CORPINFO_ID
			) bre ON bre.CORPINFO_ID = c.CORPINFO_ID
				LEFT JOIN (
				SELECT
					f.CORPINFO_ID,
					tmp.BLINDBOARD_ALL,
					COUNT( log.log_id ) BLINDBOARD_ARCHIVE_COUNT
				FROM
					bus_blindboard f
						LEFT JOIN (
						SELECT
							*
						FROM
							( SELECT *, row_number ( ) over ( PARTITION BY WORK_ID ORDER BY ACT_TIME DESC ) AS rn FROM vi_eightwork_flow_log ) pca
						WHERE
							pca.rn = 1
					) log ON log.WORK_ID = f.BLINDBOARD_ID AND log.NEXT_STEP_ID = '99' AND f.STEP_ID != - 1
						LEFT JOIN ( SELECT CORPINFO_ID, count( 1 ) BLINDBOARD_ALL FROM bus_blindboard h WHERE h.ISDELETE = 0 and h.STEP_ID >= 1 GROUP BY h.CORPINFO_ID ) tmp ON f.CORPINFO_ID = tmp.CORPINFO_ID
				WHERE
					f.ISDELETE = '0'
				  AND f.STEP_ID != - 1
				GROUP BY
					f.CORPINFO_ID
			) bli ON bli.CORPINFO_ID = c.CORPINFO_ID
				LEFT JOIN (
				SELECT
					f.CORPINFO_ID,
					tmp.CONFINEDSPACE_ALL,
					COUNT( log.log_id ) CONFINEDSPACE_ARCHIVE_COUNT
				FROM
					bus_confinedspace f
						LEFT JOIN (
						SELECT
							*
						FROM
							( SELECT *, row_number ( ) over ( PARTITION BY WORK_ID ORDER BY ACT_TIME DESC ) AS rn FROM vi_eightwork_flow_log ) pca
						WHERE
							pca.rn = 1
					) log ON log.WORK_ID = f.CONFINEDSPACE_ID AND log.NEXT_STEP_ID = '99' AND f.STEP_ID != - 1
						LEFT JOIN ( SELECT CORPINFO_ID, count( 1 ) confinedspace_ALL FROM bus_confinedspace h WHERE h.ISDELETE = 0 and h.STEP_ID >= 1 GROUP BY h.CORPINFO_ID ) tmp ON f.CORPINFO_ID = tmp.CORPINFO_ID
				WHERE
					f.ISDELETE = '0'
				  AND f.STEP_ID != - 1
				GROUP BY
					f.CORPINFO_ID
			) con ON con.CORPINFO_ID = c.CORPINFO_ID
				LEFT JOIN (
				SELECT
					f.CORPINFO_ID,
					tmp.HOISTING_ALL,
					COUNT( log.log_id ) HOISTING_ARCHIVE_COUNT
				FROM
					bus_hoisting f
						LEFT JOIN (
						SELECT
							*
						FROM
							( SELECT *, row_number ( ) over ( PARTITION BY WORK_ID ORDER BY ACT_TIME DESC ) AS rn FROM vi_eightwork_flow_log ) pca
						WHERE
							pca.rn = 1
					) log ON log.WORK_ID = f.HOISTING_ID AND log.NEXT_STEP_ID = '99' AND f.STEP_ID != - 1
						LEFT JOIN ( SELECT CORPINFO_ID, count( 1 ) HOISTING_ALL FROM bus_hoisting h WHERE h.ISDELETE = 0 and h.STEP_ID >= 1 GROUP BY h.CORPINFO_ID ) tmp ON f.CORPINFO_ID = tmp.CORPINFO_ID
				WHERE
					f.ISDELETE = '0'
				  AND f.STEP_ID != - 1
				GROUP BY
					f.CORPINFO_ID
			) hoi ON hoi.CORPINFO_ID = c.CORPINFO_ID
				LEFT JOIN (
				SELECT
					f.CORPINFO_ID,
					tmp.ELECTRICITY_ALL,
					COUNT( log.log_id ) ELECTRICITY_ARCHIVE_COUNT
				FROM
					bus_electricity f
						LEFT JOIN (
						SELECT
							*
						FROM
							( SELECT *, row_number ( ) over ( PARTITION BY WORK_ID ORDER BY ACT_TIME DESC ) AS rn FROM vi_eightwork_flow_log ) pca
						WHERE
							pca.rn = 1
					) log ON log.WORK_ID = f.ELECTRICITY_ID AND log.NEXT_STEP_ID = '99' AND f.STEP_ID != - 1
						LEFT JOIN ( SELECT CORPINFO_ID, count( 1 ) ELECTRICITY_ALL FROM bus_electricity h WHERE h.ISDELETE = 0 and h.STEP_ID >= 1 GROUP BY h.CORPINFO_ID ) tmp ON f.CORPINFO_ID = tmp.CORPINFO_ID
				WHERE
					f.ISDELETE = '0'
				  AND f.STEP_ID != - 1
				GROUP BY
					f.CORPINFO_ID
			) ele ON ele.CORPINFO_ID = c.CORPINFO_ID
				LEFT JOIN (
				SELECT
					f.CORPINFO_ID,
					tmp.CUTROAD_ALL,
					COUNT( log.log_id ) CUTROAD_ARCHIVE_COUNT
				FROM
					bus_cutroad f
						LEFT JOIN (
						SELECT
							*
						FROM
							( SELECT *, row_number ( ) over ( PARTITION BY WORK_ID ORDER BY ACT_TIME DESC ) AS rn FROM vi_eightwork_flow_log ) pca
						WHERE
							pca.rn = 1
					) log ON log.WORK_ID = f.CUTROAD_ID  AND log.NEXT_STEP_ID = '99' AND f.STEP_ID != - 1
						LEFT JOIN ( SELECT CORPINFO_ID, count( 1 ) CUTROAD_ALL FROM bus_cutroad h WHERE h.ISDELETE = 0 and h.STEP_ID >= 1  GROUP BY h.CORPINFO_ID ) tmp ON f.CORPINFO_ID = tmp.CORPINFO_ID
				WHERE
					f.ISDELETE = '0'
				  AND f.STEP_ID != - 1
				GROUP BY
					f.CORPINFO_ID
			) cut ON cut.CORPINFO_ID = c.CORPINFO_ID
		WHERE
			c.ISDELETE = 0
		and c.CORPINFO_ID = #{CORPINFO_ID}
	</select>
</mapper>
