<?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.aiccs.abnormal.mapper.FileUploadRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.abnormal.entity.FileUploadRecord">
        <id column="ID" property="id" />
        <result column="PRIPID" property="pripid" />
        <result column="UNISCID" property="uniscid" />
        <result column="ENTNAME" property="entname" />
        <result column="REGNO" property="regno" />
        <result column="UPLOAD_DIR" property="uploadDir" />
        <result column="CREATE_TIME" property="createTime" />
        <result column="UPDATE_TIME" property="updateTime" />
        <result column="ISYN" property="isyn" />
        <result column="MIGRATIONTYPE" property="migrationType" />
        <result column="RECONCILIATIONPAR" property="reconciliationPar" />
        <result column="PROCESSRECORD" property="processRecord" />
        <result column="MIN_ID" property="minId" />
        <result column="MOUT_ID" property="moutId" />
        <result column="entNamebf" property="entNamebf" />
    </resultMap>

    <!-- 带省份信息的查询结果映射 -->
    <resultMap id="FileUploadRecordVOMap" type="com.chinaweal.aiccs.aiccs.abnormal.entity.vo.FileUploadRecordVO">
        <id column="ID" property="id" />
        <result column="PRIPID" property="pripid" />
        <result column="UNISCID" property="uniscid" />
        <result column="ENTNAME" property="entname" />
        <result column="REGNO" property="regno" />
        <result column="UPLOAD_DIR" property="uploadDir" />
        <result column="CREATE_TIME" property="createTime" />
        <result column="UPDATE_TIME" property="updateTime" />
        <result column="ISYN" property="isyn" />
        <result column="MIGRATIONTYPE" property="migrationType" />
        <result column="RECONCILIATIONPAR" property="reconciliationPar" />
        <result column="PROCESSRECORD" property="processRecord" />
        <result column="MIN_ID" property="minId" />
        <result column="MOUT_ID" property="moutId" />
        <result column="PR_OUT" property="prOut" />
        <result column="PR_IN" property="prIn" />
        <result column="entNamebf" property="entNamebf" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID, PRIPID, UNISCID, ENTNAME, REGNO, UPLOAD_DIR, CREATE_TIME, UPDATE_TIME, ISYN, MIGRATIONTYPE, RECONCILIATIONPAR, PROCESSRECORD,MIN_ID,MOUT_ID,entNamebf
    </sql>

    <!-- 根据 PRIPID 查询企业基本信息 -->
    <select id="selectEntBaseInfoByPripid" resultMap="BaseResultMap">
        SELECT
            t1.UNISCID,
            t1.ENTNAME,
            t1.REGNO,
            t1.PRIPID
        FROM AICRI.E_BASEINFO t1
        WHERE t1.PRIPID = #{pripid}
        LIMIT 1
    </select>

    <!-- 批量插入文件上传记录 -->
    <insert id="insertBatch">
        INSERT INTO AICCS.FILE_UPLOAD_RECORD (ID, PRIPID, UNISCID, ENTNAME, REGNO, UPLOAD_DIR, CREATE_TIME, UPDATE_TIME, ISYN,
                                              MIGRATIONTYPE,RECONCILIATIONPAR,PROCESSRECORD,MIN_ID,MOUT_ID,entNamebf)
        VALUES
        <foreach collection="records" item="record" separator=",">
            (
                #{record.id,jdbcType=VARCHAR},
                #{record.pripid,jdbcType=VARCHAR},
                #{record.uniscid,jdbcType=VARCHAR},
                #{record.entname,jdbcType=VARCHAR},
                #{record.regno,jdbcType=VARCHAR},
                #{record.uploadDir,jdbcType=VARCHAR},
                #{record.createTime,jdbcType=TIMESTAMP},
                #{record.updateTime,jdbcType=TIMESTAMP},
                #{record.isyn,jdbcType=VARCHAR},
                #{record.migrationType,jdbcType=VARCHAR},
                #{record.reconciliationPar,jdbcType=VARCHAR},
                #{record.processRecord,jdbcType=VARCHAR},
                #{record.minId,jdbcType=VARCHAR},
                #{record.moutId,jdbcType=VARCHAR},
                #{record.entNamebf,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    <!-- 更新文件上传记录 -->
    <update id="updateRecord">
        UPDATE AICCS.FILE_UPLOAD_RECORD
        SET
            PRIPID = #{record.pripid,jdbcType=VARCHAR},
            UNISCID = #{record.uniscid,jdbcType=VARCHAR},
            ENTNAME = #{record.entname,jdbcType=VARCHAR},
            REGNO = #{record.regno,jdbcType=VARCHAR},
            UPLOAD_DIR = #{record.uploadDir,jdbcType=VARCHAR},
            UPDATE_TIME = #{record.updateTime,jdbcType=TIMESTAMP},
            ISYN = #{record.isyn,jdbcType=VARCHAR},
            MIGRATIONTYPE = #{record.migrationType,jdbcType=VARCHAR},
            RECONCILIATIONPAR = #{record.reconciliationPar,jdbcType=VARCHAR},
            PROCESSRECORD = #{record.processRecord,jdbcType=VARCHAR},
            MIN_ID = #{record.minId,jdbcType=VARCHAR},
            MOUT_ID = #{record.moutId,jdbcType=VARCHAR},
            entNamebf = #{record.entNamebf,jdbcType=VARCHAR}
        WHERE ID = #{record.id,jdbcType=VARCHAR}
    </update>

    <!-- 根据 PRIPID 和文件路径查询记录 -->
    <select id="selectByPripidAndPath" resultMap="BaseResultMap">
        SELECT
            <include refid="Base_Column_List"/>
        FROM AICCS.FILE_UPLOAD_RECORD
        WHERE PRIPID = #{pripid,jdbcType=VARCHAR}
          AND UPLOAD_DIR = #{uploadDir,jdbcType=VARCHAR}
        ORDER BY CREATE_TIME DESC
        LIMIT 1
    </select>

    <!-- 检查企业是否已经迁出成功 -->
    <select id="countMoveOutSuccessByPripid" resultType="int">
        SELECT COUNT(1)
        FROM AICCS.FILE_UPLOAD_RECORD
        WHERE PRIPID = #{pripid,jdbcType=VARCHAR} and MOUT_ID =  #{moutId,jdbcType=VARCHAR}
          AND MIGRATIONTYPE = 1
          AND ISYN = 'Y'
    </select>

    <!-- 检查企业是否已经迁入成功 -->
    <select id="countMoveInSuccessByPripid" resultType="int">
        SELECT COUNT(1)
        FROM AICCS.FILE_UPLOAD_RECORD
        WHERE PRIPID = #{pripid,jdbcType=VARCHAR} and MIN_ID =  #{minId,jdbcType=VARCHAR}
          AND MIGRATIONTYPE = 2
          AND ISYN = 'Y'
    </select>

    <!-- 根据统一社会信用代码和企业名称查询文件上传记录列表（包含省份信息） -->
    <select id="selectFileUploadRecordsWithProvince" resultMap="FileUploadRecordVOMap">
        SELECT
            fur.ID,
            fur.PRIPID,
            fur.UNISCID,
            fur.ENTNAME,
            fur.REGNO,
            fur.UPLOAD_DIR,
            fur.CREATE_TIME,
            fur.UPDATE_TIME,
            fur.ISYN,
            fur.MIGRATIONTYPE,
            fur.RECONCILIATIONPAR,
            fur.PROCESSRECORD,
            fur.MIN_ID,
            fur.MOUT_ID,
            CASE
                WHEN fur.MIGRATIONTYPE = 2 THEN (
                    SELECT emi.MOUTAREREGORG
                    FROM CRGS.E_MOVE_IN emi
                    WHERE emi.MIN_ID = fur.MIN_ID
                )
                ELSE NULL
            END AS PR_OUT,
            CASE
                WHEN fur.MIGRATIONTYPE = 1 THEN (
                    SELECT emo.MINAREREGORG
                    FROM CRGS.E_MOVE_OUT emo
                    WHERE emo.MOUT_ID = fur.MOUT_ID
                )
                ELSE NULL
            END AS PR_IN,
            fur.entNamebf
        FROM AICCS.FILE_UPLOAD_RECORD fur
        WHERE 1=1
        <if test="uniscid != null and uniscid != ''">
            AND fur.UNISCID LIKE CONCAT('%', #{uniscid,jdbcType=VARCHAR}, '%')
        </if>
        <if test="entname != null and entname != ''">
            AND fur.ENTNAME LIKE CONCAT('%', #{entname,jdbcType=VARCHAR}, '%')
        </if>
        ORDER BY fur.CREATE_TIME DESC
    </select>

</mapper>
