<?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.SchoolInfoMapper">
    
    <resultMap type="SchoolInfo" id="SchoolInfoResult">
        <result property="inscode"    column="inscode"    />
        <result property="name"    column="name"    />
        <result property="sname"    column="sname"    />
        <result property="address"    column="address"    />
        <result property="legalPersonIdNumber"    column="legal_person_id_number"    />
        <result property="responsiblePerson"    column="responsible_person"    />
        <result property="legalPersonName"    column="legal_person_name"    />
        <result property="areaName"    column="area_name"    />
        <result property="areaCode"    column="area_code"    />
        <result property="tel"    column="tel"    />
        <result property="wechar"    column="wechar"    />
        <result property="registrationDate"    column="registration_date"    />
        <result property="licenseNumber"    column="license_number"    />
        <result property="licenseWord"    column="license_word"    />
        <result property="licenseValidPeriodFrom"    column="license_valid_period_from"    />
        <result property="licenseValidPeriodEnd"    column="license_valid_period_end"    />
        <result property="credibilityGrade"    column="credibility_grade"    />
        <result property="credibilityScore"    column="credibility_score"    />
        <result property="vehicleSum"    column="vehicle_sum"    />
        <result property="regcapital"    column="regcapital"    />
        <result property="bizlicense"    column="bizlicense"    />
        <result property="poiLon"    column="poi_lon"    />
        <result property="poiLat"    column="poi_lat"    />
        <result property="removed"    column="removed"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="initialReceiptDate"    column="initial_receipt_date"    />
        <result property="businessStatusName"    column="business_status_name"    />
        <result property="pxcx"    column="pxcx"    />
        <result property="yzCode"    column="yzCode"    />
    </resultMap>

    <select id="selectSchoolInfoUrlList" parameterType="com.miaxis.app.school.dto.SchoolInfoDTO" resultType="com.miaxis.app.school.dto.SchoolInfoVO">
        SELECT s.*,(2 * 6378.137 * ASIN(SQRT(POW( SIN( PI( ) * ( #{lon}- s.poi_lon ) / 360 ), 2 ) + COS( PI( ) * #{lat} / 180 ) * COS( s.poi_lat * PI( ) / 180 ) * POW( SIN( PI( ) * ( #{lat}- s.poi_lat ) / 360 ), 2 )) ) ) AS distance FROM school_info s
        LEFT JOIN (SELECT * FROM school_images i WHERE i.is_top = 1) i
        ON s.inscode = i.inscode
        left join (SELECT inscode,avg(e.total_starts) as starts FROM school_evaluate e GROUP BY inscode) m
        on s.inscode = m.inscode
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="areaCode != null  and areaCode != ''"> and area_code = #{areaCode}</if>
            <if test="pxcx != null  and pxcx != ''"> and pxcx like concat('%', #{pxcx}, '%')</if>
        </where>

        <if test="orderName!=null and orderType!=null">
            order by ${orderName} ${orderType}
        </if>
    </select>


    <select id="selectSchoolInfoEvalList" parameterType="com.miaxis.app.school.dto.SchoolInfoEvalDTO" resultType="com.miaxis.app.school.dto.SchoolInfoEvalVO">
        SELECT s.inscode,s.name,m.stars,m.total
        FROM school_info s
        left join (SELECT inscode,avg(e.total_starts) as stars,count(1) as total FROM school_evaluate e GROUP BY inscode) m
        on s.inscode = m.inscode
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="areaCode != null  and areaCode != ''"> and area_code = #{areaCode}</if>
            <if test="pxcx != null  and pxcx != ''"> and sname like concat('%', #{pxcx}, '%')</if>
        </where>
        order by stars desc
    </select>



    <sql id="selectSchoolInfoVo">
        select inscode, name, sname, address, legal_person_id_number, responsible_person, legal_person_name, area_name, area_code, tel, wechar, registration_date, license_number, license_word, license_valid_period_from, license_valid_period_end, credibility_grade, credibility_score, vehicle_sum, regcapital, bizlicense, poi_lon, poi_lat, removed, create_time, update_time, initial_receipt_date, business_status_name, pxcx, yz_code from school_info
    </sql>

    <select id="selectSchoolInfoList" parameterType="SchoolInfo" resultMap="SchoolInfoResult">
        <include refid="selectSchoolInfoVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="sname != null  and sname != ''"> and sname like concat('%', #{sname}, '%')</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="legalPersonIdNumber != null  and legalPersonIdNumber != ''"> and legal_person_id_number = #{legalPersonIdNumber}</if>
            <if test="responsiblePerson != null  and responsiblePerson != ''"> and responsible_person = #{responsiblePerson}</if>
            <if test="legalPersonName != null  and legalPersonName != ''"> and legal_person_name like concat('%', #{legalPersonName}, '%')</if>
            <if test="areaName != null  and areaName != ''"> and area_name like concat('%', #{areaName}, '%')</if>
            <if test="areaCode != null  and areaCode != ''"> and area_code = #{areaCode}</if>
            <if test="tel != null  and tel != ''"> and tel = #{tel}</if>
            <if test="wechar != null  and wechar != ''"> and wechar = #{wechar}</if>
            <if test="registrationDate != null "> and registration_date = #{registrationDate}</if>
            <if test="licenseNumber != null  and licenseNumber != ''"> and license_number = #{licenseNumber}</if>
            <if test="licenseWord != null  and licenseWord != ''"> and license_word = #{licenseWord}</if>
            <if test="licenseValidPeriodFrom != null "> and license_valid_period_from = #{licenseValidPeriodFrom}</if>
            <if test="licenseValidPeriodEnd != null "> and license_valid_period_end = #{licenseValidPeriodEnd}</if>
            <if test="credibilityGrade != null  and credibilityGrade != ''"> and credibility_grade = #{credibilityGrade}</if>
            <if test="credibilityScore != null  and credibilityScore != ''"> and credibility_score = #{credibilityScore}</if>
            <if test="vehicleSum != null "> and vehicle_sum = #{vehicleSum}</if>
            <if test="regcapital != null "> and regcapital = #{regcapital}</if>
            <if test="bizlicense != null  and bizlicense != ''"> and bizlicense = #{bizlicense}</if>
            <if test="poiLon != null "> and poi_lon = #{poiLon}</if>
            <if test="poiLat != null "> and poi_lat = #{poiLat}</if>
            <if test="removed != null "> and removed = #{removed}</if>
            <if test="initialReceiptDate != null "> and initial_receipt_date = #{initialReceiptDate}</if>
            <if test="businessStatusName != null  and businessStatusName != ''"> and business_status_name like concat('%', #{businessStatusName}, '%')</if>
            <if test="pxcx != null  and pxcx != ''"> and pxcx = #{pxcx}</if>
            <if test="yzCode != null  and yzCode != ''"> and yz_code = #{yz_code}</if>
        </where>
    </select>
    
    <select id="selectSchoolInfoById" parameterType="String" resultMap="SchoolInfoResult">
        <include refid="selectSchoolInfoVo"/>
        where inscode = #{inscode}
    </select>
        
    <insert id="insertSchoolInfo" parameterType="SchoolInfo">
        insert into school_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inscode != null">inscode,</if>
            <if test="name != null">name,</if>
            <if test="sname != null">sname,</if>
            <if test="address != null">address,</if>
            <if test="legalPersonIdNumber != null">legal_person_id_number,</if>
            <if test="responsiblePerson != null">responsible_person,</if>
            <if test="legalPersonName != null">legal_person_name,</if>
            <if test="areaName != null">area_name,</if>
            <if test="areaCode != null">area_code,</if>
            <if test="tel != null">tel,</if>
            <if test="wechar != null">wechar,</if>
            <if test="registrationDate != null">registration_date,</if>
            <if test="licenseNumber != null">license_number,</if>
            <if test="licenseWord != null">license_word,</if>
            <if test="licenseValidPeriodFrom != null">license_valid_period_from,</if>
            <if test="licenseValidPeriodEnd != null">license_valid_period_end,</if>
            <if test="credibilityGrade != null">credibility_grade,</if>
            <if test="credibilityScore != null">credibility_score,</if>
            <if test="vehicleSum != null">vehicle_sum,</if>
            <if test="regcapital != null">regcapital,</if>
            <if test="bizlicense != null">bizlicense,</if>
            <if test="poiLon != null">poi_lon,</if>
            <if test="poiLat != null">poi_lat,</if>
            <if test="removed != null">removed,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="initialReceiptDate != null">initial_receipt_date,</if>
            <if test="businessStatusName != null">business_status_name,</if>
            <if test="pxcx != null">pxcx,</if>
            <if test="yzCode != null">yz_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="inscode != null">#{inscode},</if>
            <if test="name != null">#{name},</if>
            <if test="sname != null">#{sname},</if>
            <if test="address != null">#{address},</if>
            <if test="legalPersonIdNumber != null">#{legalPersonIdNumber},</if>
            <if test="responsiblePerson != null">#{responsiblePerson},</if>
            <if test="legalPersonName != null">#{legalPersonName},</if>
            <if test="areaName != null">#{areaName},</if>
            <if test="areaCode != null">#{areaCode},</if>
            <if test="tel != null">#{tel},</if>
            <if test="wechar != null">#{wechar},</if>
            <if test="registrationDate != null">#{registrationDate},</if>
            <if test="licenseNumber != null">#{licenseNumber},</if>
            <if test="licenseWord != null">#{licenseWord},</if>
            <if test="licenseValidPeriodFrom != null">#{licenseValidPeriodFrom},</if>
            <if test="licenseValidPeriodEnd != null">#{licenseValidPeriodEnd},</if>
            <if test="credibilityGrade != null">#{credibilityGrade},</if>
            <if test="credibilityScore != null">#{credibilityScore},</if>
            <if test="vehicleSum != null">#{vehicleSum},</if>
            <if test="regcapital != null">#{regcapital},</if>
            <if test="bizlicense != null">#{bizlicense},</if>
            <if test="poiLon != null">#{poiLon},</if>
            <if test="poiLat != null">#{poiLat},</if>
            <if test="removed != null">#{removed},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="initialReceiptDate != null">#{initialReceiptDate},</if>
            <if test="businessStatusName != null">#{businessStatusName},</if>
            <if test="pxcx != null">#{pxcx},</if>
            <if test="yzCode != null">#{yzCode},</if>
         </trim>
    </insert>

    <update id="updateSchoolInfo" parameterType="SchoolInfo">
        update school_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="sname != null">sname = #{sname},</if>
            <if test="address != null">address = #{address},</if>
            <if test="legalPersonIdNumber != null">legal_person_id_number = #{legalPersonIdNumber},</if>
            <if test="responsiblePerson != null">responsible_person = #{responsiblePerson},</if>
            <if test="legalPersonName != null">legal_person_name = #{legalPersonName},</if>
            <if test="areaName != null">area_name = #{areaName},</if>
            <if test="areaCode != null">area_code = #{areaCode},</if>
            <if test="tel != null">tel = #{tel},</if>
            <if test="wechar != null">wechar = #{wechar},</if>
            <if test="registrationDate != null">registration_date = #{registrationDate},</if>
            <if test="licenseNumber != null">license_number = #{licenseNumber},</if>
            <if test="licenseWord != null">license_word = #{licenseWord},</if>
            <if test="licenseValidPeriodFrom != null">license_valid_period_from = #{licenseValidPeriodFrom},</if>
            <if test="licenseValidPeriodEnd != null">license_valid_period_end = #{licenseValidPeriodEnd},</if>
            <if test="credibilityGrade != null">credibility_grade = #{credibilityGrade},</if>
            <if test="credibilityScore != null">credibility_score = #{credibilityScore},</if>
            <if test="vehicleSum != null">vehicle_sum = #{vehicleSum},</if>
            <if test="regcapital != null">regcapital = #{regcapital},</if>
            <if test="bizlicense != null">bizlicense = #{bizlicense},</if>
            <if test="poiLon != null">poi_lon = #{poiLon},</if>
            <if test="poiLat != null">poi_lat = #{poiLat},</if>
            <if test="removed != null">removed = #{removed},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="initialReceiptDate != null">initial_receipt_date = #{initialReceiptDate},</if>
            <if test="businessStatusName != null">business_status_name = #{businessStatusName},</if>
            <if test="pxcx != null">pxcx = #{pxcx},</if>
            <if test="yzCode != null">yz_code = #{yzCode},</if>
        </trim>
        where inscode = #{inscode}
    </update>

    <delete id="deleteSchoolInfoById" parameterType="String">
        delete from school_info where inscode = #{inscode}
    </delete>

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