<?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.tmerclub.cloud.seckill.mapper.SeckillSkuMapper">
    <resultMap id="seckillSkuMap" type="com.tmerclub.cloud.seckill.model.SeckillSku">
        <id column="seckill_sku_id" property="seckillSkuId"/>
        <result column="sku_id" property="skuId"/>
        <result column="seckill_id" property="seckillId"/>
        <result column="seckill_stocks" property="seckillStocks"/>
        <result column="seckill_origin_stocks" property="seckillOriginStocks"/>
        <result column="seckill_price" property="seckillPrice"/>
    </resultMap>

    <resultMap id="seckillSkuPointMap" extends="seckillSkuMap" type="com.tmerclub.cloud.seckill.vo.SeckillSkuVO">
        <collection property="seckillStockPointList" ofType="com.tmerclub.cloud.seckill.vo.SeckillStockPointSkuVO">
            <id column="seckill_stock_point_sku_id" property="seckillStockPointSkuId"/>
            <id column="stock_point_id" property="stockPointId"/>
            <id column="stock_point_type" property="stockPointType"/>
            <id column="stock" property="stock"/>
            <id column="type" property="type"/>
        </collection>
    </resultMap>
    <sql id="Vo_Column_List">
        `seckill_sku_id`,
        `sku_id`,
        `seckill_id`,
        `seckill_stocks`,
        `seckill_origin_stocks`,
        `seckill_price`
    </sql>
    <select id="getBySeckillSkuId" resultType="com.tmerclub.cloud.seckill.vo.SeckillSkuVO">
        select
        `seckill_sku_id`,
        `sku_id`,
        `seckill_id`,
        `seckill_stocks`,
        `seckill_price`,
        `seckill_origin_stocks`
        from seckill_sku
        where seckill_sku_id = #{seckillSkuId}
    </select>

    <select id="selectListBySeckillId" resultMap="seckillSkuPointMap">
        select
        sku.`seckill_sku_id`,
        sku.`sku_id`,
        sku.`seckill_id`,
        sku.`seckill_stocks`,
        sku.`seckill_origin_stocks`,
        sku.`seckill_price`,
        psku.seckill_stock_point_sku_id,
        psku.stock_point_id,
        psku.stock_point_type,
        psku.type,
        psku.seckill_origin_stocks stock
        from seckill_sku sku left join seckill_stock_point_sku psku on sku.seckill_sku_id = psku.seckill_sku_id
        where sku.seckill_id = #{seckillId}
    </select>

    <insert id="saveBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="seckillSkuId">
        insert into seckill_sku (`sku_id`, `seckill_id`, `seckill_stocks`,`seckill_origin_stocks`, `seckill_price`) values
        <foreach collection="seckillSkuList" item="seckillSku" separator=",">
            (#{seckillSku.skuId}, #{seckillSku.seckillId}, #{seckillSku.seckillStocks},#{seckillSku.seckillStocks}, #{seckillSku.seckillPrice})
        </foreach>
    </insert>

    <update id="updateStocks">
        update seckill_sku
        set seckill_stocks = seckill_stocks - #{count}
        where seckill_sku_id = #{seckillSkuId}
          and seckill_stocks &gt;= #{count}
    </update>

    <update id="returnStocksByOrderId">
        UPDATE seckill_sku
        SET seckill_stocks = seckill_stocks + #{count}
        WHERE seckill_sku_id = #{seckillSkuId}
    </update>

    <select id="getBySkuIdAndSeckillId" resultType="com.tmerclub.cloud.seckill.vo.SeckillSkuVO">
        select
        <include refid="Vo_Column_List"/>
        from seckill_sku
        where seckill_id = #{seckillId} and sku_id = #{skuId}
    </select>

    <select id="listMaybeNeedStockPlacementActivity" resultType="com.tmerclub.cloud.seckill.vo.SeckillSkuVO">
        SELECT sku.seckill_id, sku.seckill_sku_id,sku.seckill_id, sku.sku_id
        FROM seckill
        JOIN `seckill_sku` sku
             ON sku.seckill_id = seckill.`seckill_id`
        WHERE seckill.start_time &gt; DATE_SUB(NOW(), INTERVAL 2 HOUR)
          AND seckill.start_time &lt; NOW()
          AND seckill.seckill_total_stocks &gt; 0
          AND seckill.update_time &lt; DATE_SUB(NOW(), INTERVAL 10 SECOND)
          AND seckill.status = 1
    </select>

    <select id="listOverJustNow" resultType="com.tmerclub.cloud.seckill.vo.SeckillSkuVO">
        SELECT sku.seckill_id, sku.seckill_sku_id,sku.seckill_id, sku.sku_id
        FROM seckill
                 JOIN `seckill_sku` sku
                      ON sku.seckill_id = seckill.`seckill_id`
        WHERE seckill.end_time &gt; DATE_SUB(NOW(), INTERVAL 1 HOUR)
          AND seckill.end_time &lt; NOW()
          AND seckill.seckill_total_stocks &gt; 0
          AND seckill.update_time &lt; DATE_SUB(NOW(), INTERVAL 10 SECOND)
    </select>
</mapper>
