<?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.generalize.mapper.RetailCompanyMapper">
    <insert id="insertDomainOrgRelation">
        insert into privilage_organizational_domain (org_id, domain_id) values (#{companyId},#{domainId})
    </insert>

    <select id="selectShopIdByRetail" resultType="com.kiisoo.ic.generalize.entity.CompanyStoreDO">
        select distinct t3.entity_id as shopId,t1.id as orgId,t1.parent_id as parentId
        from privilage_organizational t,
        privilage_organizational t1,
        privilage_organizational_domain t2,
        privilage_domain_entity t3
        where t1.id = t2.org_id
        and t.id = t1.parent_id
        and t2.domain_id = t3.domain_id
        and t3.type = 3
        <if test="orgId != null">
            and t.id = #{orgId}
        </if>
        <if test="shopIds != null">
            and t3.entity_id in
            <foreach collection="shopIds" separator="," item="item" close=")" index="index" open="(">
                 #{item}
            </foreach>
        </if>
    </select>
    <select id="selectSellerIdsByShop" resultType="java.lang.Long">
        select t4.id
        from privilage_organizational t,
        privilage_organizational t1,
        privilage_organizational_domain t2,
        privilage_domain_entity t3,
        poi_store_staff t4
        where t1.id = t2.org_id
        and t.id = t1.parent_id
        and t2.domain_id = t3.domain_id
        and t3.entity_id = t4.store_id
        and t3.type = 3
        and t4.status = 1
        <if test="orgId != null">
            and t.id = #{orgId}
        </if>
        <if test="shopIds != null">
            and t4.store_id in
            <foreach collection="shopIds" open="(" index="index" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="sellerId != null">
            and t4.user_id = #{sellerId}
        </if>
    </select>
    <select id="selectChildList" resultType="Long">
        select id from privilage_organizational where
        <choose>
            <when test="companys != null and companys.size > 0">
                parent_id in
                <foreach collection="companys" item="item" index="index" close=")" open="(" separator=",">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                1 = 0
            </otherwise>
        </choose>

    </select>

    <!--所以组织对应关系list-->
    <select id="selectAllOrgListByOrgIds" resultType="com.kiisoo.ic.generalize.entity.PrivilageOrganizationalRelationVO">
        SELECT region.id as regionId,region.name as region,company.id as companyId,company.name as company,
        customer.id as customerId,customer.name as customer
        FROM `privilage_organizational` region,`privilage_organizational` company,`privilage_organizational` customer
        where company.parent_id=region.id
        and customer.parent_id = company.id
        and region.`level`=1
        and company.`level`=2
        and customer.`level`=3
        and region.type=2
        and company.type=2
        and customer.type=2
        and region.`status`=1
        and company.`status`=1
        and customer.`status`=1
        and (region.id in
        <foreach collection="orgIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
         or company.id in
        <foreach collection="orgIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        or customer.id in
        <foreach collection="orgIds" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        )
        <if test="regionId != null and regionId != 0">
            and region.id = #{regionId}
        </if>
        <if test="companyId != null and companyId != 0">
            and company.id = #{companyId}
        </if>
        <if test="customerId != null and customerId != 0">
            and customer.id = #{customerId}
        </if>
    </select>

    <select id="selectCustomerDOByStoreCode" resultType="Map">
        select t.id,t.name,t.`code` from privilage_organizational t,privilage_organizational_domain t2,privilage_domain_entity t3,poi_store t4
        where t.id = t2.org_id and t3.domain_id = t2.domain_id and t3.entity_id = t4.id and t4.`code` = #{storeCode}
    </select>

</mapper>
