<?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.model.mapper.AppletOrderMapper">
    
    <resultMap type="AppletOrder" id="AppletOrderResult">
        <result property="id" column="id"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="payTime" column="pay_time"/>
        <result property="status" column="status"/>
        <result property="price" column="price"/>
        <result property="address" column="address"/>
        <result property="type" column="type"/>
        <result property="reason" column="reason"/>
        <result property="userId" column="user_id"/>
        <result property="petId" column="pet_id"/>
        <result property="num" column="num"/>
        <result property="userIdJson" column="user_id_json"/>
        <result property="orderSn" column="order_sn"/>
        <result property="orderId" column="order_id"/>
        <result property="orderTime" column="order_time"/>

    </resultMap>

    <sql id="selectAppletOrderVo">
        select id, create_time, create_by, update_time, update_by, pay_time, status, price, address, type, reason, user_id, pet_id,num,user_id_json,order_id,order_sn,order_time  from applet_order
    </sql>

    <select id="selectAppletOrderList" parameterType="AppletOrder" resultMap="AppletOrderResult">
        <include refid="selectAppletOrderVo"/>
        <where>
            <if test="payTime != null "> and pay_time = #{payTime}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="reason != null  and reason != ''"> and reason = #{reason}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="petId != null "> and pet_id = #{petId}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="userIdJson != null "> and user_id_json = #{userIdJson}</if>
            <if test="orderSn != null "> and order_sn = #{orderSn}</if>
            <if test="orderId != null "> and order_id = #{orderId}</if>
            <if test="orderTime != null "> and order_time = #{orderTime}</if>
        </where>
        order by create_time desc

    </select>

    <select id="selectAppletOrderListLikeUserIdJson" parameterType="AppletOrder" resultMap="AppletOrderResult">

        <include refid="selectAppletOrderVo"/>
        <where>
            <!-- 根据具体需求选择方案 -->
            <!-- 方案1：自动添加通配符 -->
           type = #{type} and status = 0 and user_id_json like concat('%', #{userIdJson}, '%')

            <!-- 方案2：使用JSON函数查询（MySQL示例） -->
            <!-- JSON_CONTAINS(user_id_json, #{userIdJson}) -->
        </where>
        order by create_time desc

    </select>












    
    <select id="selectAppletOrderById" parameterType="AppletOrder" resultMap="AppletOrderResult">
        <include refid="selectAppletOrderVo"/>
        where id = #{id}
    </select>

    <select id="selectAppletOrderByOrderId" parameterType="AppletOrder" resultMap="AppletOrderResult">
        <include refid="selectAppletOrderVo"/>
        where order_id = #{orderId}
    </select>




    <insert id="insertAppletOrder" parameterType="AppletOrder" useGeneratedKeys="true" keyProperty="id">
        insert into applet_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="payTime != null">pay_time,</if>
            <if test="status != null">status,</if>
            <if test="price != null">price,</if>
            <if test="address != null">address,</if>
            <if test="type != null">type,</if>
            <if test="reason != null">reason,</if>
            <if test="userId != null">user_id,</if>
            <if test="petId != null">pet_id,</if>
            <if test="num != null">num,</if>
            <if test="userIdJson != null">user_id_json,</if>
            <if test="orderSn != null">order_sn,</if>
            <if test="orderId != null">order_id,</if>
            <if test="orderTime != null">order_time,</if>


        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="payTime != null">#{payTime},</if>
            <if test="status != null">#{status},</if>
            <if test="price != null">#{price},</if>
            <if test="address != null">#{address},</if>
            <if test="type != null">#{type},</if>
            <if test="reason != null">#{reason},</if>
            <if test="userId != null">#{userId},</if>
            <if test="petId != null">#{petId},</if>
            <if test="num != null">#{num},</if>
            <if test="userIdJson != null">#{userIdJson},</if>
            <if test="orderSn != null">#{orderSn},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="orderTime != null">#{orderTime},</if>



        </trim>
    </insert>

    <update id="updateAppletOrder" parameterType="AppletOrder">
        update applet_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="price != null">price = #{price},</if>
            <if test="address != null">address = #{address},</if>
            <if test="type != null">type = #{type},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="petId != null">pet_id = #{petId},</if>
            <if test="userIdJson != null">user_id_json = #{userIdJson},</if>
            <if test="orderSn != null">order_sn = #{orderSn},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="orderTime != null"> order_time = #{orderTime},</if>


        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppletOrderById" parameterType="Long">
        delete from applet_order where id = #{id}
    </delete>

    <delete id="deleteAppletOrderByIds" parameterType="String">
        delete from applet_order where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>