<?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.brilliance.isc.mda.dao.TrmMapper">

    <resultMap id="BaseResultMap" type="com.brilliance.isc.bo.Trm">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="trmtyp" column="trmtyp" jdbcType="VARCHAR"/>
        <result property="trmcod" column="trmcod" jdbcType="VARCHAR"/>
        <result property="acttrmcod" column="acttrmcod" jdbcType="VARCHAR"/>
        <result property="acttrmtyp" column="acttrmtyp" jdbcType="DECIMAL"/>
        <result property="nam" column="nam" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,trmtyp,trmcod,acttrmcod,acttrmtyp,nam
    </sql>

    <select id="getTrmTypByTrmCod" resultType="java.lang.String">
        SELECT DISTINCT(TRMTYP) FROM TRM WHERE TRMCOD=#{trmcod}
    </select>

    <select id="getJshTrmTyp" resultType="java.lang.String">
        SELECT TRMCOD FROM TRM WHERE TRMTYP LIKE 'LO%' OR TRMTYP LIKE 'FI%' OR TRMTYP LIKE 'CC%'
    </select>

    <select id="selectByTrmcod" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from trm
        where trmcod = #{trmcod,jdbcType=VARCHAR} limit 1
    </select>
    
    <select id="getTrmtypByAcctyp" resultMap="BaseResultMap">
        <if test="bchtypflg">
            select trmtyp, trmcod, nam from trm where trmtyp != 'AS-GA0' and trmtyp != 'AS-DF2'
            and trmcod not in ('0301','0302','0303','0304','0305','0306','0307','0399','0400','0401','0402','0403','0404','0407','0408','0409','0410','0411')
            and trmtyp like concat(#{acctyp}, '%')
        </if>
        <if test="!bchtypflg">
            select trmtyp, trmcod, nam from trm where
            trmcod not in ('0301','0302','0303','0304','0305','0306','0307','0399','0400','0401','0402','0403','0404','0407','0408','0409','0410','0411')
            and trmtyp like concat(#{acctyp}, '%')
        </if>
    </select>

    <select id="getTrmListByTrmType" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from trm
        where TRMTYP like concat(#{trmtyp},'%')
        <if test='trmtyp == "CC"'>
            OR TRMTYP LIKE 'SP%' OR TRMTYP LIKE 'AR%'
        </if>
        ORDER BY TRMCOD
    </select>

    <select id="seletByTrmtyp" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from trm
        where trmtyp = #{trmtyp,jdbcType=VARCHAR}
        limit 1
    </select>

    <select id="getTrmByAct" resultMap="BaseResultMap">
        SELECT t1.trmcod, t1.nam FROM trm t1 LEFT JOIN act t2 ON t1.trmtyp = t2.trmtyp WHERE t2.extkey = #{act,jdbcType=VARCHAR}
        <if test="trmtyp != null and trmtyp != ''">
            AND t1.trmtyp = #{trmtyp,jdbcType=VARCHAR}
        </if>
        LIMIT 1
    </select>

</mapper>