<?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.seckill.comment.dao.SeckillMapper">

    <resultMap id="seckillMap" type="com.yami.shop.seckill.comment.model.Seckill">
        <id column="seckill_id" property="seckillId"/>
        <result column="seckill_name" property="seckillName"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="seckill_tag" property="seckillTag"/>
        <result column="max_num" property="maxNum"/>
        <result column="max_cancel_time" property="maxCancelTime"/>
        <result column="shop_id" property="shopId"/>
        <result column="is_delete" property="isDelete"/>
        <result column="status" property="status"/>
        <result column="seckill_total_stocks" property="seckillTotalStocks"/>
        <result column="seckill_origin_stocks" property="seckillOriginStocks"/>
        <result column="seckill_price" property="seckillPrice"/>
    </resultMap>
    <select id="pageSeckillProd" resultType="com.yami.shop.seckill.comment.dto.SeckillProductSimpleDto">
        select s.seckill_id,s.seckill_name,s.start_time,s.end_time,s.seckill_total_stocks,
        s.seckill_origin_stocks,s.seckill_price,p.prod_id,p.prod_name,p.pic,p.price
        from tz_seckill s
        join tz_prod p on s.seckill_id = p.activity_id and p.status = 1 AND p.prod_type = 2
        where s.status = 1 and s.is_delete = 0 AND s.`end_time` &gt;= NOW()
        order by s.start_time desc
    </select>
    <select id="getPlatformSeckillPage" resultType="com.yami.shop.seckill.comment.model.Seckill">
        SELECT s.*,sd.`shop_name` FROM tz_seckill s
        LEFT JOIN tz_shop_detail sd ON s.`shop_id` = sd.`shop_id`
        WHERE s.is_delete = 0
        <if test="seckill.shopName != null">
            AND sd.shop_name like concat("%",#{seckill.shopName},"%")
        </if>
        <if test="seckill.seckillName != null">
            AND s.seckill_name like concat("%",#{seckill.seckillName},"%")
        </if>
        <if test="seckill.status != null">
            AND s.status  = #{seckill.status}
        </if>
        ORDER BY s.`seckill_id` DESC
    </select>
    <select id="pageSeckillProdNew" resultType="com.yami.shop.seckill.comment.dto.SeckillProductSimpleDto">
        select s.seckill_id,s.seckill_name,s.start_time,s.end_time,s.seckill_total_stocks,
               s.seckill_origin_stocks,s.seckill_price,p.prod_id,p.prod_name,p.imgs pic,p.price
        from tz_seckill s
             join tz_prod p on s.seckill_id = p.activity_id and p.status = 1 AND p.prod_type = 2
        left join tz_shop_detail sd on p.shop_id = sd.shop_id
        where s.status = 1 and s.is_delete = 0 AND s.`end_time` &gt;= NOW() and p.shop_id in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        order by s.start_time desc
    </select>
    <update id="updateStatus">
        UPDATE tz_seckill s SET s.`status` =#{status} WHERE s.`seckill_id` = #{seckillId}
    </update>
    <update id="returnStocksByOrderNumber">
        UPDATE tz_seckill s JOIN tz_seckill_order so ON s.seckill_id = so.seckill_id AND so.order_number = #{orderNumber}
        SET s.seckill_total_stocks = s.seckill_total_stocks + so.prod_count
    </update>
    <update id="returnStocks">
        UPDATE tz_seckill s JOIN tz_seckill_order so ON s.seckill_id = so.seckill_id AND (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(so.`create_time`))/60 &gt; s.`max_cancel_time` and so.state = 0
        SET s.seckill_total_stocks = s.seckill_total_stocks + so.prod_count
    </update>
    <update id="updateStocksById">
        UPDATE tz_seckill set seckill_total_stocks = seckill_total_stocks - #{prodCount} where seckill_id = #{seckillId} and seckill_total_stocks >= #{prodCount}
    </update>
    <update id="changeProdTypeBySeckillIdList">
      UPDATE tz_prod SET prod_type = 0 WHERE prod_id IN
      (SELECT prod_id FROM tz_seckill WHERE seckill_id IN
        <foreach collection="seckillIds" item="seckillId" separator="," open="(" close=")">
            #{seckillId}
        </foreach>
      GROUP BY prod_id
      )
    </update>
    <update id="changeSeckillStatusBySeckillIdList">
        UPDATE tz_seckill SET `status` = 0 WHERE seckill_id IN
         <foreach collection="seckillIds" item="seckillId" separator="," open="(" close=")">
             #{seckillId}
         </foreach>
    </update>
</mapper>
