<?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.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper">

    <select id="selectAllShopId" resultType="java.lang.Long">
        select id
        from poi_store where status = 1
    </select>
    <select id="selectByUserId" resultType="java.lang.Long">
        select store_id from poi_store_staff where user_id = #{userId}
    </select>
    <select id="selectSellersByShopIds" resultType="com.kiisoo.ic.store.bean.PoiStoreStaffDTO">
        select
        t2.id, t2.name, t2.mobil, t2.email, t2.address, t2.remark, t2.status,t1.id as staffId,t1.staff_code
        from poi_store_staff t1, privilage_user t2
        where t1.user_id = t2.id and
        <choose>
            <when test="shopId != null and shopId.size > 0">
                t1.store_id in
                <foreach collection="shopId"  index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                1 = 0
            </otherwise>
        </choose>
        group by t1.staff_code
    </select>

    <select id="selectSellersByShopIdsCount" resultType="Integer">
        select
        count(1)
        from poi_store_staff t1, privilage_user t2
        where t1.user_id = t2.id
            <if test="shopId != null and shopId.size > 0">
                and t1.store_id in
                <foreach collection="shopId"  index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
    </select>
    <select id="selectShopManagerByShop" resultType="java.lang.String">
        select staff_code from poi_store_staff where store_id = #{shopId} and status = 1 and type = 1 and user_id != 0
        limit 1
    </select>
    <select id="selectShopManagerDetailByShop" resultType="com.kiisoo.ic.store.entity.PoiStoreStaff">
        select id,store_id as storeId,user_id as userId,staff_code as staffCode from poi_store_staff where store_id = #{shopId} and status = 1 and type = 1 and user_id != 0
        limit 1
    </select>

    <select id="selectInfoById" resultType="com.kiisoo.ic.webappmy.vo.StafferInfoVO">
        select t2.id,t2.staff_code as stafferNo,t1.`name`,t3.`name` as shopName,t3.id as storeId
        from privilage_user t1, poi_store_staff t2, poi_store t3
        where t1.id = t2.user_id and t2.`status` = 1 and t2.store_code = t3.`code`
        and t1.id = #{userId}
    </select>

    <select id="selectStaffIdByUserId" resultType="java.lang.Long">
        select id from poi_store_staff where user_id = #{userId}
        limit 1
    </select>

    <select id="selectStaffIdByTag" resultType="java.lang.Long">
        select staff_id from poi_store_staff_tag where store_id = #{storeId} and tag = #{tag}
        limit 1
    </select>

    <select id="selectStaffTagByStoreId" resultType="java.util.Map">
        select staff_id as staffId,tag from poi_store_staff_tag where store_id = #{storeId}
    </select>

    <select id="selectUserNameById" resultType="java.lang.String">
        select IFNULL(t5.name, '') from poi_store_staff t4,
                            privilage_user t5 where  t4.user_id = t5.id and t4.id  = #{id}

    </select>

    <select id="selectStaffCodeByTagNew" resultType="java.lang.String">
        select t2.staff_code from poi_store_staff_tag t, poi_store_staff t2 where t2.store_id = t.store_id and t2.id = t.staff_id and  t.store_id = #{storeId}  and t.tag = #{tag}
        limit 1
    </select>
</mapper>
