<?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.pangu.school.mapper.SchoolClassMapper">

    <resultMap id="SchoolClassResult" type="com.pangu.school.domain.entity.SchoolClass">
        <id property="id" column="id"/>
        <result property="schoolGradeId" column="school_grade_id"/>
        <result property="classId" column="class_id"/>
        <result property="orderNum" column="order_num"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectSchoolClassVo">
        SELECT sc.id, sc.school_grade_id, sc.class_id, sc.order_num, sc.status,
               sc.create_by, sc.create_time,
               c.class_name
        FROM pg_school_class sc
        LEFT JOIN pg_class c ON sc.class_id = c.class_id AND c.del_flag = '0'
        WHERE sc.del_flag = '0'
    </sql>

    <select id="selectBySchoolGradeIds" resultMap="SchoolClassResult">
        SELECT sc.id, sc.school_grade_id, sc.class_id, sc.order_num, sc.status,
               sc.create_by, sc.create_time,
               c.class_name
        FROM pg_school_class sc
        LEFT JOIN pg_class c ON sc.class_id = c.class_id AND c.del_flag = '0'
        WHERE sc.del_flag = '0'
        AND sc.school_grade_id IN
        <foreach collection="schoolGradeIds" item="schoolGradeId" open="(" separator="," close=")">
            #{schoolGradeId}
        </foreach>
        ORDER BY sc.order_num
    </select>

    <select id="exists" resultType="boolean">
        SELECT COUNT(*) > 0
        FROM pg_school_class
        WHERE del_flag = '0'
        AND school_grade_id = #{schoolGradeId}
        AND class_id = #{classId}
    </select>

    <select id="countBySchoolGradeId" parameterType="Long" resultType="int">
        SELECT COUNT(*)
        FROM pg_school_class
        WHERE del_flag = '0'
        AND school_grade_id = #{schoolGradeId}
    </select>

    <insert id="batchInsert">
        INSERT INTO pg_school_class (school_grade_id, class_id, order_num, status, create_by, create_time, del_flag)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.schoolGradeId}, #{item.classId}, #{item.orderNum}, #{item.status}, #{item.createBy}, #{item.createTime}, '0')
        </foreach>
    </insert>

</mapper>
