<?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.customer.mapper.CustomerMapper">

    <resultMap type="Customer" id="CustomerResult">
        <result property="cusId"    column="cus_id"    />
        <result property="cusCode"    column="cus_code"    />
        <result property="cusName"    column="cus_name"    />
        <result property="cusSapCode"    column="cus_sap_code"    />
        <result property="cusStreet"    column="cus_street"    />
        <result property="cusQccStreet"    column="cus_qcc_street"    />
        <result property="cusPaymentTerms"    column="cus_payment_terms"    />
        <result property="cusPhoneNumber"    column="cus_phone_number"    />
        <result property="cusIndustryCode"    column="cus_industry_code"    />
        <result property="cusIndustryCodeQcc"    column="cus_industry_code_qcc"    />
        <result property="cusGroup"    column="cus_group"    />
        <result property="cusVatNo"    column="cus_vat_no"    />
        <result property="cusType"    column="cus_type"    />
        <result property="cusCountry"    column="cus_country"    />
        <result property="cusLanguage"    column="cus_language"    />
        <result property="cusLabel"    column="cus_label"    />
        <result property="cusClassification"    column="cus_classification"    />
        <result property="cusReceivingEmail"    column="cus_receiving_email"    />
        <result property="cusRecipient"    column="cus_recipient"    />
        <result property="cusRecipientPhone"    column="cus_recipient_phone"    />
        <result property="cusRemark"    column="cus_remark"    />
        <result property="cusReturnRemark"    column="cus_return_remark"    />
        <result property="cusState"    column="cus_state"    />
        <result property="cusApprovalStatus"    column="cus_approval_status"    />
        <result property="cusSalesman"    column="cus_salesman"    />
        <result property="createBy"     column="create_by"    />
        <result property="createName"     column="create_name"    />
        <result property="createTime"   column="create_time"  />
        <result property="updateBy"     column="update_by"    />
        <result property="updateTime"   column="update_time"  />

        <result property="cusSalesOrganization"   column="cus_sales_organization"  />
        <result property="cusDistributionChannel"   column="cus_distribution_channel"  />
        <result property="cusSalesTerritory"   column="cus_sales_territory"  />
        <result property="cusSaleOffice"   column="cus_sale_office"  />
        <result property="cusCurrency"   column="cus_currency"  />
        <result property="cusTax"   column="cus_tax"  />
        <result property="cusReconciliationAccount"   column="cus_reconciliation_account"  />
        <result property="cusAccountAllocationGroup"   column="cus_account_allocation_group"  />
    </resultMap>

    <resultMap id="CustomerBankResult" type="Customer" extends="CustomerResult">
        <collection property="bankList" notNullColumn="sub_bank_id" javaType="java.util.List" resultMap="BankResult" />
    </resultMap>

    <resultMap type="Bank" id="BankResult">
        <result property="bankId"    column="sub_bank_id"    />
        <result property="bankName"    column="sub_bank_name"    />
        <result property="bankAccount"    column="sub_bank_account"    />
        <result property="bankCode"    column="sub_bank_code"    />
        <result property="bankCountry"    column="sub_bank_country"    />
        <result property="cusId"    column="sub_cus_id"    />
    </resultMap>

    <sql id="customerJoins">
        LEFT JOIN sys_user u on u.user_name=a.create_by
        LEFT JOIN sys_dept d on u.dept_id = d.dept_id
    </sql>

    <sql id="selectCustomerVo">
        select a.cus_id, a.cus_code, a.cus_name, a.cus_sap_code, a.cus_street, a.cus_qcc_street,a.cus_payment_terms, a.cus_phone_number,
               a.cus_industry_code, a.cus_industry_code_qcc, a.cus_group, a.cus_vat_no, a.cus_type, a.cus_country, a.cus_language, a.cus_label,
               a.cus_classification, a.cus_receiving_email, a.cus_recipient, a.cus_recipient_phone, a.cus_remark,a.cus_return_remark,
               a.cus_state, a.cus_approval_status,a.cus_salesman,a.create_time,u.nick_name create_name,

               a.cus_sales_organization,a.cus_distribution_channel,a.cus_sales_territory,a.cus_sale_office,
               a.cus_currency,a.cus_tax,a.cus_reconciliation_account,a.cus_account_allocation_group
        from customer a
        <include refid="customerJoins"/>
    </sql>

    <select id="selectCustomerList" parameterType="Customer" resultMap="CustomerResult">
        <include refid="selectCustomerVo"/>
        <where>
            <if test="cusCode != null  and cusCode != ''"> and cus_code like concat('%', #{cusCode}, '%')</if>
            <if test="cusName != null  and cusName != ''"> and cus_name like concat('%', #{cusName}, '%')</if>
            <if test="cusSapCode != null  and cusSapCode != ''"> and cus_sap_code like concat('%', #{cusSapCode}, '%')</if>
            <if test="cusState != null  and cusState != ''"> and cus_state = #{cusState}</if>
            <if test="cusApprovalStatus != null  and cusApprovalStatus != ''"> and cus_approval_status = #{cusApprovalStatus}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        order by a.update_time desc
    </select>

    <select id="selectCustomerByCusId" parameterType="java.lang.String" resultMap="CustomerBankResult">
        select a.cus_id, a.cus_code, a.cus_name, a.cus_sap_code, a.cus_street,a.cus_qcc_street, a.cus_payment_terms, a.cus_phone_number, a.cus_industry_code,
               a.cus_industry_code_qcc,a.cus_group, a.cus_vat_no, a.cus_type, a.cus_country, a.cus_language, a.cus_label, a.cus_classification, a.cus_receiving_email,
               a.cus_recipient, a.cus_recipient_phone, a.cus_remark,a.cus_return_remark, a.cus_state, a.cus_approval_status,a.cus_salesman,
               a.cus_sales_organization,a.cus_distribution_channel,a.cus_sales_territory,a.cus_sale_office,
                a.cus_currency,a.cus_tax,a.cus_reconciliation_account,a.cus_account_allocation_group,
            b.bank_id as sub_bank_id, b.bank_name as sub_bank_name, b.bank_account as sub_bank_account, b.cus_id as sub_cus_id,
               b.bank_code as sub_bank_code,b.bank_country as sub_bank_country
        from customer a
        left join bank b on b.cus_id = a.cus_id
        where a.cus_id = #{cusId}
    </select>

    <insert id="insertCustomer" parameterType="Customer">
        insert into customer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cusId != null">cus_id,</if>
            <if test="cusCode != null and cusCode != ''">cus_code,</if>
            <if test="cusName != null and cusName != ''">cus_name,</if>
            <if test="cusSapCode != null and cusSapCode != ''">cus_sap_code,</if>
            <if test="cusStreet != null and cusStreet != ''">cus_street,</if>
            <if test="cusQccStreet != null and cusQccStreet != ''">cus_qcc_street,</if>
            <if test="cusPaymentTerms != null and cusPaymentTerms != ''">cus_payment_terms,</if>
            <if test="cusPhoneNumber != null and cusPhoneNumber != ''">cus_phone_number,</if>
            <if test="cusIndustryCode != null and cusIndustryCode != ''">cus_industry_code,</if>
            <if test="cusIndustryCodeQcc != null and cusIndustryCodeQcc != ''">cus_industry_code_qcc,</if>
            <if test="cusGroup != null and cusGroup != ''">cus_group,</if>
            <if test="cusVatNo != null and cusVatNo != ''">cus_vat_no,</if>
            <if test="cusType != null">cus_type,</if>
            <if test="cusCountry != null and cusCountry != ''">cus_country,</if>
            <if test="cusLanguage != null and cusLanguage != ''">cus_language,</if>
            <if test="cusLabel != null">cus_label,</if>
            <if test="cusClassification != null">cus_classification,</if>
            <if test="cusReceivingEmail != null">cus_receiving_email,</if>
            <if test="cusRecipient != null">cus_recipient,</if>
            <if test="cusRecipientPhone != null">cus_recipient_phone,</if>
            <if test="cusRemark != null">cus_remark,</if>
            <if test="cusReturnRemark != null">cus_return_remark,</if>
            <if test="cusState != null">cus_state,</if>
            <if test="cusApprovalStatus != null">cus_approval_status,</if>
            <if test="cusSalesman != null">cus_salesman,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="cusSalesOrganization != null and cusSalesOrganization != ''">cus_sales_organization,</if>
            <if test="cusDistributionChannel != null and cusDistributionChannel != ''">cus_distribution_channel,</if>
            <if test="cusSalesTerritory != null and cusSalesTerritory != ''">cus_sales_territory,</if>
            <if test="cusSaleOffice != null and cusSaleOffice != ''">cus_sale_office,</if>
            <if test="cusCurrency != null and cusCurrency != ''">cus_currency,</if>
            <if test="cusTax != null and cusTax != ''">cus_tax,</if>
            <if test="cusReconciliationAccount != null and cusReconciliationAccount != ''">cus_reconciliation_account,</if>
            <if test="cusAccountAllocationGroup != null and cusAccountAllocationGroup != ''">cus_account_allocation_group,</if>
            </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cusId != null">#{cusId},</if>
            <if test="cusCode != null and cusCode != ''">#{cusCode},</if>
            <if test="cusName != null and cusName != ''">#{cusName},</if>
            <if test="cusSapCode != null and cusSapCode != ''">#{cusSapCode},</if>
            <if test="cusStreet != null and cusStreet != ''">#{cusStreet},</if>
            <if test="cusQccStreet != null and cusQccStreet != ''">#{cusQccStreet},</if>
            <if test="cusPaymentTerms != null and cusPaymentTerms != ''">#{cusPaymentTerms},</if>
            <if test="cusPhoneNumber != null and cusPhoneNumber != ''">#{cusPhoneNumber},</if>
            <if test="cusIndustryCode != null and cusIndustryCode != ''">#{cusIndustryCode},</if>
            <if test="cusIndustryCodeQcc != null and cusIndustryCodeQcc != ''">#{cusIndustryCodeQcc},</if>
            <if test="cusGroup != null and cusGroup != ''">#{cusGroup},</if>
            <if test="cusVatNo != null and cusVatNo != ''">#{cusVatNo},</if>
            <if test="cusType != null">#{cusType},</if>
            <if test="cusCountry != null and cusCountry != ''">#{cusCountry},</if>
            <if test="cusLanguage != null and cusLanguage != ''">#{cusLanguage},</if>
            <if test="cusLabel != null">#{cusLabel},</if>
            <if test="cusClassification != null">#{cusClassification},</if>
            <if test="cusReceivingEmail != null">#{cusReceivingEmail},</if>
            <if test="cusRecipient != null">#{cusRecipient},</if>
            <if test="cusRecipientPhone != null">#{cusRecipientPhone},</if>
            <if test="cusRemark != null">#{cusRemark},</if>
            <if test="cusReturnRemark != null">#{cusReturnRemark},</if>
            <if test="cusState != null">#{cusState},</if>
            <if test="cusApprovalStatus != null">#{cusApprovalStatus},</if>
            <if test="cusSalesman != null">#{cusSalesman},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="cusSalesOrganization != null and cusSalesOrganization != ''">#{cusSalesOrganization},</if>
            <if test="cusDistributionChannel != null and cusDistributionChannel != ''">#{cusDistributionChannel},</if>
            <if test="cusSalesTerritory != null and cusSalesTerritory != ''">#{cusSalesTerritory},</if>
            <if test="cusSaleOffice != null and cusSaleOffice != ''">#{cusSaleOffice},</if>
            <if test="cusCurrency != null and cusCurrency != ''">#{cusCurrency},</if>
            <if test="cusTax != null and cusTax != ''">#{cusTax},</if>
            <if test="cusReconciliationAccount != null and cusReconciliationAccount != ''">#{cusReconciliationAccount},</if>
            <if test="cusAccountAllocationGroup != null and cusAccountAllocationGroup != ''">#{cusAccountAllocationGroup},</if>
        </trim>
    </insert>

    <update id="updateCustomer" parameterType="Customer">
        update customer
        <trim prefix="SET" suffixOverrides=",">
            <if test="cusCode != null">cus_code = #{cusCode},</if>
            <if test="cusName != null and cusName != ''">cus_name = #{cusName},</if>
            <if test="cusSapCode != null">cus_sap_code = #{cusSapCode},</if>
            <if test="cusStreet != null and cusStreet != ''">cus_street = #{cusStreet},</if>
            <if test="cusQccStreet != null and cusQccStreet != ''">cus_qcc_street = #{cusQccStreet},</if>
            <if test="cusPaymentTerms != null and cusPaymentTerms != ''">cus_payment_terms = #{cusPaymentTerms},</if>
            <if test="cusPhoneNumber != null and cusPhoneNumber != ''">cus_phone_number = #{cusPhoneNumber},</if>
            <if test="cusIndustryCode != null and cusIndustryCode != ''">cus_industry_code = #{cusIndustryCode},</if>
            <if test="cusIndustryCodeQcc != null and cusIndustryCodeQcc != ''">cus_industry_code_qcc = #{cusIndustryCodeQcc},</if>
            <if test="cusGroup != null and cusGroup != ''">cus_group = #{cusGroup},</if>
            <if test="cusVatNo != null and cusVatNo != ''">cus_vat_no = #{cusVatNo},</if>
            <if test="cusType != null">cus_type = #{cusType},</if>
            <if test="cusCountry != null and cusCountry != ''">cus_country = #{cusCountry},</if>
            <if test="cusLanguage != null and cusLanguage != ''">cus_language = #{cusLanguage},</if>
            <if test="cusLabel != null">cus_label = #{cusLabel},</if>
            <if test="cusClassification != null">cus_classification = #{cusClassification},</if>
            <if test="cusReceivingEmail != null">cus_receiving_email = #{cusReceivingEmail},</if>
            <if test="cusRecipient != null">cus_recipient = #{cusRecipient},</if>
            <if test="cusRecipientPhone != null">cus_recipient_phone = #{cusRecipientPhone},</if>
            <if test="cusRemark != null">cus_remark = #{cusRemark},</if>
            <if test="cusReturnRemark != null">cus_return_remark = #{cusReturnRemark},</if>
            <if test="cusState != null">cus_state = #{cusState},</if>
            <if test="cusApprovalStatus != null">cus_approval_status = #{cusApprovalStatus},</if>
            <if test="cusSalesman != null">cus_salesman = #{cusSalesman},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="cusSalesOrganization != null and cusSalesOrganization != ''">cus_sales_organization = #{cusSalesOrganization},</if>
            <if test="cusDistributionChannel != null and cusDistributionChannel != ''">cus_distribution_channel = #{cusDistributionChannel},</if>
            <if test="cusSalesTerritory != null and cusSalesTerritory != ''">cus_sales_territory = #{cusSalesTerritory},</if>
            <if test="cusSaleOffice != null and cusSaleOffice != ''">cus_sale_office = #{cusSaleOffice},</if>
            <if test="cusCurrency != null and cusCurrency != ''">cus_currency = #{cusCurrency},</if>
            <if test="cusTax != null and cusTax != ''">cus_tax = #{cusTax},</if>
            <if test="cusReconciliationAccount != null and cusReconciliationAccount != ''">cus_reconciliation_account = #{cusReconciliationAccount},</if>
            <if test="cusAccountAllocationGroup != null and cusAccountAllocationGroup != ''">cus_account_allocation_group = #{cusAccountAllocationGroup},</if>
        </trim>
        where cus_id = #{cusId}
    </update>

    <delete id="deleteCustomerByCusId" parameterType="java.lang.String">
        delete from customer where cus_id = #{cusId}
    </delete>

    <delete id="deleteCustomerByCusIds" parameterType="String">
        delete from customer where cus_id in
        <foreach item="cusId" collection="array" open="(" separator="," close=")">
            #{cusId}
        </foreach>
    </delete>

    <delete id="deleteBankByCusIds" parameterType="String">
        delete from bank where cus_id in
        <foreach item="cusId" collection="array" open="(" separator="," close=")">
            #{cusId}
        </foreach>
    </delete>

    <delete id="deleteBankByCusId" parameterType="java.lang.String">
        delete from bank where cus_id = #{cusId}
    </delete>

    <insert id="batchBank">
        insert into bank(bank_id, bank_name, bank_account, cus_id,bank_code,bank_country) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.bankId}, #{item.bankName}, #{item.bankAccount}, #{item.cusId}, #{item.bankCode}, #{item.bankCountry})
        </foreach>
    </insert>
</mapper>