<?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.salescontract.mapper.SalesContractMapper">

    <resultMap id="BaseResultMap" type="com.trade.service.trade.salescontract.domain.SalesContract">
        <id column="id" property="id"/>
        <id column="customer_user_id" property="customerUserId"/>
        <id column="contract_no" property="contractNo"/>
        <id column="contract_name" property="contractName"/>
        <id column="importer_cert_no" property="importerCertNo"/>
        <id column="importer_enterprise_name" property="importerEnterpriseName"/>
        <id column="agency_cert_no" property="agencyCertNo"/>
        <id column="agency_enterprise_name" property="agencyEnterpriseName"/>
        <id column="trade_terms" property="tradeTerms"/>
        <id column="port_departure" property="portDeparture"/>
        <id column="port_destination" property="portDestination"/>
        <id column="template_type" property="templateType"/>
        <id column="sign_date" property="signDate"/>
        <id column="submit_status" property="submitStatus"/>
        <id column="contract_status" property="contractStatus"/>
        <id column="audit_status" property="auditStatus"/>
        <id column="has_purchase_contract" property="hasPurchaseContract"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="gmt_modified" property="gmtModified"/>
        <id column="deleted" property="deleted"/>
        <id column="create_by" property="createBy"/>
        <id column="update_by" property="updateBy"/>
    </resultMap>

    <resultMap id="VoResultMap" type="com.trade.service.trade.salescontract.vo.SalesContractVo">
        <id column="id" property="id"/>
        <id column="customer_user_id" property="customerUserId"/>
        <id column="customer_identifier_no" property="customerIdentifierNo"/>
        <id column="customer_name" property="customerName"/>
        <id column="contract_no" property="contractNo"/>
        <id column="contract_name" property="contractName"/>
        <id column="importer_enterprise_name" property="importerEnterpriseName"/>
        <id column="agency_enterprise_name" property="agencyEnterpriseName"/>
        <id column="port_destination" property="portDestination"/>
        <id column="template_type" property="templateType"/>
        <id column="contract_status" property="contractStatus"/>
        <id column="payment_way" property="paymentWay"/>
        <id column="payment_currency" property="paymentCurrency"/>
        <id column="settlement_cycle" property="settlementCycle"/>
        <id column="gmt_create" property="gmtCreate"/>
        <id column="gmt_modified" property="gmtModified"/>
        <id column="exchange_rate" property="exchangeRate"/>
    </resultMap>

    <sql id="Base_Column_List">
        id , customer_user_id, contract_no, contract_name, importer_cert_no, importer_enterprise_name, agency_cert_no, agency_enterprise_name, trade_terms, port_departure, port_destination, template_type, sign_date, submit_status, contract_status, audit_status, gmt_create, gmt_modified,
        has_purchase_contract,deleted, exchange_rate, create_by, update_by
    </sql>


    <select id="selectByCondition"
            parameterType="com.trade.service.trade.salescontract.dto.ContractSearchDto"
            resultMap="VoResultMap">
        select
        t.id, t.customer_user_id, e.customer_identifier_no, e.name as customer_name,t.contract_no, t.contract_name,
        t.importer_enterprise_name,
        t.agency_enterprise_name, t.port_destination, t.template_type, t.contract_status,
        t.gmt_create, t.gmt_modified, t.exchange_rate, c.payment_way, c.payment_currency, c.settlement_cycle
        from sales_contract t left join enterprise e on t.customer_user_id = e.customer_id left join contract_terms c on
        t.id = c.contract_id
        <where>
            t.deleted = 0
            <if test="customerUserId != null">
                AND t.customer_user_id = #{customerUserId}
            </if>
            <if test="contractInfo != null and contractInfo != ''">
                AND t.contract_name LIKE CONCAT ('%',#{contractInfo},'%') OR t.contract_no = #{contractInfo}
            </if>
            <if test="importerCertNo != null and importerCertNo != ''">
                AND t.importer_cert_no = #{importerCertNo}
            </if>
            <if test="agencyCertNo != null and agencyCertNo != ''">
                AND t.agency_cert_no = #{agencyCertNo}
            </if>
            <if test="paymentWay != null and paymentWay != ''">
                AND c.payment_way = #{paymentWay}
            </if>
            <if test="contractStatus != null and contractStatus != ''">
                AND t.contract_status = #{contractStatus}
            </if>
            <if test="point == 0 ">
                AND t.contract_status = '2'
            </if>
            <if test="point ==1 and timeStart != null">
                AND t.gmt_create <![CDATA[ >= ]]> #{timeStart}
            </if>
            <if test="point ==0 and timeStart != null">
                AND t.gmt_modified <![CDATA[ >= ]]> #{timeStart}
            </if>
            <if test="point ==1 and timeEnd != null">
                AND t.gmt_create <![CDATA[ <= ]]> #{timeEnd}
            </if>
            <if test="point ==0 and timeEnd != null">
                AND t.gmt_modified <![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>
        <if test="customerUserId != null">
            ORDER BY t.gmt_create DESC
        </if>
        <if test="customerUserId == null">
            ORDER BY t.gmt_modified DESC
        </if>
    </select>

    <select id="queryList4FilingDocBySearch"
            resultType="com.trade.service.trade.filingdoc.vo.SalesContract4FilingDocVO">
        select a.contract_no, a.contract_name, a.importer_enterprise_name, a.agency_enterprise_name,
        a.sign_date,
        (select sum(amount) from sales_commodity sc where sc.sales_contract_id = a.id) contract_amount,
        b.signed_file_storage_id contract_file_id
        From sales_contract a
        left join contract_file b on a.contract_no = b.contract_no and a.customer_user_id = b.customer_user_id
        left join sales_order c on a.id = c.sale_contract_id
        left join tax_rebate_export_details d on c.order_no = d.sales_order_no
        WHERE a.deleted = 0
        <if test="declareDate != null and declareDate != ''">
            AND d.declaration_month_year = #{declareDate}
        </if>
        <if test="declareBatch != null and declareBatch != ''">
            AND d.declaration_batch = #{declareBatch}
        </if>
        <if test="contractNo != null and contractNo != ''">
            AND a.contract_no = #{contractNo}
        </if>
        <if test="agencyCertNo != null and agencyCertNo != ''">
            AND a.agency_cert_no = #{agencyCertNo}
        </if>
        <if test="importerCertNo != null and importerCertNo != ''">
            AND a.importer_cert_no >= #{importerCertNo}
        </if>
        <if test="signDateStart != null">
            AND a.sign_date &gt;= #{signDateStart}
        </if>
        <if test="signDateEnd != null">
            AND a.sign_date &lt; DATE_FORMAT(DATE_ADD(#{signDateEnd},INTERVAL 1 DAY),'%Y-%m-%d')
        </if>
    </select>

    <resultMap id="VoEntryResultMap" type="com.trade.service.finance.paymentcollection.vo.EntrySalesContractVo">
        <id column="id" property="contractId"/>
        <id column="contract_no" property="contractNo"/>
        <id column="contract_name" property="contractName"/>
    </resultMap>

    <select id="selectEntrySalesContract" resultMap="VoEntryResultMap">
        SELECT t.id, t.contract_no, t.contract_name FROM sales_contract t WHERE t.contract_status = '2' and  t.deleted = 0
    </select>

</mapper>
