<?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="cn.cecep.talroad.mapper.PCodeStationMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.cecep.talroad.domain.PCodeStation">
        <id column="station_id" property="stationId"/>
        <result column="province_code" property="provinceCode"/>
        <result column="province" property="province"/>
        <result column="city_code" property="cityCode"/>
        <result column="city" property="city"/>
        <result column="district_code" property="districtCode"/>
        <result column="district" property="district"/>
        <result column="township_code" property="townShipCode"/>
        <result column="township" property="townShip"/>
        <result column="station_name" property="stationName"/>
        <result column="station_code" property="stationCode"/>
        <result column="status" property="status"/>
        <result column="region_code" property="regionCode"/>
        <result column="city_assessment_point" property="regionCode"/>
        <result column="district_assessment_point" property="regionCode"/>
        <result column="street_assessment_point" property="regionCode"/>
        <result column="function_id" property="functionId"/>
        <result column="station_level" property="stationLevel"/>
        <result column="station_type" property="stationType"/>
        <result column="parent_type" property="parentType"/>
        <result column="fk_parent_id" property="fkParentId"/>
        <result column="longitude" property="longitude"/>
        <result column="latitude" property="latitude"/>
        <result column="bmap_longitude" property="bmapLongitude"/>
        <result column="bmap_latitude" property="bmapLatitude"/>
        <result column="gcg_longitude" property="gcgLongitude"/>
        <result column="gcg_latitude" property="gcgLatitude"/>
        <result column="address" property="address"/>
        <result column="principal" property="principal"/>
        <result column="phone" property="phone"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="is_assessment_city" property="isAssessmentCity"/>
        <result column="is_auto_audit" property="isAutoAudit"/>
        <result column="is_auto_statistic" property="isAutoStatistic"/>
        <result column="is_used" property="isUsed"/>
        <result column="is_maintain" property="isMaintain"/>
        <result column="meteorological_data" property="meteorologicalData"/>
        <result column="is_forecast" property="isForecast"/>
        <result column="update_time" property="updateTime"/>
        <result column="is_chinaaqi" property="isChinaaqi"/>
        <result column="data_type" property="dataType"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        station_id, province_code, province, city_code, city, district_code, district,township,township_code station_name, station_code, status, region_code,city_assessment_point,district_assessment_point,street_assessment_point, function_id, station_level, station_type, parent_type, fk_parent_id, longitude, latitude, bmap_longitude, bmap_latitude, gcg_longitude, gcg_latitude, address, principal, phone, create_user,create_time,is_assessment_city, is_auto_audit, is_auto_statistic, is_used, is_maintain, meteorological_data, is_forecast, update_time, is_chinaaqi, data_type,remark
    </sql>
    <select id="querySiteInformations" resultMap="BaseResultMap">
        SELECT * FROM p_code_station
        WHERE is_used = '0'
        <if test="site.stationName!=null and site.stationName!=''">
            AND station_name = #{site.stationName}
        </if>
        <if test="site.stationType!=null">
            AND station_type = #{site.stationType}
        </if>
        <if test="site.areaCode!=null and site.areaCode!=''">
            AND (province_code= #{site.areaCode} or city_code = #{site.areaCode} or district_code =
            #{site.areaCode})
        </if>
        <if test="site.stationId!=null and site.stationId!=''">
            AND station_id = #{site.stationId}
        </if>
        <if test="site.stationLevel!=null and site.stationLevel!=''">
            AND station_level = #{site.stationLevel}
        </if>
        <if test="site.stationIdList != null and site.stationIdList.size > 0">
            AND station_id IN
            <foreach collection="site.stationIdList" item="siteId" open="(" close=")" separator=",">
                #{siteId}
            </foreach>
        </if>
        <if test="site.stationLevelList!=null and site.stationLevelList.size > 0">
            AND station_level IN
            <foreach collection="site.stationLevelList" item="siteLevel" open="(" close=")" separator=",">
                #{siteLevel}
            </foreach>
        </if>
        <if test="site.districtCodeList!=null and site.districtCodeList.size > 0">
            AND district_code IN (
            <foreach collection="districtCodeList" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>

        <if test="site.citySort == 1 or site.citySort == '1'">
            ORDER BY latitude asc
        </if>
        <if test="site.citySort == 2 or site.citySort == '2'">
            ORDER BY latitude desc
        </if>
        <if test="site.citySort == 3 or site.citySort == '3'">
            ORDER BY longitude desc
        </if>
        <if test="site.citySort == 4 or site.citySort == '4'">
            ORDER BY longitude asc
        </if>
        <if test="site.citySort == 5 or site.citySort == '5'">
            ORDER BY longitude desc, latitude desc
        </if>
        <if test="site.citySort == 6 or site.citySort == '6'">
            ORDER BY longitude asc, latitude asc
        </if>
        <if test="site.citySort == 7 or site.citySort == '7'">
            ORDER BY longitude desc, latitude asc
        </if>
        <if test="site.citySort == 8 or site.citySort == '8'">
            ORDER BY longitude asc, latitude desc
        </if>
    </select>

    <select id="findSiteList" resultType="cn.cecep.talroad.domain.vo.SiteOperationVo">
        SELECT
        a.station_id as stationId,
        a.province as province,
        a.province_code as provinceCode,
        a.city as city,
        a.city_code as cityCode,
        a.district as county,
        a.district_code as countyCode,
        a.township as townShip,
        a.township_code as townShipCode,
        a.station_level as stationLevel,
        b.level_name as stationLevelName,
        a.station_name as stationName,
        a.station_code as stationCode,
        a.longitude as longitude,
        a.latitude as latitude,
        a.address as address,
        a.create_user as createUser,
        a.create_time as  createTime,
        a.principal as principal,
        a.phone as phone,
        a.status as status,
        a.remark as remark,
        a.city_assessment_point as cityAssesmentPoint,
        a.district_assessment_point as districtAssesmentPoint,
        a.street_assessment_point as streetAssesmentPoint
        FROM
        p_code_station as a
        LEFT JOIN s_envgas_station_control as b
        ON a.station_level = b.pk_id
        WHERE a.station_type = '1'
        AND a.is_used = '0'
        <if test="site.stationLevel!=null and site.stationLevel!=0">
            AND a.station_level = #{site.stationLevel}
        </if>
        <if test="site.provinceCode != null and site.provinceCode != ''">
            AND a.province_code = #{site.provinceCode}
        </if>
        <if test="site.cityCode != null and site.cityCode != ''">
            AND a.city_code = #{site.cityCode}
        </if>
        <if test="site.countyCode!=null and site.countyCode!=''">
            AND a.district_code = #{site.countyCode}
        </if>
        <if test="site.districtCode!=null and site.districtCode!=''">
            AND a.district_code = #{site.districtCode}
        </if>
        <if test="site.townShipCode!=null and site.townShipCode!=''">
            AND a.township_code = #{site.townShipCode}
        </if>
        <if test="site.stationName!=null and site.stationName!=''">
            AND a.station_name like concat ('%',#{site.stationName},'%')
        </if>
        <if test="site.stationCode != null and site.stationCode !=''">
            AND a.station_code like concat ('%',#{site.stationCode},'%')
        </if>
        <if test="site.stationIdList != null and site.stationIdList.size > 0">
            AND a.station_id IN (
            <foreach collection="site.stationIdList" item="item" separator=",">
                #{item}
            </foreach>
            )
        </if>
        ORDER BY a.station_level,a.station_name
    </select>
    <select id="isExist" resultType="java.lang.String">
        SELECT
        station_id
        FROM
        p_code_station
        <where>
            AND station_type = 1
            AND is_used = 0
            <if test="info.provinceCode != null and info.provinceCode != ''">
                AND province_code = #{info.provinceCode}
            </if>
            <if test="info.cityCode != null and info.cityCode != ''">
                AND city_code = #{info.cityCode}
            </if>
            <if test="info.districtCode != null and info.districtCode != ''">
                AND district_code = #{info.districtCode}
            </if>
            <if test="info.townShipCode != null and info.townShipCode != ''">
                AND township_code = #{info.townShipCode}
            </if>
            <if test="info.stationName != null and info.stationName != ''">
                AND station_name = #{info.stationName}
            </if>
            <if test="info.stationCode != null and info.stationCode != ''">
                AND station_code = #{info.stationCode}
            </if>
            <if test="info.stationId != null and info.stationId != ''">
                AND station_id != #{info.stationId}
            </if>
        </where>
    </select>
    <select id="findAllCityStations" resultType="cn.cecep.talroad.domain.PCodeStation">
        SELECT
            *
        FROM
            p_code_station
        WHERE
            station_type = 1
            AND station_level IN ( 1, 2 )
            AND is_used = 0
            AND city_code IN (
            SELECT
                region_code
            FROM
                p_code_region
            WHERE
            region_lever = 3
            )
    </select>

    <select id="findStationInfo" resultType="java.util.Map">
        SELECT
            a.station_name name,
            a.station_level level,
            b.level_name levelName
        FROM
            p_code_station a
            left JOIN s_envgas_station_control b
            on a.station_level = b.pk_id
        WHERE
            station_id = #{stationId}
    </select>
    <select id="findCityCodeByStationId" resultType="java.lang.String">
        SELECT city_code FROM p_code_station WHERE station_id = #{stationId}
    </select>
</mapper>
