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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.PROMISE_ID,
		f.PROMISE_NAME,
		f.PROMISE_TERM_START,
		f.PROMISE_TERM_END,
		f.LEVEL,
		f.PEOPLENUM_INVOLVED,
		f.CORPINFO_ID,
		f.STATE,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.TEXT,
		f.TYPE,
		f.RELEASE_PLATFORM
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		PROMISE_ID,
		PROMISE_NAME,
		PROMISE_TERM_START,
		PROMISE_TERM_END,
		LEVEL,
		PEOPLENUM_INVOLVED,
		CORPINFO_ID,
		STATE,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		TEXT,
		TYPE,
		RELEASE_PLATFORM
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{PROMISE_ID},
		#{PROMISE_NAME},
		#{PROMISE_TERM_START},
		#{PROMISE_TERM_END},
		#{LEVEL},
		#{PEOPLENUM_INVOLVED},
		#{CORPINFO_ID},
		#{STATE},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{TEXT},
		#{TYPE},
		#{RELEASE_PLATFORM}
	</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>,
		  d.NAME as LEVELNAME,
		  GROUP_CONCAT(DISTINCT dept.NAME) as DEPTNAME,
		  GROUP_CONCAT(DISTINCT post.NAME) as POSTNAME,
		(select COUNT(*) from BUS_CORPPROMISE_PEOPLE a WHERE  a.ISSIGN = 1 AND a.PROMISE_ID = f.PROMISE_ID ) as ISSIGN
		from
		<include refid="tableName"></include> f
		left join
		<include refid="dicTableName"></include> d on f.LEVEL = d.BIANMA
		left join BUS_CORPPROMISE_PEOPLE people on people.PROMISE_ID = f.PROMISE_ID
		left join oa_department dept on dept.DEPARTMENT_ID = people.DEPARTMENT_ID
		left join SYS_POST post on post.POST_ID = people.POST_ID
		left join sys_user creUser on creUser.USER_ID = f.CREATOR
		where 1=1
		<if test="pd.roleLevel != null and pd.roleLevel != ''"><!-- 权限显示 -->
			<choose>
				<when test='pd.roleLevel == "0"'>
				</when>
				<when test='pd.roleLevel == "1"'>
					and creUser.DEPARTMENT_ID  in (${pd.supDeparIds})
				</when>
				<when test='pd.roleLevel == "2"'>
					and creUser.USER_ID = #{pd.loginUserId}
				</when>
			</choose>
		</if>
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.PROMISE_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
		</if>
		<if test="pd.LEVEL != null and pd.LEVEL != ''"><!-- 关键词检索 -->
			and f.LEVEL = #{pd.LEVEL}
		</if>
		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''"><!-- 按股份公司部门检索 -->
			and dept.DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
			<if test="pd.CORPDEPARTMENT_ID != null and pd.CORPDEPARTMENT_ID != ''"><!-- 按股份公司部门和企业部门检索 -->
					or dept.DEPARTMENT_ID = #{pd.CORPDEPARTMENT_ID}
				</if>
		</if>
		<if test="pd.CORPDEPARTMENT_ID != null and pd.CORPDEPARTMENT_ID != ''"><!-- 关键词检索 -->
			and dept.DEPARTMENT_ID = #{pd.CORPDEPARTMENT_ID}
		</if>
		<if test="pd.RELEASE_PLATFORM != null and pd.RELEASE_PLATFORM != ''"><!-- 下发对象查询 -->
			and f.RELEASE_PLATFORM = #{pd.RELEASE_PLATFORM}
		</if>
		and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		GROUP BY
		f.PROMISE_ID
		order by CREATTIME desc
	</select>

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

	<!-- 列表 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,d.NAME as LEVELNAME,bci.CORP_NAME as CORPINFO_NAME
		from
		<include refid="tableName"></include> f
		left join
		<include refid="dicTableName"></include> d on f.LEVEL = d.BIANMA
		left join bus_corp_info bci on bci.CORPINFO_ID = f.CORPINFO_ID
		where 1=1
		<if test="PROMISE_ID != null and PROMISE_ID != ''"><!-- 关键词检索 -->
			and f.PROMISE_ID = #{PROMISE_ID}
		</if>
		order by CREATTIME desc
	</select>

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

		where
		PROMISE_ID = #{PROMISE_ID}
	</delete>

	<!-- 列表 -->
	<select id="dataAlllistPage" parameterType="page" resultType="pd">
		select
		f.PROMISE_ID,f.PROMISE_NAME,f.TEXT,f.TYPE
		from
		BUS_PROMISE f
		where 1=1 and f.ISDELETE = 0
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.PROMISE_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''">
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		order by CREATTIME desc
	</select>

	<!-- 副文列表 -->
	<select id="collateralall" parameterType="page" resultType="pd">
		select
		f.PROMISEDETAIL_ID,
		f.PROMISE_ID,
		f.COLLATERAL
		from
		BUS_PROMISE_DETAILS f
		<if test="PROMISE_ID != null and PROMISE_ID != ''"><!-- 关键词检索 -->
			where f.PROMISE_ID = #{PROMISE_ID}
		</if>
		ORDER BY f.SORTINDEX asc
	</select>

	<!-- 修改涉及人数-->
	<delete id="editpeople" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		PEOPLENUM_INVOLVED = #{PEOPLENUM_INVOLVED}
		where
		PROMISE_ID = #{PROMISE_ID}
	</delete>

	<!-- 列表(全部) -->
	<select id="postlistAll" parameterType="pd" resultType="pd">
		select
		*
		from
		SYS_POST f
		where f.ISDELETE = '0'
		<if test="DEPARTMENT_ID!= null and DEPARTMENT_ID != ''"><!-- 检索 -->
			and f.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
			f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="STATUS != null and STATUS != ''"><!-- 关键词检索 -->
			AND
			f.STATUS = #{STATUS}
		</if>
	</select>

	<!-- 用户列表(全部) -->
	<select id="listAllUser" parameterType="pd" resultType="pd" >
		select  u.USER_ID,
		u.USERNAME,
		u.PASSWORD,
		u.LAST_LOGIN,
		u.NAME,
		u.IP,
		u.EMAIL,
		u.NUMBER,
		u.PHONE
		from SYS_USER u
		where  u.USERNAME != 'admin'
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
			u.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''"><!-- 关键词检索 -->
			AND
			u.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
		<if test="POST_ID != null and POST_ID != ''"><!-- 关键词检索 -->
			AND
			u.POST_ID = #{POST_ID}
		</if>
		order by u.DEPARTMENT_ID asc, u.SORT asc,u.LAST_LOGIN desc
	</select>
</mapper>
