<?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.mallshoppingcar.mapper.MallShoppingCarMapper">

    <resultMap type="com.ruoyi.frequency.mallshoppingcar.entity.MallShoppingCar" id="MallShoppingCarResult">
        <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="customerId"    column="customer_id"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="skuId"    column="sku_id"    />
        <result property="quantity"    column="quantity"    />
    </resultMap>



    <sql id="selectMallShoppingCarVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.customer_id ,
            t.goods_id ,
            t.sku_id ,
            t.quantity
        from mall_shopping_car AS t

    </sql>

    <select id="selectMallShoppingCarList" parameterType="com.ruoyi.frequency.mallshoppingcar.entity.MallShoppingCar" resultMap="MallShoppingCarResult">
        <include refid="selectMallShoppingCarVo"/>
        <where> AND t.del_flag != 2
            <if test="customerId != null "> and t.customer_id = #{customerId}</if>
            <if test="goodsId != null "> and t.goods_id = #{goodsId}</if>
            <if test="skuId != null "> and t.sku_id = #{skuId}</if>
            <if test="quantity != null "> and t.quantity = #{quantity}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectMallShoppingCarById" parameterType="Long" resultMap="MallShoppingCarResult">
        <include refid="selectMallShoppingCarVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertMallShoppingCar" parameterType="com.ruoyi.frequency.mallshoppingcar.entity.MallShoppingCar">
        insert into mall_shopping_car
        <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="customerId != null">customer_id,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="skuId != null">sku_id,</if>
            <if test="quantity != null">quantity,</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="customerId != null">#{customerId},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="skuId != null">#{skuId},</if>
            <if test="quantity != null">#{quantity},</if>
        </trim>
    </insert>

    <update id="updateMallShoppingCar" parameterType="com.ruoyi.frequency.mallshoppingcar.entity.MallShoppingCar">
        update mall_shopping_car
        <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="customerId != null">customer_id = #{customerId},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="skuId != null">sku_id = #{skuId},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
        </trim>
        where id = #{id}
    </update>

    <select id="shoppingCarList" resultType="com.ruoyi.controller.resp.ShoppingCarListVo">
        SELECT
        t.id,
        t.goods_id,
        t2.id specId,
        t2.image,
        t3.`name` goodsName,
        t2.`name` specName,
        t2.price,
        t2.integral,
        t.quantity,
        t2.del_flag specFlag,
        t3.del_flag goodsFlag,
        t2.stock
        FROM
        mall_shopping_car t
        LEFT JOIN mall_specifications t2 ON t.sku_id = t2.id
        LEFT JOIN mall_goods t3 ON t.goods_id = t3.id
        WHERE
        t.del_flag = 1
        AND t.user_id = #{userVo.userId}
        AND t.user_type = #{userVo.userType}
        <if test="list != null and list.size > 0">
            AND t.id in
            <foreach collection="list" index="index" item="i" open="("
                     separator="," close=")">
                #{i}
            </foreach>
        </if>
        ORDER BY
        t.create_time DESC
    </select>
</mapper>
