<?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="com.yeejoin.amos.boot.module.jxiop.api.mapper.StationBasicMapper">


    <select id="queryPage" resultType="com.yeejoin.amos.boot.module.jxiop.api.dto.StationBasicDto">

        select
        station_basic.sequence_nbr sequenceNbr,
        station_basic.station_name stationName ,
        station_basic.station_master_name stationMasterName ,
        station_basic.mobile_phone mobilePhone ,
        station_basic.station_type stationType ,
        station_basic.station_type_name stationTypeName ,
        station_basic.address address ,
        station_basic.owner_unit ownerUnit
        from station_basic
        <where>
            is_delete=0
            <if test="stationMasterName != null and stationMasterName != ''">
                AND station_basic.station_master_name like concat('%',#{stationMasterName},'%')
            </if>
            <if test="stationName != null and stationName != ''">
                AND station_basic.station_name like concat('%',#{stationName},'%')
            </if>
            <if test="stationType != null and stationType != ''">
                AND station_basic.station_type = #{stationType}
            </if>
            <if test="orgCode != null and orgCode != ''">
                AND station_basic.project_org_code like concat('%',#{orgCode},'%')
            </if>
        </where>
        ORDER BY station_basic.rec_date DESC
        limit #{current},#{size}
    </select>

    <select id="queryPagecount" resultType="com.yeejoin.amos.boot.module.jxiop.api.dto.StationBasicDto">

        select
        station_basic.sequence_nbr sequenceNbr
        from station_basic
        <where>
            is_delete=0
            <if test="stationMasterName != null and stationMasterName != ''">
                AND station_basic.station_master_name like concat('%',#{stationMasterName},'%')
            </if>
            <if test="stationName != null and stationName != ''">
                AND station_basic.station_name like concat('%',#{stationName},'%')
            </if>
            <if test="stationType != null and stationType != ''">
                AND station_basic.station_type = #{stationType}
            </if>
            <if test="orgCode != null and orgCode != ''">
                AND station_basic.project_org_code like concat('%',#{orgCode},'%')
            </if>
        </where>
    </select>

    <delete id="deleteList">
        delete from station_basic where sequence_nbr = #{id};
        delete from station_coordinate where station_id = #{id}
    </delete>
    <delete id = "deleteStationCoordinateByStationid">
        delete from station_coordinate where station_id = #{sequence_nbr}
    </delete>

    <select id="getExStationBasicDto" resultType="com.yeejoin.amos.boot.module.jxiop.api.dto.ExStationBasicDto">

        select
        station_basic.station_name stationName ,
        station_basic.station_code stationCode,
        station_basic.station_type_name stationType,
        station_basic.station_flag stationFlag,
        station_basic.address address,
        station_basic.station_master_name stationMasterName ,
        station_basic.mobile_phone mobilePhone ,
        station_basic.phone phone,
        station_basic.email email,
        station_basic.devops_time devopsTime,
        station_basic.owner_unit ownerUnit,
        station_basic.brief_introduction briefIntroduction,
        station_basic.area_name area
        from station_basic
        <where>
            is_delete=0
            <if test="stationMasterName != null and stationMasterName != ''">
                AND station_basic.station_master_name like concat('%',#{stationMasterName},'%')
            </if>
            <if test="stationName != null and stationName != ''">
                AND station_basic.station_name like concat('%',#{stationName},'%')
            </if>
            <if test="stationType != null and stationType != ''">
                AND station_basic.station_type = #{stationType}
            </if>
            <if test="orgCode != null and orgCode != ''">
                AND station_basic.project_org_code like concat('%',#{orgCode},'%')
            </if>
            <if test="ids != null">
                and station_basic.sequence_nbr  in
                <foreach collection="ids" index="index" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>

        </where>
        ORDER BY station_basic.rec_date DESC
    </select>
    <select id="getStationList" resultType="com.yeejoin.amos.boot.module.jxiop.api.dto.StationRecordInfo">
        select
        station_basic.sequence_nbr stationId,
        station_basic.platform_station_id platformStationId,
        station_basic.station_name stationName ,
        station_basic.station_type stationType,
        station_basic.station_flag stationFlag,
        station_basic.area_code areaCode,
        station_basic.project_org_code stationCode,
        station_basic.jump_path,
        station_basic.title_pos titlePos,
        station_basic.risk_level riskLevel,
        station_basic.belong_area belongArea,
        station_coordinate.longitude,
        station_coordinate.laitude ,
        station_coordinate.is_main_coordinate
        from station_basic
        left join station_coordinate
        on station_basic.sequence_nbr = station_coordinate.station_id
        WHERE station_coordinate.sequence_nbr in
        (
        SELECT
        MIN(station_coordinate.sequence_nbr) station_coordinateID
        FROM
        station_basic
        LEFT JOIN station_coordinate ON station_basic.sequence_nbr = station_coordinate.station_id
        GROUP BY
        ( station_basic.sequence_nbr ))
        <if test="regionId != null and regionId != ''">
            AND station_basic.belong_area like concat('%',#{regionId},'%')
        </if>
    </select>
    <select id="getStationPersonByStationID" resultType="int">
        SELECT
        COUNT( 1 )
        FROM
        person_account
        LEFT JOIN station_basic ON person_account.project_id = station_basic.platform_station_id
        WHERE
        station_basic.sequence_nbr = #{sequence_nbr}
    </select>
    <select id="getStationBasicDtoList" resultType="com.yeejoin.amos.boot.module.jxiop.api.dto.StationTaksDto">
        SELECT
        station_number stationNumber,
        gateway_id gateway
        FROM
        station_basic
    </select>

    <select id="getStationListByRegionCode" resultType="com.yeejoin.amos.boot.module.jxiop.api.dto.TreeDto">
        select
            station_name as name,
            sequence_nbr as code,
            station_type as parentCode,
            sequence_nbr as id,
            '1' as isOnclick,
            scene_id as sceneId
        from station_basic
        <where>
            <if test="regionCode != null and regionCode != ''">
                belong_area like concat('%', #{regionCode}, '%')
            </if>
        </where>
    </select>

    <update id="updateYardByProjectOrgCode">
        <foreach collection="list" item="item" separator=";">
            update
            `station_basic`
            set
            `qrcode_color` = #{item.qrcodeColor},
            <if test="item.level != null and item.level != ''">
            risk_Level = #{item.level},
            </if>
            rec_date = now()
            where
            project_org_code = #{item.code}
        </foreach>
    </update>

    <select id="getStationBasicListAll" resultType="com.yeejoin.amos.boot.module.jxiop.api.dto.StationBasicDto">
        SELECT
            station_number stationNumber,
            project_org_code AS projectOrgCode
        FROM
            station_basic
    </select>
    <select id="getStationInfoByCode" resultType="com.yeejoin.amos.boot.module.jxiop.api.dto.StationBasicDto">
        select
        *
        from
        station_basic
        where
            station_code = #{stationCode}

    </select>

    <select id="getStationsByAreaCode" resultType="com.yeejoin.amos.boot.module.jxiop.api.dto.StationBasicDto">
        select
            *
        from
            station_basic
        where
            area_code = #{areaCode}
        and
            is_delete = 0

    </select>
</mapper>
