<?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.zm.order.tool.server.routing.RoutingOrderDao">

    <resultMap id="OrderMap" type="com.zm.order.tool.server.model.RoutingOrder">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="biz_type" jdbcType="INTEGER" property="bizType"/>
        <result column="biz_sub_type" jdbcType="INTEGER" property="bizSubType"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="order_type" jdbcType="INTEGER" property="orderType"/>
        <result column="order_sub_type" jdbcType="INTEGER" property="orderSubType"/>
        <result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
        <result column="store_id" jdbcType="BIGINT" property="storeId"/>
        <result column="store_order_no" jdbcType="VARCHAR" property="storeOrderNo"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="addr_id" jdbcType="INTEGER" property="addrId"/>
        <result column="source" jdbcType="INTEGER" property="source"/>
        <result column="from_system" jdbcType="INTEGER" property="fromSystem"/>
        <result column="source_no" jdbcType="VARCHAR" property="sourceNo"/>
        <result column="source_info" jdbcType="VARCHAR" property="sourceInfo"/>
        <result column="total_amount" jdbcType="INTEGER" property="totalAmount"/>
        <result column="item_amount" jdbcType="INTEGER" property="itemAmount"/>
        <result column="cost_amount" jdbcType="INTEGER" property="costAmount"/>
        <result column="pack_amount" jdbcType="INTEGER" property="packAmount"/>
        <result column="freight_amount" jdbcType="INTEGER" property="freightAmount"/>
        <result column="discount_amount" jdbcType="INTEGER" property="discountAmount"/>
        <result column="actual_amount" jdbcType="INTEGER" property="actualAmount"/>
        <result column="pay_amount" jdbcType="INTEGER" property="payAmount"/>
        <result column="change_amount" jdbcType="INTEGER" property="changeAmount"/>
        <result column="pay_type" jdbcType="INTEGER" property="payType"/>
        <result column="pay_model" jdbcType="INTEGER" property="payModel"/>
        <result column="payer" jdbcType="BIGINT" property="payer"/>
        <result column="pay_no" jdbcType="VARCHAR" property="payNo"/>
        <result column="third_pay_no" jdbcType="VARCHAR" property="thirdPayNo"/>
        <result column="pay_info" jdbcType="VARCHAR" property="payInfo"/>
        <result column="is_alone" jdbcType="INTEGER" property="isAlone"/>
        <result column="invoice_type" jdbcType="INTEGER" property="invoiceType"/>
        <result column="performance_type" jdbcType="INTEGER" property="performanceType"/>
        <result column="is_immediate_performance" jdbcType="INTEGER" property="isImmediatePerformance"/>
        <result column="performance_platform" jdbcType="VARCHAR" property="performancePlatform"/>
        <result column="performance_info" jdbcType="VARCHAR" property="performanceInfo"/>
        <result column="activity_info" jdbcType="VARCHAR" property="activityInfo"/>
        <result column="seller_remarks" jdbcType="VARCHAR" property="sellerRemarks"/>
        <result column="buyer_remarks" jdbcType="VARCHAR" property="buyerRemarks"/>
        <result column="is_receipt" jdbcType="INTEGER" property="isReceipt"/>
        <result column="receipt_terminal" jdbcType="VARCHAR" property="receiptTerminal"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="pay_status" jdbcType="INTEGER" property="payStatus"/>
        <result column="after_sale_status" jdbcType="INTEGER" property="afterSaleStatus"/>
        <result column="receive_status" jdbcType="INTEGER" property="receiveStatus"/>
        <result column="performance_status" jdbcType="INTEGER" property="performanceStatus"/>
        <result column="comment_status" jdbcType="INTEGER" property="commentStatus"/>
        <result column="refusal_reasons" jdbcType="VARCHAR" property="refusalReason"/>
        <result column="order_at" jdbcType="TIMESTAMP" property="orderAt"/>
        <result column="pay_at" jdbcType="TIMESTAMP" property="payAt"/>
        <result column="comment_at" jdbcType="TIMESTAMP" property="commentAt"/>
        <result column="completed_at" jdbcType="TIMESTAMP" property="completedAt"/>
        <result column="receive_at" jdbcType="TIMESTAMP" property="receiveAt"/>
        <result column="item_info" jdbcType="VARCHAR" property="itemInfo"/>
        <result column="discount_info" jdbcType="VARCHAR" property="discountInfo"/>
        <result column="features" jdbcType="VARCHAR" property="features"/>
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
        <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
        <result column="sync_flag" jdbcType="INTEGER" property="syncFlag"/>
        <result column="version" jdbcType="INTEGER" property="version"/>
        <result column="pay_flag" jdbcType="INTEGER" property="payFlag"/>
        <result column="pay_detail_info" jdbcType="VARCHAR" property="payDetailInfo"/>
        <result column="addr_info" jdbcType="VARCHAR" property="addrInfo"/>
        <result column="refund_flag" jdbcType="VARCHAR" property="refundFlag"/>
        <result column="db_index" jdbcType="INTEGER" property="dbIndex"/>
        <result column="tb_index" jdbcType="INTEGER" property="tbIndex"/>
        <result column="db_index" jdbcType="INTEGER" property="dbIndex"/>
        <result column="tb_index" jdbcType="INTEGER" property="tbIndex"/>
        <result column="sync_at" jdbcType="TIMESTAMP" property="syncAt"/>
    </resultMap>

    <sql id="tb">
        orders_0
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        biz_type, biz_sub_type, order_no, order_type, order_sub_type,
        seller_id, store_id, store_order_no, user_id, mobile, addr_id, source, source_no, from_system, source_info,
        total_amount, item_amount, cost_amount, pack_amount, freight_amount, discount_amount, actual_amount, pay_amount,
        change_amount, pay_type, pay_model,  payer, pay_no, third_pay_no, pay_info, is_alone, invoice_type, performance_type, is_immediate_performance, performance_platform,
        performance_info, activity_info, seller_remarks, buyer_remarks, is_receipt, receipt_terminal,
        status, pay_status, after_sale_status, receive_status, performance_status, comment_status, refusal_reasons, order_at, pay_at, comment_at, completed_at, receive_at, features,
        item_info, discount_info,
        created_at, updated_at, is_deleted, sync_flag, version, pay_flag, pay_detail_info,
        addr_info, refund_flag, db_index, tb_index, sync_at
    </sql>

    <sql id="values">
        #{id}, #{bizType}, #{bizSubType}, #{orderNo}, #{orderType}, #{orderSubType}, #{sellerId},
        #{storeId}, #{storeOrderNo}, #{userId}, #{mobile}, #{addrId},
        #{source}, #{sourceNo}, #{fromSystem}, #{sourceInfo},
        #{totalAmount}, #{itemAmount}, #{costAmount}, #{packAmount}, #{freightAmount},
        #{discountAmount}, #{actualAmount}, #{payAmount},
        #{changeAmount}, #{payType}, #{payModel}, #{payer}, #{payNo}, #{thirdPayNo},
        #{payInfo}, #{isAlone}, #{invoiceType}, #{performanceType},
        #{isImmediatePerformance}, #{performancePlatform}, #{performanceInfo}, #{activityInfo},
        #{sellerRemarks}, #{buyerRemarks}, #{isReceipt}, #{receiptTerminal}, #{status}, #{payStatus},
        #{afterSaleStatus}, #{receiveStatus}, #{performanceStatus}, #{commentStatus}, #{refusalReason},
        #{orderAt}, #{payAt}, #{commentAt}, #{completedAt}, #{receiveAt}, #{features}, #{itemInfo}, #{discountInfo},
        #{createdAt},#{updatedAt}, #{isDeleted}, #{syncFlag}, #{version}, #{payFlag}, #{payDetailInfo}, #{addrInfo}, #{refundFlag}
  </sql>


    <select id="selectByOrderNo" parameterType="map" resultMap="OrderMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no = #{orderNo}
        and user_id = #{userId}
        limit 1
    </select>

    <select id="selectByUserId" parameterType="map" resultMap="OrderMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where
        user_id = #{userId}
    </select>

    <select id="selectList" parameterType="map" resultMap="OrderMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="bizType != null">
                and biz_type = #{bizType}
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="orderNo != null">
                and order_no = #{orderNo}
            </if>
            <if test="sellerId != null">
                and seller_id = #{sellerId}
            </if>
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
            <if test="maxId != null">
                <![CDATA[ AND id < #{maxId} ]]>
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at < #{updatedAtEnd} ]]>
            </if>
            <if test="orderAtStart != null">
                <![CDATA[ AND order_at >= #{orderAtStart} ]]>
            </if>
            <if test="orderAtEnd != null">
                <![CDATA[ AND order_at <= #{orderAtEnd} ]]>
            </if>
            <if test="syncFlag != null">
                and sync_flag = #{syncFlag}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="dbIndex != null">
                and db_index = #{dbIndex}
            </if>
            <if test="tbIndex != null">
                and tb_index = #{tbIndex}
            </if>
            <if test="routed != null">
                and routed = #{routed}
            </if>
        </where>
        order by id
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

</mapper>