<?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.web.modules.trading.mapper.AppDistributorOrderMapper">

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizTradeOrder" id="BizTradeOrderResult">
        <id     property="id"               column="id"/>
        <result property="orderNo"          column="order_no"/>
        <result property="supplierId"       column="supplier_id"/>
        <result property="supplierName"     column="supplier_name"/>
        <result property="distributorId"    column="distributor_id"/>
        <result property="distributorName"  column="distributor_name"/>
        <result property="totalHeads"       column="total_heads"/>
        <result property="totalAmount"      column="total_amount"/>
        <result property="orderStatus"      column="order_status"/>
        <result property="payVoucherUrl"    column="pay_voucher_url"/>
        <result property="payVoucherPath"   column="pay_voucher_path"/>
        <result property="finishTime"       column="finish_time"/>
        <result property="delFlag"          column="del_flag"/>
        <result property="createBy"         column="create_by"/>
        <result property="createTime"       column="create_time"/>
        <result property="updateBy"         column="update_by"/>
        <result property="updateTime"       column="update_time"/>
        <result property="remark"           column="remark"/>
    </resultMap>

    <sql id="selectVo">
        select o.id, o.order_no, o.supplier_id, s.supplier_name,
               o.distributor_id, d.distributor_name,
               o.total_heads, o.total_amount, o.order_status,
               o.pay_voucher_url, o.pay_voucher_path, o.finish_time,
               o.del_flag, o.create_by, o.create_time, o.update_by, o.update_time, o.remark
        from biz_trade_order o
        left join biz_supplier s on s.id = o.supplier_id and s.del_flag = '0'
        left join biz_distributor d on d.id = o.distributor_id and d.del_flag = '0'
    </sql>

    <select id="selectDistributorOrderList" resultMap="BizTradeOrderResult">
        <include refid="selectVo"/>
        where o.del_flag = '0'
          and o.distributor_id = #{distributorId}
        <if test="orderStatus != null and orderStatus != ''">
            and o.order_status = #{orderStatus}
        </if>
        order by o.create_time desc, o.id desc
    </select>

    <select id="selectDistributorOrderById" resultMap="BizTradeOrderResult">
        <include refid="selectVo"/>
        where o.del_flag = '0'
          and o.distributor_id = #{distributorId}
          and o.id = #{orderId}
    </select>

</mapper>
