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

    <resultMap type="StoreStaffVO" id="StoreStaffResult" autoMapping="true">
        <result property="permissions" column="permissions" typeHandler="com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler"/>
    </resultMap>

    <sql id="selectStoreStaffVo">
        select
            sss.employ_id,
            sss.store_id,
            sss.user_id,
            sss.remark,
            sss.role,
            sss.point,
            sss.enabled,
            sss.permissions,
            sss.create_time,
            sss.create_by,
            sss.create_id,
            ss.name as store_name,
            ss.user_id as mch_id,
            if(su.is_real, su.real_name, su.user_name) as user_name
        from ss_store_staff sss
            left join sm_store ss on ss.store_id = sss.store_id
            left join sm_user su on su.user_id = sss.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.employId != null "> and sss.employ_id = #{query.employId}</if>
        <if test="query.storeId != null "> and sss.store_id = #{query.storeId}</if>
        <if test="query.userId != null "> and sss.user_id = #{query.userId}</if>
        <if test="query.remark != null  and query.remark != ''"> and sss.remark like concat('%', #{query.remark}, '%')</if>
        <if test="query.role != null  and query.role != ''"> and sss.role = #{query.role}</if>
        <if test="query.point != null "> and sss.point = #{query.point}</if>
        <if test="query.enabled != null "> and sss.enabled = #{query.enabled}</if>
        <if test="query.createId != null "> and sss.create_id = #{query.createId}</if>
        <if test="query.storeName != null and query.storeName != ''">and ss.name like concat('%', #{query.storeName}, '%')</if>
        <if test="query.userName != null and query.userName != ''">and if(su.is_real, su.real_name, su.user_name) like concat('%', #{query.userName}, '%')</if>
        <if test="query.mchId != null">and ss.user_id = #{query.mchId}</if>
        <if test="query.permission != null and query.permission != ''">
            and find_in_set(#{query.permission}, sss.permissions);
        </if>
        <if test="query.excludeEmployId != null">
            and sss.employ_id != #{query.excludeEmployId}
        </if>
        <if test="query.storeIds != null and query.storeIds.size() > 0">
            and sss.store_id in
            <foreach collection="query.storeIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </sql>

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

    <select id="selectStoreStaffByEmployId" parameterType="Long" resultMap="StoreStaffResult">
        <include refid="selectStoreStaffVo"/>
        where sss.employ_id = #{employId}
    </select>

    <select id="selectSumOfPoint" resultType="java.math.BigDecimal">
        select sum(sss.point)
        from ss_store_staff sss
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(sss.employ_id)
        from ss_store_staff sss
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectCountGroupByUserId" resultType="LongCountVO">
        select sss.user_id as `key`, count(sss.employ_id) as `value`
        from ss_store_staff sss
        <where>
            <include refid="searchCondition"/>
        </where>
        group by `key`
    </select>

    <insert id="insertStoreStaff" parameterType="StoreStaff" useGeneratedKeys="true" keyProperty="employId">
        insert into ss_store_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="storeId != null">store_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="remark != null">remark,</if>
            <if test="role != null and role != ''">`role`,</if>
            <if test="point != null">`point`,</if>
            <if test="enabled != null">enabled,</if>
            <if test="permissions != null and permissions != ''">permissions,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createId != null">create_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="storeId != null">#{storeId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="role != null and role != ''">#{role},</if>
            <if test="point != null">#{point},</if>
            <if test="enabled != null">#{enabled},</if>
            <if test="permissions != null and permissions != ''">#{permissions,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createId != null">#{createId},</if>
         </trim>
    </insert>

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

    <update id="bindUser">
        update ss_store_staff
        set user_id = #{userId}
        where employ_id = #{employId} and user_id is null
    </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.remark != null">remark = #{data.remark},</if>
        <if test="data.role != null and data.role != ''">`role` = #{data.role},</if>
        <if test="data.point != null">`point` = #{data.point},</if>
        <if test="data.enabled != null">enabled = #{data.enabled},</if>
        <if test="data.permissions != null and data.permissions != ''">permissions = #{data.permissions,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.createBy != null">create_by = #{data.createBy},</if>
        <if test="data.createId != null">create_id = #{data.createId},</if>
    </sql>

    <delete id="deleteStoreStaffByEmployId" parameterType="Long">
        delete from ss_store_staff where employ_id = #{employId}
    </delete>

    <delete id="deleteStoreStaffByEmployIds" parameterType="String">
        delete from ss_store_staff where employ_id in
        <foreach item="employId" collection="employIds" open="(" separator="," close=")">
            #{employId}
        </foreach>
    </delete>
</mapper>
