<?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.trade.service.salesorder.mapper.SalesOrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.trade.service.salesorder.domain.SalesOrder">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="customer_user_id" property="customerUserId" />
        <result column="order_no" property="orderNo" />
        <result column="importer_id" property="importerId" />
        <result column="sale_contract_id" property="saleContractId" />
        <result column="amount" property="amount" />
        <result column="status" property="status" />
        <result column="gmt_create" property="gmtCreate" />
        <result column="gmt_modified" property="gmtModified" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        customer_user_id, order_no, importer_id, sale_contract_id, amount, status, gmt_create, gmt_modified
    </sql>
    <select id="queryListBySearch" resultType="com.trade.service.salesorder.vo.SalesOrderVO">
        SELECT
        a.id,
        a.deleted,
        a.customer_user_id, a.order_no, a.importer_id, a.sale_contract_id, a.amount, a.status, a.gmt_create, a.gmt_modified,
        b.contract_no,
        c.payment_way,
        c.payment_currency,
        c.settlement_cycle,
        d.entity_name AS importer_name,
        e.identifier_no AS customer_identifier_no,
        f.name AS enterprise_name
        FROM
            sales_order a
                LEFT JOIN sales_contract b ON a.sale_contract_id = b.id
        LEFT JOIN contract_terms c ON b.contract_no = b.contract_no
        LEFT JOIN importer_entity d ON a.importer_id = d.id
        LEFT JOIN customer_user e ON a.customer_user_id = e.id
        LEFT JOIN enterprise f ON a.customer_user_id = f.customer_user_id
        WHERE
         a.deleted = 0
            <if test="customerUserId!= null">
                AND a.customer_user_id = #{customerUserId}
            </if>
            <if test="orderNo!= null and orderNo!= ''">
                AND a.order_no = #{orderNo}
            </if>
            <if test="paymentWay!= null and paymentWay!= ''">
                AND c.payment_way = #{paymentWay}
            </if>
            <if test="importerId!= null and importerId!= ''">
                AND a.importer_id = #{importerId}
            </if>
            <if test="contractNo!= null and contractNo!= ''">
                AND b.contract_no = #{contractNo}
            </if>
            <if test="status!= null and status!= ''">
                AND status = #{status}
            </if>
            <if test="createdTimeStart!= null and createdTimeStart!= ''">
                AND a.gmt_create &gt;= #{createdTimeStart}
            </if>
            <if test="createdTimeEnd!= null and createdTimeEnd!= ''">
                AND a.gmt_create &lt;= #{createdTimeEnd}
            </if>
            <if test="userName!= null and userName!= ''">
                AND e.identifier_no = #{userName} or f.name like CONCAT('%',#{userName},'%')
            </if>
            ORDER BY a.id DESC
    </select>

</mapper>
