<?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.trade.purchaseorder.mapper.PurchaseOrderMapper">

    <resultMap id="BaseResultMap" type="com.trade.service.trade.purchaseorder.domain.PurchaseOrder">
        <id column="amount_total" property="amountTotal"/>
        <id column="currency" property="currency"/>
        <id column="customer_user_id" property="customerUserId"/>
        <id column="deleted" property="deleted"/>
        <id column="order_no" property="orderNo"/>
        <id column="entity_no" property="entityNo"/>
        <id column="agency_cert_no" property="agencyCertNo"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="gmt_modified" property="gmtModified"/>
        <id column="id" property="id"/>
        <id column="invoice_type" property="invoiceType"/>
        <id column="purchase_contract_no" property="purchaseContractNo"/>
        <id column="sales_order_id" property="salesOrderId"/>
        <id column="tax_amount_total" property="taxAmountTotal"/>
        <id column="settlement_cycle" property="settlementCycle"/>
        <id column="invoice_pull_status" property="invoicePullStatus"/>
    </resultMap>

    <resultMap id="VoResultMap" type="com.trade.service.trade.purchaseorder.vo.PurchaseOrderVo">
        <id column="amount_total" property="amountTotal"/>
        <id column="currency" property="currency"/>
        <id column="customer_user_id" property="customerUserId"/>
        <id column="order_no" property="purchaseOrderNo"/>
        <id column="entity_no" property="vendorEntityNo"/>
        <id column="agency_cert_no" property="agencyCertNo"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="id" property="id"/>
        <id column="invoice_type" property="invoiceType"/>
        <id column="purchase_contract_no" property="purchaseContractNo"/>
        <id column="sales_order_no" property="salesOrderNo"/>
        <id column="tax_amount_total" property="taxAmountTotal"/>
        <id column="enterprise_name" property="enterpriseName"/>
        <id column="customer_identifier_no" property="customerIdentifierNo"/>
        <id column="entity_name" property="vendorEntityName"/>
        <id column="platform_name" property="agencyEnterpriseName"/>
        <id column="invoice_pull_status" property="invoicePullStatus"/>
    </resultMap>

    <resultMap id="VoDetailsResultMap" type="com.trade.service.trade.purchaseorder.vo.PurchaseOrderDetailsVo">
        <id column="amount_total" property="amountTotal"/>
        <id column="customer_user_id" property="customerUserId"/>
        <id column="currency" property="currency"/>
        <id column="order_no" property="purchaseOrderNo"/>
        <id column="purchase_contract_no" property="purchaseContractNo"/>
        <id column="sales_order_no" property="salesOrderNo"/>
        <id column="tax_amount_total" property="taxAmountTotal"/>
        <id column="entity_name" property="vendorEntityName"/>
        <id column="platform_name" property="agencyEnterpriseName"/>
        <id column="settlement_cycle" property="settlementCycle"/>
        <id column="contract_name" property="purchaseContractName"/>
        <id column="enterprise_name" property="enterpriseName"/>
        <id column="customer_identifier_no" property="customerIdentifierNo"/>
    </resultMap>

    <resultMap id="VoInvoiceResultMap" type="com.trade.service.trade.purchaseorder.vo.TradingPlatformInvoiceVo">
        <id column="id" property="orderId"/>
        <id column="order_no" property="orderNo"/>
        <id column="amount_total" property="amountTotal"/>
        <id column="tax_num" property="taxNum"/>
        <id column="xt_user_id" property="xtUserId"/>
        <id column="xt_eid" property="xtEid"/>
        <id column="customer_user_id" property="customerUserId"/>
        <id column="cert_no" property="certNo"/>
    </resultMap>
    <sql id="Base_Column_List">
        amount_total
        , currency, customer_user_id, deleted, order_no, entity_no, agency_cert_no, gmt_create, gmt_modified, id, invoice_type, purchase_contract_no,
        sales_order_id, tax_amount_total, settlement_cycle, invoice_pull_status
    </sql>

    <select id="selectByCondition"
            parameterType="com.trade.service.trade.purchaseorder.dto.PurchaseSearchDto"
            resultMap="VoResultMap">
        select
        t.amount_total, t.currency, t.customer_user_id, t.order_no, t.entity_no, t.agency_cert_no, t.gmt_create,
        t.id, t.invoice_type, t.purchase_contract_no, t.tax_amount_total, t.invoice_pull_status, e.name AS
        enterprise_name, e.customer_identifier_no,
        v.entity_name, p.platform_name, s.order_no as sales_order_no
        from purchase_order t left join enterprise e on t.customer_user_id = e.customer_id
        left join vendor_entity v on v.entity_no = t.entity_no
        left join trading_platform p on t.agency_cert_no = p.cert_no
        left join sales_order s on t.sales_order_id = s.id
        <where>
            t.deleted = 0
            <if test="customerUserId != null">
                AND t.customer_user_id = #{customerUserId}
            </if>
            <if test="purchaseOrderNo != null and purchaseOrderNo != ''">
                AND t.order_no = #{purchaseOrderNo}
            </if>
            <if test="salesOrderNo != null and salesOrderNo != ''">
                AND s.order_no = #{salesOrderNo}
            </if>
            <if test="purchaseContractNo != null and purchaseContractNo != ''">
                AND t.purchase_contract_no = #{purchaseContractNo}
            </if>
            <if test="customerUserInfo != null and customerUserInfo != ''">
                AND e.name LIKE CONCAT ('%',#{customerUserInfo},'%') OR e.customer_identifier_no = #{customerUserInfo}
            </if>
            <if test="vendorEntityNo != null and vendorEntityNo != ''">
                AND t.entity_no = #{vendorEntityNo}
            </if>
            <if test="agencyCertNo != null and agencyCertNo != ''">
                AND t.agency_cert_no = #{agencyCertNo}
            </if>
            <if test="point == 0 ">
                AND (t.agency_cert_no is not null and t.agency_cert_no != '')
            </if>
            <if test="timeStart != null">
                AND t.gmt_create <![CDATA[ >= ]]> #{timeStart}
            </if>
            <if test="timeEnd != null">
                AND t.gmt_create <![CDATA[ <= ]]> #{timeEnd}
            </if>
            <if test="null != customerUserIdList and customerUserIdList.size()>0">
                AND t.customer_user_id IN
                <foreach collection="customerUserIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY t.gmt_create DESC
    </select>
    <select id="selectDetailsById" resultMap="VoDetailsResultMap">
        select t.amount_total,
               t.currency,
               t.order_no,
               t.customer_user_id,
               t.purchase_contract_no,
               t.tax_amount_total,
               e.name     as enterprise_name,
               e.customer_identifier_no,
               t.settlement_cycle,
               v.entity_name,
               p.platform_name,
               s.order_no as sales_order_no,
               pc.contract_name
        from purchase_order t
                 left join enterprise e on t.customer_user_id = e.customer_id
                 left join vendor_entity v on v.entity_no = t.entity_no
                 left join trading_platform p on t.agency_cert_no = p.cert_no
                 left join sales_order s on t.sales_order_id = s.id
                 left join purchase_contract pc on (t.purchase_contract_no = pc.contract_no and pc.customer_user_id = t.customer_user_id)
        where t.id = #{id}
    </select>

    <select id="selectCustTaxNum" resultMap="VoInvoiceResultMap">
        SELECT e.tax_no as tax_num, e.cert_no, p.xt_user_id, p.xt_eid, t.customer_user_id, t.id, t.order_no, t.amount_total
        FROM purchase_order t
        left join enterprise e on t.customer_user_id = e.customer_id
        left join trading_platform p on t.agency_cert_no = p.cert_no
        WHERE (t.agency_cert_no is not null and t.agency_cert_no != '')
        AND t.deleted = 0
        <if test="invoicePullStatus != null and invoicePullStatus != '' ">
            AND t.invoice_pull_status = #{invoicePullStatus}
        </if>
        <if test="null != ids and ids.size()>0">
            AND t.id IN
            <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectClientTaxNum" resultMap="VoInvoiceResultMap">
        SELECT v.vat_no as tax_num, v.cert_no, c.xt_uid as xt_user_id, e.xt_eid, t.customer_user_id, t.id, t.order_no, t.amount_total
        FROM purchase_order t
        left join enterprise e on t.customer_user_id = e.customer_id
        left join purchase_contract pc on (t.purchase_contract_no = pc.contract_no and pc.customer_user_id = t.customer_user_id)
        left join vendor_entity v on v.entity_no = pc.vendor_entity_no
        left join customer_user c on t.customer_user_id = c.id
        WHERE (t.agency_cert_no is null or t.agency_cert_no = '')
        AND t.deleted = 0
        <if test="invoicePullStatus != null and invoicePullStatus != '' ">
            AND t.invoice_pull_status = #{invoicePullStatus}
        </if>
        <if test="null != ids and ids.size()>0">
            AND t.id IN
            <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectAgencyTaxNum" resultMap="VoInvoiceResultMap">
        SELECT p.tax_num, p.xt_user_id, p.xt_eid, t.customer_user_id, t.id, t.order_no, t.amount_total
        FROM purchase_order t
        left join trading_platform p on t.agency_cert_no = p.cert_no
        WHERE (t.agency_cert_no is not null and t.agency_cert_no != '')
        AND t.deleted = 0
        <if test="invoicePullStatus != null and invoicePullStatus != '' ">
            AND t.invoice_pull_status = #{invoicePullStatus}
        </if>
        <if test="null != ids and ids.size()>0">
            AND t.id IN
            <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <update id="updateInvoicePullStatus">
        update purchase_order
        set invoice_pull_status = #{status}
        where id = #{id}
    </update>

</mapper>
