<?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.sqx.modules.activity.dao.ActivityShopDao">

    <select id="pageShopByActivityId" resultType="com.sqx.modules.activity.vo.ActivityShopVO">
        select
            ach.id as activityShopId,
            ach.shop_id,
            ach.activity_id,
            gs.shop_name,
            gs.phone as shopPhone,
            st.shop_type_name,
            gs.shop_cover as shopImg,
            gs.shop_banner as shopSwiperImg
        from activity_shop ach
        left join goods_shop gs on gs.shop_id = ach.shop_id
        left join shop_type st on st.id = gs.shop_type_id
        where ach.activity_id = ${activityId}
    </select>

    <resultMap id="shopActivityGroupVO" type="com.sqx.modules.activity.vo.ShopActivityGroupVO">
        <result column="shop_id" property="shopId"/>
        <collection property="activityTypeGroups" ofType="com.sqx.modules.activity.vo.ShopActivityGroupVO$ActivityTypeGroup">
            <result column="type" property="activityType"/>
            <collection property="activityInGroups" ofType="com.sqx.modules.activity.vo.ShopActivityGroupVO$ActivityInGroup">
                <result column="title" property="activityTitle"/>
                <result column="end_time" property="activityEndTime"/>
                <result column="suit_type" property="suitType"/>
                <result column="limit_type" property="limitType"/>
                <result column="limit_value" property="limitValue"/>
            </collection>
        </collection>
    </resultMap>

    <select id="getShopActivityGroup" resultMap="shopActivityGroupVO">
        select
            ach.shop_id,
            a.type,
            a.title,
            a.end_time,
            ach.suit_type,
            ach.limit_type,
            ach.limit_value
        from activity_shop ach
        left join activity a on a.id = ach.activity_id
        where
              a.del_flag = '0'
              and a.enable_flag = '1'
              and now() >= a.start_time
              and now() <![CDATA[<=]]> a.end_time
              and ach.shop_id = #{shopId}
    </select>

    <select id="getActivityByShopIds" resultType="com.sqx.modules.activity.vo.ShopActivityVO">
        select
            ach.id activityShopId,
            ach.shop_id,
            ach.activity_id,
            a.type activityType,
            a.title activityName,
            a.image activityImage,
            a.start_time,
            a.end_time
        from activity_shop ach
        left join activity a on a.id = ach.activity_id
        where
              a.del_flag = '0'
              and a.enable_flag = '1'
              and ach.shop_id in
                <foreach collection="shopIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
    </select>

    <select id="getShopActivityBOByShopId" resultType="com.sqx.modules.activity.bo.ShopActivityBO">
        select
            a.id activityId,
            ach.id activityShopId,
            ach.shop_id,
            ach.suit_type,
            ach.limit_type,
            ach.limit_value,
            a.title,
            a.content,
            a.type,
            a.start_time,
            a.end_time,
            a.config
        from activity_shop ach
            left join activity a on ach.activity_id = a.id
       where ach.shop_id = #{shopId}
         and a.enable_flag = '1'
         and a.del_flag = '0'
    </select>
</mapper>