<?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.activity.mapper.PmnActivityInstanceDOMapper">
    <resultMap type="com.kiisoo.ic.activity.bean.ActivityInstanceVO"
               id="activityInstanceVOMap">
        <result column="schedule_id" property="scheduleId"/>
        <result column="name" property="name"/>
        <result column="begin_time" property="beginTime"/>
        <result column="end_time" property="endTime"/>
        <result column="status" property="status"/>
        <result column="id" property="id"/>
        <result column="store_name" property="storeName"/>
        <result column="store_count" property="storeCount"/>
        <result column="company_name" property="companyName"/>
        <result column="company_count" property="companyCount"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <collection property="params" javaType="ArrayList"
                    column="id" ofType="com.kiisoo.ic.activity.entity.PmnActivityPromotionParam"
                    select="selectParamsByInstanceId">
        </collection>
    </resultMap>
    <resultMap type="com.kiisoo.ic.activity.bean.ActivityInstanceVO"
               id="activityInstanceVOMapStores">
        <result column="schedule_id" property="scheduleId"/>
        <result column="name" property="name"/>
        <result column="begin_time" property="beginTime"/>
        <result column="end_time" property="endTime"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="id" property="id"/>
        <collection property="params" javaType="ArrayList"
                    column="id" ofType="com.kiisoo.ic.activity.entity.PmnActivityPromotionParam"
                    select="selectParamsByInstanceId">
        </collection>
        <collection property="stores" javaType="ArrayList"
                    column="id" ofType="com.kiisoo.ic.store.entity.PoiStore"
                    select="selectStoresByInstanceId">
        </collection>
        <collection property="organizations" javaType="ArrayList"
                    column="id" ofType="com.kiisoo.ic.generalize.entity.PrivilageOrganizational"
                    select="selectOrgByInstanceId">
        </collection>
    </resultMap>
    <select id="selectParamsByInstanceId" parameterType="int"
            resultType="com.kiisoo.ic.activity.entity.PmnActivityPromotionParam">
select * from pmn_activity_promotion_param where id in(select param_id from pmn_activity_instance_config where instance_id=#{instanceId})
    </select>

    <select id="selectOrgByInstanceId" parameterType="int"
            resultType="com.kiisoo.ic.generalize.entity.PrivilageOrganizational">
        select * from privilage_organizational
         where
         id in(select org_id from pmn_activity_store_relation where instance_id=#{instanceId})
    </select>

    <select id="selectStoresByInstanceId" parameterType="int"
            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
         t.id in(select store_id from pmn_activity_store_relation where instance_id=#{instanceId})
    </select>

    <select id="pageVO" resultMap="activityInstanceVOMap">
        select t.*,t1.store_count,t2.abbreviation as store_name,t3.company_count,t4.name as company_name
        from pmn_activity_instance t
        left join (select instance_id, count(1) as store_count,store_id,org_id from pmn_activity_store_relation group by
        instance_id) t1 on t.id=t1.instance_id
        left join poi_store t2 on t2.id=t1.store_id
        left join (select instance_id, count(DISTINCT org_id) as company_count,store_id from pmn_activity_store_relation
        group by instance_id) t3 on t.id=t3.instance_id
        left join privilage_organizational t4 on t4.id=t1.org_id
        where 1 =1
        <if test="storeId != null">
            and t.id in (select instance_id from pmn_activity_store_relation where
            store_id=#{storeId}
            )
        </if>
        <if test="storeIds != null and storeIds != '' and storeId == null">
            and t.id in (select instance_id from pmn_activity_store_relation where
            store_id in
            <foreach collection="storeIds" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
            )
        </if>
        <if test="organizationId != null and organizationId != ''">
            and t.id in (select instance_id from pmn_activity_store_relation where
            org_id = #{organizationId})
        </if>
        <if test="status != null">
            and t.status = #{status}
        </if>
        <if test="categoryCode != null">
            and t.category_code = #{categoryCode}
        </if>
        <if test="startDate != null and endDate != null">
            and ((begin_time >= #{startDate} AND begin_time <![CDATA[ <= ]]> #{endDate})
            OR (begin_time <![CDATA[ <= ]]> #{startDate} AND end_time >= #{endDate})
            OR (end_time >= #{startDate} AND end_time <![CDATA[ <= ]]> #{endDate}))
        </if>
        and t.status &lt;&gt; 4
        order by create_time desc
    </select>

    <select id="getVO" resultMap="activityInstanceVOMapStores">
        select t.* from pmn_activity_instance t where id = #{instanceId} limit 0,1
    </select>
</mapper>
