<?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.ym.mec.biz.dal.dao.InspectionItemPlanConclusionDao">
    <resultMap id="InspectionItemPlanConclusion" type="com.ym.mec.biz.dal.entity.InspectionItemPlanConclusion">
        <!--@mbg.generated-->
        <!--@Table inspection_item_plan_conclusion-->
        <id column="id_" property="id"/>
        <result column="plan_id_" property="planId"/>
        <result column="course_schedule_id_" property="courseScheduleId"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="check_item_" property="checkItem"/>
        <result column="check_conclusion_" property="checkConclusion"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="classInfo" property="classInfo"/>
		<result column="tenant_id_" property="tenantId" />
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id_, plan_id_,course_schedule_id_, teacher_id_, check_item_, check_conclusion_, create_time_, update_time_, tenant_id_
    </sql>
    <select id="get" resultMap="InspectionItemPlanConclusion">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from inspection_item_plan_conclusion
        where id_ = #{id}
    </select>
    <delete id="delete">
        <!--@mbg.generated-->
        delete from inspection_item_plan_conclusion
        where id_ = #{id}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id"
            parameterType="com.ym.mec.biz.dal.entity.InspectionItemPlanConclusion" useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into inspection_item_plan_conclusion (plan_id_,course_schedule_id_, teacher_id_,check_item_,
        check_conclusion_, create_time_,update_time_,tenant_id_)
        values (#{planId}, #{courseScheduleId},#{teacherId}, #{checkItem}, #{checkConclusion},#{createTime},
        #{updateTime},#{tenantId})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.InspectionItemPlanConclusion">
        <!--@mbg.generated-->
        update inspection_item_plan_conclusion
        <set>
            <if test="planId != null">
                plan_id_ = #{planId},
            </if>
            <if test="courseScheduleId != null != null">
                course_schedule_id_ = #{courseScheduleId},
            </if>
            <if test="teacherId != null">
                teacher_id_ = #{teacherId},
            </if>
            <if test="checkItem != null">
                check_item_ = #{checkItem},
            </if>
            <if test="checkConclusion != null">
                check_conclusion_ = #{checkConclusion},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
        </set>
        where id_ = #{id} and tenant_id_ = #{tenantId}
    </update>
    <delete id="delByPlanIdAndCheckItem">
        DELETE
        FROM inspection_item_plan_conclusion
        WHERE plan_id_ = #{planId}
          ANd check_item_ = #{checkItem}
    </delete>
    <insert id="batchAdd" keyColumn="id_" keyProperty="id"
            parameterType="com.ym.mec.biz.dal.entity.InspectionItemPlanConclusion"
            useGeneratedKeys="true">
        INSERT INTO inspection_item_plan_conclusion (plan_id_,course_schedule_id_, teacher_id_, check_item_,
        check_conclusion_,create_time_,update_time_,tenant_id_)
        VALUE
        <foreach collection="inspectionItemPlanConclusions" item="inspectionItemPlanConclusion" separator=",">
            (#{inspectionItemPlanConclusion.planId},#{inspectionItemPlanConclusion.courseScheduleId},
            #{inspectionItemPlanConclusion.teacherId},
            #{inspectionItemPlanConclusion.checkItem},
            #{inspectionItemPlanConclusion.checkConclusion}, #{inspectionItemPlanConclusion.createTime},
            #{inspectionItemPlanConclusion.updateTime},#{inspectionItemPlanConclusion.tenantId})
        </foreach>
    </insert>

    <select id="getLastCheckItem" resultType="com.ym.mec.biz.dal.dto.CheckItemDto">
        SELECT plan_id_ planId,MAX(check_item_) checkItem FROM inspection_item_plan_conclusion WHERE plan_id_ IN
        <foreach collection="planIds" item="planId" open="(" close=")" separator=",">
            #{planId}
        </foreach>
        GROUP BY plan_id_
    </select>

    <select id="getInspectionItemPlanConclusion" resultMap="InspectionItemPlanConclusion">
        SELECT iipc.*,CONCAT(cg.name_, ' - ', su.real_name_) classInfo FROM inspection_item_plan_conclusion iipc
        LEFT JOIN course_schedule cs ON iipc.course_schedule_id_ = cs.id_
        LEFT JOIN class_group cg ON cg.id_=cs.class_group_id_
        LEFT JOIN sys_user su on su.id_ = iipc.teacher_id_
        WHERE iipc.plan_id_=#{planId} AND cs.pre_course_flag_ = 0
        <if test="checkItem != null">
            AND iipc.check_item_ = #{checkItem}
        </if>
        ORDER BY iipc.check_item_ ASC,iipc.course_schedule_id_ ASC
    </select>

    <select id="getPlanConclusionCourses" resultMap="com.ym.mec.biz.dal.dao.CourseScheduleDao.CourseSchedule">
        SELECT iipc.course_schedule_id_               id_,
               iipc.teacher_id_                       actual_teacher_id_,
               CONCAT(cg.name_, ' - ', su.real_name_) name_
        FROM inspection_item_plan_conclusion iipc
                 LEFT JOIN course_schedule cs ON iipc.course_schedule_id_ = cs.id_
                 LEFT JOIN class_group cg ON cg.id_ = cs.class_group_id_
                 LEFT JOIN sys_user su on iipc.teacher_id_ = su.id_
        WHERE plan_id_ = #{planId}
        GROUP BY iipc.course_schedule_id_, iipc.teacher_id_
        ORDER BY iipc.course_schedule_id_ ASC
    </select>

    <select id="getCountByPlanIdAndCheckConclusion" resultType="int">
        SELECT COUNT(*)
        FROM inspection_item_plan_conclusion
        WHERE plan_id_ = #{planId}
          AND check_conclusion_ = #{checkConclusion}
    </select>
</mapper>