<?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.system.PostMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.DEPARTMENT_ID,
		f.CORPINFO_ID,
		f.NAME,
		f.DESCR,
		f.STATUS,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.POST_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		DEPARTMENT_ID,
		CORPINFO_ID,
		NAME,
		DESCR,
		STATUS,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		POST_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{DEPARTMENT_ID},
		#{CORPINFO_ID},
		#{NAME},
		#{DESCR},
		#{STATUS},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{POST_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
			POST_ID = #{POST_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			NAME = #{NAME},
			DESCR = #{DESCR},
			STATUS = #{STATUS},
			POST_ID = POST_ID
		where
			POST_ID = #{POST_ID}
	</update>

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

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		d.NAME as DEPARTMENT_NAME
		from
		<include refid="tableName"></include> f
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID=f.DEPARTMENT_ID
		where f.ISDELETE = '0'
		<if test="pd.DEPARTMENT_ID!= null and pd.DEPARTMENT_ID != ''"><!-- 检索 -->
			and f.DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			AND
				f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					f.DESCR LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> 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="NAME!= null and NAME != ''"><!-- 检索 -->
			and f.NAME = #{NAME}
		</if>
		<if test="STATUS != null and STATUS != ''"><!-- 关键词检索 -->
			AND
				f.STATUS = #{STATUS}
		</if>
	</select>

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

	<!-- 获取同部门下的名称 -->
	<select id="findByNameAndDepartment" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
			where f.ISDELETE = '0'
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
			and f.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
			<if test="NAME  !=null and NAME != ''">
			and f.NAME = #{NAME}
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''">
			AND
				f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
	</select>

</mapper>
