<?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="cn.iocoder.mall.order.biz.dao.cart.CartMapper">

    <sql id="FIELDS">
        id, status, delete_time, selected, user_id,
        spu_id, sku_id, quantity, order_id, order_create_time,
        create_time
    </sql>

    <select id="selectById" parameterType="Integer" resultType="CartItemDO">
        SELECT
            <include refid="FIELDS" />
        FROM cart_item
        WHERE id = #{id}
--         AND deleted = 0
    </select>

    <select id="selectByIds" resultType="CartItemDO">
        SELECT
            <include refid="FIELDS" />
        FROM cart_item
        WHERE id IN
            <foreach item="id" collection="ids" separator="," open="(" close=")" index="">
                #{id}
            </foreach>
--         AND deleted = 0
    </select>

    <select id="selectByUserIdAndSkuIdAndStatus" resultType="CartItemDO">
        SELECT
            <include refid="FIELDS" />
        FROM cart_item
        WHERE user_id = #{userId}
        AND sku_id = #{skuId}
        AND status = #{status}
--         AND deleted = 0
        LIMIT 1
    </select>

    <select id="selectByUserIdAndStatusAndSelected" resultType="CartItemDO">
        SELECT
            <include refid="FIELDS" />
        FROM cart_item
        WHERE user_id = #{userId}
        AND status = #{status}
        <if test="selected != null">
            AND selected = #{selected}
        </if>
        --         AND deleted = 0
    </select>

    <select id="selectQuantitySumByUserIdAndStatus" resultType="Integer">
        SELECT
            SUM(quantity)
        FROM cart_item
        WHERE user_id = #{userId}
        AND status = #{status}
--         AND deleted = 0
    </select>

    <insert id="insert" parameterType="CartItemDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO cart_item (
            status, delete_time, selected, user_id,
            spu_id, sku_id, quantity, order_id, order_create_time,
            create_time
        ) VALUES (
            #{status}, #{deleteTime}, #{selected}, #{userId},
            #{spuId}, #{skuId}, #{quantity}, #{orderId}, #{orderCreateTime},
            #{createTime}
        )
    </insert>

    <update id="update" parameterType="CartItemDO">
        UPDATE cart_item
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="deleteTime != null">
                delete_time = #{deleteTime},
            </if>
            <if test="selected != null">
                selected = #{selected},
            </if>
            <if test="quantity != null">
                quantity = #{quantity},
            </if>
            <if test="orderId != null">
                order_id = #{orderId},
            </if>
            <if test="orderCreateTime != null">
                order_create_time = #{orderCreateTime},
            </if>
            <if test="price != null">
                price = #{price},
            </if>
            <if test="quantity != null">
                quantity = #{quantity},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <update id="updateQuantity" parameterType="CartItemDO">
        UPDATE cart_item
        SET quantity = quantity + #{quantityIncr}
        WHERE id = #{id}
    </update>

    <update id="updateListByUserIdAndSkuId">
        UPDATE cart_item
        <set>
            <if test="selected != null">
                selected = #{selected},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
        </set>
        WHERE user_id = #{userId}
        AND sku_id IN
            <foreach item="skuId" collection="skuIds" separator="," open="(" close=")" index="">
                #{skuId}
            </foreach>
        --         AND deleted = 0
    </update>

</mapper>
