<?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.yami.shop.groupbuy.comment.dao.GroupActivityMapper">

    <resultMap id="groupActivityMap" type="com.yami.shop.groupbuy.comment.model.GroupActivity">
        <id column="group_activity_id" property="groupActivityId"/>
        <result column="shop_id" property="shopId"/>
        <result column="activity_name" property="activityName"/>
        <result column="status" property="status"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="group_valid_time" property="groupValidTime"/>
        <result column="group_number" property="groupNumber"/>
        <result column="has_max_num" property="hasMaxNum"/>
        <result column="max_num" property="maxNum"/>
        <result column="has_robot" property="hasRobot"/>
        <result column="has_leader_price" property="hasLeaderPrice"/>
        <result column="is_preheat" property="isPreheat"/>
        <result column="has_group_tip" property="hasGroupTip"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>
    <update id="updateToDelete">
        UPDATE tz_group_activity SET status = -1 WHERE group_activity_id = #{groupActivityId}
    </update>

    <select id="getGroupActivityPage" resultType="com.yami.shop.groupbuy.comment.dto.GroupActivityDto">
        SELECT
            ga.*,
            (
                SELECT sum(gp.group_order_count) FROM tz_group_prod gp
                WHERE ga.group_activity_id = gp.group_activity_id
            ) AS group_order_count,
            sd.shop_name
        FROM tz_group_activity ga
        LEFT JOIN tz_shop_detail sd ON ga.shop_id = sd.shop_id
        <where>
            ga.`status` != -1
            <if test="groupActivityDto.activityName != null">
               AND ga.activity_name LIKE concat('%',#{groupActivityDto.activityName},'%')
            </if>
            <if test="groupActivityDto.shopName != null">
               AND sd.shop_name LIKE concat('%',#{groupActivityDto.shopName},'%')
            </if>
            <if test="groupActivityDto.shopId != null">
               AND ga.shop_id = #{groupActivityDto.shopId}
            </if>
        </where>
        ORDER BY ga.create_time DESC
    </select>

    <select id="getApiGroupActivityInfo" resultType="com.yami.shop.groupbuy.comment.api.dto.ApiGroupActivityDto">
        SELECT
          ga.*
        FROM tz_group_activity ga
        LEFT JOIN tz_group_prod gp ON ga.group_activity_id = gp.group_activity_id AND gp.`status` = 1
        WHERE ga.group_activity_id = #{groupActivityId} AND gp.prod_id = #{prodId}
    </select>

    <select id="getActivityFinishProduct" resultType="com.yami.shop.bean.model.Product">
        SELECT p.* FROM tz_group_activity ga
        INNER JOIN tz_prod p ON ga.group_activity_id = p.activity_id
        WHERE ga.`status` = 1 AND ga.end_time <![CDATA[ < ]]> #{newDate}
    </select>

    <update id="updateStatus">
        UPDATE tz_group_activity ga SET ga.`status` =#{status}  WHERE ga.`group_activity_id` = #{activityId}
    </update>
    <update id="changeProdTypeByGroupActivityIdList">
        UPDATE tz_prod SET prod_type = 0,activity_id = 0 WHERE prod_id IN(
        SELECT  prod_id FROM tz_group_prod  WHERE group_activity_id IN
        <foreach collection="groupActivityIds" item="groupActivityId" separator="," open="(" close=")">
            #{groupActivityId}
        </foreach>
        GROUP BY prod_id
        )
    </update>
    <update id="changeGroupActivityStatusByGroupActivityIdList">
        UPDATE tz_group_activity SET `status` = 5 WHERE group_activity_id IN
        <foreach collection="groupActivityIds" item="groupActivityId" separator="," open="(" close=")">
            #{groupActivityId}
        </foreach>
    </update>
</mapper>
