<?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.chinaweal.aiccs.aicepsqy.mapper.datamove.CapitalModelMapper">

    <!-- 动态表名插入：根据reportYear字段定位表 -->
    <insert id="insertDynamicTable" parameterType="com.chinaweal.aiccs.aicepsqy.entity.datamove.CapitalModel">
        <![CDATA[
        insert into
            TREntCapitalTemp_${entity.reportYear}(
            CapID,EntID,EntityNo,UserName,CapShould,CapReal,CapDate,CapType,CapShouldDate,CapShouldType,isShow, ReportYear
        )
        values
            (
                #{entity.capId},#{entity.entId}, #{entity.entityNo},#{entity.userName,jdbcType=VARCHAR},
                #{entity.capShould,jdbcType=NUMERIC},#{entity.capReal,jdbcType=NUMERIC},#{entity.capDate,jdbcType=TIMESTAMP},
                #{entity.capType,jdbcType=VARCHAR},#{entity.capShouldDate,jdbcType=TIMESTAMP},#{entity.capShouldType,jdbcType=VARCHAR},
                #{entity.isShow,jdbcType=VARCHAR}, #{entity.reportYear}
            )
        ]]>
    </insert>

    <insert id="insertDynamicTablezs" parameterType="com.chinaweal.aiccs.aicepsqy.entity.datamove.CapitalModel">
        <![CDATA[
        insert into
            TREntCapital_${entity.reportYear}(
            CapID,EntID,EntityNo,UserName,CapShould,CapReal,CapDate,CapType,CapShouldDate,CapShouldType,isShow, ReportYear
        )
        values
            (
                #{entity.capId},#{entity.entId}, #{entity.entityNo},#{entity.userName,jdbcType=VARCHAR},
                #{entity.capShould,jdbcType=NUMERIC},#{entity.capReal,jdbcType=NUMERIC},#{entity.capDate,jdbcType=TIMESTAMP},
                #{entity.capType,jdbcType=VARCHAR},#{entity.capShouldDate,jdbcType=TIMESTAMP},#{entity.capShouldType,jdbcType=VARCHAR},
                #{entity.isShow,jdbcType=VARCHAR}, #{entity.reportYear}
            )
        ]]>
    </insert>

    <!-- 动态查询：根据EntityNo和ReportYear -->
    <select id="selectByEntityNoAndReportYear"
            resultType="com.chinaweal.aiccs.aicepsqy.entity.datamove.CapitalModel">
        <![CDATA[
        select
            CapID,EntID,EntityNo,UserName,CapShould,CapReal,CapDate,CapType,
            CapShouldDate,CapShouldType,isShow,ReportYear
        from TREntCapital_${reportYear}
        where EntityNo = #{entityNo}
          and ReportYear = #{reportYear} and Dstate !='2'
        ]]>
    </select>

    <!-- 动态更新：根据EntityNo和ReportYear -->
    <update id="updateByEntityNoAndReportYear"
            parameterType="com.chinaweal.aiccs.aicepsqy.entity.datamove.CapitalModel">
        <![CDATA[
        update TREntCapital_${reportYear}
        set
            EntID = #{entity.entId},
            UserName = #{entity.userName,jdbcType=VARCHAR},
            CapShould = #{entity.capShould,jdbcType=NUMERIC},
            CapReal = #{entity.capReal,jdbcType=NUMERIC},
            CapDate = #{entity.capDate,jdbcType=TIMESTAMP},
            CapType = #{entity.capType,jdbcType=VARCHAR},
            CapShouldDate = #{entity.capShouldDate,jdbcType=TIMESTAMP},
            CapShouldType = #{entity.capShouldType,jdbcType=VARCHAR},
            isShow = #{entity.isShow,jdbcType=VARCHAR},
            ReportYear = #{entity.reportYear},
            updateDate = getdate()
        where
            EntityNo = #{entityNo}
          and ReportYear = #{reportYear} and Dstate !='2'
        ]]>
    </update>



    <!-- 根据CapId主键查询（动态分表） -->
    <select id="selectCapitalModelByCapId" resultType="com.chinaweal.aiccs.aicepsqy.entity.datamove.CapitalModel">
    <![CDATA[
        select * from TREntCapital_${reportYear}
        where CapId = #{capId}
        ]]>
    </select>

    <!-- 根据CapId主键删除（动态分表） -->
    <delete id="deleteCapitalModelByCapId">
    <![CDATA[
        delete from TREntCapitalTemp_${reportYear}
        where CapId = #{capId}
        ]]>
    </delete>

    <delete id="deleteCapitalModelByCapIdzs">
    <![CDATA[
        delete from TREntCapital_${reportYear}
        where CapId = #{capId}
        ]]>
    </delete>

</mapper>
