<?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="jnpf.store.mapper.StoreMapper">

    <resultMap id="storeResultMap" type="jnpf.entity.StoreEntity">
        <id property="id" column="F_Id" jdbcType="VARCHAR"/>
        <result property="organizeid" column="F_OrganizeId" jdbcType="VARCHAR"/>
        <result property="storeheaduserid" column="F_StoreHeadUserId" jdbcType="VARCHAR"/>
        <result property="storename" column="F_StoreName" jdbcType="VARCHAR"/>
        <result property="storepic" column="F_StorePic" jdbcType="VARCHAR"/>
        <result property="longitude" column="F_Longitude" jdbcType="VARCHAR"/>
        <result property="latitude" column="F_Latitude" jdbcType="VARCHAR"/>
        <result property="address" column="F_Address" jdbcType="VARCHAR"/>
        <result property="disabled" column="F_Disabled" jdbcType="INTEGER"/>
        <result property="areaNum" column="F_AreaNum" jdbcType="DECIMAL"/>
        <result property="setRegion" column="F_SetRegion" jdbcType="INTEGER"/>
        <result property="creatoruserid" column="F_CreatorUserId" jdbcType="VARCHAR"/>
        <result property="creatortime" column="F_CreatorTime" jdbcType="TIMESTAMP"/>
        <result property="lastmodifyuserid" column="F_LastModifyUserId" jdbcType="VARCHAR"/>
        <result property="lastmodifytime" column="F_LastModifyTime" jdbcType="TIMESTAMP"/>
        <result property="deletemark" column="F_DeleteMark" jdbcType="INTEGER"/>
        <result property="deletetime" column="F_DeleteTime" jdbcType="TIMESTAMP"/>
        <result property="deleteuserid" column="F_DeleteUserId" jdbcType="VARCHAR"/>
        <result property="tenantid" column="F_TenantId" jdbcType="VARCHAR"/>
    </resultMap>

    <insert id="createStore" parameterType="jnpf.entity.StoreEntity">
        insert into ftb_store (F_Id, F_OrganizeId, F_StoreHeadUserId, F_StoreName, F_StorePic,
        F_Longitude, F_Latitude, F_Address, F_Disabled, F_AreaNum, F_SetRegion, F_CreatorUserId, F_CreatorTime, F_LastModifyUserId,
        F_LastModifyTime, F_DeleteMark, F_DeleteTime, F_DeleteUserId, F_TenantId)
        values(
            #{id},
            #{organizeid},
            #{storeheaduserid},
            #{storename},
            #{storepic},
            #{longitude},
            #{latitude},
            #{address},
            #{disabled},
            #{areaNum},
            #{setRegion},
            #{creatoruserid},
            #{creatortime},
            #{lastmodifyuserid},
            #{lastmodifytime},
            #{deletemark},
            #{deletetime},
            #{deleteuserid},
            #{tenantid}
            )
    </insert>

    <select id="getStoreById" resultMap="storeResultMap">
        SELECT F_Id, F_OrganizeId, F_StoreHeadUserId, F_StoreName, F_StorePic, F_Longitude, F_Latitude, F_Address, F_Disabled, F_AreaNum, F_SetRegion FROM ftb_store WHERE F_Id = #{id}
    </select>

    <select id="getStoreByIds" resultMap="storeResultMap">
        SELECT F_Id, F_OrganizeId, F_StoreHeadUserId, F_StoreName, F_StorePic, F_Longitude, F_Latitude, F_Address, F_Disabled
        FROM ftb_store WHERE F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getStoresByOrganizeId" resultMap="storeResultMap">
        SELECT F_Id, F_OrganizeId, F_StoreHeadUserId, F_StoreName, F_StorePic, F_Longitude, F_Latitude, F_Address, F_Disabled
        FROM ftb_store
        WHERE F_OrganizeId = #{organizeId} and F_Disabled = 0 and F_DeleteMark = 0
        ORDER BY F_LastModifyTime ASC
    </select>

    <!-- OR INSTR(fullName ,#{keyword})> 0 OR INSTR(realName ,#{keyword})> 0 OR INSTR(address ,#{keyword})> 0 -->
    <select id="getStoresByPage"  resultType="jnpf.entity.StoreEntity">
        SELECT
        fs.F_Id id,
        fs.F_OrganizeId organizeid,
        fs.F_StoreHeadUserId storeheaduserid,
        fs.F_StoreName storename,
        fs.F_StorePic storepic,
        fs.F_Longitude longitude,
        fs.F_Latitude latitude,
        fs.F_Address address,
        fs.F_Disabled disabled,
        bo.F_FullName fullName,
        bu.F_RealName realName,
        fs.F_CreatorTime creatortime
        FROM
        ftb_store fs
        LEFT JOIN base_organize bo ON bo.F_Id = fs.F_OrganizeId
        LEFT JOIN base_user bu ON bu.F_Id = fs.F_StoreHeadUserId
        WHERE
        fs.F_DeleteMark = 0
        <if test="keyword != null and keyword != ''">
            AND ( INSTR(bo.F_FullName ,#{keyword})> 0
            OR INSTR(bu.F_RealName ,#{keyword})> 0
            OR INSTR(fs.F_StoreName ,#{keyword})> 0
            OR INSTR(fs.F_Address ,#{keyword})> 0
            )
        </if>
        <if test="searchStatus != null and searchStatus !=-1">
            AND fs.F_Disabled = #{searchStatus}
        </if>
        GROUP BY
        fs.F_Id
        ORDER BY
        fs.F_CreatorTime DESC
    </select>

    <select id="getStoresByStorePage"  resultType="jnpf.entity.StoreEntity">
        SELECT * FROM (
        SELECT fs.F_Id id, fs.F_OrganizeId organizeid, fs.F_StoreHeadUserId storeheaduserid, fs.F_StoreName storename, fs.F_StorePic storepic, fs.F_Longitude longitude, fs.F_Latitude latitude, fs.F_Address address, fs.F_Disabled disabled
        ,(SELECT F_FullName FROM base_organize WHERE FIND_IN_SET(F_Id, fs.F_OrganizeId) > 0 )  fullName ,
        (SELECT F_RealName FROM base_user WHERE fs.F_StoreHeadUserId = F_Id) realName ,fs.F_CreatorTime creatortime
        FROM ftb_store fs where fs.F_Disabled = 0 and fs.F_DeleteMark = 0
        ) t
        WHERE 1=1
        <if test="keyword != null and keyword != ''">
            AND INSTR(storename, #{keyword}) > 0
        </if>
        GROUP BY t.id
        ORDER BY t.creatortime DESC
    </select>

    <select id="getStores" resultMap="storeResultMap">
        SELECT F_Id, F_OrganizeId, F_StoreHeadUserId, F_StoreName, F_StorePic, F_Longitude, F_Latitude, F_Address, F_Disabled
        FROM ftb_store
        WHERE F_Disabled = 0 and (F_DeleteMark = 0 OR F_DeleteMark IS NULL)
        ORDER BY F_LastModifyTime ASC
    </select>

    <select id="getStoresEnabled" resultMap="storeResultMap">
        SELECT F_Id, F_OrganizeId, F_StoreHeadUserId, F_StoreName, F_StorePic, F_Longitude, F_Latitude, F_Address, F_Disabled FROM ftb_store
        WHERE F_Disabled = 0
        ORDER BY F_LastModifyTime ASC
    </select>

    <select id="getStoresCount" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT fs.F_Id)
        FROM ftb_store fs
        JOIN base_organize bo ON FIND_IN_SET(bo.F_Id, fs.F_OrganizeId) > 0
        JOIN base_user bu ON fs.F_StoreHeadUserId = bu.F_Id
        <where>
            <if test="keyword != null and keyword != ''">
                AND (bo.F_FullName LIKE CONCAT('%', #{keyword}, '%')
                OR bu.F_RealName LIKE CONCAT('%', #{keyword}, '%')
                OR fs.F_StoreName LIKE CONCAT('%', #{keyword}, '%')
                OR fs.F_Address LIKE CONCAT('%', #{keyword}, '%'))
            </if>
        </where>
    </select>
    <!-- 判断门店是否已经存在 -->
    <select id="getStoreByName" resultType="java.lang.Integer">
        select count(*) from ftb_store
        where F_StoreName = #{storeName}
        <if test="null != storeId">
            and F_Id != #{storeId}
        </if>
    </select>
    <!-- 查询所有门店 -->
    <select id="getAllStoreList" resultType="jnpf.model.store.Store">
        SELECT F_Id id, F_OrganizeId organizeid, F_StoreHeadUserId storeheaduserid, F_StoreName storename, F_StorePic storepic,
               F_Longitude longitude, F_Latitude latitude, F_Address address, F_Disabled disabled
        FROM ftb_store WHERE F_Disabled = 0 and F_DeleteMark = 0
        <if test="null != list and list.size > 0">
            and F_OrganizeId in
            <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="null != storeName and '' != storeName">
            AND INSTR(F_StoreName ,#{storeName})> 0
        </if>
        ORDER BY F_LastModifyTime ASC
    </select>
<!--    获取登录用户的门店信息-->
    <select id="getUserStoreList" resultType="jnpf.model.store.Store">
        SELECT s.F_Id  id, s.F_StoreName storename
        FROM ftb_store_user u
        LEFT JOIN ftb_store s ON u.F_StoreId = s.F_Id
        WHERE u.F_UserId = #{userId}  AND s.F_Disabled = 0 AND s.F_DeleteMark = 0
    </select>

    <!--    获取登录用户的门店信息-->
    <select id="getUserStoreListDuty" resultType="jnpf.model.store.Store">
        SELECT distinct s.F_Id  id, s.F_StoreName storename
        FROM ftb_store_user u
                 LEFT JOIN ftb_store s ON u.F_StoreId = s.F_Id
        WHERE   s.F_Disabled = 0 AND s.F_DeleteMark = 0 and ( u.F_UserId = #{userId}
        <if test="null != positionIds and positionIds.size > 0">
            or u.F_PositionId in
            <foreach collection="positionIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        )
    </select>

    <!-- 检查哪些用户不能被选择 -->
    <select id="checkUserCantChoose" resultType="java.lang.String">
        select distinct F_UserId from ftb_store_user
        where 1 = 1
        <if test="exceptList.size > 0">
            and F_UserId not in
            <foreach collection="exceptList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and F_UserId in
        <foreach collection="chooseList" index="index" item="item" open="(" separator="," close=")">
          #{item}
        </foreach>
    </select>
    <!-- 根据组织查询门店列表 -->
    <select id="getStoreListByOrganizeIds" resultType="jnpf.model.store.Store">
        select F_Id id, F_StoreName storename, F_StoreHeadUserId storeheaduserid from ftb_store
        where F_Disabled = 0 and F_DeleteMark = 0 and F_OrganizeId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <update id="update" parameterType="jnpf.entity.StoreEntity" >
        UPDATE ftb_store
        <set>
            F_OrganizeId = #{store.organizeid},
            F_StoreHeadUserId = #{store.storeheaduserid},
            F_StoreName = #{store.storename},
            F_StorePic = #{store.storepic},
            F_Longitude = #{store.longitude},
            F_Latitude = #{store.latitude},
            F_Address = #{store.address},
            F_Disabled = #{store.disabled},
            F_LastModifyUserId = #{store.lastmodifyuserid},
            F_LastModifyTime = #{store.lastmodifytime},
            F_AreaNum =  #{store.areaNum},
            F_SetRegion = #{store.setRegion}
        </set>
        WHERE F_Id = #{id}
    </update>
<!--    获取所有的门店列表-->
    <select id="getAllStore" resultType="jnpf.model.store.Store">
        select F_Id id, F_StoreName storename, F_StoreHeadUserId storeheaduserid , F_OrganizeId organizeid
        from ftb_store
        where F_Disabled = 0 and F_DeleteMark = 0
    </select>

    <select id="getStoreByNameAndOrgId" resultType="java.lang.Integer">
        select count(*) from ftb_store
        where F_StoreName = #{storeName}
            and F_OrganizeId = #{orgId}
    </select>

    <select id="getAllStoreByOrganizeId" resultMap="storeResultMap">
        SELECT F_Id, F_OrganizeId, F_StoreHeadUserId, F_StoreName, F_StorePic, F_Longitude, F_Latitude, F_Address, F_Disabled
        FROM ftb_store
        WHERE F_OrganizeId = #{organizeId}
          and F_DeleteMark = 0
        ORDER BY F_CreatorTime DESC
    </select>

    <!-- 根据门店ids分页查询门店 -->
    <select id="getStorePageByIds" resultType="jnpf.model.vo.StoreExecutionVo">
        select F_Id storeId, F_StoreName storeName
        from ftb_store
        where F_Disabled = 0 and F_DeleteMark = 0
        <if test="null != list and list.size > 0">
            and F_Id in
            <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="null != storeName and '' != storeName">
            and instr(F_StoreName, #{storeName}) > 0
        </if>
        <if test="1 == sortNum">
            order by F_CreatorTime asc
        </if>
        <if test="0 == sortNum and null != list and list.size > 0">
            order by field(F_Id,
            <foreach collection="list" index="index" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>
    </select>
    <!-- 查询异常的门店 -->
    <select id="getAbnormalStoreIds" resultType="java.lang.String">
        select F_Id from ftb_store
        where F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and (F_Disabled = 1 or F_DeleteMark = 1)
    </select>
    <select id="getAllStoreListNew" resultType="jnpf.model.store.Store">
        SELECT F_Id id, F_OrganizeId organizeid, F_StoreHeadUserId storeheaduserid, F_StoreName storename, F_StorePic storepic,
        F_Longitude longitude, F_Latitude latitude, F_Address address, F_Disabled disabled
        FROM ftb_store WHERE F_Disabled = 0 and F_DeleteMark = 0
        <if test="null != list and list.size > 0">
            and F_OrganizeId in
            <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="null != queryStoreIds and queryStoreIds.size > 0">
            and F_Id in
            <foreach collection="queryStoreIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="null != storeName and '' != storeName">
            AND INSTR(F_StoreName ,#{storeName})> 0
        </if>
        ORDER BY F_LastModifyTime ASC
    </select>
<!--    校验是否是值班人-->
    <select id="checkStoreUser" resultType="java.lang.Integer">
        SELECT COUNT(0)
        FROM ftb_store_user
        WHERE F_UserId =  #{userId}
    </select>
    <select id="queryShopManager" resultType="jnpf.entity.StoreEntity">
        select *
        from ftb_store
        where F_StoreHeadUserId = #{userId}
          and F_Disabled = 0
          AND F_DeleteMark = 0
    </select>
</mapper>
