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

    <resultMap id="SchoolResult" type="com.pangu.school.domain.entity.School">
        <id property="schoolId" column="school_id"/>
        <result property="schoolCode" column="school_code"/>
        <result property="schoolName" column="school_name"/>
        <result property="schoolType" column="school_type"/>
        <result property="regionId" column="region_id"/>
        <result property="regionPath" column="region_path"/>
        <result property="address" column="address"/>
        <result property="contactPerson" column="contact_person"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="status" column="status"/>
        <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="remark" column="remark"/>
    </resultMap>

    <resultMap id="SchoolVOResult" type="com.pangu.school.domain.vo.SchoolVO">
        <id property="schoolId" column="school_id"/>
        <result property="schoolCode" column="school_code"/>
        <result property="schoolName" column="school_name"/>
        <result property="schoolType" column="school_type"/>
        <result property="regionId" column="region_id"/>
        <result property="regionPath" column="region_path"/>
        <result property="address" column="address"/>
        <result property="contactPerson" column="contact_person"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectSchoolVo">
        SELECT school_id, school_code, school_name, school_type, region_id, region_path,
               address, contact_person, contact_phone, status,
               create_by, create_time, update_by, update_time, remark
        FROM pg_school
        WHERE del_flag = '0'
    </sql>

    <select id="selectSchoolsByRegionId" parameterType="Long" resultMap="SchoolResult">
        <include refid="selectSchoolVo"/>
        <if test="regionId != null">
            AND region_id = #{regionId}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="selectSchoolList" parameterType="com.pangu.school.domain.dto.SchoolQueryDTO" resultMap="SchoolVOResult">
        <include refid="selectSchoolVo"/>
        <if test="regionId != null">
            AND region_id = #{regionId}
        </if>
        <if test="schoolName != null and schoolName != ''">
            AND school_name LIKE CONCAT('%', #{schoolName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="selectSchoolById" parameterType="Long" resultMap="SchoolVOResult">
        <include refid="selectSchoolVo"/>
        AND school_id = #{schoolId}
    </select>

    <select id="selectMaxCode" parameterType="String" resultType="String">
        SELECT MAX(school_code)
        FROM pg_school
        WHERE del_flag = '0'
        AND school_code LIKE CONCAT(#{prefix}, '%')
    </select>

    <select id="countByRegionId" parameterType="Long" resultType="int">
        SELECT COUNT(*)
        FROM pg_school
        WHERE del_flag = '0'
        AND region_id = #{regionId}
    </select>

    <insert id="insertSchool" parameterType="com.pangu.school.domain.entity.School" useGeneratedKeys="true" keyProperty="schoolId">
        INSERT INTO pg_school (
            school_code, school_name, school_type, region_id, region_path,
            address, contact_person, contact_phone, status,
            create_by, create_time, del_flag, remark
        ) VALUES (
            #{schoolCode}, #{schoolName}, #{schoolType}, #{regionId}, #{regionPath},
            #{address}, #{contactPerson}, #{contactPhone}, #{status},
            #{createBy}, #{createTime}, '0', #{remark}
        )
    </insert>

    <update id="updateSchool" parameterType="com.pangu.school.domain.entity.School">
        UPDATE pg_school
        <set>
            <if test="schoolName != null and schoolName != ''">school_name = #{schoolName},</if>
            <if test="schoolType != null and schoolType != ''">school_type = #{schoolType},</if>
            <if test="regionId != null">region_id = #{regionId},</if>
            <if test="regionPath != null">region_path = #{regionPath},</if>
            <if test="address != null">address = #{address},</if>
            <if test="contactPerson != null">contact_person = #{contactPerson},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_by = #{updateBy},
            update_time = #{updateTime}
        </set>
        WHERE school_id = #{schoolId}
    </update>

    <update id="deleteSchoolById" parameterType="Long">
        UPDATE pg_school SET del_flag = '1' WHERE school_id = #{schoolId}
    </update>

</mapper>
