<?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.sunxung.factoring.mapper.cashflowmanage.BankCardInfoSetMapper">

    <resultMap type="BankCardInfo" id="bankCardResult">
        <result property="id" column="id"/>
        <result property="companyBelong" column="company_belong"/>
        <result property="bankName" column="bank_name"/>
        <result property="bankAccount" column="bank_account"/>
        <result property="effectiveDate" column="effective_date"/>
        <result property="beginValue" column="begin_value"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <association property="companyBelongDict" column="company_belong" resultMap="companyBelongResult"/>
        <association property="currencyDict" column="currency" resultMap="dictionaryResult"/>
    </resultMap>

    <resultMap id="companyBelongResult" type="Dictionary">
        <result property="id" column="company_belong_id"/>
        <result property="name" column="company_belong_name"/>
        <result property="code" column="company_belong_code" />
    </resultMap>

    <resultMap id="dictionaryResult" type="Dictionary">
        <result property="id" column="did"/>
        <result property="name" column="dname"/>
        <result property="code" column="dcode"/>
    </resultMap>

    <resultMap type="com.sunxung.factoring.entity.cashflowmanage.vo.CompanyBelongResultVo" id="CompanyBelongOfResult">
        <result property="companyBelong" column="company_belong"/>
        <result property="companyName" column="company_name"/>
    </resultMap>

    <select id="findList" parameterType="com.sunxung.factoring.entity.cashflowmanage.vo.SearchBankCardInfoSetVo"
            resultMap="bankCardResult">
        SELECT
        a.id,
        a.bank_name,
        a.bank_account,
        a.effective_date,
        a.begin_value,
        a.status,
        a.remark,
        t.id company_belong_id,
        t.name company_belong_name,
        t.code company_belong_code,
        td.id did,
        td.NAME dname,
        td.CODE dcode
        FROM
        bank_card_info a
        LEFT JOIN t_dictionary t ON a.company_belong = t.id
        LEFT JOIN t_dictionary td ON a.currency = td.id
        <where>
            <if test="companyBelong != null">
                AND a.company_belong = #{companyBelong}
            </if>
            <if test="fastSearch != null">
                AND (a.bank_name LIKE concat('%', #{fastSearch}, '%') OR a.bank_account LIKE concat('%', #{fastSearch},
                '%'))
            </if>
        </where>
        order by a.gmt_create desc
    </select>

    <select id="findById" parameterType="com.sunxung.factoring.entity.cashflowmanage.vo.SearchBankCardInfoSetVo"
            resultMap="bankCardResult">
        SELECT a.id,
               a.bank_name,
               a.bank_account,
               a.effective_date,
               a.begin_value,
               a.status,
               a.remark,
               t.id company_belong_id,
               t.name company_belong_name,
               t.code company_belong_code,
               td.id   did,
               td.NAME dname,
               td.CODE dcode
        FROM bank_card_info a
                 LEFT JOIN t_dictionary t ON a.company_belong = t.id
                 LEFT JOIN t_dictionary td ON a.currency = td.id
        WHERE a.id = #{id}
    </select>

    <select id="getCompanyBelong" resultMap="CompanyBelongOfResult">
        SELECT DISTINCT
			a.company_belong,
			b.name as company_name
        FROM bank_card_info a,
             t_dictionary b
        WHERE a.company_belong = b.id AND a.status =1
    </select>

    <select id="getBankName" parameterType="java.lang.Long" resultType="java.lang.String">
        SELECT DISTINCT
            a.bank_name
        FROM bank_card_info a
        WHERE a.company_belong = #{companyBelong} AND a.status =1
    </select>

    <select id="getBankAccount" resultType="java.lang.String">
        SELECT DISTINCT
            a.bank_account
        FROM bank_card_info a
        WHERE a.bank_name = #{bankName}
          AND a.company_belong = #{companyBelong}
          AND a.status =1
    </select>
</mapper>
