<?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.srs.diagnostic.mapper.DiaReportDetailsMapper">

    <resultMap type="DiaReportDetails" id="DiaReportDetailsResult">
        <result property="id" column="id"/>
        <result property="relId" column="relId"/>
        <result property="reportName" column="reportName"/>
        <result property="userId" column="user_id"/>
        <result property="stuNo" column="stu_no"/>
        <result property="studentName" column="student_name"/>
        <result property="submitterType" column="submitter_type"/>
        <result property="collegeId" column="college_id"/>
        <result property="collegeName" column="college_name"/>
        <result property="status" column="status"/>
        <result property="operatorId" column="operator_id"/>
        <result property="createdAt" column="created_at"/>
        <result property="updatedAt" column="updated_at"/>
        <result property="reportId" column="report_id"/>
        <result property="dimensionId" column="dimension_id"/>
        <result property="dimensionName" column="dimension_name"/>
        <result property="qualityControlPointId" column="quality_control_point_id"/>
        <result property="qualityControlPointName" column="quality_control_point_name"/>
        <result property="indicatorId" column="indicator_id"/>
        <result property="indicatorName" column="indicator_name"/>
        <result property="schoolQualifiedStandard" column="school_qualified_standard"/>
        <result property="secondaryCollegeStandard" column="secondary_college_standard"/>
        <result property="personalTargetStandard" column="personal_target_standard"/>
        <result property="detailsStatus" column="details_status"/>
    </resultMap>

    <sql id="selectDiaReportDetailsVo">
        SELECT id, relId, reportName, user_id, stu_no, student_name, submitter_type, college_id, college_name,
               status, operator_id, created_at, updated_at, report_id, dimension_id, dimension_name,
               quality_control_point_id, quality_control_point_name, indicator_id, indicator_name,
               school_qualified_standard, secondary_college_standard, personal_target_standard, details_status
        FROM dia_report_details d
    </sql>

    <select id="selectDiaReportDetailsList" parameterType="DiaReportDetails" resultMap="DiaReportDetailsResult">
        SELECT
        d.id,
        d.relId,
        d.reportName,
        d.user_id,
        d.stu_no,
        u.nick_name AS student_name,
        d.submitter_type,
        d.college_id,
        p.dept_name AS college_name,
        d.status,
        d.operator_id,
        d.created_at,
        d.updated_at,
        d.report_id,
        d.dimension_id,
        d.dimension_name,
        d.quality_control_point_id,
        d.quality_control_point_name,
        d.indicator_id,
        d.indicator_name,
        d.school_qualified_standard,
        d.secondary_college_standard,
        d.personal_target_standard,
        d.details_status
        FROM dia_report_details d
        LEFT JOIN sys_user u ON d.user_id = u.user_id
        LEFT JOIN sys_dept p ON d.college_id = p.dept_id

        <where>

            <!-- 层级查询条件 -->
            <if test="params.reportId != null">
                AND d.report_id = #{params.reportId}
            </if>
            <if test="params.dimensionId != null">
                AND d.dimension_id = #{params.dimensionId}
            </if>
            <if test="params.qualityControlPointId != null">
                AND d.quality_control_point_id = #{params.qualityControlPointId}
            </if>
            <if test="indicatorId != null">
                AND d.indicator_id = #{params.indicatorId}
            </if>

            <!-- 根据用户ID查询 -->
            <if test="userId != null">
                AND d.user_id = #{userId}
            </if>

            <!-- 层级关系查询 -->
            <if test="params.needHierarchy != null and params.needHierarchy == true">
                AND EXISTS (
                SELECT 1 FROM dia_reports r1
                LEFT JOIN dia_reports r2 ON r2.parentId = r1.reportId
                LEFT JOIN dia_reports r3 ON r3.parentId = r2.reportId
                LEFT JOIN dia_reports r4 ON r4.parentId = r3.reportId
                WHERE r1.parentId = 0
                AND (
                d.report_id = r1.reportId
                OR d.dimension_id = r2.reportId
                OR d.quality_control_point_id = r3.reportId
                OR d.indicator_id = r4.reportId
                )
                <if test="params.topReportName != null and params.topReportName != ''">
                    AND r1.name LIKE concat('%', #{params.topReportName}, '%')
                </if>
                )
            </if>
        </where>

        ORDER BY d.report_id, d.dimension_id, d.quality_control_point_id, d.indicator_id
    </select>


    <!--查询后进行修改名字-->
    <update id="updateStudentNameFromNickName" parameterType="com.srs.diagnostic.domain.DiaReportDetails">
        UPDATE dia_report_details d
        LEFT JOIN sys_user u ON d.user_id = u.user_id
        LEFT JOIN sys_dept p ON d.college_id = p.dept_id
        SET
        d.student_name = u.nick_name,
        d.college_name = p.dept_name
        WHERE
        d.student_name IS NULL
        <if test="userId != null">
            AND u.user_id = #{userId}
        </if>
    </update>

    <!-- 新增的批量插入方法，基于层级关系 -->
    <insert id="batchInsertReportDetailsByHierarchy" parameterType="java.util.List">
        INSERT INTO dia_report_details (
            relId, report_id, dimension_id, dimension_name,
            quality_control_point_id, quality_control_point_name,
            indicator_id, indicator_name, reportName,
            user_id, stu_no, student_name, submitter_type,
            college_id, college_name, status, operator_id,
            created_at, updated_at, details_status
        )
        SELECT
            #{list[0].userId} AS relId,
            r1.reportId AS report_id,
            r2.reportId AS dimension_id,
            r2.name AS dimension_name,
            r3.reportId AS quality_control_point_id,
            r3.name AS quality_control_point_name,
            r4.reportId AS indicator_id,
            r4.name AS indicator_name,
            r1.name AS reportName,
            #{list[0].userId} AS user_id,
            #{list[0].stuNo} AS stu_no,
            #{list[0].studentName} AS student_name,
            #{list[0].submitterType} AS submitter_type,
            #{list[0].collegeId} AS college_id,
            #{list[0].collegeName} AS college_name,
            '0' AS status,
            #{list[0].operatorId} AS operator_id,
            NOW() AS created_at,
            NOW() AS updated_at,
            '0' AS details_status
        FROM
            dia_reports r1
                LEFT JOIN dia_reports r2 ON r2.parentId = r1.reportId
                LEFT JOIN dia_reports r3 ON r3.parentId = r2.reportId
                LEFT JOIN dia_reports r4 ON r4.parentId = r3.reportId
        WHERE
            r1.parentId = 0
          AND r2.reportId IS NOT NULL

            ON DUPLICATE KEY UPDATE
                                 updated_at = VALUES(updated_at),
                                 status = VALUES(status);
    </insert>

    <insert id="insertDiaStudentInfo" parameterType="DiaReportDetails" useGeneratedKeys="true" keyProperty="id">
        insert into dia_student_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stuNo != null and studentNumber != ''">stu_no,</if>
            <if test="collegeId != null">college_id,</if>
            <if test="collegeName != null and collegeName != ''">college_name,</if>
            <if test="majorId != null">major_id,</if>
            <if test="majorName != null and majorName != ''">major_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="studentName != null and studentName != ''">student_name,</if>
            <if test="semesterId != null">semester_id,</if>
            <if test="semesterName != null and semesterName != ''">semester_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stuNo != null and studentNumber != ''">#{stuNo},</if>
            <if test="collegeId != null">#{collegeId},</if>
            <if test="collegeName != null and collegeName != ''">#{collegeName},</if>
            <if test="majorId != null">#{deptId},</if>
            <if test="majorName != null and majorName != ''">#{deptName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="studentName != null and studentName != ''">#{studentName},</if>
            <if test="semesterId != null">#{semesterId},</if>
            <if test="semesterName != null and semesterName != ''">#{semesterName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <select id="selectDiaReportDetailsById" parameterType="Long" resultMap="DiaReportDetailsResult">
        <include refid="selectDiaReportDetailsVo"/>
        WHERE id = #{id}
    </select>

    <insert id="insertDiaReportDetails"  parameterType="DiaReportDetails" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO dia_report_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="relId != null">relId,</if>
            <if test="reportName != null">reportName,</if>
            <if test="userId != null">user_id,</if>
            <if test="stuNo != null">stu_no,</if>
            <if test="studentName != null">student_name,</if>
            <if test="submitterType != null">submitter_type,</if>
            <if test="collegeId != null">college_id,</if>
            <if test="collegeName != null">college_name,</if>
            <if test="status != null">status,</if>
            <if test="operatorId != null">operator_id,</if>
            <if test="reportId != null">report_id,</if>
            <if test="dimensionId != null">dimension_id,</if>
            <if test="dimensionName != null">dimension_name,</if>
            <if test="qualityControlPointId != null">quality_control_point_id,</if>
            <if test="qualityControlPointName != null">quality_control_point_name,</if>
            <if test="indicatorId != null">indicator_id,</if>
            <if test="indicatorName != null">indicator_name,</if>
            <if test="schoolQualifiedStandard != null">school_qualified_standard,</if>
            <if test="secondaryCollegeStandard != null">secondary_college_standard,</if>
            <if test="personalTargetStandard != null">personal_target_standard,</if>
            <if test="detailsStatus != null">details_status,</if>
            created_at,
            updated_at
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="relId != null">#{relId},</if>
            <if test="reportName != null">#{reportName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="stuNo != null">#{stuNo},</if>
            <if test="studentName != null">#{studentName},</if>
            <if test="submitterType != null">#{submitterType},</if>
            <if test="collegeId != null">#{collegeId},</if>
            <if test="collegeName != null">#{collegeName},</if>
            <if test="status != null">#{status},</if>
            <if test="operatorId != null">#{operatorId},</if>
            <if test="reportId != null">#{reportId},</if>
            <if test="dimensionId != null">#{dimensionId},</if>
            <if test="dimensionName != null">#{dimensionName},</if>
            <if test="qualityControlPointId != null">#{qualityControlPointId},</if>
            <if test="qualityControlPointName != null">#{qualityControlPointName},</if>
            <if test="indicatorId != null">#{indicatorId},</if>
            <if test="indicatorName != null">#{indicatorName},</if>
            <if test="schoolQualifiedStandard != null">#{schoolQualifiedStandard},</if>
            <if test="secondaryCollegeStandard != null">#{secondaryCollegeStandard},</if>
            <if test="personalTargetStandard != null">#{personalTargetStandard},</if>
            <if test="detailsStatus != null">#{detailsStatus},</if>
            NOW(),
            NOW()
        </trim>
    </insert>

    <insert id="saveOrUpdate" parameterType="java.util.List">
        INSERT INTO dia_report_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            relId, reportName, user_id, stu_no, student_name,
            submitter_type, college_id, college_name, status,
            operator_id, report_id, dimension_id, dimension_name,
            quality_control_point_id, quality_control_point_name,
            indicator_id, indicator_name, school_qualified_standard,
            secondary_college_standard, personal_target_standard,
            details_status, created_at, updated_at
        </trim>
        VALUES
        <foreach collection="list" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.relId}, #{item.reportName}, #{item.userId},
                #{item.stuNo}, #{item.studentName}, #{item.submitterType},
                #{item.collegeId}, #{item.collegeName}, #{item.status},
                #{item.operatorId}, #{item.reportId}, #{item.dimensionId},
                #{item.dimensionName}, #{item.qualityControlPointId},
                #{item.qualityControlPointName}, #{item.indicatorId},
                #{item.indicatorName}, #{item.schoolQualifiedStandard},
                #{item.secondaryCollegeStandard}, #{item.personalTargetStandard},
                #{item.detailsStatus}, NOW(), NOW()
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="item" separator=";">
            UPDATE dia_report_details
            <set>
                school_qualified_standard = #{item.schoolQualifiedStandard},
                secondary_college_standard = #{item.secondaryCollegeStandard},
                personal_target_standard = #{item.personalTargetStandard},
                updated_at = NOW()
            </set>
            WHERE indicator_id = #{item.indicatorId}

            <if test="item.userId  != null">
                AND user_id = #{item.userId}
            </if>
        </foreach>
    </update>

    <update id="updateDiaReportDetails" parameterType="DiaReportDetails">
        UPDATE dia_report_details
        <set>
            <if test="relId != null">relId = #{relId},</if>
            <if test="reportName != null">reportName = #{reportName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="stuNo != null">stu_no = #{stuNo},</if>
            <if test="studentName != null">student_name = #{studentName},</if>
            <if test="submitterType != null">submitter_type = #{submitterType},</if>
            <if test="collegeId != null">college_id = #{collegeId},</if>
            <if test="collegeName != null">college_name = #{collegeName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="operatorId != null">operator_id = #{operatorId},</if>
            <if test="reportId != null">report_id = #{reportId},</if>
            <if test="dimensionId != null">dimension_id = #{dimensionId},</if>
            <if test="dimensionName != null">dimension_name = #{dimensionName},</if>
            <if test="qualityControlPointId != null">quality_control_point_id = #{qualityControlPointId},</if>
            <if test="qualityControlPointName != null">quality_control_point_name = #{qualityControlPointName},</if>
            <if test="indicatorId != null">indicator_id = #{indicatorId},</if>
            <if test="indicatorName != null">indicator_name = #{indicatorName},</if>
            <if test="schoolQualifiedStandard != null">school_qualified_standard = #{schoolQualifiedStandard},</if>
            <if test="secondaryCollegeStandard != null">secondary_college_standard = #{secondaryCollegeStandard},</if>
            <if test="personalTargetStandard != null">personal_target_standard = #{personalTargetStandard},</if>
            <if test="detailsStatus != null">details_status = #{detailsStatus},</if>
            updated_at = NOW()
        </set>
        WHERE id = #{id}
    </update>

    <delete id="deleteDiaReportDetailsById" parameterType="Long">
        DELETE FROM dia_report_details WHERE id = #{id}
    </delete>

    <delete id="deleteDiaReportDetailsByIds" parameterType="Long">
        DELETE FROM dia_report_details WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
