<?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.PortTerminalMapper">

    <resultMap id="portTerminalResultMap" type="org.springblade.system.pojo.vo.PortTerminalVO">
        <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="name" property="name"/>
        <result column="category" property="category"/>
        <result column="parent_id" property="parentId"/>
        <result column="parent_code" property="parentCode"/>
        <result column="parent_name" property="parentName"/>
        <result column="country" property="country"/>
        <result column="city" property="city"/>
        <result column="district_code" property="districtCode"/>
        <result column="district_name" property="districtName"/>
        <result column="district_code" property="regionCode"/>
        <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="selectPortTerminalPage" resultMap="portTerminalResultMap">
        SELECT
            pt.id,
            pt.create_user,
            pt.create_dept,
            pt.create_time,
            pt.update_user,
            bu.real_name AS update_user_name,
            pt.update_time,
            pt.status,
            pt.is_deleted,
            pt.code,
            pt.name,
            pt.category,
            pt.parent_id,
            pt.parent_code,
            pt.parent_name,
            pt.country,
            pt.city,
            pt.district_code,
            pt.district_name,
            pt.detail_address,
            CASE WHEN pt.longitude BETWEEN -180 AND 180 THEN pt.longitude ELSE NULL END AS longitude,
            CASE WHEN pt.latitude BETWEEN -90 AND 90 THEN pt.latitude ELSE NULL END AS latitude,
            CASE WHEN pt.data_source = '初始导入' THEN '初始化导入' ELSE pt.data_source END AS data_source,
            pt.remark
        FROM
            blade_port_terminal pt
            LEFT JOIN blade_user bu ON bu.id = pt.update_user
        WHERE
            pt.is_deleted = 0
        <if test="portTerminal.code != null and portTerminal.code != ''">
            <bind name="codeLike" value="'%' + portTerminal.code + '%'"/>
            AND pt.code LIKE #{codeLike}
        </if>
        <if test="portTerminal.name != null and portTerminal.name != ''">
            <bind name="nameLike" value="'%' + portTerminal.name + '%'"/>
            AND pt.name LIKE #{nameLike}
        </if>
        <if test="portTerminal.category != null and portTerminal.category != ''">
            AND pt.category = #{portTerminal.category}
        </if>
        <if test="portTerminal.parentId != null">
            AND pt.parent_id = #{portTerminal.parentId}
        </if>
        <if test="portTerminal.dataSource != null and portTerminal.dataSource != ''">
            <choose>
                <when test="portTerminal.dataSource == '初始化导入'">
                    AND pt.data_source IN ('初始化导入', '初始导入')
                </when>
                <otherwise>
                    AND pt.data_source = #{portTerminal.dataSource}
                </otherwise>
            </choose>
        </if>
        <if test="portTerminal.city != null and portTerminal.city != ''">
            AND pt.city = #{portTerminal.city}
        </if>
        <if test="portTerminal.districtCode != null and portTerminal.districtCode != ''">
            AND pt.district_code = #{portTerminal.districtCode}
        </if>
        <if test="portTerminal.districtName != null and portTerminal.districtName != ''">
            <bind name="districtNameLike" value="'%' + portTerminal.districtName + '%'"/>
            AND pt.district_name LIKE #{districtNameLike}
        </if>
        <if test="portTerminal.regionCode != null and portTerminal.regionCode != ''">
            AND pt.district_code = #{portTerminal.regionCode}
        </if>
        <if test="portTerminal.detailAddress != null and portTerminal.detailAddress != ''">
            <bind name="detailAddressLike" value="'%' + portTerminal.detailAddress + '%'"/>
            AND pt.detail_address LIKE #{detailAddressLike}
        </if>
        <if test="portTerminal.country != null and portTerminal.country != ''">
            AND pt.country = #{portTerminal.country}
        </if>
        <if test="portTerminal.status != null">
            AND pt.status = #{portTerminal.status}
        </if>
        ORDER BY pt.create_time DESC
    </select>

</mapper>
