<?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.ruoyi.voluntary.mapper.BusiClassesMapper">

  <resultMap type="BusiClasses" id="BusiClassesResult">
    <result property="classId" column="class_id" />
    <result property="schoolId" column="school_id" />
    <result property="schoolName" column="school_name" />
    <result property="gradeId" column="grade_id" />
    <result property="gradeName" column="grade_name" />
    <result property="year" column="year" />
    <result property="name" column="name" />
    <result property="status" column="status" />
    <result property="createTime" column="create_time" />
    <result property="updateTime" column="update_time" />
  </resultMap>

    <sql id="selectBusiClassesVo">
        select class_id, school_id, grade_id, year, name, status, create_time, update_time from b_busi_classes
    </sql>

    <select id="selectBusiClassesList" parameterType="BusiClasses" resultMap="BusiClassesResult">
         select t1.class_id, t1.school_id,t2.name school_name, t1.grade_id,t3.name grade_name,
         t1.year, t1.name, t1.status, t1.create_time, t1.update_time 
         from b_busi_classes t1 
         left join b_busi_schools t2 on t1.school_id = t2.id
         left join b_busi_grade t3 on t1.grade_id = t3.id
        <where>  
            <if test="schoolId != null "> and t1.school_id = #{schoolId}</if>
            <if test="gradeId != null "> and t1.grade_id = #{gradeId}</if>
            <if test="year != null "> and t1.year = #{year}</if>
            <if test="name != null  and name != ''"> and t1.name like concat('%', #{name}, '%')</if>
            <if test="status != null "> and t1.status = #{status}</if>
        </where>
        order by t1.class_id desc
    </select>
    
    <select id="selectBusiClassesByClassId" parameterType="Long" resultMap="BusiClassesResult">
        <include refid="selectBusiClassesVo"/>
        where class_id = #{classId}
    </select>
        
    <insert id="insertBusiClasses" parameterType="BusiClasses" useGeneratedKeys="true" keyProperty="classId">
        insert into b_busi_classes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">school_id,</if>
            <if test="gradeId != null">grade_id,</if>
            <if test="year != null">year,</if>
            <if test="name != null">name,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">#{schoolId},</if>
            <if test="gradeId != null">#{gradeId},</if>
            <if test="year != null">#{year},</if>
            <if test="name != null">#{name},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateBusiClasses" parameterType="BusiClasses">
        update b_busi_classes
        <trim prefix="SET" suffixOverrides=",">
            <if test="schoolId != null">school_id = #{schoolId},</if>
            <if test="gradeId != null">grade_id = #{gradeId},</if>
            <if test="year != null">year = #{year},</if>
            <if test="name != null">name = #{name},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where class_id = #{classId}
    </update>

    <delete id="deleteBusiClassesByClassId" parameterType="Long">
        delete from b_busi_classes where class_id = #{classId}
    </delete>

    <delete id="deleteBusiClassesByClassIds" parameterType="String">
        delete from b_busi_classes where class_id in 
        <foreach item="classId" collection="array" open="(" separator="," close=")">
            #{classId}
        </foreach>
    </delete>
</mapper>