<?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.fs.company.mapper.CompanyPostMapper">

    <resultMap type="CompanyPost" id="CompanyPostResult">
        <result property="postId"    column="post_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="postCode"    column="post_code"    />
        <result property="postName"    column="post_name"    />
        <result property="postSort"    column="post_sort"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectCompanyPostVo">
        select post_id, company_id, post_code, post_name, post_sort, status, create_by, create_time, update_by, update_time, remark from company_post
    </sql>

    <select id="selectCompanyPostList" parameterType="CompanyPost" resultMap="CompanyPostResult">
        <include refid="selectCompanyPostVo"/>
        <where>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="postCode != null  and postCode != ''"> and post_code = #{postCode}</if>
            <if test="postName != null  and postName != ''"> and post_name like concat('%', #{postName}, '%')</if>
            <if test="postSort != null "> and post_sort = #{postSort}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by post_sort, create_time desc


    </select>

    <select id="selectCompanyPostById" parameterType="Long" resultMap="CompanyPostResult">
        <include refid="selectCompanyPostVo"/>
        where post_id = #{postId}
    </select>

    <insert id="insertCompanyPost" parameterType="CompanyPost" useGeneratedKeys="true" keyProperty="postId">
        insert into company_post
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="postCode != null and postCode != ''">post_code,</if>
            <if test="postName != null and postName != ''">post_name,</if>
            <if test="postSort != null">post_sort,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="postCode != null and postCode != ''">#{postCode},</if>
            <if test="postName != null and postName != ''">#{postName},</if>
            <if test="postSort != null">#{postSort},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCompanyPost" parameterType="CompanyPost">
        update company_post
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="postCode != null and postCode != ''">post_code = #{postCode},</if>
            <if test="postName != null and postName != ''">post_name = #{postName},</if>
            <if test="postSort != null">post_sort = #{postSort},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where post_id = #{postId}
    </update>

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

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


    <select id="selectPostsByUserName" parameterType="String" resultMap="CompanyPostResult">
		select p.post_id, p.post_name, p.post_code
		from company_post p
			 left join company_user_post up on up.post_id = p.post_id
			 left join company_user u on u.user_id = up.user_id
		where u.user_name = #{userName}
	</select>
    <select id="selectPostListByUserId" parameterType="Long" resultType="Integer">
		select p.post_id
        from company_post p
	        left join company_user_post up on up.post_id = p.post_id
	        left join company_user u on u.user_id = up.user_id
	    where u.user_id = #{userId}
	</select>

        <select id="selectPostsByUserId"  resultMap="CompanyPostResult">
		select p.post_id, p.post_name, p.post_code
		from company_post p
			 left join company_user_post up on up.post_id = p.post_id
			 left join company_user u on u.user_id = up.user_id
		where u.user_id = #{user_id}
	</select>
</mapper>
