Files

96 lines
4.2 KiB
XML
Raw Permalink Normal View History

2026-01-11 18:14:42 +08:00
<?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.nlshop.mapper.CartMapper">
<resultMap id="BaseResultMap" type="com.nlshop.entity.CartItem">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="product_id" property="productId"/>
<result column="spec_id" property="specId"/>
<result column="quantity" property="quantity"/>
<result column="custom_sweetness" property="customSweetness"/>
<result column="custom_ice" property="customIce"/>
<result column="toppings" property="toppings"/>
<result column="create_time" property="createTime"/>
<association property="product" javaType="com.nlshop.entity.Product">
<id column="p_id" property="id"/>
<result column="p_name" property="name"/>
<result column="p_image" property="image"/>
<result column="p_base_price" property="basePrice"/>
<result column="p_merchant_id" property="merchantId"/>
</association>
<association property="spec" javaType="com.nlshop.entity.ProductSpec">
<id column="s_id" property="id"/>
<result column="s_spec_name" property="specName"/>
<result column="s_price_adjust" property="priceAdjust"/>
</association>
</resultMap>
<insert id="insert" parameterType="com.nlshop.entity.CartItem" useGeneratedKeys="true" keyProperty="id">
INSERT INTO cart (user_id, product_id, spec_id, quantity, custom_sweetness, custom_ice, toppings)
VALUES (#{userId}, #{productId}, #{specId}, #{quantity}, #{customSweetness}, #{customIce}, #{toppings})
</insert>
<select id="selectByUserId" resultMap="BaseResultMap">
SELECT c.*,
p.id as p_id, p.name as p_name, p.image as p_image, p.base_price as p_base_price, p.merchant_id as p_merchant_id,
s.id as s_id, s.spec_name as s_spec_name, s.price_adjust as s_price_adjust
FROM cart c
LEFT JOIN product p ON c.product_id = p.id
LEFT JOIN product_spec s ON c.spec_id = s.id
WHERE c.user_id = #{userId}
ORDER BY c.create_time DESC
</select>
<select id="selectById" resultMap="BaseResultMap">
SELECT c.*,
p.id as p_id, p.name as p_name, p.image as p_image, p.base_price as p_base_price, p.merchant_id as p_merchant_id,
s.id as s_id, s.spec_name as s_spec_name, s.price_adjust as s_price_adjust
FROM cart c
LEFT JOIN product p ON c.product_id = p.id
LEFT JOIN product_spec s ON c.spec_id = s.id
WHERE c.id = #{id}
</select>
<select id="selectByUserAndProduct" resultMap="BaseResultMap">
SELECT * FROM cart
WHERE user_id = #{userId} AND product_id = #{productId}
AND (spec_id = #{specId} OR (#{specId} IS NULL AND spec_id IS NULL))
AND (custom_sweetness = #{customSweetness} OR (#{customSweetness} IS NULL AND custom_sweetness IS NULL))
AND (custom_ice = #{customIce} OR (#{customIce} IS NULL AND custom_ice IS NULL))
AND (toppings = #{toppings} OR (#{toppings} IS NULL AND toppings IS NULL))
LIMIT 1
</select>
<update id="update" parameterType="com.nlshop.entity.CartItem">
UPDATE cart
<set>
<if test="quantity != null">quantity = #{quantity},</if>
<if test="customSweetness != null">custom_sweetness = #{customSweetness},</if>
<if test="customIce != null">custom_ice = #{customIce},</if>
<if test="toppings != null">toppings = #{toppings},</if>
</set>
WHERE id = #{id}
</update>
<update id="updateQuantity">
UPDATE cart SET quantity = #{quantity} WHERE id = #{id}
</update>
<delete id="delete">
DELETE FROM cart WHERE id = #{id}
</delete>
<delete id="deleteByUserId">
DELETE FROM cart WHERE user_id = #{userId}
</delete>
<delete id="deleteBatch">
DELETE FROM cart
WHERE id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
AND user_id = #{userId}
</delete>
</mapper>