<?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.kexue.skills.mapper.PaymentOrderMapper">

    <resultMap type="com.kexue.skills.entity.PaymentOrder" id="PaymentOrderMap">
        <result property="orderId" column="order_id" jdbcType="BIGINT"/>
        <result property="orderNo" column="order_no" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="BIGINT"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="amount" column="amount" jdbcType="DECIMAL"/>
        <result property="payType" column="pay_type" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="channelOrderNo" column="channel_order_no" jdbcType="VARCHAR"/>
        <result property="BusinessId" column="business_id" jdbcType="BIGINT"/>
        <result property="BusinessType" column="business_type" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="deleteFlag" column="delete_flag" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->    
    <select id="queryById" resultMap="PaymentOrderMap">
        select 
          order_id, order_no, user_id, user_name, amount, pay_type, status, channel_order_no, 
          business_id, business_type, remark, create_time, update_time, create_by, update_by, delete_flag
        from payment_order
        where order_id = #{orderId}
    </select>

    <!--通过订单号查询-->    
    <select id="queryByOrderNo" resultMap="PaymentOrderMap">
        select 
          order_id, order_no, user_id, user_name, amount, pay_type, status, channel_order_no, 
          business_id, business_type, remark, create_time, update_time, create_by, update_by, delete_flag
        from payment_order
        where order_no = #{orderNo}
    </select>

    <!--分页查询-->    
    <select id="getPageList" resultMap="PaymentOrderMap">
        select 
          order_id, order_no, user_id, user_name, amount, pay_type, status, channel_order_no, 
          business_id, business_type, remark, create_time, update_time, create_by, update_by, delete_flag
        from payment_order
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="orderNo != null and orderNo != ''">
                and order_no like concat('%', #{orderNo}, '%')
            </if>
            <if test="payType != null">
                and pay_type = #{payType}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="businessType != null and businessType != ''">
                and business_type = #{businessType}
            </if>
            <if test="createTimeStart != null">
                and create_time <![CDATA[ >= ]]> #{createTimeStart}
            </if>
            <if test="createTimeEnd != null">
                and create_time <![CDATA[ <= ]]> #{createTimeEnd}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        <if test="sortBy != null and sortBy != ''">
            order by ${sortBy} ${sortDesc ? 'desc' : 'asc'}
        </if>
    </select>

    <!--查询列表-->    
    <select id="getList" resultMap="PaymentOrderMap">
        select 
          order_id, order_no, user_id, user_name, amount, pay_type, status, channel_order_no, 
          business_id, business_type, remark, create_time, update_time, create_by, update_by, delete_flag
        from payment_order
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="orderNo != null and orderNo != ''">
                and order_no like concat('%', #{orderNo}, '%')
            </if>
            <if test="payType != null">
                and pay_type = #{payType}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="businessType != null and businessType != ''">
                and business_type = #{businessType}
            </if>
            <if test="createTimeStart != null">
                and create_time <![CDATA[ >= ]]> #{createTimeStart}
            </if>
            <if test="createTimeEnd != null">
                and create_time <![CDATA[ <= ]]> #{createTimeEnd}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
    </select>

    <!--新增数据-->    
    <insert id="insert" useGeneratedKeys="true" keyProperty="orderId">
        insert into payment_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">order_no,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="amount != null">amount,</if>
            <if test="payType != null">pay_type,</if>
            <if test="status != null">status,</if>
            <if test="channelOrderNo != null">channel_order_no,</if>
            <if test="businessId != null">business_id,</if>
            <if test="businessType != null">business_type,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="deleteFlag != null">delete_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">#{orderNo},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="amount != null">#{amount},</if>
            <if test="payType != null">#{payType},</if>
            <if test="status != null">#{status},</if>
            <if test="channelOrderNo != null">#{channelOrderNo},</if>
            <if test="businessId != null">#{businessId},</if>
            <if test="businessType != null">#{businessType},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="deleteFlag != null">#{deleteFlag},</if>
        </trim>
    </insert>

    <!--更新数据-->    
    <update id="update">
        update payment_order
        <set>
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="payType != null">pay_type = #{payType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="channelOrderNo != null">channel_order_no = #{channelOrderNo},</if>
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="businessType != null">business_type = #{businessType},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="deleteFlag != null">delete_flag = #{deleteFlag},</if>
        </set>
        where order_id = #{orderId}
    </update>

    <!--更新订单状态-->    
    <update id="updateStatus">
        update payment_order
        set status = #{status}, 
            channel_order_no = #{channelOrderNo},
            update_time = now()
        where order_id = #{orderId}
    </update>

    <!--逻辑删除-->    
    <update id="logicDeleteById">
        update payment_order
        set delete_flag = 1,
            update_by = #{updateBy},
            update_time = now()
        where order_id = #{orderId}
    </update>

    <!--物理删除-->    
    <delete id="deleteById">
        delete from payment_order
        where order_id = #{orderId}
    </delete>
</mapper>