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

    <resultMap id="railwayStationResultMap" type="org.springblade.system.pojo.vo.RailwayStationVO">
        <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="tmis_code" property="tmisCode"/>
        <result column="telegraph_code" property="telegraphCode"/>
        <result column="name" property="name"/>
        <result column="railway_line" property="railwayLine"/>
        <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="district_code" property="districtCode"/>
        <result column="district_name" property="districtName"/>
        <result column="district_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="selectRailwayStationPage" resultMap="railwayStationResultMap">
        SELECT
            rs.id,
            rs.create_user,
            rs.create_dept,
            rs.create_time,
            rs.update_user,
            bu.real_name AS update_user_name,
            rs.update_time,
            rs.status,
            rs.is_deleted,
            rs.code,
            rs.tmis_code,
            rs.telegraph_code,
            rs.name,
            rs.railway_line,
            rs.province_code,
            rs.province_name,
            rs.city_code,
            rs.city_name,
            rs.district_code,
            rs.district_name,
            rs.region_name,
            rs.detail_address,
            rs.longitude,
            rs.latitude,
            CASE
                WHEN rs.data_source = '初始导入' THEN '初始化导入'
                WHEN rs.data_source = '批量导入' THEN '批量'
                WHEN rs.data_source IN ('手工导入', '手动录入') THEN '手动'
                ELSE rs.data_source
            END AS data_source,
            rs.remark
        FROM
            blade_railway_station rs
            LEFT JOIN blade_user bu ON bu.id = rs.update_user
        WHERE
            rs.is_deleted = 0
        <if test="railwayStation.code != null and railwayStation.code != ''">
            <bind name="codeLike" value="'%' + railwayStation.code + '%'"/>
            AND rs.code LIKE #{codeLike}
        </if>
        <if test="railwayStation.name != null and railwayStation.name != ''">
            <bind name="nameLike" value="'%' + railwayStation.name + '%'"/>
            AND rs.name LIKE #{nameLike}
        </if>
        <if test="railwayStation.tmisCode != null and railwayStation.tmisCode != ''">
            AND rs.tmis_code = #{railwayStation.tmisCode}
        </if>
        <if test="railwayStation.telegraphCode != null and railwayStation.telegraphCode != ''">
            AND rs.telegraph_code = #{railwayStation.telegraphCode}
        </if>
        <if test="railwayStation.regionCode != null and railwayStation.regionCode != ''">
            AND rs.district_code = #{railwayStation.regionCode}
        </if>
        <if test="railwayStation.districtCode != null and railwayStation.districtCode != ''">
            AND rs.district_code = #{railwayStation.districtCode}
        </if>
        <if test="railwayStation.districtName != null and railwayStation.districtName != ''">
            <bind name="districtNameLike" value="'%' + railwayStation.districtName + '%'"/>
            AND rs.district_name LIKE #{districtNameLike}
        </if>
        <if test="railwayStation.regionName != null and railwayStation.regionName != ''">
            <bind name="regionNameLike" value="'%' + railwayStation.regionName + '%'"/>
            AND rs.region_name LIKE #{regionNameLike}
        </if>
        <if test="railwayStation.detailAddress != null and railwayStation.detailAddress != ''">
            <bind name="detailAddressLike" value="'%' + railwayStation.detailAddress + '%'"/>
            AND rs.detail_address LIKE #{detailAddressLike}
        </if>
        <if test="railwayStation.dataSource != null and railwayStation.dataSource != ''">
            <choose>
                <when test="railwayStation.dataSource == '初始化导入'">
                    AND rs.data_source IN ('初始化导入', '初始导入')
                </when>
                <when test="railwayStation.dataSource == '批量'">
                    AND rs.data_source IN ('批量', '批量导入')
                </when>
                <when test="railwayStation.dataSource == '手动'">
                    AND rs.data_source IN ('手动', '手动录入', '手工导入')
                </when>
                <otherwise>
                    AND rs.data_source = #{railwayStation.dataSource}
                </otherwise>
            </choose>
        </if>
        <if test="railwayStation.status != null">
            AND rs.status = #{railwayStation.status}
        </if>
        ORDER BY rs.create_time DESC
    </select>

</mapper>
