<?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.commitment.CorpPromisePeopleMapper">

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

	<!--数据字典表名 -->
	<sql id="dicTableName">
		`qa-gwj-prevention`.sys_dictionaries
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.PROMISEPEOPLE_ID,
		f.PROMISE_ID,
		f.DEPARTMENT_ID,
		f.POST_ID,
		f.USER_ID,
		f.ISSIGN,
		f.SIGNTIME,
		f.CORPINFO_ID,
		f.FILEPATH,
		f.ISPROMISE_PEOPLE,
		f.ISREAD
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		PROMISEPEOPLE_ID,
		PROMISE_ID,
		DEPARTMENT_ID,
		POST_ID,
		USER_ID,
		ISSIGN,
		SIGNTIME,
		CORPINFO_ID,
		FILEPATH,
		ISPROMISE_PEOPLE,
		ISREAD
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{PROMISEPEOPLE_ID},
		#{PROMISE_ID},
		#{DEPARTMENT_ID},
		#{POST_ID},
		#{USER_ID},
		#{ISSIGN},
		#{SIGNTIME},
		#{CORPINFO_ID},
		#{FILEPATH},
		#{ISPROMISE_PEOPLE},
		#{ISREAD}
	</sql>

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

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,u.NAME USERNAME,d.NAME DEPTNAME,p.NAME POST_NAME
		from
		<include refid="tableName"></include> f
		left join
		sys_user u on u.USER_ID = f.USER_ID
		left join
		oa_department d on d.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join
		SYS_POST p on p.POST_ID = f.POST_ID
		where 1=1
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.PROMISE_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
		</if>
		<if test="pd.PROMISE_ID != null and pd.PROMISE_ID != ''"><!-- 关键词检索 -->
			and f.PROMISE_ID = #{pd.PROMISE_ID}
		</if>
		<if test="pd.ISPROMISE_PEOPLE != null and pd.ISPROMISE_PEOPLE != ''"><!-- 关键词检索 -->
			and f.ISPROMISE_PEOPLE = #{pd.ISPROMISE_PEOPLE}
		</if>

	</select>

	<!-- 禁用启用-->
	<delete id="editdelete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = #{ISDELETE}
		where
		PROMISE_ID = #{PROMISE_ID}
	</delete>

	<!-- 列表 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where 1=1
		<if test="PROMISEPEOPLE_ID != null and PROMISEPEOPLE_ID != ''"><!-- 关键词检索 -->
			and f.PROMISEPEOPLE_ID = #{PROMISEPEOPLE_ID}
		</if>
	</select>

	<!-- 修改承诺书-->
	<delete id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = #{ISDELETE},
		OPERATTIME = #{OPERATTIME},
		PROMISE_NAME =#{PROMISE_NAME},
		LEVEL = #{LEVEL},
		TEXT = #{TEXT}
		where
		PROMISE_ID = #{PROMISE_ID}
	</delete>

	<!-- 承诺人列表 -->
	<select id="findByPromiseId" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,u.NAME as USERNAME,o.NAME as DEPTNAME,p.NAME as POSTNAME,i.CORP_NAME
		from
		<include refid="tableName"></include> f
		left join
		sys_user u on u.USER_ID = f.USER_ID
		left join
		oa_department o on o.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join
		SYS_POST p on p.POST_ID = f.POST_ID
		left join BUS_CORP_INFO i on i.CORPINFO_ID = f.CORPINFO_ID
		where 1=1 and f.ISPROMISE_PEOPLE = '0'
		<if test="PROMISE_ID != null and PROMISE_ID != ''"><!-- 关键词检索 -->
			and f.PROMISE_ID = #{PROMISE_ID}
		</if>
		<if test="ISSIGN != null and ISSIGN != ''"><!-- 关键词检索 -->
			and f.ISSIGN = #{ISSIGN}
		</if>
	</select>

	<!-- 承诺人列表 -->
	<select id="findByCoverPromiseId" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,u.NAME as USERNAME,o.NAME as DEPTNAME,p.NAME as POSTNAME,i.CORP_NAME
		from
		<include refid="tableName"></include> f
		left join
		sys_user u on u.USER_ID = f.USER_ID
		left join
		oa_department o on o.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join
		SYS_POST p on p.POST_ID = f.POST_ID
		left join BUS_CORP_INFO i on i.CORPINFO_ID = f.CORPINFO_ID
		where 1=1 and f.ISPROMISE_PEOPLE = '1'
		<if test="PROMISE_ID != null and PROMISE_ID != ''"><!-- 关键词检索 -->
			and f.PROMISE_ID = #{PROMISE_ID}
		</if>
		ORDER BY f.SIGNTIME desc
	</select>

	<select id="findByUserId" parameterType="pd" resultType="pd">
		select
		*
		from
		<include refid="tableName"></include> f
		left join bus_corppromise c on c.PROMISE_ID = f.PROMISE_ID
		where 1=1 and f.ISPROMISE_PEOPLE = '0'
		and f.ISSIGN = 0
		and c.STATE = 0
		and f.USER_ID = #{USER_ID}
	</select>

	<!-- 删除-->
	<delete id="deletePromiseId" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where
		PROMISE_ID = #{PROMISE_ID}
	</delete>

	<update id="editSign" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		FILEPATH = #{FILEPATH},
		SIGNTIME = #{SIGNTIME},
		ISSIGN =#{ISSIGN},
		ISREAD = #{ISREAD}
		where
		PROMISEPEOPLE_ID = #{PROMISEPEOPLE_ID}
	</update>

	<!-- 列表 -->
	<select id="promiselistPage" parameterType="page" resultType="pd">
		select
		f.PROMISEPEOPLE_ID,
		u.NAME,
		f.SIGNTIME,
		f.PROMISE_ID,
		(select u.NAME from bus_corppromise_people c left join sys_user u on c.USER_ID = u.USER_ID where c.ISPROMISE_PEOPLE = '1' and f.PROMISE_ID=c.PROMISE_ID) as coverpeople
		from
		<include refid="tableName"></include> f
		left join
		sys_user u on u.USER_ID = f.USER_ID
		where 1=1 and f.ISSIGN = 1
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.PROMISE_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
		</if>
		<if test="pd.USER_ID != null and pd.USER_ID != ''"><!-- 关键词检索 -->
			and f.USER_ID = #{pd.USER_ID}
		</if>
		AND f.ISPROMISE_PEOPLE = '0'
		ORDER BY f.SIGNTIME desc
	</select>


	<!-- 接收承诺列表 -->
	<select id="receivePromiselistPage" parameterType="page" resultType="pd">
		select
		f.PROMISEPEOPLE_ID,
		u.NAME,
		f.SIGNTIME,
		f.PROMISE_ID,
		f.ISREAD,
		(select u.NAME from bus_corppromise_people c left join sys_user u on c.USER_ID = u.USER_ID where c.ISPROMISE_PEOPLE = '1' and f.PROMISE_ID=c.PROMISE_ID) as coverpeople
		from
		<include refid="tableName"></include> f
		left join
		sys_user u on u.USER_ID = f.USER_ID
		where f.PROMISE_ID in  (select PROMISE_ID from bus_corppromise_people where USER_ID = #{pd.USER_ID} and ISPROMISE_PEOPLE = '1')
		and f.ISSIGN = 1
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.PROMISE_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
		</if>
		AND f.ISPROMISE_PEOPLE = '0'
		ORDER BY f.SIGNTIME desc
	</select>


	<!-- 各企业安全承诺统计 -->
	<select id="corppromiselistPage" parameterType="page" resultType="pd">
		select
		f.CORPINFO_ID,
		f.CORP_NAME,
		(select COUNT(*) from bus_corppromise_people where PROMISE_ID in(select PROMISE_ID from  bus_corppromise c where c.CORPINFO_ID = f.CORPINFO_ID and c.CREATTIME LIKE CONCAT(CONCAT(#{pd.YEAR}),'%')) and ISPROMISE_PEOPLE = '0'
		)as ISSUED_NUM,
		(select COUNT(*) from bus_corppromise_people where PROMISE_ID in(select PROMISE_ID from  bus_corppromise c where c.CORPINFO_ID = f.CORPINFO_ID and c.CREATTIME LIKE CONCAT(CONCAT(#{pd.YEAR}),'%')) and ISPROMISE_PEOPLE = '0' and ISSIGN = 1
		) as SIGNING_NUM,
		(select COUNT(*) from  bus_corppromise c where c.CORPINFO_ID = f.CORPINFO_ID and c.CREATTIME LIKE CONCAT(CONCAT(#{pd.YEAR}),'%')) as PROMISE_NUM

		from bus_corp_info f where f.ISDELETE = 0
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.CORP_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
		</if>
	</select>

	<!-- 各企业安全承诺查看统计 -->
	<select id="corppromiseDetaillistPage" parameterType="page" resultType="pd">
		SELECT
		    f.PROMISE_ID,
			f.PROMISE_NAME,
			f.PROMISE_TERM_START,
			f.PROMISE_TERM_END,
			d.NAME as LEVEL_NAME,
			(select COUNT(*) from bus_corppromise_people p where p.PROMISE_ID = f.PROMISE_ID and p.ISPROMISE_PEOPLE = '0')as ISSUED_NUM,
			(select COUNT(*) from bus_corppromise_people p where p.PROMISE_ID = f.PROMISE_ID and p.ISPROMISE_PEOPLE = '0' and ISSIGN = 1) as SIGNING_NUM
		FROM
			bus_corppromise f
				left join sys_dictionaries d on d.BIANMA = f.LEVEL
		WHERE
			CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.PROMISE_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
		</if>
		<if test="pd.YEAR != null and pd.YEAR != ''"><!-- 关键词检索 -->
			and f.CREATTIME LIKE CONCAT(CONCAT(#{pd.YEAR}),'%')
		</if>
		ORDER BY f.CREATTIME desc
	</select>


	<!-- 承诺人查看 -->
	<select id="peopleDetaillistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,u.NAME USERNAME,o.NAME as DEPTNAME,
		(select CREATTIME from bus_corppromise where PROMISE_ID = #{pd.PROMISE_ID}) as CREATTIME
		from
		<include refid="tableName"></include> f
		left join
		sys_user u on u.USER_ID = f.USER_ID
		left join
		oa_department o on o.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join
		SYS_POST p on p.POST_ID = f.POST_ID
		left join BUS_CORP_INFO i on i.CORPINFO_ID = f.CORPINFO_ID
		where 1=1 and f.ISPROMISE_PEOPLE = '0'
		<if test="pd.PROMISE_ID != null and pd.PROMISE_ID != ''"><!-- 关键词检索 -->
			and f.PROMISE_ID = #{pd.PROMISE_ID}
		</if>
	</select>

	<update id="editIsRead" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISREAD = #{ISREAD}
		where
		PROMISEPEOPLE_ID = #{PROMISEPEOPLE_ID}
	</update>
</mapper>
