<?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.TrcorrectionsMapper">

    <!-- 动态表名拼接：trcorrections_${reportYear}（年度来源：参数或实体） -->
    <sql id="dynamicTableName">
        trcorrectionsTemp_${reportYear}
    </sql>

    <sql id="dynamicTableNamezs">
        trcorrections_${reportYear}
    </sql>

    <!-- 公共查询字段 -->
    <sql id="baseSelectFields">
        correctionsid,
        entid,
        entityno,
        subitemid,
        parentitem,
        subitem,
        subitemcolumn,
        correctionsbefore,
        correctionsafter,
        correctionsdate,
        corrserialize,
        corrType,
        isshow,
        ReportYear
    </sql>

    <!-- 1. 动态表名 - 插入 -->
    <insert id="insertDynamic" parameterType="com.chinaweal.aiccs.aicepsqy.entity.datamove.TrcorrectionsModel">
        INSERT INTO
        <include refid="dynamicTableName"/>
        (
        correctionsid,
        entid,
        entityno,
        subitemid,
        parentitem,
        subitem,
        subitemcolumn,
        correctionsbefore,
        correctionsafter,
        correctionsdate,
        corrserialize,
        corrType,
        isshow,
        ReportYear
        )
        VALUES
        (
        #{correctionsid,jdbcType=VARCHAR},
        #{entid,jdbcType=VARCHAR},
        #{entityno,jdbcType=VARCHAR},
        #{subitemid,jdbcType=VARCHAR},
        #{parentitem,jdbcType=VARCHAR},
        #{subitem,jdbcType=VARCHAR},
        #{subitemcolumn,jdbcType=VARCHAR},
        #{correctionsbefore,jdbcType=VARCHAR},
        #{correctionsafter,jdbcType=VARCHAR},
        #{correctionsdate},
        #{corrserialize,jdbcType=VARCHAR},
        #{corrType,jdbcType=VARCHAR},
        #{isshow,jdbcType=CHAR},
        #{reportYear}
        )
    </insert>

    <insert id="insertDynamiczs" parameterType="com.chinaweal.aiccs.aicepsqy.entity.datamove.TrcorrectionsModel">
        INSERT INTO
        <include refid="dynamicTableNamezs"/>
        (
        correctionsid,
        entid,
        entityno,
        subitemid,
        parentitem,
        subitem,
        subitemcolumn,
        correctionsbefore,
        correctionsafter,
        correctionsdate,
        corrserialize,
        corrType,
        isshow,
        ReportYear
        )
        VALUES
        (
        #{correctionsid,jdbcType=VARCHAR},
        #{entid,jdbcType=VARCHAR},
        #{entityno,jdbcType=VARCHAR},
        #{subitemid,jdbcType=VARCHAR},
        #{parentitem,jdbcType=VARCHAR},
        #{subitem,jdbcType=VARCHAR},
        #{subitemcolumn,jdbcType=VARCHAR},
        #{correctionsbefore,jdbcType=VARCHAR},
        #{correctionsafter,jdbcType=VARCHAR},
        #{correctionsdate},
        #{corrserialize,jdbcType=VARCHAR},
        #{corrType,jdbcType=VARCHAR},
        #{isshow,jdbcType=CHAR},
        #{reportYear}
        )
    </insert>

    <!-- 2. 按 主键correctionsid + 年度reportYear 查询单条记录 -->
    <select id="selectByPrimaryKey" resultType="com.chinaweal.aiccs.aicepsqy.entity.datamove.TrcorrectionsModel">
        SELECT
        <include refid="baseSelectFields"/>
        FROM
        <include refid="dynamicTableNamezs"/>
        WHERE
        correctionsid = #{correctionsid,jdbcType=VARCHAR}
        <!-- 可选：ReportYear 字段与表名年度一致，双重校验 -->
        AND ReportYear = #{reportYear}
    </select>

    <!-- 3. 条件查询（支持主键+多条件组合） -->
    <select id="selectByCondition" parameterType="com.chinaweal.aiccs.aicepsqy.entity.datamove.TrcorrectionsModel" resultType="com.chinaweal.aiccs.aicepsqy.entity.datamove.TrcorrectionsModel">
        SELECT
        <include refid="baseSelectFields"/>
        FROM
        <include refid="dynamicTableName"/>
        <where>
            <!-- 主键条件（可选） -->
            <if test="correctionsid != null and correctionsid != ''">
                AND correctionsid = #{correctionsid,jdbcType=VARCHAR}
            </if>
            <!-- 企业ID条件（可选） -->
            <if test="entid != null and entid != ''">
                AND entid = #{entid,jdbcType=VARCHAR}
            </if>
            <!-- 子项ID条件（可选） -->
            <if test="subitemid != null and subitemid != ''">
                AND subitemid = #{subitemid,jdbcType=VARCHAR}
            </if>
            <!-- 修正类型条件（可选） -->
            <if test="corrType != null and corrType != ''">
                AND corrType = #{corrType,jdbcType=VARCHAR}
            </if>
            <!-- 年度双重校验（与表名一致） -->
            AND ReportYear = #{reportYear}
        </where>
        ORDER BY
        correctionsdate DESC
    </select>

    <!-- 4. 按 主键correctionsid + 年度reportYear 删除单条记录 -->
    <delete id="deleteByPrimaryKey">
        DELETE FROM
        <include refid="dynamicTableName"/>
        WHERE
        correctionsid = #{correctionsid,jdbcType=VARCHAR}
        AND ReportYear = #{reportYear}
    </delete>

    <delete id="deleteByPrimaryKeyzs">
        DELETE FROM
        <include refid="dynamicTableNamezs"/>
        WHERE
        correctionsid = #{correctionsid,jdbcType=VARCHAR}
        AND ReportYear = #{reportYear}
    </delete>

    <!-- 5. 批量删除（多个correctionsid + 统一reportYear） -->
    <delete id="deleteBatchByPrimaryKeys">
        DELETE FROM
        <include refid="dynamicTableName"/>
        WHERE
        correctionsid IN
        <foreach collection="correctionsids" item="correctionsid" open="(" separator="," close=")">
            #{correctionsid,jdbcType=VARCHAR}
        </foreach>
        AND ReportYear = #{reportYear}
    </delete>

    <!-- 6. 清空指定年度表数据（谨慎使用！） -->
    <delete id="truncateDynamic">
        TRUNCATE TABLE
        <include refid="dynamicTableName"/>
    </delete>

</mapper>
