<?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.keyProjects.KeyprojectPunishMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.KEYPROJECTCHECK_ID,
		f.HIDDEN_ID,
		f.UNITS_ID,
		f.PERSONNELMANAGEMENT_ID,
		f.REASON,
		f.AMOUT,
		f.DATE,
		f.HANDLED,
		f.HANDLE_IMG,
		f.HANLDE_TIME,
		f.KEYPROJECTPUNISH_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		KEYPROJECTCHECK_ID,
		HIDDEN_ID,
		UNITS_ID,
		PERSONNELMANAGEMENT_ID,
		REASON,
		AMOUT,
		DATE,
		HANDLED,
		HANDLE_IMG,
		HANLDE_TIME,
		KEYPROJECTPUNISH_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{KEYPROJECTCHECK_ID},
		#{HIDDEN_ID},
		#{UNITS_ID},
		#{PERSONNELMANAGEMENT_ID},
		#{REASON},
		#{AMOUT},
		#{DATE},
		#{HANDLED},
		#{HANDLE_IMG},
		#{HANLDE_TIME},
		#{KEYPROJECTPUNISH_ID}
	</sql>

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

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

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			CREATOR = #{CREATOR},
			CREATTIME = #{CREATTIME},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			ISDELETE = #{ISDELETE},
			KEYPROJECTCHECK_ID = #{KEYPROJECTCHECK_ID},
			HIDDEN_ID = #{HIDDEN_ID},
			UNITS_ID = #{UNITS_ID},
			PERSONNELMANAGEMENT_ID = #{PERSONNELMANAGEMENT_ID},
			REASON = #{REASON},
			AMOUT = #{AMOUT},
			DATE = #{DATE},
			HANDLED = #{HANDLED},
			HANDLE_IMG = #{HANDLE_IMG},
			HANLDE_TIME = #{HANLDE_TIME},
			KEYPROJECTPUNISH_ID = KEYPROJECTPUNISH_ID
		where
			KEYPROJECTPUNISH_ID = #{KEYPROJECTPUNISH_ID}
	</update>

	<!-- 修改 -->
	<update id="editstate" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		HANDLED = #{HANDLED},
		HANDLE_IMG = #{HANDLE_IMG},
		HANLDE_TIME = #{HANLDE_TIME}
		where
		KEYPROJECTPUNISH_ID = #{KEYPROJECTPUNISH_ID}
	</update>


	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		cr.NAME CREATOR_NAME,
		cre.NAME CREATOR_NAMES,
		bo.OUTSOURCED_NAME,
		u.UNITS_NAME,
		p.NAME AS PERSON_NAME
		from
		<include refid="tableName"></include> f
		left join vi_user_all cr on cr.USER_ID = f.CREATOR
		left join sys_user cre on cre.USER_ID = f.CREATOR
		left join BUS_UNITS u on u.UNITS_ID = f.UNITS_ID
		left join bus_keyproject_hidden h on f.HIDDEN_ID = h.HIDDEN_ID
		LEFT JOIN BUS_KEYPROJECTCHECK ch ON ch.OUTSOURCED_ID = h.FOREIGN_ID or  h.FOREIGN_ID = ch.KEYPROJECTCHECK_ID
		LEFT JOIN BUS_OUTSOURCED bo ON bo.OUTSOURCED_ID = ch.OUTSOURCED_ID <!-- 用于安全环保检查隐患的重点工程名称 -->
		left join BUS_PERSONNELMANAGEMENT p on p.PERSONNELMANAGEMENT_ID = f.PERSONNELMANAGEMENT_ID
		where
		<if test="KEYPROJECTPUNISH_ID != null and KEYPROJECTPUNISH_ID != ''"><!-- 关键词检索 -->
			f.KEYPROJECTPUNISH_ID = #{KEYPROJECTPUNISH_ID}
		</if>
		<if test="HIDDEN_ID != null and HIDDEN_ID != ''"><!-- 关键词检索 -->
			f.HIDDEN_ID = #{HIDDEN_ID}
		</if>
		GROUP BY f.KEYPROJECTPUNISH_ID
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		cre.NAME CREATOR_NAMES,
		bo.OUTSOURCED_ID,
		bo.OUTSOURCED_NAME,
		u.UNITS_NAME,
		p.NAME AS PERSON_NAME
		from
		<include refid="tableName"></include> f
		left join sys_user cre on cre.USER_ID = f.CREATOR
		left join BUS_UNITS u on u.UNITS_ID = f.UNITS_ID
		left join bus_keyproject_hidden h on f.HIDDEN_ID = h.HIDDEN_ID
		LEFT JOIN BUS_KEYPROJECTCHECK ch ON ch.OUTSOURCED_ID = h.FOREIGN_ID or  h.FOREIGN_ID = ch.KEYPROJECTCHECK_ID
		LEFT JOIN BUS_OUTSOURCED bo ON bo.OUTSOURCED_ID = ch.OUTSOURCED_ID <!-- 用于安全环保检查隐患的重点工程名称 -->
		left join BUS_PERSONNELMANAGEMENT p on p.PERSONNELMANAGEMENT_ID = f.PERSONNELMANAGEMENT_ID
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			bo.OUTSOURCED_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			or
			u.UNITS_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		<if test="pd.OUTSOURCED_ID != null and pd.OUTSOURCED_ID !='' " >
			and bo.OUTSOURCED_ID = #{pd.OUTSOURCED_ID}
		</if>
		<if test="pd.HANDLED != null and pd.HANDLED !='' and pd.HANDLED == 1" >
			and f.HANDLED = 0
		</if>
		<if test="pd.HANDLED != null and pd.HANDLED !='' and pd.HANDLED == 2" >
			and f.HANDLED &gt; 0
		</if>
		<if test="pd.PERSONNELMANAGEMENT_ID != null and pd.PERSONNELMANAGEMENT_ID !='' " >
			and f.PERSONNELMANAGEMENT_ID = #{pd.PERSONNELMANAGEMENT_ID}
		</if>
		group by f.KEYPROJECTPUNISH_ID
		order by f.CREATTIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			KEYPROJECTPUNISH_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>
	<!-- 根据添加人查询罚单 -->
	<select id="findByOperator" parameterType="pd" resultType="pd">
		select
		fadan.CREATOR,
		fadan.CREATTIME,
		fadan.OPERATOR,
		keyp.OUTSOURCED_ID,
		keyp.KEYPROJECTCHECK_ID
		from
		<include refid="tableName"></include> fadan
		left join bus_keyproject_hidden hidden on fadan.HIDDEN_ID = hidden.HIDDEN_ID
		left join bus_keyprojectcheck keyp on hidden.FOREIGN_ID = keyp.KEYPROJECTCHECK_ID
		where
		keyp.OUTSOURCED_ID = #{OUTSOURCED_ID}
		and
		(fadan.CREATOR = #{CREATOR}
		or
		fadan.OPERATOR = #{CREATOR})

	</select>
</mapper>
