<?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.newdatasource.NewDataSourceOrderDao">

    <resultMap id="OrderMap" type="com.zm.order.tool.server.model.Order">
        <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="total_amount" jdbcType="INTEGER" property="totalAmount"/>
        <result column="item_amount" jdbcType="INTEGER" property="itemAmount"/>
        <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="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="sub_status" jdbcType="INTEGER" property="subStatus"/>
        <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="new_features" jdbcType="VARCHAR" property="newFeatures"/>
        <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="invoice_status" jdbcType="INTEGER" property="invoiceStatus"/>
        <result column="mobile_cipher" jdbcType="VARCHAR" property="mobileCipher"/>
        <result column="master_data_source" jdbcType="INTEGER" property="masterDataSource" />
        <result column="income_amount" jdbcType="INTEGER" property="incomeAmount"/>
        <result column="seller_bear_amount" jdbcType="INTEGER" property="sellerBearAmount"/>
        <result column="commission_amount" jdbcType="INTEGER" property="commissionAmount"/>
        <result column="bill_at" jdbcType="TIMESTAMP" property="billAt"/>
        <result column="bill_info" jdbcType="VARCHAR" property="billInfo"/>
        <result column="features_cipher" jdbcType="VARCHAR" property="featuresCipher"/>
        <result column="performance_info_cipher" jdbcType="VARCHAR" property="performanceInfoCipher"/>
    </resultMap>

    <sql id="tb">
        orders
    </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,
        total_amount, item_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, seller_remarks, buyer_remarks, is_receipt, receipt_terminal,
        status, sub_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,
        invoice_status, currency, platform_user_id, mobile_cipher, master_data_source, new_features, income_amount, seller_bear_amount,
        commission_amount, bill_at, bill_info, features_cipher, performance_info_cipher
    </sql>

    <sql id="values">
        #{id}, #{bizType}, #{bizSubType}, #{orderNo}, #{orderType}, #{orderSubType}, #{sellerId},
        #{storeId}, #{storeOrderNo}, #{userId}, #{mobile}, #{addrId},
        #{source}, #{sourceNo}, #{fromSystem},
        #{totalAmount}, #{itemAmount}, #{packAmount}, #{freightAmount},
        #{discountAmount}, #{actualAmount}, #{payAmount},
        #{changeAmount}, #{payType}, #{payModel}, #{payer}, #{payNo}, #{thirdPayNo},
        #{payInfo}, #{isAlone}, #{invoiceType}, #{performanceType},
        #{isImmediatePerformance}, #{performancePlatform}, #{performanceInfo},
        #{sellerRemarks}, #{buyerRemarks}, #{isReceipt}, #{receiptTerminal}, #{status}, #{subStatus}, #{payStatus},
        #{afterSaleStatus}, #{receiveStatus}, #{performanceStatus}, 0, #{refusalReason},
        #{orderAt}, #{payAt}, #{commentAt}, #{completedAt}, #{receiveAt}, #{features}, #{itemInfo}, #{discountInfo},
        now(),now(), 0, #{syncFlag}, 0, #{payFlag}, #{payDetailInfo}, #{addrInfo}, #{refundFlag}, #{invoiceStatus},
        #{currency}, #{platformUserId},#{mobileCipher}, #{masterDataSource}, #{newFeatures},
        #{incomeAmount}, #{sellerBearAmount}, #{commissionAmount}, #{billAt}, #{billInfo}, #{featuresCipher}, #{performanceInfoCipher}
  </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>


    <update id="updateSubType" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        <set>
            order_sub_type = #{orderSubType}, updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <select id="selectById" parameterType="map" resultMap="OrderMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where id = #{id}
        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>


    <insert id="insert" parameterType="com.zm.order.tool.server.model.Order" keyColumn="id" keyProperty="id"
            useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        (<include refid="values"/>)
    </insert>

    <insert id="insertList" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="list" item="order" index="index" separator=",">
            (
            #{order.id}, #{order.bizType}, #{order.bizSubType}, #{order.orderNo}, #{order.orderType},
            #{order.orderSubType}, #{order.sellerId},
            #{order.storeId}, #{order.storeOrderNo}, #{order.userId}, #{order.mobile}, #{order.addrId},
            #{order.source}, #{order.sourceNo}, #{order.fromSystem},
            #{order.totalAmount}, #{order.itemAmount}, #{order.packAmount}, #{order.freightAmount},
            #{order.discountAmount}, #{order.actualAmount}, #{order.payAmount},
            #{order.changeAmount}, #{order.payType}, #{order.payModel}, #{order.payer}, #{order.payNo},
            #{order.thirdPayNo},
            #{order.payInfo}, #{order.isAlone}, #{order.invoiceType}, #{order.performanceType},
            #{order.isImmediatePerformance}, #{order.performancePlatform}, #{order.performanceInfo},
            #{order.sellerRemarks}, #{order.buyerRemarks}, #{order.isReceipt}, #{order.receiptTerminal},
            #{order.status}, #{order.subStatus}, #{order.payStatus},
            #{order.afterSaleStatus}, #{order.receiveStatus}, #{order.performanceStatus}, #{order.commentStatus},
            #{order.refusalReason},
            #{order.orderAt}, #{order.payAt}, #{order.commentAt}, #{order.completedAt}, #{order.receiveAt},
            #{order.features}, #{order.itemInfo}, #{order.discountInfo},
            #{order.createdAt},#{order.updatedAt}, #{order.isDeleted}, #{order.syncFlag},#{order.version},
            #{order.payFlag}, #{order.payDetailInfo},
            #{order.addrInfo}, #{order.refundFlag}, #{order.invoiceStatus},
            #{order.currency}, #{order.platformUserId},#{order.mobileCipher}, #{order.masterDataSource},
            #{order.newFeatures}, #{order.incomeAmount}, #{order.sellerBearAmount},
            #{order.commissionAmount}, #{order.billAt}, #{order.billInfo},
            #{order.featuresCipher}, #{order.performanceInfoCipher}
            )
        </foreach>
    </insert>

    <insert id="replace" parameterType="com.zm.order.tool.server.model.Order" >
        replace into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        value
        (
        #{id}, #{bizType}, #{bizSubType}, #{orderNo}, #{orderType}, #{orderSubType}, #{sellerId},
        #{storeId}, #{storeOrderNo}, #{userId}, #{mobile}, #{addrId},
        #{source}, #{sourceNo}, #{fromSystem},
        #{totalAmount}, #{itemAmount}, #{packAmount}, #{freightAmount},
        #{discountAmount}, #{actualAmount}, #{payAmount},
        #{changeAmount}, #{payType}, #{payModel}, #{payer}, #{payNo}, #{thirdPayNo},
        #{payInfo}, #{isAlone}, #{invoiceType}, #{performanceType},
        #{isImmediatePerformance}, #{performancePlatform}, #{performanceInfo},
        #{sellerRemarks}, #{buyerRemarks}, #{isReceipt}, #{receiptTerminal}, #{status}, #{subStatus}, #{payStatus},
        #{afterSaleStatus}, #{receiveStatus}, #{performanceStatus}, #{commentStatus}, #{refusalReason},
        #{orderAt}, #{payAt}, #{commentAt}, #{completedAt}, #{receiveAt}, #{features}, #{itemInfo}, #{discountInfo},
        #{createdAt},#{updatedAt}, #{isDeleted}, #{syncFlag},#{version}, #{payFlag}, #{payDetailInfo},
        #{addrInfo}, #{refundFlag}, #{invoiceStatus},
        #{currency}, #{platformUserId},#{mobileCipher}, #{masterDataSource}, #{newFeatures},
        #{incomeAmount}, #{sellerBearAmount}, #{commissionAmount}, #{billAt},
        #{billInfo}, #{featuresCipher}, #{performanceInfoCipher}
        )
    </insert>

    <insert id="replaceList" parameterType="list" keyColumn="id" keyProperty="id">
        replace into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="list" item="order" index="index" separator=",">
            (
            #{order.id}, #{order.bizType}, #{order.bizSubType}, #{order.orderNo}, #{order.orderType},
            #{order.orderSubType}, #{order.sellerId},
            #{order.storeId}, #{order.storeOrderNo}, #{order.userId}, #{order.mobile}, #{order.addrId},
            #{order.source}, #{order.sourceNo}, #{order.fromSystem},
            #{order.totalAmount}, #{order.itemAmount}, #{order.packAmount}, #{order.freightAmount},
            #{order.discountAmount}, #{order.actualAmount}, #{order.payAmount},
            #{order.changeAmount}, #{order.payType}, #{order.payModel}, #{order.payer}, #{order.payNo},
            #{order.thirdPayNo},
            #{order.payInfo}, #{order.isAlone}, #{order.invoiceType}, #{order.performanceType},
            #{order.isImmediatePerformance}, #{order.performancePlatform}, #{order.performanceInfo},
            #{order.sellerRemarks}, #{order.buyerRemarks}, #{order.isReceipt}, #{order.receiptTerminal},
            #{order.status}, #{order.subStatus}, #{order.payStatus},
            #{order.afterSaleStatus}, #{order.receiveStatus}, #{order.performanceStatus}, #{order.commentStatus},
            #{order.refusalReason},
            #{order.orderAt}, #{order.payAt}, #{order.commentAt}, #{order.completedAt}, #{order.receiveAt},
            #{order.features}, #{order.itemInfo}, #{order.discountInfo},
            #{order.createdAt},#{order.updatedAt}, #{order.isDeleted}, #{order.syncFlag},#{order.version},
            #{order.payFlag}, #{order.payDetailInfo},
            #{order.addrInfo}, #{order.refundFlag}, #{order.invoiceStatus},
            #{order.currency}, #{order.platformUserId},#{order.mobileCipher}, #{order.masterDataSource}, #{order.newFeatures},
            #{order.incomeAmount}, #{order.sellerBearAmount},
            #{order.commissionAmount}, #{order.billAt}, #{order.billInfo}, #{order.featuresCipher}, #{order.performanceInfoCipher}
            )
        </foreach>
    </insert>

    <select id="count" parameterType="map" resultType="int">
        select
        count(1)
        from
        <include refid="tb"/>
        <where>
            user_id = #{userId}
            <if test="orderNo != null and orderNo != ''">
                AND order_no = #{orderNo}
            </if>
            <if test="orderNoList != null and orderNoList.size > 0">
                AND order_no in
                <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                    #{orderNo}
                </foreach>
            </if>
            <if test="statusList != null">
                AND status in
                <foreach collection="statusList" item="status" index="index" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </if>
            <if test="orderTypeList != null">
                AND order_type in
                <foreach collection="orderTypeList" item="orderType" index="index" open="(" close=")" separator=",">
                    #{orderType}
                </foreach>
            </if>
            <if test="sellerId != null">
                AND seller_id = #{sellerId}
            </if>
            <if test="storeId != null">
                AND store_id = #{storeId}
            </if>
            <if test="payStatus != null">
                AND pay_status = #{payStatus}
            </if>
            <if test="performanceStatus != null">
                AND performance_status = #{performanceStatus}
            </if>
            <if test="orderAtStart != null">
                <![CDATA[ AND order_at >= #{orderAtStart} ]]>
            </if>
            <if test="orderAtEnd != null">
                <![CDATA[ AND order_at <= #{orderAtEnd} ]]>
            </if>
            <if test="operateFlag == 'paying'">
                AND status = 1 AND pay_status = 0
            </if>
            <if test="operateFlag == 'sending'">
                AND order_type = 3 AND status = 2 AND performance_status IN ( 0, 2 )
            </if>
            <if test="operateFlag == 'receiving'">
                AND order_type = 3 AND status = 2 AND performance_status = 1
            </if>
            <if test="operateFlag == 'takeFoodOrder'">
                AND ((order_type IN ( 1, 2, 6 ) AND status IN ( 2, 10, 12 ) ) OR ( order_type = 3 AND status IN ( 2 )))
            </if>
            <if test="operateFlag == 'payed'">
                AND pay_status = 1
            </if>
            <if test="operateFlag == 'takeFood'">
                AND status IN (10, 12)
            </if>
            <if test="operateFlag == 'completed'">
                AND pay_status = 5
            </if>
            <if test="operateFlag == 'comment'">
                AND status = 5 AND comment_status = 0
            </if>
            <if test="operateFlag == 'refunding'">
                AND status = 2 AND after_sale_status = 1
            </if>
            <if test="condition1Flag">
                AND ( ( `status` IN ( 3, 6) AND `pay_status` = 1 ) OR ( `status` NOT IN ( 3, 6 ) ) )
            </if>
            <if test="condition2Flag">
                AND ( `pay_status` = 1 )
            </if>
            <if test="condition3Flag">
                AND (( order_type = 5 AND pay_status = 1 ) OR ( order_type IN ( 1, 2, 3, 6)))
            </if>
            <if test="condition4Flag">
                AND ((order_sub_type = 2 and user_id = #{userId}) OR (order_sub_type != 2))
            </if>
            <if test="condition5Flag">
                AND (pay_status = 1 or status = 1)
            </if>
            <if test="deleteTypeList != null and deleteTypeList.size > 0">
                AND is_deleted in
                <foreach collection="deleteTypeList" item="deleteType" index="index" open="(" close=")" separator=",">
                    #{deleteType}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectList" parameterType="map" resultMap="OrderMap">
        select
        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,
        total_amount, item_amount, pack_amount, freight_amount, discount_amount, actual_amount, pay_amount,
        change_amount, pay_type, pay_model,  payer, pay_no, third_pay_no, is_alone, invoice_type,
        performance_type, is_immediate_performance, performance_platform,
        seller_remarks, buyer_remarks, is_receipt, receipt_terminal,
        status, sub_status, pay_status, after_sale_status, receive_status, performance_status, comment_status
        from
        <include refid="tb"/>
        <where>
            <if test="orderNo != null">
                order_no = #{orderNo}
            </if>
            <if test="orderNoList != null and orderNoList.size > 0">
                AND order_no in
                <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                    #{orderNo}
                </foreach>
            </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="deleteTypeList != null and deleteTypeList.size > 0">
                AND is_deleted in
                <foreach collection="deleteTypeList" item="deleteType" index="index" open="(" close=")" separator=",">
                    #{deleteType}
                </foreach>
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="userIdList != null and userIdList.size > 0">
                AND user_id in
                <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
        </where>
        <if test="lastId != null">
            order by id asc
        </if>
        <if test="updatedAtStart != null">
            order by updated_at asc
        </if>
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>


    <select id="selectListByOrderNosUserIds" parameterType="map" resultMap="OrderMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <choose>
                <when test="userIdList != null and userIdList.size > 0">
                    AND user_id in
                    <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
                        #{userId}
                    </foreach>
                </when>
                <otherwise>
                    AND user_id = #{userId}
                </otherwise>
            </choose>
            <if test="orderNoList != null and orderNoList.size > 0">
                AND order_no in
                <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                    #{orderNo}
                </foreach>
            </if>
            <if test="idList != null and idList.size > 0">
                AND id in
                <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>


    <update id="resetSyncFlag" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        set sync_flag = 0
        where id = #{id}
        and user_id = #{userId}
        AND version = #{version}
    </update>

    <update id="updateByIdsUserIds" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        set status = 8,
            updated_at = now(),
            version = version + 1
        where id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        and user_id in
        <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </update>

    <select id="selectOne" parameterType="map" resultMap="OrderMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            user_id = #{userId}
            <if test="orderNo != null and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            <if test="sellerId != null">
                and seller_id = #{sellerId}
            </if>
            <if test="sourceNo != null and sourceNo != ''">
                and source_no = #{sourceNo}
            </if>
            <if test="payNo != null and payNo != ''">
                and pay_no = #{payNo}
            </if>
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
        </where>

    </select>


    <update id="updateBizType" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        set features = #{features},
        updated_at = now()
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateBizType8" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        set biz_type = 8,
        updated_at = now()
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="update" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        <set>
            <if test="itemInfo != null and itemInfo != ''">
                item_info = #{itemInfo},
            </if>
            <if test="payDetailInfo != null and payDetailInfo != ''">
                pay_detail_info = #{payDetailInfo},
            </if>
            <if test="status != null and status != ''">
                status = #{status},
            </if>
            <if test="isDeleted != null">
                is_deleted = #{isDeleted},
            </if>
            <if test="completedAt != null">
                completed_at = #{completedAt},
            </if>
            <if test="buyerRemarks != null and buyerRemarks != ''">
                buyer_remarks = #{buyerRemarks},
            </if>
            <if test="refundFlag != null">
                refund_flag = #{refundFlag},
            </if>
            <if test="performanceStatus != null">
                performance_status = #{performanceStatus},
            </if>
            <if test="performanceInfo != null">
                performance_info = #{performanceInfo},
            </if>
            <if test="addrInfo != null">
                addr_info = #{addrInfo},
            </if>
            <if test="orderSubType != null">
                order_sub_type = #{orderSubType},
            </if>
            <if test="source != null">
                source = #{source},
            </if>
            <if test="afterSaleStatus != null">
                after_sale_status = #{afterSaleStatus},
            </if>
            <if test="receiveStatus != null">
                receive_status = #{receiveStatus},
            </if>
            <if test="performanceStatus != null">
                performance_status = #{performanceStatus},
            </if>
            <if test="payStatus != null">
                pay_status = #{payStatus},
            </if>
            <if test="addrInfo != null">
                addr_info = #{addrInfo},
            </if>
            <if test="features != null">
                features = #{features},
            </if>
            <if test="payType != null">
                pay_type = #{payType},
            </if>
            <if test="payModel != null">
                pay_model = #{payModel},
            </if>
            <if test="payer != null">
                payer = #{payer},
            </if>
            <if test="payAmount != null">
                pay_amount = #{payAmount},
            </if>
            <if test="payInfo != null">
                pay_info = #{payInfo},
            </if>
            <if test="payNo != null">
                pay_no = #{payNo},
            </if>
            <if test="invoiceStatus != null">
                invoice_status = #{invoiceStatus},
            </if>
            <if test="thirdPayNo != null and thirdPayNo != ''">
                third_pay_no = #{thirdPayNo},
            </if>
            <if test="payAt != null">
                pay_at = #{payAt},
            </if>
            <if test="commentAt != null">
                comment_at = #{commentAt},
            </if>
            <if test="storeOrderNo != null">
                store_order_no = #{storeOrderNo},
            </if>
            <if test="changeAmount != null">
                change_amount = #{changeAmount},
            </if>
            <if test="performanceType != null">
                performance_type = #{performanceType},
            </if>
            <if test="isAlone != null">
                is_alone = #{isAlone},
            </if>
            <if test="refusalReason != null">
                refusal_reasons = #{refusalReason},
            </if>
            <if test="performancePlatform != null and performancePlatform != ''">
                performance_platform = #{performancePlatform},
            </if>
            <if test="performanceInfo != null and performanceInfo != '' ">
                performance_info = #{performanceInfo},
            </if>
            <if test="isReceipt != null">
                is_receipt = #{isReceipt},
            </if>
            <if test="receiptTerminal != null and receiptTerminal != ''">
                receipt_terminal = #{receiptTerminal},
            </if>
            <if test="totalAmount != null">
                total_amount = #{totalAmount},
            </if>
            <if test="packAmount != null">
                pack_amount = #{packAmount},
            </if>
            <if test="itemAmount != null">
                item_amount = #{itemAmount},
            </if>
            <if test="freightAmount != null">
                freight_amount = #{freightAmount},
            </if>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            <if test="actualAmount != null">
                actual_amount = #{actualAmount},
            </if>
            <if test="receiveAt != null">
                receive_at = #{receiveAt},
            </if>
            <if test="commentStatus != null">
                comment_status = #{commentStatus},
            </if>
            <if test="features != null and features != ''">
                features = #{features},
            </if>
            <if test="discountInfo != null and discountInfo != ''">
                discount_info = #{discountInfo},
            </if>
            <if test="sellerRemarks != null and sellerRemarks != ''">
                seller_remarks = #{sellerRemarks},
            </if>
            <if test="mobile != null and mobile != ''">
                mobile = #{mobile},
            </if>
            <if test="syncFlag != null">
                sync_flag = #{syncFlag},
            </if>
            <if test="payFlag != null">
                pay_flag = #{payFlag},
            </if>
            <if test="subStatus != null">
                sub_status = #{subStatus},
            </if>
            <if test="sourceNo != null and sourceNo != ''">
                source_no = #{sourceNo},
            </if>
            version = version + 1,
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateId" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        <set>
            id = #{updateId}, updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>

    <update id="updateFeatures" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        <set>
            <if test="features != null and features != ''">
                features = #{features},
            </if>
            version = version + 1,
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateDiscountAmount" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        <set>
            <if test="discountAmount != null and discountAmount != ''">
                discount_amount = #{discountAmount},
            </if>
            sync_flag = 1,
            version = version + 1,
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateDeleted" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            is_deleted = #{isDeleted},
            updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>

    <update id="updateStoreId" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        <set>
            store_id = #{storeId},
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>


    <update id="updateItemAndDiscount" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        <set>
            <if test="itemInfo != null and itemInfo != ''">
                item_info = #{itemInfo},
            </if>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            <if test="totalAmount != null">
                total_amount = #{totalAmount},
            </if>
            <if test="payAmount != null">
                pay_amount = #{payAmount},
            </if>
            <if test="actualAmount != null">
                actual_amount = #{actualAmount},
            </if>
            <if test="discountInfo != null and discountInfo != ''">
                discount_info = #{discountInfo},
            </if>
            <if test="payDetailInfo != null and payDetailInfo != ''">
                pay_detail_info = #{payDetailInfo},
            </if>
            version = version + 1,
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

  <update id="updateCompletedAt" parameterType="map">
        update
        <include refid="tb"/>
        set
        status = 5,
        sync_flag =1,
        completed_at =updated_at,
        updated_at = now()
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateUserId" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            user_id = #{newUserId},
            is_deleted = 0,
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateUpdated" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>

    <update id="updatePayDetail" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            pay_detail_info = #{payDetailInfo}, updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updatePayInfo" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            pay_info = #{payInfo}
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updatePayDetailInfo" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            pay_detail_info = #{payDetailInfo}
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="delete" parameterType="map">
        update
        <include refid="tb"/>
        set is_deleted = #{deleteType},
        updated_at = now()
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updatePayType" parameterType="map">
        update
        <include refid="tb"/>
        set pay_type = #{payType},
        updated_at = now()
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateItemInfo" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            <if test="itemInfo != null">
                item_info = #{itemInfo},
            </if>
            <if test="payDetailInfo != null">
                pay_detail_info = #{payDetailInfo},
            </if>
            <if test="discountInfo != null">
                discount_info = #{discountInfo},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="dataUpdate" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="afterSaleStatus != null">
                after_sale_status = #{afterSaleStatus},
            </if>
            <if test="buyerRemarks != null">
                buyer_remarks = #{buyerRemarks},
            </if>
            <if test="receiveStatus != null">
                receive_status = #{receiveStatus},
            </if>
            <if test="performanceStatus != null">
                performance_status = #{performanceStatus},
            </if>
            <if test="payStatus != null">
                pay_status = #{payStatus},
            </if>
            <if test="payType != null">
                pay_type = #{payType},
            </if>
            <if test="payAmount != null">
                pay_amount = #{payAmount},
            </if>
            <if test="payNo != null">
                pay_no = #{payNo},
            </if>
            <if test="thirdPayNo != null and thirdPayNo != ''">
                third_pay_no = #{thirdPayNo},
            </if>
            <if test="payAt != null">
                pay_at = #{payAt},
            </if>
            <if test="completedAt != null">
                completed_at = #{completedAt},
            </if>
            <if test="storeOrderNo != null">
                store_order_no = #{storeOrderNo},
            </if>
            <if test="changeAmount != null">
                change_amount = #{changeAmount},
            </if>
            <if test="performanceType != null">
                performance_type = #{performanceType},
            </if>
            <if test="totalAmount != null">
                total_amount = #{totalAmount},
            </if>

            <if test="packAmount != null">
                pack_amount = #{packAmount},
            </if>
            <if test="itemAmount != null">
                item_amount = #{itemAmount},
            </if>
            <if test="freightAmount != null">
                freight_amount = #{freightAmount},
            </if>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            <if test="actualAmount != null">
                actual_amount = #{actualAmount},
            </if>
            <if test="commentStatus != null">
                comment_status = #{commentStatus},
            </if>
            <if test="sourceNo != null and sourceNo != ''">
                source_no = #{sourceNo},
            </if>
            <if test="isDeleted != null">
                is_deleted = #{isDeleted},
            </if>
            <if test="payDetailInfo != null and payDetailInfo != ''">
                pay_detail_info = #{payDetailInfo},
            </if>
            version = version + 1,
            updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>

    <update id="businessOrderUpdate" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            <if test="payType != null">
                pay_type = #{payType},
            </if>
            <if test="payer != null">
                payer = #{payer},
            </if>
            <if test="payNo != null">
                pay_no = #{payNo},
            </if>
            <if test="thirdPayNo != null">
                third_pay_no = #{thirdPayNo},
            </if>
            <if test="payInfo != null">
                pay_info = #{payInfo},
            </if>
            <if test="payDetailInfo != null">
                pay_detail_info = #{payDetailInfo},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="payStatus != null">
                pay_status = #{payStatus},
            </if>
            <if test="payAt != null">
                pay_at = #{payAt},
            </if>
            <if test="completedAt != null">
                completed_at = #{completedAt},
            </if>
            <if test="performanceStatus != null">
                performance_status = #{performanceStatus},
            </if>
            <if test="performanceType != null">
                performance_type = #{performanceType},
            </if>
            <if test="buyerRemarks != null">
                buyer_remarks = #{buyerRemarks},
            </if>
            <if test="sellerRemarks != null and sellerRemarks != ''">
                seller_remarks = #{sellerRemarks},
            </if>
            <if test="features != null and features != ''">
                features = #{features},
            </if>
            <if test="storeId != null">
                store_id = #{storeId},
            </if>
            <if test="totalAmount != null">
                total_amount = #{totalAmount},
            </if>
            <if test="packAmount != null">
                pack_amount = #{packAmount},
            </if>
            <if test="itemAmount != null">
                item_amount = #{itemAmount},
            </if>
            <if test="freightAmount != null">
                freight_amount = #{freightAmount},
            </if>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            <if test="actualAmount != null">
                actual_amount = #{actualAmount},
            </if>
            <if test="payAmount != null">
                pay_amount = #{payAmount},
            </if>
            version = version + 1,
            updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>

    <update id="orderDataUpdate" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="afterSaleStatus != null">
                after_sale_status = #{afterSaleStatus},
            </if>
            <if test="buyerRemarks != null">
                buyer_remarks = #{buyerRemarks},
            </if>
            <if test="sellerRemarks != null">
                seller_remarks = #{sellerRemarks},
            </if>
            <if test="orderType != null">
                order_type = #{orderType},
            </if>
            <if test="orderSubType != null">
                order_sub_type = #{orderSubType},
            </if>
            <if test="storeId != null">
                store_id = #{storeId},
            </if>
            <if test="sellerId != null">
                seller_id = #{sellerId},
            </if>
            <if test="receiveStatus != null">
                receive_status = #{receiveStatus},
            </if>
            <if test="performanceStatus != null">
                performance_status = #{performanceStatus},
            </if>
            <if test="payStatus != null">
                pay_status = #{payStatus},
            </if>
            <if test="payType != null">
                pay_type = #{payType},
            </if>
            <if test="payAmount != null">
                pay_amount = #{payAmount},
            </if>
            <if test="payNo != null">
                pay_no = #{payNo},
            </if>
            <if test="thirdPayNo != null">
                third_pay_no = #{thirdPayNo},
            </if>
            <if test="payAt != null">
                pay_at = #{payAt},
            </if>
            <if test="completedAt != null">
                completed_at = #{completedAt},
            </if>
            <if test="storeOrderNo != null">
                store_order_no = #{storeOrderNo},
            </if>
            <if test="changeAmount != null">
                change_amount = #{changeAmount},
            </if>
            <if test="performanceType != null">
                performance_type = #{performanceType},
            </if>
            <if test="totalAmount != null">
                total_amount = #{totalAmount},
            </if>

            <if test="packAmount != null">
                pack_amount = #{packAmount},
            </if>
            <if test="itemAmount != null">
                item_amount = #{itemAmount},
            </if>
            <if test="freightAmount != null">
                freight_amount = #{freightAmount},
            </if>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            <if test="actualAmount != null">
                actual_amount = #{actualAmount},
            </if>
            <if test="commentStatus != null">
                comment_status = #{commentStatus},
            </if>
            <if test="sourceNo != null">
                source_no = #{sourceNo},
            </if>
            <if test="isDeleted != null">
                is_deleted = #{isDeleted},
            </if>
            <if test="payDetailInfo != null and payDetailInfo != ''">
                pay_detail_info = #{payDetailInfo},
            </if>
            <if test="features != null and features != ''">
                features = #{features},
            </if>
            <if test="payInfo != null and payInfo != ''">
                pay_info = #{payInfo},
            </if>
            version = version + 1,
            updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>

    <update id="deleteTemp" parameterType="com.zm.order.tool.server.model.Order">
        delete
        from
        <include refid="tb"/>
        where
        user_id = #{userId}
        AND order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </update>

    <update id="deleteByIdUserId" parameterType="com.zm.order.tool.server.model.Order">
        delete
        from
        <include refid="tb"/>
        where
        user_id in
        <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        AND id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <update id="updateDiscountInfo" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        <set>
            <if test="discountInfo != null and discountInfo != ''">
                discount_info = #{discountInfo},
            </if>
            version = version + 1,
            updated_at = now()
        </set>
        where order_no = #{orderNo}
        and user_id = #{userId}
    </update>

    <update id="updateAddrInfo" parameterType="com.zm.order.tool.server.model.Order">
        update
        <include refid="tb"/>
        <set>
            addr_info = #{addrInfo},
            version = version + 1,
            updated_at = now()
        </set>
        where order_no = #{orderNo}
        and user_id = #{userId}
    </update>

    <update id="realDelete" parameterType="map">
        delete
        from
        <include refid="tb"/>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="deleteByOrderNos" parameterType="com.zm.order.tool.server.model.Order">
        delete
        from
        <include refid="tb"/>
        where
        user_id in
        <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        AND order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </update>

    <update id="updateMasterDataSource" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            master_data_source = #{masterDataSource}, updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>
</mapper>