<?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.ruoyi.ccdi.project.mapper.CcdiFileUploadRecordMapper">

    <resultMap type="com.ruoyi.ccdi.project.domain.entity.CcdiFileUploadRecord" id="CcdiFileUploadRecordResult">
        <id     property="id"               column="id"                 />
        <result property="projectId"       column="project_id"         />
        <result property="lsfxProjectId"   column="lsfx_project_id"    />
        <result property="logId"           column="log_id"             />
        <result property="fileName"        column="file_name"          />
        <result property="fileSize"        column="file_size"          />
        <result property="fileStatus"      column="file_status"        />
        <result property="sourceType"      column="source_type"        />
        <result property="sourceProjectId" column="source_project_id"  />
        <result property="sourceProjectName" column="source_project_name" />
        <result property="enterpriseNames" column="enterprise_names"   />
        <result property="accountNos"      column="account_nos"        />
        <result property="errorMessage"    column="error_message"      />
        <result property="uploadTime"      column="upload_time"        />
        <result property="uploadUser"      column="upload_user"        />
    </resultMap>

    <sql id="selectCcdiFileUploadRecordVo">
        select id, project_id, lsfx_project_id, log_id, file_name, file_size,
               file_status, source_type, source_project_id, source_project_name,
               enterprise_names, account_nos, error_message, upload_time, upload_user
        from ccdi_file_upload_record
    </sql>

    <!-- 批量插入 -->
    <insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into ccdi_file_upload_record (
            project_id, lsfx_project_id, log_id, file_name, file_size, file_status,
            source_type, source_project_id, source_project_name,
            enterprise_names, account_nos, upload_time, upload_user
        ) values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.projectId}, #{item.lsfxProjectId}, #{item.logId}, #{item.fileName},
                #{item.fileSize}, #{item.fileStatus}, #{item.sourceType},
                #{item.sourceProjectId}, #{item.sourceProjectName},
                #{item.enterpriseNames}, #{item.accountNos}, #{item.uploadTime}, #{item.uploadUser}
            )
        </foreach>
    </insert>

    <select id="selectSuccessfulRecordsByProjectIds" resultMap="CcdiFileUploadRecordResult">
        <include refid="selectCcdiFileUploadRecordVo"/>
        where project_id in
        <foreach collection="projectIds" item="projectId" open="(" separator="," close=")">
            #{projectId}
        </foreach>
          and file_status = 'parsed_success'
          and log_id is not null
        order by project_id asc, log_id asc, id asc
    </select>

    <!-- 统计各状态文件数量 -->
    <select id="countByStatus" resultType="java.util.Map">
        select file_status as `status`, count(*) as count
        from ccdi_file_upload_record
        where project_id = #{projectId}
        group by file_status
    </select>

</mapper>
