<?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="cn.com.tenlion.systembase.dao.school.ISchoolDao">

    <resultMap id="schoolDTO" type="cn.com.tenlion.systembase.pojo.dtos.school.SchoolDTO">
        <result column="school_id" property="schoolId"/>
        <result column="school_name" property="schoolName"/>
        <result column="school_address" property="schoolAddress"/>
        <result column="running_type" property="runningType"/>
        <result column="running_type_name" property="runningTypeName"/>
        <result column="running_type_code" property="runningTypeCode"/>
        <result column="belong_dept" property="belongDept"/>
        <result column="belong_dept_name" property="belongDeptName"/>
        <result column="belong_dept_code" property="belongDeptCode"/>
        <result column="stu_count" property="stuCount"/>
        <result column="principal_name" property="principalName"/>
        <result column="principal_phone" property="principalPhone"/>
        <result column="defend_name" property="defendName"/>
        <result column="defend_phone" property="defendPhone"/>
        <result column="safe_name" property="safeName"/>
        <result column="safe_phone" property="safePhone"/>
        <result column="security_name" property="securityName"/>
        <result column="security_phone" property="securityPhone"/>
        <result column="security_count" property="securityCount"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="location" property="location"/>
        <result column="location_code" property="locationCode"/>
        <result column="grid" property="grid"/>
        <result column="data_from" property="dataFrom"/>
        <result column="remake" property="remake"/>
    </resultMap>

    <resultMap id="schoolBO" type="cn.com.tenlion.systembase.pojo.bos.school.SchoolBO">
        <result column="school_id" property="schoolId"/>
        <result column="school_name" property="schoolName"/>
        <result column="school_address" property="schoolAddress"/>
        <result column="running_type" property="runningType"/>
        <result column="running_type_name" property="runningTypeName"/>
        <result column="running_type_code" property="runningTypeCode"/>
        <result column="belong_dept" property="belongDept"/>
        <result column="belong_dept_name" property="belongDeptName"/>
        <result column="belong_dept_code" property="belongDeptCode"/>
        <result column="stu_count" property="stuCount"/>
        <result column="principal_name" property="principalName"/>
        <result column="principal_phone" property="principalPhone"/>
        <result column="defend_name" property="defendName"/>
        <result column="defend_phone" property="defendPhone"/>
        <result column="safe_name" property="safeName"/>
        <result column="safe_phone" property="safePhone"/>
        <result column="security_name" property="securityName"/>
        <result column="security_phone" property="securityPhone"/>
        <result column="security_count" property="securityCount"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
    </resultMap>

    <resultMap id="schoolPO" type="cn.com.tenlion.systembase.pojo.pos.school.SchoolPO">
        <result column="school_id" property="schoolId"/>
        <result column="school_name" property="schoolName"/>
        <result column="school_address" property="schoolAddress"/>
        <result column="running_type" property="runningType"/>
        <result column="running_type_name" property="runningTypeName"/>
        <result column="running_type_code" property="runningTypeCode"/>
        <result column="belong_dept" property="belongDept"/>
        <result column="belong_dept_name" property="belongDeptName"/>
        <result column="belong_dept_code" property="belongDeptCode"/>
        <result column="stu_count" property="stuCount"/>
        <result column="principal_name" property="principalName"/>
        <result column="principal_phone" property="principalPhone"/>
        <result column="defend_name" property="defendName"/>
        <result column="defend_phone" property="defendPhone"/>
        <result column="safe_name" property="safeName"/>
        <result column="safe_phone" property="safePhone"/>
        <result column="security_name" property="securityName"/>
        <result column="security_phone" property="securityPhone"/>
        <result column="security_count" property="securityCount"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
    </resultMap>

    <!-- 新增学校 -->
    <insert id="save" parameterType="map">
        INSERT INTO data_school(
            school_id,
            school_name,
            school_address,
            running_type,
            running_type_name,
            running_type_code,
            belong_dept,
            belong_dept_name,
            belong_dept_code,
            stu_count,
            principal_name,
            principal_phone,
            defend_name,
            defend_phone,
            safe_name,
            safe_phone,
            security_name,
            security_phone,
            security_count,
            longitude,
            latitude,
            location,
            location_code,
            grid,
            data_from,
            remake,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        )VALUES (
            #{schoolId},
            #{schoolName},
            #{schoolAddress},
            #{runningType},
            #{runningTypeName},
            #{runningTypeCode},
            #{belongDept},
            #{belongDeptName},
            #{belongDeptCode},
            #{stuCount},
            #{principalName},
            #{principalPhone},
            #{defendName},
            #{defendPhone},
            #{safeName},
            #{safePhone},
            #{securityName},
            #{securityPhone},
            #{securityCount},
            #{longitude},
            #{latitude},
            #{location},
            #{locationCode},
            #{grid},
            #{dataFrom},
            #{remake},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除学校 -->
    <update id="remove" parameterType="map">
        UPDATE
        data_school
        SET
        gmt_modified = #{gmtModified},
        modifier = #{modifier},
        is_delete = 1
        WHERE
        school_id IN
        <foreach collection="schoolIds" index="index" open="(" separator="," close=")">
            #{schoolIds[${index}]}
        </foreach>
    </update>

    <!-- 删除学校（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            data_school
        WHERE
            school_id IN
        <foreach collection="schoolIds" index="index" open="(" separator="," close=")">
            #{schoolIds[${index}]}
        </foreach>
    </update>

    <!-- 修改学校 -->
    <update id="update" parameterType="map">
        UPDATE
            data_school
        SET
        <if test="schoolName != null and schoolName != ''">
            school_name = #{schoolName},
        </if>
        <if test="schoolAddress != null and schoolAddress != ''">
            school_address = #{schoolAddress},
        </if>
        <if test="runningType != null and runningType != ''">
            running_type = #{runningType},
        </if>
        <if test="runningTypeName != null and runningTypeName != ''">
            running_type_name = #{runningTypeName},
        </if>
        <if test="runningTypeCode != null and runningTypeCode != ''">
            running_type_code = #{runningTypeCode},
        </if>
        <if test="belongDept != null and belongDept != ''">
            belong_dept = #{belongDept},
        </if>
        <if test="belongDeptName != null and belongDeptName != ''">
            belong_dept_name = #{belongDeptName},
        </if>
        <if test="belongDeptCode != null and belongDeptCode != ''">
            belong_dept_code = #{belongDeptCode},
        </if>
        <if test="stuCount != null">
            stu_count = #{stuCount},
        </if>
        <if test="principalName != null and principalName != ''">
            principal_name = #{principalName},
        </if>
        <if test="principalPhone != null and principalPhone != ''">
            principal_phone = #{principalPhone},
        </if>
        <if test="defendName != null and defendName != ''">
            defend_name = #{defendName},
        </if>
        <if test="defendPhone != null and defendPhone != ''">
            defend_phone = #{defendPhone},
        </if>
        <if test="safeName != null and safeName != ''">
            safe_name = #{safeName},
        </if>
        <if test="safePhone != null and safePhone != ''">
            safe_phone = #{safePhone},
        </if>
        <if test="securityName != null and securityName != ''">
            security_name = #{securityName},
        </if>
        <if test="securityPhone != null and securityPhone != ''">
            security_phone = #{securityPhone},
        </if>
        <if test="securityCount != null">
            security_count = #{securityCount},
        </if>
        <if test="longitude != null">
            longitude = #{longitude},
        </if>
        <if test="latitude != null">
            latitude = #{latitude},
        </if>
        <if test="location != null and location != ''">
            location = #{location},
        </if>
        <if test="locationCode != null and locationCode != ''">
            location_code = #{locationCode},
        </if>
        <if test="grid != null and grid != ''">
            grid = #{grid},
        </if>
        <if test="remake != null and remake != ''">
            remake = #{remake},
        </if>
        gmt_modified = #{gmtModified},
        modifier = #{modifier},
        school_id = school_id
        WHERE
        school_id = #{schoolId}
    </update>

    <!-- 学校详情 -->
    <select id="get" parameterType="map" resultMap="schoolDTO">
        SELECT
            t1.school_name,
            t1.school_address,
            t1.running_type,
            t1.running_type_name,
            t1.running_type_code,
            t1.belong_dept,
            t1.belong_dept_name,
            t1.belong_dept_code,
            t1.stu_count,
            t1.principal_name,
            t1.principal_phone,
            t1.defend_name,
            t1.defend_phone,
            t1.safe_name,
            t1.safe_phone,
            t1.security_name,
            t1.security_phone,
            t1.security_count,
            t1.longitude,
            t1.latitude,
            t1.location,
            t1.location_code,
            t1.grid,
            t1.data_from,
            t1.remake,
            t1.school_id
        FROM
            data_school t1
        WHERE
            t1.is_delete = 0
        <if test="schoolId != null and schoolId != ''">
            AND
            t1.school_id = #{schoolId}
        </if>
        <if test="schoolName != null and schoolName != ''">
            AND
            t1.school_name = #{schoolName}
        </if>
        <if test="schoolAddress != null and schoolAddress != ''">
            AND
            t1.school_address = #{schoolAddress}
        </if>
        LIMIT 1
    </select>

    <!-- 学校详情 -->
    <select id="getBO" parameterType="map" resultMap="schoolBO">
        SELECT
            t1.school_id,
            t1.school_name,
            t1.school_address,
            t1.running_type,
            t1.running_type_name,
            t1.running_type_code,
            t1.belong_dept,
            t1.belong_dept_name,
            t1.belong_dept_code,
            t1.stu_count,
            t1.principal_name,
            t1.principal_phone,
            t1.defend_name,
            t1.defend_phone,
            t1.safe_name,
            t1.safe_phone,
            t1.security_name,
            t1.security_phone,
            t1.security_count,
            t1.longitude,
            t1.latitude
        FROM
            data_school t1
        WHERE
            t1.is_delete = 0
        <if test="schoolId != null and schoolId != ''">
            AND
            t1.school_id = #{schoolId}
        </if>
    </select>

    <!-- 学校详情 -->
    <select id="getPO" parameterType="map" resultMap="schoolPO">
        SELECT
            t1.school_id,
            t1.school_name,
            t1.school_address,
            t1.running_type,
            t1.running_type_name,
            t1.running_type_code,
            t1.belong_dept,
            t1.belong_dept_name,
            t1.belong_dept_code,
            t1.stu_count,
            t1.principal_name,
            t1.principal_phone,
            t1.defend_name,
            t1.defend_phone,
            t1.safe_name,
            t1.safe_phone,
            t1.security_name,
            t1.security_phone,
            t1.security_count,
            t1.longitude,
            t1.latitude
        FROM
            data_school t1
        WHERE
            t1.is_delete = 0
        <if test="schoolId != null and schoolId != ''">
            AND
            t1.school_id = #{schoolId}
        </if>
    </select>

    <!-- 学校列表 -->
    <select id="list" parameterType="map" resultMap="schoolDTO">
        SELECT
            t1.school_id,
            t1.school_name,
            t1.school_address,
            t1.running_type,
            t1.running_type_name,
            t1.running_type_code,
            t1.belong_dept,
            t1.belong_dept_name,
            t1.belong_dept_code,
            t1.stu_count,
            t1.principal_name,
            t1.principal_phone,
            t1.defend_name,
            t1.defend_phone,
            t1.safe_name,
            t1.safe_phone,
            t1.security_name,
            t1.security_phone,
            t1.security_count,
            t1.longitude,
            t1.latitude,
            t1.location,
            t1.location_code,
            t1.grid,
            1
        FROM
            data_school t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
            AND (
            t1.school_name LIKE CONCAT('%', #{keywords}, '%')
            )
        </if>
        <if test="startTime != null and startTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="schoolName != null and schoolName != ''">
            AND
            t1.school_name = #{schoolName}
        </if>
        <if test="schoolAddress != null and schoolAddress != ''">
            AND
            t1.school_address = #{schoolAddress}
        </if>
        <if test="schoolIds != null and schoolIds.size > 0">
            AND
            t1.school_id IN
            <foreach collection="schoolIds" index="index" open="(" separator="," close=")">
                #{schoolIds[${index}]}
            </foreach>
        </if>
        ORDER BY
            t1.gmt_create DESC
    </select>

    <!-- 学校列表 -->
    <select id="listBO" parameterType="map" resultMap="schoolBO">
        SELECT
            t1.school_id,
            t1.school_name,
            t1.school_address,
            t1.running_type,
            t1.running_type_name,
            t1.running_type_code,
            t1.belong_dept,
            t1.belong_dept_name,
            t1.belong_dept_code,
            t1.stu_count,
            t1.principal_name,
            t1.principal_phone,
            t1.defend_name,
            t1.defend_phone,
            t1.safe_name,
            t1.safe_phone,
            t1.security_name,
            t1.security_phone,
            t1.security_count,
            t1.longitude,
            t1.latitude
        FROM
            data_school t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
            AND (
            t1.id LIKE CONCAT('%', #{keywords}, '%')
            )
        </if>
        <if test="startTime != null and startTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="schoolIds != null and schoolIds.size > 0">
            AND
            t1.school_id IN
            <foreach collection="schoolIds" index="index" open="(" separator="," close=")">
                #{schoolIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 学校列表 -->
    <select id="listPO" parameterType="map" resultMap="schoolPO">
        SELECT
            t1.school_id,
            t1.school_name,
            t1.school_address,
            t1.running_type,
            t1.running_type_name,
            t1.running_type_code,
            t1.belong_dept,
            t1.belong_dept_name,
            t1.belong_dept_code,
            t1.stu_count,
            t1.principal_name,
            t1.principal_phone,
            t1.defend_name,
            t1.defend_phone,
            t1.safe_name,
            t1.safe_phone,
            t1.security_name,
            t1.security_phone,
            t1.security_count,
            t1.longitude,
            t1.latitude
        FROM
            data_school t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
            AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
            )
        </if>
        <if test="startTime != null and startTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="schoolIds != null and schoolIds.size > 0">
            AND
            t1.school_id IN
            <foreach collection="schoolIds" index="index" open="(" separator="," close=")">
                #{schoolIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 学校统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT COUNT(*)
        FROM data_school t1
        WHERE t1.is_delete = 0
    </select>

</mapper>