<?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.TdhMapper">
    <resultMap id="BaseResultMap" type="com.brilliance.isc.bo.Tdh">
        <result property="inr" column="INR" jdbcType="VARCHAR"/>
        <result property="cur" column="CUR" jdbcType="VARCHAR"/>
        <result property="datesource" column="DATESOURCE" jdbcType="VARCHAR"/>
        <result property="dat" column="DAT" jdbcType="TIMESTAMP"/>
        <result property="dateday" column="DATEDAY" jdbcType="VARCHAR"/>
        <result property="datStr" column="DAT_STR" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="VoResultMap" type="com.brilliance.isc.vo.manager.TdhVo">
        <result property="inr" column="INR" jdbcType="VARCHAR"/>
        <result property="cur" column="CUR" jdbcType="VARCHAR"/>
        <result property="curname" column="curname" jdbcType="VARCHAR"/>
        <result property="datesource" column="DATESOURCE" jdbcType="VARCHAR"/>
        <result property="dat" column="DAT" jdbcType="TIMESTAMP"/>
        <result property="dateday" column="DATEDAY" jdbcType="VARCHAR"/>
        <result property="datStr" column="DAT_STR" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="BaseResultMap">
        select
        INR, CUR, DATESOURCE, DAT, DATEDAY, date_format(DAT, '%Y-%m-%d') DAT_STR
        from TDH
        <where>
            <if test="cur != null and cur != ''">
                AND CUR like concat(concat ('%',#{cur}),'%')
            </if>
            <if test="dat != null and dat != ''">
                and DAT like concat(concat ('%',#{dat}),'%')
            </if>
        </where>
        ORDER BY DAT
    </select>

    <!--查询单个-->
    <select id="queryByCurAndDat" resultMap="BaseResultMap">
        select
          INR, CUR, DATESOURCE, DAT, DATEDAY,date_format(DAT, '%Y-%m-%d') DAT_STR
        from TDH
        where DAT = #{dat} and CUR = #{cur}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from TDH
        <where>
            <if test="inr != null and inr != ''">
                AND INR = #{inr}
            </if>
            <if test="cur != null and cur != ''">
                AND CUR = #{cur}
            </if>
            <if test="datesource != null and datesource != ''">
                AND DATESOURCE = #{datesource}
            </if>
            <if test="dat != null">
                AND DAT = #{dat}
            </if>
            <if test="dateday != null and dateday != ''">
                AND DATEDAY = #{dateday}
            </if>
        </where>
    </select>

    <select id="check" resultType="java.lang.Long">
        select count(1)
        from TDH
        <where>
            <if test="dat != null">
                AND DAT = #{dat}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="inr" useGeneratedKeys="true">
        insert into TDH(INR, CUR, DATESOURCE, DAT, DATEDAY)
        values (#{inr}, #{cur}, #{datesource}, #{dat}, #{dateday})
    </insert>

    <!--批量导入-->
    <insert id="insertBatch" keyProperty="" useGeneratedKeys="true">
        insert into TDH(INR,CUR,DAT,DATESOURCE,DATEDAY)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.inr}, #{entity.cur}, #{entity.dat}, #{entity.datesource}, #{entity.dateday})
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update TDH
        <set>
            <if test="cur != null and cur != ''">
                CUR = #{cur},
            </if>
            <if test="datesource != null and datesource != ''">
                DATESOURCE = #{datesource},
            </if>
            <if test="dat != null">
                DAT = #{dat},
            </if>
            <if test="dateday != null and dateday != ''">
                DATEDAY = #{dateday},
            </if>
        </set>
        where INR = #{inr}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from TDH where inr = #{inr}
    </delete>

    <!--查询指定行数据-->
    <select id="queryDat" resultMap="BaseResultMap">
        select
        INR, CUR, DATESOURCE, DAT, DATEDAY, date_format(DAT, '%Y-%m-%d') DAT_STR
        from TDH
        <where>
            <if test="begdat != null">
                and DAT <![CDATA[>=]]> #{begdat,jdbcType=DATE}
            </if>
            <if test="enddat != null">
                and DAT <![CDATA[<=]]> #{enddat,jdbcType=DATE}
            </if>
        </where>
        ORDER BY DAT
    </select>

    <select id="queryByDat" resultMap="BaseResultMap">
        select
        INR, CUR, DATESOURCE, DAT, DATEDAY, date_format(DAT, '%Y-%m-%d') DAT_STR
        from TDH where Dat=#{dat}
    </select>

    <select id="listTdh" resultMap="VoResultMap">
        select a.INR INR, a.CUR CUR,concat (a.cur,' - ',b.TXT) curname, a.DATESOURCE DATESOURCE, a.DAT DAT, a.DATEDAY DATEDAY,
        date_format(a.DAT, '%Y-%m-%d') DAT_STR from TDH a left join stb b on b.TBL = 'CURTXT' and a.CUR = b.COD and b.UIL = 'EN'
        <where>
            <choose>
                <when test="cur != null and cur != ''">
                    a.CUR like concat(concat ('%',#{cur}),'%')
                </when>
                <otherwise>
                    a.CUR in ('CNY','USD','EUR','GBP','JPY','HKD')
                </otherwise>
            </choose>
            <if test="dat != null and dat != ''">
                and a.DAT like concat(concat ('%',#{dat}),'%')
            </if>
        </where>
        order by b.SRT ,a.DAT
    </select>

    <select id="getCurName" resultType="java.lang.String">
        select concat (s.cod,' - ',s.TXT) curname from stb s where s.TBL = 'CURTXT'  and s.UIL  = 'EN' and s.cod = #{cur}
    </select>


</mapper>