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

    <resultMap type="DiaFillOutReport" id="DiaFillOutReportResult">
        <result property="id" column="id"/>
        <result property="completionStatus" column="completion_status"/>
        <result property="reasonForIncompletion" column="reason_for_incompletion"/>
        <result property="improvementActions" column="improvement_actions"/>
        <result property="userId" column="user_id"/>
    </resultMap>

    <sql id="selectDiaFillOutReportVo">
        SELECT id, reason_for_incompletion, improvement_actions,user_id
        FROM dia_report_submissions
    </sql>

    <select id="selectDiaFillOutReportList" parameterType="DiaFillOutReport" resultMap="DiaFillOutReportResult">
        SELECT
            d.*,
            s.completion_status,
            s.reason_for_incompletion,
            s.improvement_actions,
            dept.dept_name
        FROM
            dia_report_details d
                LEFT JOIN dia_report_submissions s
                          ON d.id = s.id

                LEFT JOIN sys_dept dept
                          ON d.college_id = dept.dept_id
        WHERE
            d.user_id = #{userId};
    </select>

    <select id="selectDiaFillOutReportListUser" parameterType="String" resultMap="DiaFillOutReportResult">
        SELECT
            d.*,
            s.completion_status,
            s.reason_for_incompletion,
            s.improvement_actions,
            dept.dept_name
        FROM
            dia_report_details d
                LEFT JOIN dia_report_submissions s
                          ON d.id = s.id

                LEFT JOIN sys_dept dept
                          ON d.college_id = dept.dept_id
        WHERE
            d.stu_no = #{stuNo};
    </select>

    <insert id="insertDiaFillOutReport"  parameterType="DiaFillOutReport" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO dia_report_submissions
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="completionStatus != null">completion_status,</if>
            <if test="reasonForIncompletion != null">reason_for_incompletion,</if>
            <if test="improvementActions != null">improvement_actions,</if>
            created_at,
            updated_at
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="completionStatus != null">#{completionStatus},</if>
            <if test="reasonForIncompletion != null">#{reasonForIncompletion},</if>
            <if test="improvementActions != null">#{improvementActions},</if>
            NOW(),
            NOW()
        </trim>
    </insert>

    <insert id="insertDiaFillOutReportList" parameterType="java.util.List">
        INSERT INTO dia_report_submissions
        ( completion_status, reason_for_incompletion, improvement_actions, updated_at,user_id)
        VALUES
        <foreach collection="list" item="item" separator=",">
            ( #{item.completionStatus}, #{item.reasonForIncompletion}, #{item.improvementActions}, NOW(),  #{item.userId})
        </foreach>
    </insert>

    <update id="updateDiaFillOutReportList" parameterType="java.util.List">
        <foreach collection="list" item="item" separator=";">
            UPDATE dia_report_submissions s
            LEFT JOIN dia_report_details d ON s.id = d.id
            <set>
                <if test="item.completionStatus != null">s.completion_status = #{item.completionStatus},</if>
                <if test="item.reasonForIncompletion != null">s.reason_for_incompletion = #{item.reasonForIncompletion},</if>
                <if test="item.improvementActions != null">s.improvement_actions = #{item.improvementActions},</if>
                s.updated_at = NOW()
            </set>
            WHERE
            d.user_id = #{item.userId}
            AND s.id = #{item.id}
        </foreach>
    </update>

</mapper>
