<?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.zfire.mall.manager.commonmapper.record.order.exec.OrderExecRecordReserveMapper">

    <select id="v2RetailListPage"
            resultType="com.zfire.mall.manager.bean.record.order.exec.v2.RetailOrderExecRecordVO">
        SELECT
            a.*,
            b.sale_type_code,
            b.sale_type_name,
            b.service_name,
            b.material_old_number,
            b.material_code,
            b.material_name,
            b.specification,
            b.unit,
            b.qty * b.direction AS qty,
            b.price,
            b.disc_amount * b.direction AS disc_amount,
            b.total_amount * b.direction AS total_amount,
            b.customer_wallet_id,
            b.customer_wallet_name,
            b.customer_wallet_id2,
            b.customer_wallet_name2,
            b.total_disc_amount * b.direction AS total_disc_amount,
            b.pay_amount * b.direction AS pay_amount,
            b.pay_rebate_amount * b.direction AS pay_rebate_amount
        FROM
            retail_order a  JOIN order_exec_record b ON a.id = b.ref_order_id
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.id DESC, b.ref_order_item_id, b.create_time
        </if>
        ${ex.orderBy}
    </select>
    <select id="v2TradeEExecListPage"
            resultType="com.zfire.mall.manager.bean.record.order.exec.v2.TradeEOrderExecRecordVO">
        SELECT
            a.*,
            b.sale_type_code AS itemSaleTypeCode,
            b.sale_type_name AS itemSaleTypeName,
            b.service_name AS itemServiceName,
            b.material_old_number,
            b.material_code,
            b.material_name,
            b.specification,
            b.unit,
            b.qty * b.direction AS qty,
            b.price,
            b.disc_amount * b.direction AS disc_amount,
            b.total_amount * b.direction AS total_amount,
            b.customer_wallet_id,
            b.customer_wallet_name,
            b.customer_wallet_id2,
            b.customer_wallet_name2,
            b.total_disc_amount * b.direction AS total_disc_amount,
            b.pay_amount * b.direction AS pay_amount,
            b.pay_rebate_amount * b.direction AS pay_rebate_amount
        FROM
            engin_order a  JOIN order_exec_record b ON a.engin_order_no = b.ref_order_id
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.engin_order_id DESC, b.ref_order_item_id, b.create_time
        </if>
        ${ex.orderBy}
    </select>
    <select id="v2ExecListPage"
            resultType="com.zfire.mall.manager.bean.record.order.exec.v2.OrderExecRecordVO">
        SELECT
        a.*,
        a.qty * a.direction AS item_qty,
        a.pay_amount * a.direction AS item_pay_amount,
        a.pay_rebate_amount * a.direction AS item_pay_rebate_amount,
        a.disc_amount * a.direction AS item_disc_amount,
        a.total_amount * a.direction AS item_total_amount,
        a.total_disc_amount * a.direction AS item_total_disc_amount,
        kdm.material_group_name
        FROM
            order_exec_record a
            JOIN customer_product_type_rela c ON c.customer_wallet_id = a.customer_wallet_id
            JOIN king_dee_material kdm ON a.material_id = kdm.id
        ${ex.query}
        <if test="serviceIds != null and serviceIds.size > 0">
            AND c.service_id IN
            <foreach item="item" index="index" collection="serviceIds" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC, a.ref_order_id DESC, a.sale_type_id, a.material_id
        </if>
        ${ex.orderBy}
    </select>
</mapper>