<?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.ruoyi.mall.mapper.MallSeckillGoodsMapper">

    <resultMap type="MallSeckillGoods" id="MallSeckillGoodsResult">
        <result property="id"    column="id"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="price"    column="price"    />
        <result property="stock"    column="stock"    />
        <result property="timeId"    column="time_id"    />
        <result property="endTime"    column="end_time"    />
        <result property="saleNum"    column="sale_num"    />
        <result property="status"    column="status"    />
        <result property="createAt"    column="create_at"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="purchaseLimits"    column="purchase_limits"    />
    </resultMap>

    <sql id="selectMallSeckillGoodsVo">
        select id, goods_id, price, stock, time_id, end_time, sale_num, purchase_limits, status, create_at, del_flag from mall_seckill_goods
    </sql>

    <select id="selectMallSeckillGoodsList" parameterType="MallSeckillGoods" resultType="MallSeckillGoods">
        select msg.id id, msg.goods_id goodsId, msg.price price, msg.stock stock, msg.time_id timeId, msg.end_time endTime, msg.sale_num saleNum, msg.purchase_limits purchaseLimits,
               msg.status status, msg.create_at createAt, msg.del_flag delFlag, mgg.goods_name goodsName, mgg.goods_title goodsTitle, mgg.goods_img goodsImg,
               mgc.name className, CONCAT(mst.time_hour,'-',mst.time_end) seckillTime
        from mall_seckill_goods msg
            LEFT JOIN mall_goods_goods mgg
                ON msg.goods_id = mgg.id
            LEFT JOIN mall_goods_class mgc
                ON mgg.class_id = mgc.id
            LEFT JOIN mall_seckill_time mst
                ON mst.id = msg.time_id
        <where>
            <if test="id != null "> and msg.id = #{id}</if>
            <if test="goodsId != null "> and msg.goods_id = #{goodsId}</if>
            <if test="price != null "> and msg.price = #{price}</if>
            <if test="stock != null "> and msg.stock = #{stock}</if>
            <if test="timeId != null "> and msg.time_id = #{timeId}</if>
            <if test="endTime != null  and endTime != ''"> and msg.end_time = #{endTime}</if>
            <if test="saleNum != null "> and msg.sale_num = #{saleNum}</if>
            <if test="status != null "> and msg.status = #{status}</if>
            <if test="createAt != null  and createAt != ''"> and msg.create_at = #{createAt}</if>
            and msg.del_flag = '0'
        </where>
        order by msg.create_at desc
    </select>

    <update id="editSeckillGoodsInfo" >
        update mall_seckill_goods set
        <if test="type == 0">stock = stock-#{count}</if>
        <if test="type == 1">sale_num = sale_num+#{count}</if>
        where goods_id = #{goodsId}
    </update>

    <select id="seckillGoodsInfo" parameterType="map" resultType="SeckillGoodsVO">
        SELECT
            msg.price seckillPrice,
            msg.purchase_limits purchaseLimits,
            msg.id seckillGoodsId,
            mst.`time_hour` timeStart,
            mst.`time_end` timeEnd
        FROM
            mall_seckill_goods msg
                LEFT JOIN mall_seckill_time mst
                          ON msg.`time_id` = mst.`id`
        <where>
            <if test="goodsId != null">and msg.`goods_id` = #{goodsId}</if>
            <if test="seckillGoodsId != null">and msg.`id` = #{seckillGoodsId}</if>
        </where>
    </select>

    <select id="enableSeckillGoodsList" resultType="map">
        SELECT
            mgg.id goodsId,
            mgg.goods_title goodsTitle,
            mgc.name className
        FROM
            mall_goods_goods mgg
                LEFT JOIN mall_goods_class mgc
                          ON mgg.class_id = mgc.id
        WHERE mgg.status = '0'
          AND mgg.del_flag = '0'
          AND mgc.status = '0'
          AND mgc.del_flag = '0'
    </select>

    <select id="getSeckillGoodsList" parameterType="integer" resultType="map">
        SELECT
            mgg.id goodsId,
            mgg.goods_title goodsTitle,
            msg.id seckillGoodsId,
            msg.stock seckillStock,
            msg.sale_num seckillSaleNum,
            msg.price seckillPrice,
            mgp.price goodsPrice,
            mgg.goods_img goodsImg,
            msg.purchase_limits purchaseLimits
        FROM
            mall_seckill_goods msg
                LEFT JOIN mall_goods_goods mgg
                          ON mgg.id = msg.goods_id
                LEFT JOIN mall_goods_class mgc
                          ON mgg.class_id = mgc.id
                LEFT JOIN mall_goods_product mgp
                          ON mgg.id = mgp.goods_id
        <where>
            <if test="seckillTimeId != null">and msg.time_id = #{seckillTimeId}</if>
            and mgg.status = '0'
            and msg.status = '0'
            and mgg.del_flag = '0'
            and mgc.status = '0'
            and mgc.del_flag = '0'
            and msg.del_flag = '0'
            and mgp.is_default = '1'
        </where>
        order by seckillSaleNum desc,seckillStock asc
    </select>

    <select id="selectMallSeckillGoodsById" parameterType="Integer" resultMap="MallSeckillGoodsResult">
        <include refid="selectMallSeckillGoodsVo"/>
        where id = #{id}
    </select>

    <select id="selectMallSeckillGoodsByGoodsId" parameterType="integer" resultType="integer">
        select count(id) from mall_seckill_goods where goods_id = #{goodsId} and del_flag = '0'
    </select>

    <insert id="insertMallSeckillGoods" parameterType="MallSeckillGoods" useGeneratedKeys="true" keyProperty="id">
        insert into mall_seckill_goods
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">goods_id,</if>
            <if test="price != null">price,</if>
            <if test="stock != null">stock,</if>
            <if test="timeId != null">time_id,</if>
            <if test="endTime != null">end_time,</if>
            <if test="saleNum != null">sale_num,</if>
            <if test="status != null">status,</if>
            <if test="createAt != null">create_at,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="purchaseLimits != null">purchase_limits,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">#{goodsId},</if>
            <if test="price != null">#{price},</if>
            <if test="stock != null">#{stock},</if>
            <if test="timeId != null">#{timeId},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="saleNum != null">#{saleNum},</if>
            <if test="status != null">#{status},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="purchaseLimits != null">#{purchaseLimits},</if>
         </trim>
    </insert>

    <update id="updateMallSeckillGoods" parameterType="MallSeckillGoods">
        update mall_seckill_goods
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="price != null">price = #{price},</if>
            <if test="stock != null">stock = #{stock},</if>
            <if test="timeId != null">time_id = #{timeId},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="saleNum != null">sale_num = #{saleNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="purchaseLimits != null">purchase_limits = #{purchaseLimits},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMallSeckillGoodsById" parameterType="Integer">
        <!--delete from mall_seckill_goods where id = #{id}-->
        update mall_seckill_goods set del_flag = '1' where id = #{id}
    </delete>

    <delete id="deleteMallSeckillGoodsByIds" parameterType="String">
        <!--delete from mall_seckill_goods where id in-->
        update mall_seckill_goods set del_flag = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
