<?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.vctgo.system.mapper.SysPostMapper">

	<resultMap type="com.vctgo.system.domain.SysPost" id="SysPostResult">
		<id     property="postId"        column="post_id"       />
		<result property="postCode"      column="post_code"     />
		<result property="postName"      column="post_name"     />
		<result property="postSort"      column="post_sort"     />
		<result property="isSysPost"     column="is_sys_post"        />
		<result property="status"        column="status"        />
		<result property="createById"    column="create_by_id"    />
		<result property="createBy"      column="create_by"     />
		<result property="createTime"    column="create_time"   />
		<result property="updateById"    column="update_by_id"    />
		<result property="updateBy"      column="update_by"     />
		<result property="updateTime"    column="update_time"   />
		<result property="remark"        column="remark"        />
	</resultMap>

	<sql id="selectPostVo">
        select post_id, post_code, post_name, post_sort, is_sys_post, status, create_by, create_time, remark, create_by_id, update_by_id
		from sys_post
    </sql>

	<select id="selectPostList"  resultMap="SysPostResult">
		SELECT
			p.post_id,
			p.post_code,
			p.post_name,
			p.post_sort,
			p.is_sys_post,
			p.STATUS,
		    IFNULL(u.nick_name,'--') create_by,
			p.create_time,
			p.remark,
			p.create_by_id,
			p.update_by_id
		FROM
			sys_post p
		LEFT JOIN sys_user u ON p.create_by_id = u.user_id
		<where>
			<if test="query.postCode != null and query.postCode != ''">
				AND p.post_code like concat('%', #{query.postCode}, '%')
			</if>
			<if test="query.status != null and query.status != ''">
				AND p.status = #{query.status}
			</if>
			<if test="query.postName != null and query.postName != ''">
				AND p.post_name like concat('%', #{query.postName}, '%')
			</if>
			<if test="query.createBy != null  and query.createBy != ''"> and u.nick_name like concat('%', #{query.createBy}, '%')</if>
			<if test="query.params.beginCreateTime != null and query.params.beginCreateTime != ''">
				and date_format(p.create_time,'%y%m%d') &gt;= date_format(#{query.params.beginCreateTime},'%y%m%d')
			</if>
			<if test="query.params.endCreateTime != null and query.params.endCreateTime != ''">
				and date_format(p.create_time,'%y%m%d') &lt;= date_format(#{query.params.endCreateTime},'%y%m%d')
			</if>
			<if test="query.tenantId != null and query.tenantId != ''">
				and p.tenant_id IN (
				select t.id FROM sys_tenant t WHERE t.garden_id =
				(SELECT st.garden_id FROM sys_tenant st WHERE st.id = #{query.tenantId}) union select #{query.tenantId})
			</if>
		</where>
		order by p.is_sys_post desc, p.create_time desc,
		(case when p.is_sys_post ='Y' then p.post_id else 1 end)
	</select>

	<select id="selectPostByTenantId" resultMap="SysPostResult">
		<include refid="selectPostVo"/>
		where status ='0' and tenant_id = #{tenantId}
	</select>

	<select id="selectPostAll" resultMap="SysPostResult">
		<include refid="selectPostVo"/>
		where status ='0' and tenant_id IN (
		select id FROM sys_tenant WHERE garden_id =
		(SELECT garden_id FROM sys_tenant WHERE id = #{tenantId}))
		ORDER BY is_sys_post desc, post_sort, create_time desc,
		(case when is_sys_post ='Y' then post_id else 1 end)
	</select>

	<select id="selectPostByPostSortAndTime" resultMap="SysPostResult">
		<include refid="selectPostVo"/>
		where status ='0' and tenant_id = #{tenantId}
		ORDER BY is_sys_post desc, post_sort, create_time desc,
		(case when is_sys_post ='Y' then post_id else 1 end)
	</select>

	<select id="selectPostById" parameterType="Long" resultMap="SysPostResult">
		<include refid="selectPostVo"/>
		where post_id = #{postId}
	</select>

	<select id="selectPostListByUserId" parameterType="Long" resultType="Long">
		select p.post_id
        from sys_post p
	        left join sys_user_post up on up.post_id = p.post_id
	        left join sys_user u on u.user_id = up.user_id
	    where u.user_id = #{userId}
	</select>

	<select id="selectPostsByUserName" parameterType="String" resultMap="SysPostResult">
		select p.post_id, p.post_name, p.post_code
		from sys_post p
			 left join sys_user_post up on up.post_id = p.post_id
			 left join sys_user u on u.user_id = up.user_id
		where u.user_name = #{userName}
	</select>

	<select id="checkPostNameUnique" resultMap="SysPostResult">
		<include refid="selectPostVo"/>
		 where post_name=#{postName}
		 <if test="tenantId != null and tenantId != ''">
			and tenant_id IN (
			select id FROM sys_tenant WHERE garden_id =
			(SELECT garden_id FROM sys_tenant WHERE id = #{tenantId}))
		 </if>
		 limit 1
	</select>

	<select id="checkPostCodeUnique" parameterType="String" resultMap="SysPostResult">
		<include refid="selectPostVo"/>
		 where post_code=#{postCode} limit 1
	</select>

	<update id="updatePost" >
 		update sys_post
 		<set>
 			<if test="postCode != null and postCode != ''">post_code = #{postCode},</if>
 			<if test="postName != null and postName != ''">post_name = #{postName},</if>
 			<if test="postSort != null and postSort != ''">post_sort = #{postSort},</if>
			<if test="isSysPost != null and isSysPost != ''">is_sys_post = #{isSysPost},</if>
 			<if test="status != null and status != ''">status = #{status},</if>
 			<if test="remark != null">remark = #{remark},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
			<if test="updateById != null and updateById != ''">update_by_id = #{updateById},</if>
 			update_time = sysdate()

 		</set>
 		where post_id = #{postId}
	</update>

 	<insert id="insertPost"  useGeneratedKeys="true" keyProperty="postId">
 		insert into sys_post(
 			<if test="postId != null and postId != 0">post_id,</if>
 			<if test="postCode != null and postCode != ''">post_code,</if>
 			<if test="postName != null and postName != ''">post_name,</if>
 			<if test="postSort != null and postSort != ''">post_sort,</if>
 			<if test="isSysPost != null and isSysPost != ''">is_sys_post,</if>
 			<if test="status != null and status != ''">status,</if>
 			<if test="remark != null and remark != ''">remark,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
		    <if test="createById != null and createById != ''">create_by_id,</if>
 			create_time
 		)values(
 			<if test="postId != null and postId != 0">#{postId},</if>
 			<if test="postCode != null and postCode != ''">#{postCode},</if>
 			<if test="postName != null and postName != ''">#{postName},</if>
 			<if test="postSort != null and postSort != ''">#{postSort},</if>
			<if test="isSysPost != null and isSysPost != ''">#{isSysPost},</if>
 			<if test="status != null and status != ''">#{status},</if>
 			<if test="remark != null and remark != ''">#{remark},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
		    <if test="createById != null and createById != ''">#{createById},</if>
 			sysdate()
 		)
	</insert>

	<update id="multiAdjustBranchPost" parameterType="Long">
		update sys_post set tenant_id = (select id from sys_tenant where tenant_type=1
							and garden_id = #{gardenId} and del_flag=0)
		where tenant_id = (select id from sys_tenant where tenant_type = #{tenantType}
		and branch_id = #{branchId} and del_flag =0)
	</update>

	<delete id="deletePostById" parameterType="Long">
		delete from sys_post where post_id = #{postId}
	</delete>

	<delete id="deletePostByIds" parameterType="Long">
 		delete from sys_post where post_id in
 		<foreach collection="array" item="postId" open="(" separator="," close=")">
 			#{postId}
        </foreach>
 	</delete>

	<delete id="deletePostByTenantId">
		delete from sys_post where tenant_id in
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

	<delete id="deleteSysDefaultPost">
		delete from sys_post where tenant_id in
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
        and is_sys_post = 'Y'
	</delete>

</mapper>
