<?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.transport.mapper.CommonAddressMapper">

    <resultMap id="commonAddressResultMap" type="org.springblade.transport.pojo.vo.CommonAddressVO">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <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_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="address_code" property="addressCode"/>
        <result column="address_name" property="addressName"/>
        <result column="address_type" property="addressType"/>
        <result column="source_id" property="sourceId"/>
        <result column="site_code" property="siteCode"/>
        <result column="detail_address" property="detailAddress"/>
        <result column="region_code" property="regionCode"/>
        <result column="region_name" property="regionName"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="contact_name" property="contactName"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <sql id="BaseColumn">
        id,
        tenant_id,
        create_user,
        create_dept,
        create_time,
        update_user,
        update_time,
        status,
        is_deleted,
        address_code,
        address_name,
        address_type,
        source_id,
        site_code,
        detail_address,
        region_code,
        region_name,
        longitude,
        latitude,
        dept_id,
        dept_name,
        contact_name,
        contact_phone,
        remark
    </sql>

    <sql id="QueryCondition">
        is_deleted = 0
        <if test="address.addressName != null and address.addressName != ''">
            <bind name="addressNameLike" value="'%' + address.addressName + '%'"/>
            AND address_name LIKE #{addressNameLike}
        </if>
        <if test="address.addressType != null and address.addressType != ''">
            AND address_type = #{address.addressType}
        </if>
        <if test="address.detailAddress != null and address.detailAddress != ''">
            <bind name="detailAddressLike" value="'%' + address.detailAddress + '%'"/>
            AND detail_address LIKE #{detailAddressLike}
        </if>
        <if test="address.regionName != null and address.regionName != ''">
            <bind name="regionNameLike" value="'%' + address.regionName + '%'"/>
            AND region_name LIKE #{regionNameLike}
        </if>
        <if test="address.siteCode != null and address.siteCode != ''">
            AND site_code = #{address.siteCode}
        </if>
        <if test="address.deptId != null">
            AND dept_id = #{address.deptId}
        </if>
        <if test="address.contactName != null and address.contactName != ''">
            <bind name="contactNameLike" value="'%' + address.contactName + '%'"/>
            AND contact_name LIKE #{contactNameLike}
        </if>
        <if test="address.contactPhone != null and address.contactPhone != ''">
            <bind name="contactPhoneLike" value="'%' + address.contactPhone + '%'"/>
            AND contact_phone LIKE #{contactPhoneLike}
        </if>
    </sql>

    <select id="selectCommonAddressPage" resultMap="commonAddressResultMap">
        SELECT
            <include refid="BaseColumn"/>
        FROM
            blade_common_address
        WHERE
            <include refid="QueryCondition"/>
        ORDER BY update_time DESC, create_time DESC
    </select>

    <select id="selectMaxAddressCode" resultType="java.lang.String">
        SELECT MAX(address_code)
        FROM blade_common_address
        WHERE address_code LIKE 'DZ%'
    </select>

    <select id="selectPortTerminalSource" resultMap="commonAddressResultMap">
        SELECT
            id AS source_id,
            code AS site_code,
            name AS address_name,
            CONCAT_WS('', country, city, district_name) AS region_name,
            district_code AS region_code,
            detail_address,
            longitude,
            latitude
        FROM blade_port_terminal
        WHERE id = #{id} AND status = 1 AND is_deleted = 0
    </select>

    <select id="selectRailwayStationSource" resultMap="commonAddressResultMap">
        SELECT
            id AS source_id,
            code AS site_code,
            name AS address_name,
            CONCAT_WS('', province_name, city_name) AS region_name,
            city_code AS region_code,
            CONCAT_WS('', province_name, city_name, name) AS detail_address,
            longitude,
            latitude
        FROM blade_railway_station
        WHERE id = #{id} AND status = 1 AND is_deleted = 0
    </select>

    <select id="selectAirportMasterSource" resultMap="commonAddressResultMap">
        SELECT
            id AS source_id,
            code AS site_code,
            name AS address_name,
            CONCAT_WS('', province_name, city_name) AS region_name,
            city_code AS region_code,
            CONCAT_WS('', province_name, city_name, name) AS detail_address,
            longitude,
            latitude
        FROM blade_airport_master
        WHERE id = #{id} AND status = 1 AND is_deleted = 0
    </select>

    <select id="selectSourceOptions" resultMap="commonAddressResultMap">
        <choose>
            <when test="addressType == '港口/码头'">
                SELECT
                    id AS source_id,
                    code AS site_code,
                    name AS address_name,
                    CONCAT_WS('', country, city, district_name) AS region_name,
                    district_code AS region_code,
                    detail_address,
                    longitude,
                    latitude
                FROM blade_port_terminal
                WHERE status = 1 AND is_deleted = 0
                <if test="keyword != null and keyword != ''">
                    <bind name="keywordLike" value="'%' + keyword + '%'"/>
                    AND (name LIKE #{keywordLike} OR code LIKE #{keywordLike})
                </if>
                ORDER BY code ASC
                LIMIT 50
            </when>
            <when test="addressType == '铁路车站'">
                SELECT
                    id AS source_id,
                    code AS site_code,
                    name AS address_name,
                    CONCAT_WS('', province_name, city_name) AS region_name,
                    city_code AS region_code,
                    CONCAT_WS('', province_name, city_name, name) AS detail_address,
                    longitude,
                    latitude
                FROM blade_railway_station
                WHERE status = 1 AND is_deleted = 0
                <if test="keyword != null and keyword != ''">
                    <bind name="keywordLike" value="'%' + keyword + '%'"/>
                    AND (name LIKE #{keywordLike} OR code LIKE #{keywordLike})
                </if>
                ORDER BY code ASC
                LIMIT 50
            </when>
            <when test="addressType == '空港机场'">
                SELECT
                    id AS source_id,
                    code AS site_code,
                    name AS address_name,
                    CONCAT_WS('', province_name, city_name) AS region_name,
                    city_code AS region_code,
                    CONCAT_WS('', province_name, city_name, name) AS detail_address,
                    longitude,
                    latitude
                FROM blade_airport_master
                WHERE status = 1 AND is_deleted = 0
                <if test="keyword != null and keyword != ''">
                    <bind name="keywordLike" value="'%' + keyword + '%'"/>
                    AND (name LIKE #{keywordLike} OR code LIKE #{keywordLike} OR iata_code LIKE #{keywordLike})
                </if>
                ORDER BY code ASC
                LIMIT 50
            </when>
            <otherwise>
                SELECT NULL AS source_id, NULL AS site_code, NULL AS address_name, NULL AS region_name,
                       NULL AS region_code,
                       NULL AS detail_address, NULL AS longitude, NULL AS latitude
                WHERE 1 = 0
            </otherwise>
        </choose>
    </select>

    <select id="countBusinessReference" resultType="java.lang.Integer">
        SELECT 0
    </select>

</mapper>
