<?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.kindergartenCheckReport.KindergartenCheckReportMapper">

    <resultMap type="KindergartenCheckReportDTO" id="KindergartenCheckReportResult">
        <result property="id" column="id"/>
        <result property="kindergartenId" column="kindergarten_id"/>
        <result property="batchCode" column="batch_code"/>
        <result property="reportCode" column="report_code"/>
        <result property="reportName" column="report_name"/>
        <result property="physicalSynthAnalysis" column="physical_synth_analysis"/>
        <result property="downloadAddress" column="download_address"/>
        <result property="motionLibraryId" column="motion_library_id"/>
        <result property="checkUserId" column="check_user_id"/>
        <result property="checkTime" column="check_time"/>
        <result property="checkStatus" column="check_status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="kindergartenName" column="kindergarten_name"/>
        <result property="downloadAddress" column="download_address"/>
        <result property="checkUserName" column="check_user_name"/>
    </resultMap>

    <sql id="selectKindergartenCheckReportVo">
        select id,
               kindergarten_id,
               batch_code,
               report_code,
               report_name,
               physical_synth_analysis,
               download_address,
               motion_library_id,
               check_user_id,
               check_time,
               check_status,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from kindergarten_check_report
    </sql>

    <select id="selectKindergartenCheckReportList" parameterType="KindergartenCheckReportDTO"
            resultMap="KindergartenCheckReportResult">
        SELECT
        ki.kindergarten_name,
        ki.id kindergarten_id,
        tic.batch_code,
        kcr.report_code,
        kcr.report_name,
        kcr.check_status,
        u.user_name check_user_name,
        kcr.id,
        kcr.download_address,
        tsb.batch_name
        FROM
        test_item_score tic
        LEFT JOIN student_info si ON si.id = tic.student_id
        LEFT JOIN kindergarten_info ki ON ki.id = si.kindergarten_id
        LEFT JOIN kindergarten_check_report kcr ON kcr.batch_code = tic.batch_code AND kcr.kindergarten_id = ki.id
        LEFT JOIN test_score_batch tsb ON tsb.batch_code = tic.batch_code
        LEFT JOIN sys_user u ON kcr.check_user_id = u.user_id
        <where>
            ki.del_flag = 0
            <if test="kindergartenName != null and kindergartenName != ''">
                and ki.kindergarten_name like concat('%', #{kindergartenName}, '%')
            </if>
            <if test="kindergartenId != null ">
                and ki.id = #{kindergartenId}
            </if>
            <if test="batchCode != null  and batchCode != ''">
                and tic.batch_code = #{batchCode}
            </if>
            <if test="reportCode != null  and reportCode != ''">
                and kcr.report_code = #{reportCode}
            </if>
            <if test="reportName != null  and reportName != ''">
                and kcr.report_name like concat('%', #{reportName}, '%')
            </if>
            <if test="checkStatus != null ">
                and kcr.check_status = #{checkStatus}
            </if>
        </where>
        GROUP BY
        ki.id, tic.batch_code
        order by kcr.create_time desc
    </select>

    <select id="selectKindergartenCheckReportListAll" parameterType="string"
            resultType="com.xinyilu.system.domain.dto.KindergartenExamineAndApproveDTO">
        SELECT
        ki.kindergarten_name,
        ki.id kindergarten_id,
        tic.batch_code,
        tsb.batch_name
        FROM
        test_item_score tic
        LEFT JOIN student_info si ON si.id = tic.student_id
        LEFT JOIN kindergarten_info ki ON ki.id = si.kindergarten_id
        LEFT JOIN test_score_batch tsb ON tsb.batch_code = tic.batch_code
        <where>
            ki.del_flag = 0
            <if test="batchCode != null  and batchCode != ''">
                and tic.batch_code = #{batchCode}
            </if>
        </where>
        GROUP BY
        ki.id, tic.batch_code
    </select>

    <select id="selectKindergartenCheckReportPass" resultType="com.xinyilu.system.domain.dto.KindergartenExamineAndApproveDTO">
        SELECT
        kcf.kindergarten_id kindergarten_id,
        kcf.batch_code,
        kcf.report_name
        FROM
        kindergarten_check_report kcf
        <where>
            kcf.del_flag =0
            <if test="checkStatus != null">
                and kcf.check_status = #{checkStatus}
            </if>
            <if test="batchCode != null  and batchCode != ''">
                and kcf.batch_code = #{batchCode}
            </if>
        </where>
    </select>

    <select id="selectKindergartenCheckReportById" parameterType="Long"
            resultMap="KindergartenCheckReportResult">
        <include refid="selectKindergartenCheckReportVo"/>
        where id = #{id}
    </select>

    <insert id="insertKindergartenCheckReport" parameterType="KindergartenCheckReport" useGeneratedKeys="true"
            keyProperty="id">
        insert into kindergarten_check_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="kindergartenId != null">kindergarten_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="physicalSynthAnalysis != null">physical_synth_analysis,
            </if>
            <if test="downloadAddress != null">download_address,
            </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="kindergartenId != null">#{kindergartenId},
            </if>
            <if test="batchCode != null">#{batchCode},
            </if>
            <if test="reportCode != null">#{reportCode},
            </if>
            <if test="reportName != null">#{reportName},
            </if>
            <if test="physicalSynthAnalysis != null">#{physicalSynthAnalysis},
            </if>
            <if test="downloadAddress != null">#{downloadAddress},
            </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>

    <update id="updateKindergartenCheckReport" parameterType="KindergartenCheckReport">
        update kindergarten_check_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="kindergartenId != null">kindergarten_id =
                #{kindergartenId},
            </if>
            <if test="batchCode != null">batch_code =
                #{batchCode},
            </if>
            <if test="reportCode != null">report_code =
                #{reportCode},
            </if>
            <if test="reportName != null">report_name =
                #{reportName},
            </if>
            <if test="physicalSynthAnalysis != null">physical_synth_analysis =
                #{physicalSynthAnalysis},
            </if>
            <if test="downloadAddress != null">download_address =
                #{downloadAddress},
            </if>
            <if test="motionLibraryId != null">motion_library_id =
                #{motionLibraryId},
            </if>
            <if test="checkUserId != null">check_user_id =
                #{checkUserId},
            </if>
            <if test="checkTime != null">check_time =
                #{checkTime},
            </if>
            <if test="checkStatus != null">check_status =
                #{checkStatus},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteKindergartenCheckReportById" parameterType="Long">
        delete
        from kindergarten_check_report
        where id = #{id}
    </delete>

    <delete id="deleteKindergartenCheckReportByIds" parameterType="String">
        delete from kindergarten_check_report where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getKindergartenCheckReportInfo"
            resultType="com.xinyilu.system.domain.vo.personalcheckreport.ExistCheckReportVO">
        select id, download_address
        from kindergarten_check_report
        where kindergarten_id = #{kindergartenId}
          and batch_code = #{batchCode}
    </select>


    <delete id="deleteKindergartenReportInfo">
        delete
        from kindergarten_check_report
        where kindergarten_id = #{kindergartenId}
          and batch_code = #{batchCode}
    </delete>
</mapper>
