<?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.willalp.canteen.mapper.HsUserShopCarMapper">

    <resultMap type="HsUserShopCar" id="HsUserShopCarResult">
        <result property="id" column="id"/>
        <result property="userCode" column="user_code"/>
        <result property="jsonData" column="json_data"/>
    </resultMap>

    <sql id="selectHsUserShopCarVo">
        select id, user_code, json_data
        from hs_user_shop_car
    </sql>
    <delete id="deleteJson">
        DELETE
        FROM hs_user_shop_car
        WHERE json_data ->> '$.giftId' = #{id}
        <if test="userCode != null and userCode != ''">
            AND user_code = #{userCode}
        </if>
    </delete>

    <select id="countJson" resultType="java.lang.Integer">
        SELECT json_data ->> '$.num'
        FROM hs_user_shop_car
        WHERE json_data ->> '$.giftId' = #{id}
          AND user_code = #{userCode}
    </select>

    <update id="addJsonNum">
        update hs_user_shop_car
        set json_data = JSON_SET(json_data, '$.num', #{num})
        where json_data ->> '$.giftId' = #{jsonKey}
    </update>

    <select id="selectHsUserShopCarList" parameterType="HsUserShopCar" resultMap="HsUserShopCarResult">
        <include refid="selectHsUserShopCarVo"/>
        <where>
            <if test="userCode != null  and userCode != ''">and user_code = #{userCode}</if>
            <if test="jsonData != null  and jsonData != ''">and json_data = #{jsonData}</if>
        </where>
    </select>

</mapper>
