<?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.ruoyi.bst.storeStaff.mapper.StoreStaffMapper">

    <resultMap type="StoreStaffVO" id="StoreStaffResult" autoMapping="true">
        <result property="id"    column="id"    />
        <result property="storeId"    column="store_id"    />
        <result property="userId"    column="user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="createId"    column="create_id"    />
        <result property="permissions"    column="permissions"   typeHandler="com.ruoyi.common.mybatis.typehandler.StringSplitListTypeHandler" />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectStoreStaffVo">
        select
            bss.id,
            bss.store_id,
            bss.user_id,
            bss.create_time,
            bss.create_id,
            bss.permissions,
            bss.remark,
            su.user_name as user_phone,
            bs.store_name,
            su_creater.user_name as creator
        <include refid="searchTables"></include>
    </sql>

    <sql id="searchTables">
        from bst_store_staff bss
        left join sys_user su on bss.user_id = su.user_id
        left join sys_user su_creater on bss.create_id = su_creater.user_id
        left join bst_store bs on bs.store_id = bss.store_id
    </sql>

    <sql id="searchCondition">
        <if test="query.storeId != null "> and bss.store_id = #{query.storeId}</if>
        <if test="query.id != null "> and bss.id = #{query.id}</if>
        <if test="query.userId != null "> and bss.user_id = #{query.userId}</if>
        <if test="query.createId != null "> and bss.create_id = #{query.createId}</if>
        <if test="query.permissions != null  and query.permissions != ''"> and bss.permissions = #{query.permissions}</if>
        <if test="query.remark != null  and query.remark != ''"> and bss.remark like concat ('%', #{query.remark},'%')</if>
        <if test="query.excludeId != null">and bss.id != #{query.excludeId}</if>
        <if test="query.ids != null and query.ids.size() > 0">
            and bss.id in
            <foreach item="item" collection="query.ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
        .userSetAlias("bs.user_id")
        .build()
         }
        ${query.params.dataScope}
    </sql>

    <select id="selectStoreStaffList" parameterType="StoreStaffQuery" resultMap="StoreStaffResult">
        <include refid="selectStoreStaffVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectStoreStaffById" parameterType="Long" resultMap="StoreStaffResult">
        <include refid="selectStoreStaffVo"/>
        where id = #{id}
    </select>

    <select id="selectIdList" resultType="java.lang.Long">
        select bss.id
        <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(bss.id)
        <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <insert id="insertStoreStaff" parameterType="StoreStaff" useGeneratedKeys="true" keyProperty="id">
        insert into bst_store_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="storeId != null">store_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createId != null">create_id,</if>
            <if test="permissions != null">permissions,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="storeId != null">#{storeId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createId != null">#{createId},</if>
            <if test="permissions != null">#{permissions,typeHandler=com.ruoyi.common.mybatis.typehandler.StringSplitListTypeHandler},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="StoreStaff" useGeneratedKeys="true" keyProperty="id">
        insert into bst_store_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            store_id,
            user_id,
            create_time,
            create_id,
            permissions,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.storeId != null ">#{i.storeId},</if>
                <if test="i.storeId == null ">default,</if>
                <if test="i.userId != null ">#{i.userId},</if>
                <if test="i.userId == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
                <if test="i.createId != null ">#{i.createId},</if>
                <if test="i.createId == null ">default,</if>
                <if test="i.permissions != null ">#{i.permissions},</if>
                <if test="i.permissions == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_store_staff
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="store_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.storeId != null ">
                        WHEN #{item.id} THEN #{item.storeId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `store_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="user_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.userId != null ">
                        WHEN #{item.id} THEN #{item.userId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `user_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createId != null ">
                        WHEN #{item.id} THEN #{item.createId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `create_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="permissions = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.permissions != null ">
                        WHEN #{item.id} THEN #{item.permissions}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `permissions`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateStoreStaff" parameterType="StoreStaff">
        update bst_store_staff
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.storeId != null">store_id = #{data.storeId},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.createId != null">create_id = #{data.createId},</if>
        <if test="data.permissions != null">permissions = #{data.permissions,typeHandler=com.ruoyi.common.mybatis.typehandler.StringSplitListTypeHandler},</if>
        <if test="data.remark != null">remark = #{data.remark},</if>
    </sql>

    <delete id="deleteStoreStaffById" parameterType="Long">
        delete from bst_store_staff where id = #{id}
    </delete>

    <delete id="deleteStoreStaffByIds" parameterType="String">
        delete from bst_store_staff where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
