<?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.pe.PerformanceExamineDeptMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_PERFORMANCE_EXAMINE_DEPT
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.CORPINFO_ID,
		f.SCORE1,
		f.SCORE2,
		f.SCORE3,
		f.SCORE4,
		f.SCORE5,
		f.DEPARTMENT_ID,
		f.EXAMINE_DATE,
		f.PERFORMANCE_EXAMINE_DEPT_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CORPINFO_ID,
		SCORE1,
		SCORE2,
		SCORE3,
		SCORE4,
		SCORE5,
		DEPARTMENT_ID,
		EXAMINE_DATE,
		PERFORMANCE_EXAMINE_DEPT_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CORPINFO_ID},
		#{SCORE1},
		#{SCORE2},
		#{SCORE3},
		#{SCORE4},
		#{SCORE5},
		#{DEPARTMENT_ID},
		#{EXAMINE_DATE},
		#{PERFORMANCE_EXAMINE_DEPT_ID}
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue2">
		#{item.CORPINFO_ID},
		#{item.SCORE1},
		#{item.SCORE2},
		#{item.SCORE3},
		#{item.SCORE4},
		#{item.SCORE5},
		#{item.DEPARTMENT_ID},
		#{item.EXAMINE_DATE},
		#{item.PERFORMANCE_EXAMINE_DEPT_ID}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
		<include refid="tableName"></include>
		(
			<include refid="Field2"></include>
		) values (
			<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 新增-->
	<insert id="saveBatch" parameterType="pd">
		insert into <include refid="tableName"></include>
			(<include refid="Field2"></include>) values
		<foreach item="item" index="index" collection="list" separator="," >
			(<include refid="FieldValue2"></include>)
		</foreach>
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			PERFORMANCE_EXAMINE_DEPT_ID = #{PERFORMANCE_EXAMINE_DEPT_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			CORPINFO_ID = #{CORPINFO_ID},
			SCORE1 = #{SCORE1},
			SCORE2 = #{SCORE2},
			SCORE3 = #{SCORE3},
			SCORE4 = #{SCORE4},
			SCORE5 = #{SCORE5},
			DEPARTMENT_ID = #{DEPARTMENT_ID},
			EXAMINE_DATE = #{EXAMINE_DATE},
			PERFORMANCE_EXAMINE_DEPT_ID = PERFORMANCE_EXAMINE_DEPT_ID
		where
			PERFORMANCE_EXAMINE_DEPT_ID = #{PERFORMANCE_EXAMINE_DEPT_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.PERFORMANCE_EXAMINE_DEPT_ID = #{PERFORMANCE_EXAMINE_DEPT_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
			avg(e.SCORE1) as SCORE1,
			avg(e.SCORE2) as SCORE2,
			avg(e.SCORE3) as SCORE3,
			avg(e.SCORE4) as SCORE4,
			avg(e.SCORE5) as SCORE5,
			getFullName(e.DEPARTMENT_ID) as DEPT_NAME_ALL
		from
			<include refid="tableName"></include> e
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID = e.DEPARTMENT_ID
		where 1=1
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
				d.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 企业名称 -->
			and  e.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.YEAR != null and pd.YEAR != ''"><!-- 年 -->
			and  e.EXAMINE_DATE LIKE CONCAT(CONCAT( #{pd.YEAR}),'%')
		</if>
		<if test="pd.QUARTER ==1"><!-- 季度1 -->
			and ( e.EXAMINE_DATE LIKE '%-01' or e.EXAMINE_DATE LIKE '%-02' or e.EXAMINE_DATE LIKE '%-03' )
		</if>
		<if test="pd.QUARTER ==2"><!-- 季度2 -->
			and ( e.EXAMINE_DATE LIKE '%-04' or e.EXAMINE_DATE LIKE '%-05' or e.EXAMINE_DATE LIKE '%-06' )
		</if>
		<if test="pd.QUARTER ==3"><!-- 季度3 -->
			and ( e.EXAMINE_DATE LIKE '%-07' or e.EXAMINE_DATE LIKE '%-08' or e.EXAMINE_DATE LIKE '%-09' )
		</if>
		<if test="pd.QUARTER ==4"><!-- 季度4 -->
			and ( e.EXAMINE_DATE LIKE '%-10' or e.EXAMINE_DATE LIKE '%-11' or e.EXAMINE_DATE LIKE '%-12' )
		</if>
		<if test="pd.MONTH != null and pd.MONTH != ''"><!-- 企业名称 -->
			and  e.EXAMINE_DATE = #{pd.MONTH}
		</if>
		<if test="pd.LEVEL != null and pd.LEVEL == 1"><!-- 考核等级 -->
			and  ((e.SCORE1)+(e.SCORE2)+(e.SCORE3)+(e.SCORE4)+(e.SCORE5)) &gt;= 75
		</if>
		<if test="pd.LEVEL != null and pd.LEVEL == 2"><!-- 考核等级 -->
			and  ((e.SCORE1)+(e.SCORE2)+(e.SCORE3)+(e.SCORE4)+(e.SCORE5)) between 50 and 74
		</if>
		<if test="pd.LEVEL != null and pd.LEVEL == 3"><!-- 考核等级 -->
			and  ((e.SCORE1)+(e.SCORE2)+(e.SCORE3)+(e.SCORE4)+(e.SCORE5)) between 25 and 49
		</if>
		<if test="pd.LEVEL != null and pd.LEVEL == 4"><!-- 考核等级 -->
			and  ((e.SCORE1)+(e.SCORE2)+(e.SCORE3)+(e.SCORE4)+(e.SCORE5)) &lt; 25
		</if>
		<if test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
			and
			e.DEPARTMENT_ID in
			<foreach item="item" index="index"
					 collection="pd.DEPT_IDS" open="(" separator="," close=")">
				'${item}'
			</foreach>
		</if>
		group by e.DEPARTMENT_ID
		ORDER BY (avg(e.SCORE1)+avg(e.SCORE2)+avg(e.SCORE3)+avg(e.SCORE4)+avg(e.SCORE5)) desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		avg(e.SCORE1) as SCORE1,
		avg(e.SCORE2) as SCORE2,
		avg(e.SCORE3) as SCORE3,
		avg(e.SCORE4) as SCORE4,
		avg(e.SCORE5) as SCORE5,
		getFullName(e.DEPARTMENT_ID) as DEPT_NAME_ALL
		from
		<include refid="tableName"></include> e
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID = e.DEPARTMENT_ID
		where 1=1
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			d.NAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			)
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 企业名称 -->
			and  e.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="YEAR != null and YEAR != ''"><!-- 年 -->
			and  e.EXAMINE_DATE LIKE CONCAT(CONCAT( #{YEAR}),'%')
		</if>
		<if test="QUARTER ==1"><!-- 季度1 -->
			and ( e.EXAMINE_DATE LIKE '%-01' or e.EXAMINE_DATE LIKE '%-02' or e.EXAMINE_DATE LIKE '%-03' )
		</if>
		<if test="QUARTER ==2"><!-- 季度2 -->
			and ( e.EXAMINE_DATE LIKE '%-04' or e.EXAMINE_DATE LIKE '%-05' or e.EXAMINE_DATE LIKE '%-06' )
		</if>
		<if test="QUARTER ==3"><!-- 季度3 -->
			and ( e.EXAMINE_DATE LIKE '%-07' or e.EXAMINE_DATE LIKE '%-08' or e.EXAMINE_DATE LIKE '%-09' )
		</if>
		<if test="QUARTER ==4"><!-- 季度4 -->
			and ( e.EXAMINE_DATE LIKE '%-10' or e.EXAMINE_DATE LIKE '%-11' or e.EXAMINE_DATE LIKE '%-12' )
		</if>
		<if test="MONTH != null and MONTH != ''"><!-- 企业名称 -->
			and  e.EXAMINE_DATE = #{MONTH}
		</if>
		<if test="LEVEL != null and LEVEL == 1"><!-- 考核等级 -->
			and  ((e.SCORE1)+(e.SCORE2)+(e.SCORE3)+(e.SCORE4)+(e.SCORE5)) &gt;= 75
		</if>
		<if test="LEVEL != null and LEVEL == 2"><!-- 考核等级 -->
			and  ((e.SCORE1)+(e.SCORE2)+(e.SCORE3)+(e.SCORE4)+(e.SCORE5)) between 50 and 74
		</if>
		<if test="LEVEL != null and LEVEL == 3"><!-- 考核等级 -->
			and  ((e.SCORE1)+(e.SCORE2)+(e.SCORE3)+(e.SCORE4)+(e.SCORE5)) between 25 and 49
		</if>
		<if test="LEVEL != null and LEVEL == 4"><!-- 考核等级 -->
			and  ((e.SCORE1)+(e.SCORE2)+(e.SCORE3)+(e.SCORE4)+(e.SCORE5)) &lt; 25
		</if>
		<if test="DEPT_IDS != null and DEPT_IDS != ''">
			and
			e.DEPARTMENT_ID in
			<foreach item="item" index="index"
					 collection="DEPT_IDS" open="(" separator="," close=")">
				'${item}'
			</foreach>
		</if>
		group by e.DEPARTMENT_ID
		ORDER BY (avg(e.SCORE1)+avg(e.SCORE2)+avg(e.SCORE3)+avg(e.SCORE4)+avg(e.SCORE5)) desc
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			PERFORMANCE_EXAMINE_DEPT_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

</mapper>
