<?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.frequency.mallspecifications.mapper.MallSpecificationsMapper">

    <resultMap type="com.ruoyi.frequency.mallspecifications.entity.MallSpecifications" id="MallSpecificationsResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="name"    column="name"    />
        <result property="sales"    column="sales"    />
        <result property="stock"    column="stock"    />
        <result property="price"    column="price"    />
        <result property="image"    column="image"    />
        <result property="integral"    column="integral"    />
    </resultMap>



    <sql id="selectMallSpecificationsVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.goods_id ,
            t.name ,
            t.sales ,
            t.stock ,
            t.price ,
            t.image ,
            t.integral
        from mall_specifications AS t

    </sql>

    <select id="selectMallSpecificationsList" parameterType="com.ruoyi.frequency.mallspecifications.entity.MallSpecifications" resultMap="MallSpecificationsResult">
        <include refid="selectMallSpecificationsVo"/>
        <where> AND t.del_flag != 2
            <if test="goodsId != null "> and t.goods_id = #{goodsId}</if>
            <if test="name != null  and name != ''"> and t.name like concat('%', #{name}, '%')</if>
            <if test="sales != null "> and t.sales = #{sales}</if>
            <if test="stock != null "> and t.stock = #{stock}</if>
            <if test="price != null "> and t.price = #{price}</if>
            <if test="image != null  and image != ''"> and t.image = #{image}</if>
            <if test="integral != null "> and t.integral = #{integral}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectMallSpecificationsById" parameterType="Long" resultMap="MallSpecificationsResult">
        <include refid="selectMallSpecificationsVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertMallSpecifications" parameterType="com.ruoyi.frequency.mallspecifications.entity.MallSpecifications">
        insert into mall_specifications
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="name != null">name,</if>
            <if test="sales != null">sales,</if>
            <if test="stock != null">stock,</if>
            <if test="price != null">price,</if>
            <if test="image != null">image,</if>
            <if test="integral != null">integral,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="name != null">#{name},</if>
            <if test="sales != null">#{sales},</if>
            <if test="stock != null">#{stock},</if>
            <if test="price != null">#{price},</if>
            <if test="image != null">#{image},</if>
            <if test="integral != null">#{integral},</if>
        </trim>
    </insert>

    <update id="updateMallSpecifications" parameterType="com.ruoyi.frequency.mallspecifications.entity.MallSpecifications">
        update mall_specifications
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="sales != null">sales = #{sales},</if>
            <if test="stock != null">stock = #{stock},</if>
            <if test="price != null">price = #{price},</if>
            <if test="image != null">image = #{image},</if>
            <if test="integral != null">integral = #{integral},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="subStock" parameterType="java.util.List">
        <foreach item="item" collection="list"  separator=";" >
            update mall_specifications
            <trim prefix="SET" suffixOverrides=",">
                stock = stock - #{item.stock}
            </trim>
            where id = #{item.id}
            AND stock >= #{item.stock}
        </foreach>
    </update>

    <update id="addStock" parameterType="java.util.List">
        <foreach item="item" collection="list"  separator=";" >
            update mall_specifications
            <trim prefix="SET" suffixOverrides=",">
                stock = stock + #{item.stock}
            </trim>
            where id = #{item.id}
        </foreach>
    </update>

    <update id="subSales" parameterType="java.util.List">
        <foreach item="item" collection="list"  separator=";" >
            update mall_specifications
            <trim prefix="SET" suffixOverrides=",">
                sales = sales - #{item.sales}
            </trim>
            where id = #{item.id}
            AND sales >= #{item.sales}
        </foreach>
    </update>

    <update id="addSales" parameterType="java.util.List">
        <foreach item="item" collection="list"  separator=";" >
            update mall_specifications
            <trim prefix="SET" suffixOverrides=",">
                sales = sales + #{item.sales}
            </trim>
            where id = #{item.id}
        </foreach>
    </update>

</mapper>
