<?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.system.mapper.OrderInfoMapper">
    
    <resultMap type="OrderInfo" id="OrderInfoResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="orderName"    column="order_name"    />
        <result property="userId"    column="user_id"    />
        <result property="amount"    column="amount"    />
        <result property="payType"    column="pay_type"    />
        <result property="payTime"    column="pay_time"    />
        <result property="payStatus"    column="pay_status"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="idDel"    column="id_del"    />
        <result property="updateTime"    column="update_time"    />
        <result property="callTime"    column="call_time"    />
        <result property="packageType"    column="package_type"    />
        <result property="callbackContent"    column="callback_content"    />
        <result property="tradeNo"    column="trade_no"    />
        <result property="refundAmount"    column="refund_amount"    />
        <result property="refundTime"    column="refund_time"    />
        <result property="clientIp"    column="client_ip"    />
        <result property="deviceType"    column="device_type"    />
        <result property="version"    column="version"    />
    </resultMap>

    <sql id="selectOrderInfoVo">
        select id, order_id, order_name, user_id, amount, pay_type, pay_time, pay_status, start_time, end_time, create_time, id_del, update_time, call_time, package_type, callback_content, trade_no, refund_amount, refund_time, client_ip, device_type, version from order_info
    </sql>

    <select id="selectOrderInfoList" parameterType="OrderInfo" resultMap="OrderInfoResult">
        <include refid="selectOrderInfoVo"/>
        <where>  
            <if test="orderId != null "> and order_id = #{orderId}</if>
            <if test="orderName != null  and orderName != ''"> and order_name like concat('%', #{orderName}, '%')</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="payType != null  and payType != ''"> and pay_type = #{payType}</if>
            <if test="payTime != null "> and pay_time = #{payTime}</if>
            <if test="payStatus != null "> and pay_status = #{payStatus}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="idDel != null "> and id_del = #{idDel}</if>
            <if test="callTime != null "> and call_time = #{callTime}</if>
            <if test="packageType != null  and packageType != ''"> and package_type = #{packageType}</if>
            <if test="callbackContent != null  and callbackContent != ''"> and callback_content = #{callbackContent}</if>
            <if test="tradeNo != null  and tradeNo != ''"> and trade_no = #{tradeNo}</if>
            <if test="refundAmount != null "> and refund_amount = #{refundAmount}</if>
            <if test="refundTime != null "> and refund_time = #{refundTime}</if>
            <if test="clientIp != null  and clientIp != ''"> and client_ip = #{clientIp}</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="version != null "> and version = #{version}</if>
        </where>
    </select>
    
    <select id="selectOrderInfoById" parameterType="String" resultMap="OrderInfoResult">
        <include refid="selectOrderInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertOrderInfo" parameterType="OrderInfo" useGeneratedKeys="true" keyProperty="id">
        insert into order_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="orderName != null">order_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="amount != null">amount,</if>
            <if test="payType != null">pay_type,</if>
            <if test="payTime != null">pay_time,</if>
            <if test="payStatus != null">pay_status,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="idDel != null">id_del,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="callTime != null">call_time,</if>
            <if test="packageType != null">package_type,</if>
            <if test="callbackContent != null">callback_content,</if>
            <if test="tradeNo != null">trade_no,</if>
            <if test="refundAmount != null">refund_amount,</if>
            <if test="refundTime != null">refund_time,</if>
            <if test="clientIp != null">client_ip,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="version != null">version,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="orderName != null">#{orderName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="payType != null">#{payType},</if>
            <if test="payTime != null">#{payTime},</if>
            <if test="payStatus != null">#{payStatus},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="idDel != null">#{idDel},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="callTime != null">#{callTime},</if>
            <if test="packageType != null">#{packageType},</if>
            <if test="callbackContent != null">#{callbackContent},</if>
            <if test="tradeNo != null">#{tradeNo},</if>
            <if test="refundAmount != null">#{refundAmount},</if>
            <if test="refundTime != null">#{refundTime},</if>
            <if test="clientIp != null">#{clientIp},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="version != null">#{version},</if>
         </trim>
    </insert>

    <update id="updateOrderInfo" parameterType="OrderInfo">
        update order_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="orderName != null">order_name = #{orderName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="idDel != null">id_del = #{idDel},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="callTime != null">call_time = #{callTime},</if>
            <if test="packageType != null">package_type = #{packageType},</if>
            <if test="callbackContent != null">callback_content = #{callbackContent},</if>
            <if test="tradeNo != null">trade_no = #{tradeNo},</if>
            <if test="refundAmount != null">refund_amount = #{refundAmount},</if>
            <if test="refundTime != null">refund_time = #{refundTime},</if>
            <if test="clientIp != null">client_ip = #{clientIp},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="version != null">version = #{version},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrderInfoById" parameterType="String">
        delete from order_info where id = #{id}
    </delete>

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

    <select id="selectOrderByOrderId" parameterType="String" resultMap="OrderInfoResult">
        <include refid="selectOrderInfoVo"/>
        where order_id = #{orderId} and id_del = 0
    </select>

    <resultMap type="OrderInfoVO" id="OrderInfoVOResult">
        <result property="orderId"    column="order_id"    />
        <result property="userNickname"    column="user_nickname"    />
        <result property="packageName"    column="package_name"    />
        <result property="amountYuan"    column="amount_yuan"    />
        <result property="payTypeName"    column="pay_type_name"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="statusName"    column="status_name"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <select id="selectOrderInfoVOList" parameterType="OrderInfo" resultMap="OrderInfoVOResult">
        SELECT 
            o.order_id,
            su.nickname as user_nickname,
            CASE 
                WHEN o.package_type = '1' THEN '包月'
                WHEN o.package_type = '3' THEN '包季度'
                WHEN o.package_type = '6' THEN '半年'
                WHEN o.package_type = '12' THEN '一年'
                ELSE '未知套餐'
            END as package_name,
            ROUND(o.amount / 100.0, 2) as amount_yuan,
            CASE 
                WHEN o.pay_type = 'aliPay' THEN '支付宝'
                WHEN o.pay_type = 'wechatPay' THEN '微信支付'
                WHEN o.pay_type = 'applePay' THEN 'Apple Pay'
                ELSE '未知支付方式'
            END as pay_type_name,
            o.start_time,
            o.end_time,
            CASE 
                WHEN o.pay_status = 1 THEN '待支付'
                WHEN o.pay_status = 2 THEN '待出货'
                WHEN o.pay_status = 3 THEN '待退款'
                WHEN o.pay_status = 4 THEN '已退款'
                WHEN o.pay_status = 5 THEN '已完成'
                ELSE '未知状态'
            END as status_name,
            o.create_time
        FROM order_info o
        LEFT JOIN shop_user su ON o.user_id = su.user_id
        <where>
            o.id_del = 0
            <if test="orderId != null "> and o.order_id = #{orderId}</if>
            <if test="orderName != null  and orderName != ''"> and o.order_name like concat('%', #{orderName}, '%')</if>
            <if test="userId != null "> and o.user_id = #{userId}</if>
            <if test="amount != null "> and o.amount = #{amount}</if>
            <if test="payType != null  and payType != ''"> and o.pay_type = #{payType}</if>
            <if test="payTime != null "> and o.pay_time = #{payTime}</if>
            <if test="payStatus != null "> and o.pay_status = #{payStatus}</if>
            <if test="startTime != null "> and o.start_time = #{startTime}</if>
            <if test="endTime != null "> and o.end_time = #{endTime}</if>
            <if test="packageType != null  and packageType != ''"> and o.package_type = #{packageType}</if>
            <if test="clientIp != null  and clientIp != ''"> and o.client_ip = #{clientIp}</if>
            <if test="deviceType != null  and deviceType != ''"> and o.device_type = #{deviceType}</if>
        </where>
        ORDER BY o.create_time DESC
    </select>
</mapper>