<?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.mallordergoods.mapper.MallOrderGoodsMapper">

    <resultMap type="com.ruoyi.frequency.mallordergoods.entity.MallOrderGoods" id="MallOrderGoodsResult">
        <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="orderId"    column="order_id"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="specId"    column="spec_id"    />
        <result property="buyNum"    column="buy_num"    />
        <result property="unitPrice"    column="unit_price"    />
        <result property="goodsName"    column="goods_name"    />
        <result property="specName"    column="spec_name"    />
        <result property="goodsImage"    column="goods_image"    />
        <result property="specImage"    column="spec_image"    />
        <result property="specType"    column="spec_type"    />
        <result property="unitIntegral"    column="unit_integral"    />
        <result property="goodsTypeName"    column="goodsTypeName"    />
    </resultMap>



    <sql id="selectMallOrderGoodsVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.order_id ,
            t.goods_id ,
            t.spec_id ,
            t.buy_num ,
            t.unit_price ,
            t.goods_name ,
            t.spec_name ,
            t.goods_image ,
            t.spec_image ,
            t.spec_type ,
            t.member_price ,
            t.unit_integral,
            t3.`name` goodsTypeName
        FROM
            mall_order_goods t
                LEFT JOIN mall_goods t2 ON t.goods_id = t2.id
                LEFT JOIN mall_goods_type t3 ON t3.id = t2.goods_type_id

    </sql>

    <select id="selectMallOrderGoodsList" parameterType="com.ruoyi.frequency.mallordergoods.entity.MallOrderGoods" resultMap="MallOrderGoodsResult">
        <include refid="selectMallOrderGoodsVo"/>
        <where> AND t.del_flag != 2
            <if test="orderId != null "> and t.order_id = #{orderId}</if>
            <if test="goodsId != null "> and t.goods_id = #{goodsId}</if>
            <if test="specId != null "> and t.spec_id = #{specId}</if>
            <if test="buyNum != null "> and t.buy_num = #{buyNum}</if>
            <if test="unitPrice != null "> and t.unit_price = #{unitPrice}</if>
            <if test="goodsName != null  and goodsName != ''"> and t.goods_name like concat('%', #{goodsName}, '%')</if>
            <if test="specName != null  and specName != ''"> and t.spec_name like concat('%', #{specName}, '%')</if>
            <if test="goodsImage != null  and goodsImage != ''"> and t.goods_image = #{goodsImage}</if>
            <if test="specImage != null  and specImage != ''"> and t.spec_image = #{specImage}</if>
            <if test="specType != null "> and t.spec_type = #{specType}</if>
            <if test="unitIntegral != null "> and t.unit_integral = #{unitIntegral}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
            <if test="beginTime != null ">
                and  t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and  t.create_time &lt; #{endTime}
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectMallOrderGoodsById" parameterType="Long" resultMap="MallOrderGoodsResult">
        <include refid="selectMallOrderGoodsVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertMallOrderGoods" parameterType="com.ruoyi.frequency.mallordergoods.entity.MallOrderGoods">
        insert into mall_order_goods
        <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="orderId != null">order_id,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="specId != null">spec_id,</if>
            <if test="buyNum != null">buy_num,</if>
            <if test="unitPrice != null">unit_price,</if>
            <if test="goodsName != null">goods_name,</if>
            <if test="specName != null">spec_name,</if>
            <if test="goodsImage != null">goods_image,</if>
            <if test="specImage != null">spec_image,</if>
            <if test="specType != null">spec_type,</if>
            <if test="unitIntegral != null">unit_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="orderId != null">#{orderId},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="specId != null">#{specId},</if>
            <if test="buyNum != null">#{buyNum},</if>
            <if test="unitPrice != null">#{unitPrice},</if>
            <if test="goodsName != null">#{goodsName},</if>
            <if test="specName != null">#{specName},</if>
            <if test="goodsImage != null">#{goodsImage},</if>
            <if test="specImage != null">#{specImage},</if>
            <if test="specType != null">#{specType},</if>
            <if test="unitIntegral != null">#{unitIntegral},</if>
        </trim>
    </insert>

    <update id="updateMallOrderGoods" parameterType="com.ruoyi.frequency.mallordergoods.entity.MallOrderGoods">
        update mall_order_goods
        <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="orderId != null">order_id = #{orderId},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="specId != null">spec_id = #{specId},</if>
            <if test="buyNum != null">buy_num = #{buyNum},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</if>
            <if test="goodsName != null">goods_name = #{goodsName},</if>
            <if test="specName != null">spec_name = #{specName},</if>
            <if test="goodsImage != null">goods_image = #{goodsImage},</if>
            <if test="specImage != null">spec_image = #{specImage},</if>
            <if test="specType != null">spec_type = #{specType},</if>
            <if test="unitIntegral != null">unit_integral = #{unitIntegral},</if>
        </trim>
        where id = #{id}
    </update>

    <select id="orderGoodsList" resultType="com.ruoyi.frequency.mallordergoods.entity.MallOrderGoods">
        SELECT
        t.id orderGoodsId,
        t.order_id,
        t.goods_id,
        t.goods_name,
        t.goods_image,
        t.spec_id,
        t.spec_image,
        t.spec_name,
        t.spec_type,
        t.buy_num,
        t.unit_price,
        t.unit_integral,
        t3.`name` goodsTypeName
        FROM
        mall_order_goods t
        LEFT JOIN mall_goods t2 ON t.goods_id = t2.id
        LEFT JOIN mall_goods_type t3 ON t3.id = t2.goods_type_id
        WHERE
        t.order_id IN
        <foreach collection="list" index="index" item="i" open="("
                 separator="," close=")">
            #{i}
        </foreach>
    </select>
</mapper>
