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

    <resultMap id="groupSkuMap" type="com.yami.shop.groupbuy.comment.model.GroupSku">
        <id 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"/>
    </resultMap>

    <insert id="saveBatchGroupSku">
        INSERT INTO tz_group_sku (group_prod_id,sku_id,act_price,leader_price,sell_num) VALUES
        <foreach collection="groupSkuList" separator="," item="groupSku">
            (#{groupSku.groupProdId},#{groupSku.skuId},#{groupSku.actPrice},#{groupSku.leaderPrice},#{groupSku.sellNum})
        </foreach>
    </insert>

    <select id="getApiByGroupActivityIdAndProdId" resultType="com.yami.shop.groupbuy.comment.api.dto.ApiGroupSkuDto">
        SELECT
            gs.*,
            s.price AS price,
            s.sku_name AS sku_name,
            s.pic AS pic,
            s.properties AS properties
        FROM tz_group_sku gs
        LEFT JOIN tz_sku s ON gs.sku_id = s.sku_id
        LEFT JOIN tz_group_prod gp ON gs.group_prod_id = gp.group_prod_id AND gp.`status` = 1
        WHERE gp.group_activity_id = #{groupActivityId} AND gp.prod_id = #{prodId}
    </select>

    <select id="getSkuInfoByGroupSkuId" resultType="com.yami.shop.groupbuy.comment.api.dto.ApiGroupSkuInfoDto">
        SELECT
            gs.`group_prod_id`,
            gs.`sku_id`,
            gs.`group_sku_id`,
            gs.`sku_id`,
            gs.`act_price`,
            gs.`leader_price`,
            s.`price`,
            s.`sku_name`,
            s.`stocks`,
            s.`pic` AS sku_pic,
            p.`prod_name`,
            p.`status` as prod_status,
            p.pic AS prod_pic,
            sd.`shop_id`,
            sd.`shop_name`
        FROM tz_group_sku gs
        LEFT JOIN tz_sku s ON s.`sku_id` = gs.`sku_id`
        LEFT JOIN tz_prod p ON s.`prod_id` = p.`prod_id`
        LEFT JOIN tz_shop_detail sd ON sd.`shop_id` = p.`shop_id`
        WHERE gs.`group_sku_id` = #{groupSkuId}  AND s.`status` = 1 AND p.`status` = 1 AND s.`stocks`> 0
    </select>
</mapper>
