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

    <select id="getGroupProdPage" resultType="com.yami.shop.groupbuy.comment.dto.GroupProdDto">
        SELECT
        gp.group_prod_id,
        gp.shop_id,
        gp.group_activity_id,
        gp.group_prod_id,
        gp.prod_id,
        gp.status AS groupProdStatus,
        gp.create_time,
        p.prod_name,
        p.pic,
        ga.start_time,
        ga.status AS groupActivityStatus,
        ga.end_time
        FROM tz_group_prod gp
        LEFT JOIN tz_prod p ON p.`prod_id` = gp.`prod_id`
        LEFT JOIN tz_group_activity ga ON ga.group_activity_id = gp.group_activity_id
        WHERE gp.group_activity_id = #{groupProd.groupActivityId}
        AND gp.status != -1
        <if test="groupProd.shopId != null">
            AND gp.shop_id = #{groupProd.shopId}
        </if>

    </select>
    <resultMap id="groupProdMap" type="com.yami.shop.groupbuy.comment.model.GroupProd">
        <id column="group_prod_id" property="groupProdId"/>
        <result column="shop_id" property="shopId"/>
        <result column="group_activity_id" property="groupActivityId"/>
        <result column="prod_id" property="prodId"/>
        <result column="create_time" property="createTime"/>
        <result column="group_order_count" property="groupOrderCount"/>
        <result column="group_number_count" property="groupNumberCount"/>
        <result column="status" property="status"/>
    </resultMap>
    <insert id="saveBatchGroupProd" useGeneratedKeys="true" keyColumn="group_prod_id" keyProperty="groupProdId">
        INSERT INTO tz_group_prod
        (shop_id,group_activity_id,prod_id,create_time,group_order_count,group_number_count,status)
        VALUES
        <foreach collection="groupProdList" separator="," item="groupProd">
            (#{groupProd.shopId},#{groupProd.groupActivityId},#{groupProd.prodId},#{groupProd.createTime},#{groupProd.groupOrderCount},#{groupProd.groupNumberCount},#{groupProd.status})
        </foreach>
    </insert>

    <resultMap id="groupProdAndSkuDtoMap" type="com.yami.shop.groupbuy.comment.dto.GroupProdAndSkuListDto">
        <result column="group_prod_id" property="groupProdId"/>
        <result column="prod_id" property="prodId"/>
        <result column="prod_name" property="prodName"/>
        <result column="status" property="status"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="has_leader_price" property="hasLeaderPrice"/>
        <collection property="skuDtoList" javaType="list" ofType="com.yami.shop.groupbuy.comment.model.GroupSku">
            <result column="group_sku_id" property="groupSkuId"/>
            <result column="group_prod_id" property="groupProdId"/>
            <result column="sku_id" property="skuId"/>
            <result column="act_price" property="actPrice"/>
            <result column="leader_price" property="leaderPrice"/>
            <result column="sell_num" property="sellNum"/>
            <result column="sku_name" property="skuName"/>
            <result column="price" property="price"/>
            <result column="stocks" property="stocks"/>
        </collection>
    </resultMap>
    <select id="getGroupProdAndSkuListById" resultMap="groupProdAndSkuDtoMap">
       SELECT
          gp.`prod_id`,
          gp.`group_prod_id`,
          gs.sku_id,
          gs.leader_price,
          gs.act_price,
          ga.has_leader_price,
          gs.group_sku_id,
          p.prod_name,
          s.price,
          s.sku_name,
          s.stocks,
          ga.status,
          ga.start_time,
          ga.end_time
        FROM tz_group_prod gp
        LEFT JOIN tz_group_sku gs ON gs.`group_prod_id` = gp.`group_prod_id`
        LEFT JOIN tz_prod p ON p.prod_id = gp.prod_id
        LEFT JOIN tz_group_activity ga on ga.group_activity_id = gp.group_activity_id
        LEFT JOIN tz_sku s ON s.sku_id = gs.sku_id
        WHERE gp.`group_prod_id` = #{groupProdId}
    </select>

    <update id="updateStatusToDelById">
        UPDATE tz_group_prod SET `status` = -1 WHERE group_prod_id = #{groupProdId}
    </update>

    <sql id="apiGroupProdSql">
        SELECT
            ga.group_number AS group_number,
            ga.start_time AS start_time,
            ga.end_time AS end_time,
            gp.create_time AS create_time,
            gp.group_activity_id AS group_activity_id,
            gp.prod_id AS prod_id,
            tp.prod_name AS prod_name,
            tp.pic AS prod_pic,
            tp.price AS price,
            (
                SELECT MIN(gs.act_price) FROM tz_group_sku gs WHERE gp.group_prod_id = gs.group_prod_id
            ) AS act_price
        FROM tz_group_prod gp
        LEFT JOIN tz_group_activity ga ON gp.group_activity_id = ga.group_activity_id
        LEFT JOIN tz_prod tp ON gp.prod_id = tp.prod_id
    </sql>

    <select id="getApiGroupProdDto" resultType="com.yami.shop.groupbuy.comment.api.dto.ApiGroupProdDto">
        <include refid="apiGroupProdSql"></include>
        WHERE gp.group_prod_id = #{groupProdId} AND gp.`status` = 1 and tp.status = 1
    </select>

    <select id="getApiGroupProdPage" resultType="com.yami.shop.groupbuy.comment.api.dto.ApiGroupProdDto">
        <include refid="apiGroupProdSql"></include>
        WHERE ga.start_time &lt;= #{nowTime} AND ga.end_time &gt;= #{nowTime} AND gp.`status` = 1 AND ga.status = 1  and tp.status = 1
        ORDER BY ga.start_time DESC, gp.create_time DESC
    </select>

    <select id="getGroupProdByGroupSkuId" resultType="com.yami.shop.groupbuy.comment.model.GroupProd">
       SELECT gp.* FROM tz_group_prod gp
       LEFT JOIN tz_group_sku gs ON gs.`group_prod_id` = gp.`group_prod_id`
       WHERE gs.`group_sku_id` = #{groupSkuId}
    </select>
    <update id="invalidGroupProdByActivityId">
        UPDATE tz_group_prod SET `status` = -1 WHERE group_activity_id IN
        <foreach collection="groupActivityIds" item="groupActivityId" open="(" close=")" separator=",">
            #{groupActivityId}
        </foreach>
    </update>
</mapper>
