<?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="org.springblade.system.mapper.AirportMasterMapper">

    <resultMap id="airportMasterResultMap" type="org.springblade.system.pojo.vo.AirportMasterVO">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_user_name" property="updateUserName"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="code" property="code"/>
        <result column="iata_code" property="iataCode"/>
        <result column="icao_code" property="icaoCode"/>
        <result column="name" property="name"/>
        <result column="short_name" property="shortName"/>
        <result column="province_code" property="provinceCode"/>
        <result column="province_name" property="provinceName"/>
        <result column="city_code" property="cityCode"/>
        <result column="city_name" property="cityName"/>
        <result column="region_code" property="regionCode"/>
        <result column="region_name" property="regionName"/>
        <result column="detail_address" property="detailAddress"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="data_source" property="dataSource"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <select id="selectAirportMasterPage" resultMap="airportMasterResultMap">
        SELECT
            am.id,
            am.create_user,
            am.create_dept,
            am.create_time,
            am.update_user,
            bu.real_name AS update_user_name,
            am.update_time,
            am.status,
            am.is_deleted,
            am.code,
            am.iata_code,
            am.icao_code,
            am.name,
            am.short_name,
            am.province_code,
            am.province_name,
            am.city_code,
            am.city_name,
            am.region_code,
            am.region_name,
            am.detail_address,
            am.longitude,
            am.latitude,
            CASE WHEN am.data_source = '手工导入' THEN '手动录入' ELSE am.data_source END AS data_source,
            am.remark
        FROM
            blade_airport_master am
            LEFT JOIN blade_user bu ON bu.id = am.update_user
        WHERE
            am.is_deleted = 0
        <if test="airportMaster.code != null and airportMaster.code != ''">
            <bind name="codeLike" value="'%' + airportMaster.code + '%'"/>
            AND am.code LIKE #{codeLike}
        </if>
        <if test="airportMaster.name != null and airportMaster.name != ''">
            <bind name="nameLike" value="'%' + airportMaster.name + '%'"/>
            AND am.name LIKE #{nameLike}
        </if>
        <if test="airportMaster.iataCode != null and airportMaster.iataCode != ''">
            AND am.iata_code = #{airportMaster.iataCode}
        </if>
        <if test="airportMaster.icaoCode != null and airportMaster.icaoCode != ''">
            AND am.icao_code = #{airportMaster.icaoCode}
        </if>
        <if test="airportMaster.regionName != null and airportMaster.regionName != ''">
            <bind name="regionNameLike" value="'%' + airportMaster.regionName + '%'"/>
            AND am.region_name LIKE #{regionNameLike}
        </if>
        <if test="airportMaster.detailAddress != null and airportMaster.detailAddress != ''">
            <bind name="detailAddressLike" value="'%' + airportMaster.detailAddress + '%'"/>
            AND am.detail_address LIKE #{detailAddressLike}
        </if>
        <if test="airportMaster.dataSource != null and airportMaster.dataSource != ''">
            <choose>
                <when test="airportMaster.dataSource == '手动录入'">
                    AND am.data_source IN ('手动录入', '手工导入')
                </when>
                <otherwise>
                    AND am.data_source = #{airportMaster.dataSource}
                </otherwise>
            </choose>
        </if>
        <if test="airportMaster.status != null">
            AND am.status = #{airportMaster.status}
        </if>
        ORDER BY am.update_time DESC, am.create_time DESC
    </select>

</mapper>
