<?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.ho.user.mapper.SysPositionMapper">


    <sql id="Base_Column_List">
        id
        ,post_code,
        name,status,
        create_time,update_time,
        deleted,`desc`
    </sql>

    <sql id="Base_SysUser_List">
        u
        .
        id
        , u.username,  u.dept_id, u.group_id, u.remark,u.real_name
    </sql>
    <select id="selectByPrimaryKey" resultType="com.ho.user.entity.SysPosition">
        select
        <include refid="Base_Column_List"/>
        from sys_position
        where id = #{id,jdbcType=INTEGER}
        and deleted = 1
    </select>


    <select id="selectByInfo" resultType="com.ho.user.entity.SysPosition">
        select
        <include refid="Base_Column_List"/>
        from sys_position
        <where>
            and deleted = 1
            <if test="postCode != null and name != null">
                and ( post_code = #{postCode} or `name` = #{name})
            </if>
            <if test="id != null">
                and id != #{id}
            </if>
        </where>
    </select>

    <select id="selectPostListByPostIds" resultType="com.ho.user.entity.SysPosition">
        select
        <include refid="Base_Column_List"/>
        from sys_position
        <where>
            and deleted = 1
            <if test="list!=null and list.size !=0">
                and id in
                <foreach collection="list" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectObscure" resultType="com.ho.user.entity.SysPosition">
        select
        <include refid="Base_Column_List"/>
        from sys_position
        <where>
            and deleted = 1
            <if test="postCode != null">
                and post_code LIKE concat('%',#{postCode},'%')
            </if>
            <if test="name != null">
                and name LIKE concat('%',#{name},'%')
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
        </where>
    </select>

    <select id="getPositionByTop" resultType="com.ho.user.entity.SysPosition">
        select
        <include refid="Base_Column_List"/>
        from sys_position
        where deleted = 1
    </select>

    <select id="selectPostIds" resultType="java.lang.Integer">
        select
        id
        from sys_position
        <where>
            and deleted = 1
            <if test="list!=null and list.size !=0">
                and post_code in
                <foreach collection="list" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="select" resultType="com.ho.user.entity.SysPosition">
        select
        <include refid="Base_Column_List"/>
        from sys_position
        <where>
            and deleted = 1
            <if test="status != null">
                and status = #{status}
            </if>
        </where>
    </select>


    <delete id="deleteByPrimaryKey">
        delete
        from sys_position
        where id = #{id}
    </delete>


    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.user.entity.SysPosition"
            useGeneratedKeys="true">
        insert into sys_position
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="updateTime != null">update_time,</if>
            <if test="postCode != null">post_code,</if>
            <if test="name != null">name,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="deleted != null">deleted,</if>
            <if test="desc != null">`desc`,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="updateTime != null">#{updateTime},</if>
            <if test="postCode != null">#{postCode,jdbcType=VARCHAR},</if>
            <if test="name != null">#{name,jdbcType=VARCHAR},</if>
            <if test="status != null">#{status,jdbcType=INTEGER},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="deleted != null">#{deleted,jdbcType=INTEGER},</if>
            <if test="desc != null">#{desc},</if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ho.user.entity.SysPosition">
        update sys_position
        <set>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="postCode != null">
                post_code = #{postCode,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status = #{status,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="deleted != null">
                deleted = #{deleted,jdbcType=INTEGER},
            </if>
            <if test="desc != null">
                `desc` = #{desc},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>


    <select id="selectUserByPostIds" resultType="com.ho.user.api.entity.SysUser">
        select distinct
        <include refid="Base_SysUser_List"/>
        from `sys_user` u
        left join
        `sys_user_position` sup
        on u.id = sup.user_id
        <where>
            <if test="vo.groupId!= null">
                and u.group_id = #{vo.groupId}
            </if>
            <if test="list!=null and list.size !=0">
                and sup.position_id in
                <foreach collection="list" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            and u.status = 1
        </where>
    </select>
</mapper>
