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

    <resultMap id="BaseResultMap" type="com.trade.service.sales.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="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.sales.vo.SalesContractVo">
        <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_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"/>
    </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,
        deleted, create_by, update_by
    </sql>


    <select id="selectByCondition"
            parameterType="com.trade.service.sales.dto.ContractSearchDto"
            resultMap="VoResultMap">
        select
        t.id, t.customer_user_id, 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, c.payment_way, c.payment_currency, c.settlement_cycle
        from sales_contract t join contract_terms c on t.contract_no = c.contract_no
        <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 = #{paymentCurrency}
            </if>
            <if test="point == 0 ">
                AND t.contractStatus = '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>

</mapper>