<?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.xinyilu.system.mapper.checkreport.CheckReportMapper">

    <resultMap type="TestBodyScoreInfoVO" id="PhysicalSubstanceTestInfo">
        <result property="id" column="id"/>
        <result property="studentId" column="student_id"/>
        <result property="testItemId" column="test_item_id"/>
        <result property="scoreDictValue" column="score_dict_value"/>
        <result property="batchCode" column="batch_code"/>
        <result property="itemScore" column="item_score"/>
        <result property="syntheticalAssess" column="synthetical_assess"/>
        <result property="itemName" column="item_name"/>
        <result property="measureUnit" column="measure_unit"/>
        <result property="scoreDictId" column="score_dict_id"/>
        <collection property="testItemLevelList" javaType="java.util.List" resultMap="TestItemLevelInfo"/>
    </resultMap>

    <resultMap type="TestItemLevel" id="TestItemLevelInfo">
        <result property="itemId" column="item_id"/>
        <result property="levelName" column="level_name"/>
        <result property="levelMinScore" column="level_min_score"/>
        <result property="levelMaxScore" column="level_max_score"/>
    </resultMap>

    <select id="getPhysicalSubstanceTestInfo" parameterType="TestBodyScoreInfoVO" resultMap="PhysicalSubstanceTestInfo">
        SELECT
        ti.item_name,
        ti.measure_unit,
        tis.id,
        tis.student_id,
        tis.test_item_id,
        tis.score_dict_value,
        tis.batch_code,
        tis.item_score,
        tis.synthetical_assess,
        tis.score_dict_id,
        til.item_id,
        til.level_name,
        til.level_min_score,
        til.level_max_score
        FROM
        test_item_score tis
        LEFT JOIN test_items ti ON ti.id = tis.test_item_id
        LEFT JOIN test_item_level til ON til.item_id = ti.id
        <where>
            <if test="studentId != null ">
                and tis.student_id = #{studentId}
            </if>
            <if test="batchCode != null  and batchCode != ''">
                and tis.batch_code = #{batchCode}
            </if>
            and tis.del_flag = 0
        </where>
        order by tis.create_time desc
    </select>

    <insert id="insertStudentPersonReportInfo" parameterType="StudentPersonalReportDTO" useGeneratedKeys="true"
            keyProperty="id">
        insert into personal_check_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="studentId != null">student_id,
            </if>
            <if test="batchCode != null">batch_code,
            </if>
            <if test="reportCode != null">report_code,
            </if>
            <if test="reportName != null">report_name,
            </if>
            <if test="syntheticalAssess != null">synthetical_assess,
            </if>
            <if test="physicalAnalysisExplain != null">physical_analysis_explain,
            </if>
            <if test="unqualifyItemExplain != null">unqualify_item_explain,
            </if>
            <if test="downloadAddress != null">download_address,
            </if>
            <if test="prescriptionAdvice != null">prescription_advice,
            </if>
            <if test="motionLibraryId != null">motion_library_id,
            </if>
            <if test="checkUserId != null">check_user_id,
            </if>
            <if test="checkTime != null">check_time,
            </if>
            <if test="checkStatus != null">check_status,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="studentId != null">#{studentId},
            </if>
            <if test="batchCode != null">#{batchCode},
            </if>
            <if test="reportCode != null">#{reportCode},
            </if>
            <if test="reportName != null">#{reportName},
            </if>
            <if test="syntheticalAssess != null">#{syntheticalAssess},
            </if>
            <if test="physicalAnalysisExplain != null">#{physicalAnalysisExplain},
            </if>
            <if test="unqualifyItemExplain != null">#{unqualifyItemExplain},
            </if>
            <if test="downloadAddress != null">#{downloadAddress},
            </if>
            <if test="prescriptionAdvice != null">#{prescriptionAdvice},
            </if>
            <if test="motionLibraryId != null">#{motionLibraryId},
            </if>
            <if test="checkUserId != null">#{checkUserId},
            </if>
            <if test="checkTime != null">#{checkTime},
            </if>
            <if test="checkStatus != null">#{checkStatus},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <delete id="deleteStudentPersonReportInfo">
        delete from personal_check_report where student_id = #{studentId} and batch_code = #{batchCode}
    </delete>
</mapper>