<?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.miaxis.app.school.mapper.SchoolRegionMapper">
    
    <resultMap type="SchoolRegion" id="SchoolRegionResult">
        <result property="id"    column="id"    />
        <result property="inscode"    column="inscode"    />
        <result property="seq"    column="seq"    />
        <result property="name"    column="name"    />
        <result property="address"    column="address"    />
        <result property="area"    column="area"    />
        <result property="type"    column="type"    />
        <result property="vehicletype"    column="vehicletype"    />
        <result property="polygon"    column="polygon"    />
        <result property="totalvehnum"    column="totalvehnum"    />
        <result property="curvehnum"    column="curvehnum"    />
        <result property="status"    column="status"    />
        <result property="flag"    column="flag"    />
        <result property="areaCode"    column="area_code"    />
        <result property="poiLon"    column="poi_lon"    />
        <result property="poiLat"    column="poi_lat"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <select id="selectSchoolRegionUrlList" parameterType="com.miaxis.app.school.dto.SchoolRegionDTO" resultType="com.miaxis.app.school.dto.SchoolRegionVO">
        SELECT sr.id,sr.NAME region_name,s.NAME school_name,sr.address,sr.create_time,sr.update_time,sr.poi_lon lon,sr.poi_lat lat,
        sr.area,sr.type,sr.vehicletype,sr.totalvehnum,sr.curvehnum,sr.poi_lon,sr.poi_lat,sri.url,
        (2 * 6378.137 * ASIN(SQRT(POW( SIN( PI( ) * ( #{lon}- sr.poi_lon ) / 360 ), 2 ) + COS( PI( ) * 30.186584 / 180 ) * COS( #{lat} * PI( ) / 180 ) * POW( SIN( PI( ) * ( #{lat}- sr.poi_lat ) / 360 ), 2 )) ) ) AS distance
        FROM school_region sr
        left JOIN school_info s ON sr.inscode = s.inscode
        left join school_region_images sri on sr.id = sri. region_id
        <where>
            <if test="areaCode != null  and areaCode != ''"> and area_code = #{areaCode}</if>
        </where>
        order by distance desc
    </select>


    <sql id="selectSchoolRegionVo">
        select id, inscode, seq, name, address, area, type, vehicletype, polygon, totalvehnum, curvehnum, status, flag, area_code, poi_lon, poi_lat, create_time, update_time from school_region
    </sql>

    <select id="selectSchoolRegionList" parameterType="SchoolRegion" resultMap="SchoolRegionResult">
        <include refid="selectSchoolRegionVo"/>
        <where>  
            <if test="inscode != null  and inscode != ''"> and inscode = #{inscode}</if>
            <if test="seq != null  and seq != ''"> and seq = #{seq}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="area != null "> and area = #{area}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="vehicletype != null  and vehicletype != ''"> and vehicletype = #{vehicletype}</if>
            <if test="polygon != null  and polygon != ''"> and polygon = #{polygon}</if>
            <if test="totalvehnum != null "> and totalvehnum = #{totalvehnum}</if>
            <if test="curvehnum != null "> and curvehnum = #{curvehnum}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="flag != null "> and flag = #{flag}</if>
            <if test="areaCode != null  and areaCode != ''"> and area_code = #{areaCode}</if>
            <if test="poiLon != null "> and poi_lon = #{poiLon}</if>
            <if test="poiLat != null "> and poi_lat = #{poiLat}</if>
        </where>
    </select>
    
    <select id="selectSchoolRegionById" parameterType="Long" resultMap="SchoolRegionResult">
        <include refid="selectSchoolRegionVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSchoolRegion" parameterType="SchoolRegion">
        insert into school_region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="inscode != null">inscode,</if>
            <if test="seq != null">seq,</if>
            <if test="name != null">name,</if>
            <if test="address != null">address,</if>
            <if test="area != null">area,</if>
            <if test="type != null">type,</if>
            <if test="vehicletype != null">vehicletype,</if>
            <if test="polygon != null">polygon,</if>
            <if test="totalvehnum != null">totalvehnum,</if>
            <if test="curvehnum != null">curvehnum,</if>
            <if test="status != null">status,</if>
            <if test="flag != null">flag,</if>
            <if test="areaCode != null">area_code,</if>
            <if test="poiLon != null">poi_lon,</if>
            <if test="poiLat != null">poi_lat,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="inscode != null">#{inscode},</if>
            <if test="seq != null">#{seq},</if>
            <if test="name != null">#{name},</if>
            <if test="address != null">#{address},</if>
            <if test="area != null">#{area},</if>
            <if test="type != null">#{type},</if>
            <if test="vehicletype != null">#{vehicletype},</if>
            <if test="polygon != null">#{polygon},</if>
            <if test="totalvehnum != null">#{totalvehnum},</if>
            <if test="curvehnum != null">#{curvehnum},</if>
            <if test="status != null">#{status},</if>
            <if test="flag != null">#{flag},</if>
            <if test="areaCode != null">#{areaCode},</if>
            <if test="poiLon != null">#{poiLon},</if>
            <if test="poiLat != null">#{poiLat},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSchoolRegion" parameterType="SchoolRegion">
        update school_region
        <trim prefix="SET" suffixOverrides=",">
            <if test="inscode != null">inscode = #{inscode},</if>
            <if test="seq != null">seq = #{seq},</if>
            <if test="name != null">name = #{name},</if>
            <if test="address != null">address = #{address},</if>
            <if test="area != null">area = #{area},</if>
            <if test="type != null">type = #{type},</if>
            <if test="vehicletype != null">vehicletype = #{vehicletype},</if>
            <if test="polygon != null">polygon = #{polygon},</if>
            <if test="totalvehnum != null">totalvehnum = #{totalvehnum},</if>
            <if test="curvehnum != null">curvehnum = #{curvehnum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="flag != null">flag = #{flag},</if>
            <if test="areaCode != null">area_code = #{areaCode},</if>
            <if test="poiLon != null">poi_lon = #{poiLon},</if>
            <if test="poiLat != null">poi_lat = #{poiLat},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSchoolRegionById" parameterType="Long">
        delete from school_region where id = #{id}
    </delete>

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