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

    <resultMap type="MallCombineGoods" id="MallCombineGoodsResult">
        <result property="combineId"    column="combine_id"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="price"    column="price"    />
        <result property="stock"    column="stock"    />
        <result property="peopleNum"    column="people_num"    />
        <result property="endMinute"    column="end_minute"    />
        <result property="timeId"    column="time_id"    />
        <result property="endTime"    column="end_time"    />
        <result property="saleNum"    column="sale_num"    />
        <result property="purchaseLimits"    column="purchase_limits"    />
        <result property="status"    column="status"    />
        <result property="createAt"    column="create_at"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="goodsName"    column="goodsName"    />
        <result property="goodsTitle"    column="goodsTitle"    />
        <result property="goodsImg"    column="goodsImg"    />
        <result property="className"    column="className"    />
    </resultMap>

    <sql id="selectMallCombineGoodsVo">
        select combine_id, goods_id, price, stock, people_num, end_minute, time_id, end_time, sale_num, purchase_limits, status, create_at, update_at, del_flag from mall_combine_goods
    </sql>

    <select id="selectMallCombineGoodsList" parameterType="MallCombineGoods" resultMap="MallCombineGoodsResult">
        select mcg.combine_id, mcg.goods_id, mcg.price, mcg.stock, mcg.people_num, mcg.end_minute, mcg.time_id, mcg.end_time, mcg.sale_num, mcg.purchase_limits,
               mcg.status, mcg.create_at, mcg.update_at, mcg.del_flag,
               mgg.goods_name goodsName, mgg.goods_title goodsTitle, mgg.goods_img goodsImg,
               mgc.name className
        from mall_combine_goods mcg
        LEFT JOIN mall_goods_goods mgg ON mcg.goods_id = mgg.id
        LEFT JOIN mall_goods_class mgc ON mgg.class_id = mgc.id
        <where>
            <if test="goodsId != null "> and mcg.goods_id = #{goodsId}</if>
            <if test="price != null "> and mcg.price = #{price}</if>
            <if test="stock != null "> and mcg.stock = #{stock}</if>
            <if test="peopleNum != null "> and mcg.people_num = #{peopleNum}</if>
            <if test="endMinute != null "> and mcg.end_minute = #{endMinute}</if>
            <if test="timeId != null "> and mcg.time_id = #{timeId}</if>
            <if test="endTime != null  and endTime != ''"> and mcg.end_time = #{endTime}</if>
            <if test="saleNum != null "> and mcg.sale_num = #{saleNum}</if>
            <if test="purchaseLimits != null "> and mcg.purchase_limits = #{purchaseLimits}</if>
            <if test="status != null "> and mcg.status = #{status}</if>
            <if test="createAt != null  and createAt != ''"> and mcg.create_at = #{createAt}</if>
            <if test="updateAt != null  and updateAt != ''"> and mcg.update_at = #{updateAt}</if>
            and mcg.del_flag = 0
        </where>
        order by mcg.create_at desc
    </select>

    <select id="selectMallCombineGoodsByCombineId" parameterType="Long" resultMap="MallCombineGoodsResult">
        <include refid="selectMallCombineGoodsVo"/>
        where combine_id = #{combineId}
    </select>

    <insert id="insertMallCombineGoods" parameterType="MallCombineGoods" useGeneratedKeys="true" keyProperty="combineId">
        insert into mall_combine_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="peopleNum != null">people_num,</if>
            <if test="endMinute != null">end_minute,</if>
            <if test="timeId != null">time_id,</if>
            <if test="endTime != null">end_time,</if>
            <if test="saleNum != null">sale_num,</if>
            <if test="purchaseLimits != null">purchase_limits,</if>
            <if test="status != null">status,</if>
            <if test="createAt != null">create_at,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="delFlag != null">del_flag,</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="peopleNum != null">#{peopleNum},</if>
            <if test="endMinute != null">#{endMinute},</if>
            <if test="timeId != null">#{timeId},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="saleNum != null">#{saleNum},</if>
            <if test="purchaseLimits != null">#{purchaseLimits},</if>
            <if test="status != null">#{status},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateMallCombineGoods" parameterType="MallCombineGoods">
        update mall_combine_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="peopleNum != null">people_num = #{peopleNum},</if>
            <if test="endMinute != null">end_minute = #{endMinute},</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="purchaseLimits != null">purchase_limits = #{purchaseLimits},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where combine_id = #{combineId}
    </update>

    <delete id="deleteMallCombineGoodsByCombineId" parameterType="Long">
        delete from mall_combine_goods where combine_id = #{combineId}
    </delete>

    <delete id="deleteMallCombineGoodsByCombineIds" parameterType="String">
        delete from mall_combine_goods where combine_id in
        <foreach item="combineId" collection="array" open="(" separator="," close=")">
            #{combineId}
        </foreach>
    </delete>

    <select id="enableCombineGoodsList" resultType="map">
        SELECT mgg.id          goodsId,
               mgg.goods_name  goodsName,
               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="selectMallCombineGoodsByGoodsId" parameterType="long" resultType="java.lang.Integer">
        select count(combine_id) from mall_combine_goods where goods_id = #{goodsId} and del_flag = 0
    </select>

    <select id="getCombineGoodsList" resultType="map">
        SELECT
        mgg.id goodsId,
        mgg.goods_name goodsName,
        mgg.goods_title goodsTitle,
        mcg.combine_id combineId,
        mcg.people_num peopleNum,
        mcg.stock combineStock,
        mcg.sale_num combineSaleNum,
        mcg.price combinePrice,
        mgp.price goodsPrice,
        mgg.goods_img goodsImg,
        mcg.purchase_limits purchaseLimits
        FROM mall_combine_goods mcg
        LEFT JOIN mall_goods_goods mgg ON mgg.id = mcg.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="goodsId != null">and mcg.goods_id = #{goodsId}</if>
            and mgg.status = '0'
            and mcg.status = '0'
            and mgg.del_flag = '0'
            and mgc.status = '0'
            and mgc.del_flag = '0'
            and mcg.del_flag = '0'
            and mgp.is_default = '1'
        </where>
        order by mcg.create_at desc, mcg.sale_num desc,mcg.stock asc
    </select>

    <select id="selectCombineGoodsByGoodsId" parameterType="Long" resultMap="MallCombineGoodsResult">
        <include refid="selectMallCombineGoodsVo"/>
        where goods_id = #{goodsId}
        and del_flag = '0'
        limit 1
    </select>

    <update id="editCombineGoodsInfo" >

        <if test="type == 0">
            update mall_combine_goods set
            stock = stock - #{count},
            sale_num = sale_num + #{count}
            where goods_id = #{goodsId}
            and stock > 0
        </if>
        <if test="type == 1">
            update mall_combine_goods set
            stock = stock + #{count},
            sale_num = sale_num - #{count}
            where goods_id = #{goodsId}
        </if>

    </update>
</mapper>
