<?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.web.modules.cart.mapper.CartAppMapper">

    <sql id="cartItemSelect">
        select c.cart_item_id as cartItemId,
               c.shop_id as shopId,
               c.goods_id as goodsId,
               c.spec_text as specText,
               c.quantity as quantity,
               c.checked as checked,
               c.update_time as updateTime,
               g.goods_name as goodsName,
               g.main_pic as mainPic,
               g.sale_price as salePrice,
               g.stock as stock,
               g.goods_status as goodsStatus,
               g.del_flag as goodsDelFlag,
               g.category_id as categoryId,
               s.shop_name as shopName,
               s.shop_avatar as shopAvatar,
               s.shop_status as shopStatus,
               s.del_flag as shopDelFlag
        from biz_member_cart_item c
        left join biz_goods g on c.goods_id = g.goods_id
        left join biz_shop s on c.shop_id = s.shop_id
    </sql>

    <select id="selectItemRowsByMemberId" resultType="com.ruoyi.web.modules.cart.dto.CartItemRowDTO">
        <include refid="cartItemSelect"/>
        where c.member_id = #{memberId}
        order by c.cart_item_id desc
    </select>

    <select id="selectItemRowsByIdsAndMember" resultType="com.ruoyi.web.modules.cart.dto.CartItemRowDTO">
        <include refid="cartItemSelect"/>
        where c.member_id = #{memberId}
          and c.cart_item_id in
        <foreach collection="cartItemIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectByIdAndMember" resultType="com.ruoyi.web.modules.cart.domain.BizMemberCartItem">
        select cart_item_id as cartItemId,
               member_id as memberId,
               shop_id as shopId,
               goods_id as goodsId,
               spec_key as specKey,
               spec_text as specText,
               quantity as quantity,
               checked as checked,
               create_time as createTime,
               update_time as updateTime
        from biz_member_cart_item
        where cart_item_id = #{cartItemId}
          and member_id = #{memberId}
    </select>

    <select id="selectByMemberGoodsSpec" resultType="com.ruoyi.web.modules.cart.domain.BizMemberCartItem">
        select cart_item_id as cartItemId,
               member_id as memberId,
               shop_id as shopId,
               goods_id as goodsId,
               spec_key as specKey,
               spec_text as specText,
               quantity as quantity,
               checked as checked,
               create_time as createTime,
               update_time as updateTime
        from biz_member_cart_item
        where member_id = #{memberId}
          and goods_id = #{goodsId}
          and spec_key = #{specKey}
    </select>

    <insert id="insert" parameterType="com.ruoyi.web.modules.cart.domain.BizMemberCartItem"
            useGeneratedKeys="true" keyProperty="cartItemId">
        insert into biz_member_cart_item (
            member_id, shop_id, goods_id, spec_key, spec_text, quantity, checked, create_time, update_time
        ) values (
            #{memberId}, #{shopId}, #{goodsId}, #{specKey}, #{specText}, #{quantity}, #{checked}, #{createTime}, #{updateTime}
        )
    </insert>

    <update id="updateQuantity">
        update biz_member_cart_item
        set quantity = #{quantity},
            update_time = sysdate()
        where cart_item_id = #{cartItemId}
          and member_id = #{memberId}
    </update>

    <update id="updateChecked">
        update biz_member_cart_item
        set checked = #{checked},
            update_time = sysdate()
        where cart_item_id = #{cartItemId}
          and member_id = #{memberId}
    </update>

    <delete id="deleteByIdAndMember">
        delete from biz_member_cart_item
        where cart_item_id = #{cartItemId}
          and member_id = #{memberId}
    </delete>

    <delete id="deleteByIdsAndMember">
        delete from biz_member_cart_item
        where member_id = #{memberId}
          and cart_item_id in
        <foreach collection="cartItemIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
