<?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.KeyprojectCheckMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.CORPINFO_ID,
		f.ISDELETE,
		f.SOURCE,
		f.OUTSOURCED_ID,
		f.UNITS_ID,
		f.PERSONNELMANAGEMENT_ID,
		f.PERSON_SIGN_IMG,
		f.PERSON_SIGN_TIME,
		f.INSPECTION_TYPE,
		f.INSPECTION_PLACE,
		f.INSPECTION_TIME_START,
		f.INSPECTION_TIME_END,
		f.INSPECTION_USERS,
		f.INSPECTION_STATUS,
		f.INSPECTION_CATEGORY,
		f.KEYPROJECTCHECK_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		CORPINFO_ID,
		ISDELETE,
		SOURCE,
		OUTSOURCED_ID,
		UNITS_ID,
		PERSONNELMANAGEMENT_ID,
		PERSON_SIGN_IMG,
		PERSON_SIGN_TIME,
		INSPECTION_TYPE,
		INSPECTION_PLACE,
		INSPECTION_TIME_START,
		INSPECTION_TIME_END,
		INSPECTION_USERS,
		INSPECTION_STATUS,
		INSPECTION_CATEGORY,
		KEYPROJECTCHECK_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{CORPINFO_ID},
		#{ISDELETE},
		#{SOURCE},
		#{OUTSOURCED_ID},
		#{UNITS_ID},
		#{PERSONNELMANAGEMENT_ID},
		#{PERSON_SIGN_IMG},
		#{PERSON_SIGN_TIME},
		#{INSPECTION_TYPE},
		#{INSPECTION_PLACE},
		#{INSPECTION_TIME_START},
		#{INSPECTION_TIME_END},
		#{INSPECTION_USERS},
		#{INSPECTION_STATUS},
		#{INSPECTION_CATEGORY},
		#{KEYPROJECTCHECK_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
			KEYPROJECTCHECK_ID = #{KEYPROJECTCHECK_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			CREATOR = #{CREATOR},
			CREATTIME = #{CREATTIME},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			CORPINFO_ID = #{CORPINFO_ID},
			ISDELETE = #{ISDELETE},
			SOURCE = #{SOURCE},
			OUTSOURCED_ID = #{OUTSOURCED_ID},
			UNITS_ID = #{UNITS_ID},
			PERSONNELMANAGEMENT_ID = #{PERSONNELMANAGEMENT_ID},
			PERSON_SIGN_IMG = #{PERSON_SIGN_IMG},
			PERSON_SIGN_TIME = #{PERSON_SIGN_TIME},
			INSPECTION_TYPE = #{INSPECTION_TYPE},
			INSPECTION_PLACE = #{INSPECTION_PLACE},
			INSPECTION_TIME_START = #{INSPECTION_TIME_START},
			INSPECTION_TIME_END = #{INSPECTION_TIME_END},
			INSPECTION_USERS = #{INSPECTION_USERS},
			INSPECTION_STATUS = #{INSPECTION_STATUS},
			INSPECTION_CATEGORY = #{INSPECTION_CATEGORY},
			KEYPROJECTCHECK_ID = KEYPROJECTCHECK_ID
		where
			KEYPROJECTCHECK_ID = #{KEYPROJECTCHECK_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		o.OUTSOURCED_NAME,
		u.UNITS_NAME,
		p.NAME as PERSON_NAME,
		d.NAME as INSPECTION_TYPE_NAME
		from
		<include refid="tableName"></include> f
		left join BUS_OUTSOURCED o on o.OUTSOURCED_ID = f.OUTSOURCED_ID
		left join BUS_UNITS u on u.UNITS_ID = f.UNITS_ID
		left join BUS_PERSONNELMANAGEMENT p on p.PERSONNELMANAGEMENT_ID = f.PERSONNELMANAGEMENT_ID
		LEFT JOIN SYS_DICTIONARIES d on d.BIANMA = f.INSPECTION_TYPE
		where
			f.KEYPROJECTCHECK_ID = #{KEYPROJECTCHECK_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		o.OUTSOURCED_NAME,
		u.UNITS_NAME,
		p.NAME as PERSON_NAME,
		d.NAME as INSPECTION_TYPE_NAME,
		(select count(1) from BUS_KEYPROJECT_HIDDEN h where h.FOREIGN_ID = f.KEYPROJECTCHECK_ID) as HIDDEN_COUNT
		from
		<include refid="tableName"></include> f
		left join BUS_OUTSOURCED o on o.OUTSOURCED_ID = f.OUTSOURCED_ID
		left join BUS_UNITS u on u.UNITS_ID = f.UNITS_ID
		left join BUS_PERSONNELMANAGEMENT p on p.PERSONNELMANAGEMENT_ID = f.PERSONNELMANAGEMENT_ID
		left join SYS_DICTIONARIES d on d.BIANMA = f.INSPECTION_TYPE
		where f.ISDELETE = '0'
		<if test="pd.OUTSOURCED_ID != null and pd.OUTSOURCED_ID != ''">
			and f.OUTSOURCED_ID = #{pd.OUTSOURCED_ID}
		</if>
		<if test="pd.PERSONNELMANAGEMENT_ID != null and pd.PERSONNELMANAGEMENT_ID != ''">
			and f.PERSONNELMANAGEMENT_ID = #{pd.PERSONNELMANAGEMENT_ID}
		</if>
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			<!--	根据需求自己加检索条件-->
			f.INSPECTION_PLACE LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		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
			KEYPROJECTCHECK_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

	<!-- 确认 -->
	<update id="confirm" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		INSPECTION_STATUS = 1,
		PERSON_SIGN_IMG = #{PERSON_SIGN_IMG},
		PERSON_SIGN_TIME = #{PERSON_SIGN_TIME}
		where
		KEYPROJECTCHECK_ID = #{KEYPROJECTCHECK_ID}
	</update>

	<select id="getSmsList" parameterType="pd" resultType="pd">
		SELECT
			ry.PHONENUM as PHONENUM,
			yh.HIDDENDESCR as HIDDENDESCR,
			un.CORPINFO_ID
		FROM BUS_KEYPROJECTCHECK t
		LEFT JOIN BUS_OUTSOURCED gc on gc.OUTSOURCED_ID = t.OUTSOURCED_ID
		LEFT JOIN BUS_KEYPROJECT_HIDDEN yh on yh.FOREIGN_ID = t.KEYPROJECTCHECK_ID
		LEFT JOIN BUS_PERSONNELMANAGEMENT ry on ry.PERSONNELMANAGEMENT_ID = yh.RECTIFICATIONOR
		LEFT JOIN bus_units un on ry.UNITS_ID = un.UNITS_ID

		WHERE t.KEYPROJECTCHECK_ID=#{KEYPROJECTCHECK_ID}
		AND gc.IS_SMS = '0'
		AND t.INSPECTION_STATUS = 1
	</select>

</mapper>
