<?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.PoiStoreDOMapper">

    <select id="selectRegionShop" resultType="com.kiisoo.ic.store.entity.PoiStore">
        select t1.id,t1.name,t1.type,t1.code from poi_store t1, poi_store_region t2
        where t1.id = t2.store_id and t2.region_id = #{regionId}
    </select>
    <select id="selectShopByName" resultType="com.kiisoo.ic.store.entity.PoiStore">
        select t1.id,t1.name,t1.type,t1.code from poi_store t1
        where name = #{name}
    </select>
    <select id="selectShopMsg" resultType="com.kiisoo.ic.generalize.entity.ShopRegionDO">
        select t1.name as shopName,t3.name as regionName
        from poi_store t1 left join poi_store_region t2 on t1.id = t2.store_id left join poi_region t3 on t2.region_id = t3.id
        where t1.id = #{shopId}
    </select>
    <select id="selectCompanyStore" resultType="com.kiisoo.ic.generalize.entity.CompanyStoreDO">
        select t6.name as companyName,t5.name as shopName,t5.code as shopCode
        from poi_store_staff t1,
        privilage_organizational t2,
        privilage_organizational t6,
        privilage_organizational_domain t3,
        privilage_domain_entity t4,
        poi_store t5
        where t2.id = t3.org_id
        and t2.parent_id = t6.id
        and t1.store_id = t5.id
        and t3.domain_id = t4.domain_id
        and t4.type = 3
        and t1.store_id = t4.entity_id
        and t1.id = #{sellerId}
        limit 1
    </select>
    <select id="selectShopByCompany" resultType="com.kiisoo.ic.store.entity.PoiStore">
        select t3.id,t3.name
        from privilage_organizational t,
        privilage_organizational_domain t1,
        privilage_domain_entity t2,
        poi_store t3
        where 1 = 1
        <if test="orgId != null">
            and t.parent_id = #{orgId}
        </if>
        and t.level = 3
        and t.id = t1.org_id
        and t1.domain_id = t2.domain_id
        and t2.entity_id = t3.id
        and t2.type = 3
    </select>
    <select id="selectDTOBatchIds" resultType="com.kiisoo.ic.store.bean.PoiStoreDTO">
        SELECT
        t.*,
        t1.domain_id,
        t2.org_id AS company_id,
        t3.parent_id
        FROM
        poi_store t
        LEFT JOIN privilage_domain_entity t1 ON t.id = t1.entity_id
        LEFT JOIN privilage_organizational_domain t2 ON t1.domain_id = t2.domain_id
        LEFT JOIN privilage_organizational t3 ON t2.org_id = t3.id
        WHERE
        1 = 1
        <choose>
            <when test="shopIds != null and shopIds.size > 0">
                and t.id in
                <foreach collection="shopIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and 1 = 0
            </otherwise>
        </choose>
    </select>
    <select id="selectCustomerShopByCompany" resultType="com.kiisoo.ic.store.entity.PoiStore">
        select t3.id,t3.name
        from privilage_organizational t,
        privilage_organizational_domain t1,
        privilage_domain_entity t2,
        poi_store t3
        where 1 = 1
        and t.id = #{orgId}
        and t.level = 3
        and t.id = t1.org_id
        and t1.domain_id = t2.domain_id
        and t2.entity_id = t3.id
        and t2.type = 3
    </select>


    <select id="selectShopByCustomerIds" resultType="com.kiisoo.ic.store.entity.PoiStore">
        select t3.id,t3.name,t3.code
        from privilage_organizational_domain t1,
        privilage_domain_entity t2,
        poi_store t3
        where t1.domain_id = t2.domain_id
        and t2.entity_id = t3.id
        and t2.type = 3
        <choose>
            <when test="customerIds != null and customerIds.size > 0">
                and t1.org_id in
                <foreach collection="customerIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and 1 = 0
            </otherwise>
        </choose>
    </select>
    <select id="selectSellerByShop" resultType="com.kiisoo.ic.system.entity.PrivilageUserDO">
        select t2.id ,t2.name
        from poi_store_staff t1,
        privilage_user t2
        where t1.user_id = t2.id
        <if test="shopId != null">
            and t1.store_id = #{shopId}
        </if>
    </select>
    <select id="selectStoreQrCode" resultType="java.lang.String">
        select t2.qr_code as qrCode
        from privilage_cp_user_store t1,privilage_cp_user t2
        where t1.cp_user_id = t2.id
        and t1.store_id = #{shopId}
        limit 1
    </select>
    <select id="selectShopByNameOrCode" resultType="com.kiisoo.ic.store.entity.PoiStore">
        select t1.id,t1.name,t1.type,t1.code from poi_store t1
        <if test="name != null and name != ''">
            where (t1.name LIKE CONCAT('%',#{name},'%') OR t1.code LIKE
            CONCAT('%',#{name},'%'))
        </if>
    </select>

    <select id="selectAllCompanyStore" resultType="com.kiisoo.ic.generalize.entity.CompanyStoreDO">
        select t6.name as companyName,t5.name as shopName,t5.code as shopCode,t1.id as sellerId
        from poi_store_staff t1,
             privilage_organizational t2,
             privilage_organizational t6,
             privilage_organizational_domain t3,
             privilage_domain_entity t4,
             poi_store t5
        where t2.id = t3.org_id
          and t2.parent_id = t6.id
          and t1.store_id = t5.id
          and t3.domain_id = t4.domain_id
          and t4.type = 3
          and t1.store_id = t4.entity_id
    </select>
    <select id="selectProvinceName" resultType="java.lang.String">
        select province_cn from t_area_conf where area_id = #{cityId}
        limit 1
    </select>
    <select id="selectShopByCustomerIdAndStore" resultType="com.kiisoo.ic.store.entity.PoiStore">
        select t3.id,t3.name,t3.code
        from privilage_organizational_domain t1 join privilage_domain_entity t2 on t1.domain_id = t2.domain_id and
        t2.type = 3

        join poi_store t3 on t2.entity_id = t3.id

        <where>
            <if test="null != storeId and storeId != 0">
                and t2.entity_id = #{storeId}
            </if>
            <if test="null != code and code != ''">
                and (t3.name LIKE CONCAT('%',#{code},'%') OR t3.code LIKE
                CONCAT('%',#{code},'%'))
            </if>
            <if test="customerIds != null and customerIds.size > 0">
                and t1.org_id in
                <foreach collection="customerIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

        </where>


        <!--        <choose>-->
        <!--            <when test="customerIds != null and customerIds.size > 0">-->
        <!--                and t1.org_id in-->
        <!--                <foreach collection=         <foreaindex="index" item     <foreaindex="index" ite," close=")">-->
        <!--                    #{item}-->
        <!--                </foreach>-->
        <!--            </when>-->
        <!--            <otherwise>-->
        <!--                and 0 = 0-->
        <!--            </otherwise>-->
        <!--        </choose>-->
        <!--        <if test="customerIds != null and customerIds.size > 0">-->
        <!--            and t1.org_id in-->
        <!--            <foreach collection="customerIds"  index="index"="customerIds"  index="index"="customerIds"  index separator="," close=")">-->
        <!--                #{reach>-->
        <!--        </if>-->

    </select>

    <select id="selectShopIdByCustomerId" resultType="java.lang.Long">

        select t5.id from privilage_organizational t,privilage_organizational_domain t2,privilage_domain t3,privilage_domain_entity t4,poi_store t5
        where t.parent_id  = #{orgId}
          and t.id = t2.org_id
          and t2.domain_id = t3.id
          and t3.id = t4.domain_id
          and t4.entity_id = t5.id
          and t4.type = 3 ;
    </select>

    <select id="findStoreCompany" resultType="com.kiisoo.ic.generalize.entity.PrivilageOrganizational">
        SELECT
            t.*
        FROM
            privilage_organizational t
        WHERE
            t.id = (
            SELECT
                t1.parent_id
            FROM
                privilage_organizational t1
            WHERE
                id = (
                SELECT
                    t1.org_id
                FROM
                    privilage_organizational_domain t1
                WHERE
                    t1.domain_id = (
                    SELECT
                        t2.domain_id
                    FROM
                        privilage_domain_entity t2
                    WHERE
                        t2.entity_id = #{storeId}
                    )
                )
            )
            AND t.LEVEL = 2
            LIMIT 0,
            1;
    </select>


    <select id="organizationalList" resultType="com.kiisoo.ic.store.entity.PoiStore">
        SELECT
        d.*
        FROM
        privilage_organizational a
        INNER JOIN privilage_organizational_domain b ON a.id = b.org_id
        INNER JOIN privilage_domain_entity c ON c.domain_id = b.domain_id
        INNER JOIN poi_store d ON c.entity_id = d.id
        WHERE 1=1
        <if test="organizationalIdList!=null and organizationalIdList!=''">
            AND a.id IN
            <foreach item="item" collection="organizationalIdList.split(',')" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

</mapper>
