<?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.inspect.metadata.station.mapper.BasedataStationMapper">

    <resultMap type="BasedataStation" id="BasedataStationResult">
        <result property="stationId" column="station_id"/>
        <result property="stationNo" column="station_no"/>
        <result property="stationName" column="station_name"/>
        <result property="stationType" column="station_type"/>
        <result property="description" column="description"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="voltLevel" column="volt_level"/>
        <result property="nodeId" column="node_id"/>
        <result property="provinceName" column="province_name"/>
        <result property="cityName" column="city_name"/>
    </resultMap>

    <sql id="selectBasedataStationVo">
        select station_id,
               station_no,
               station_name,
               station_type,
               description,
               create_by,
               create_time,
               update_by,
               update_time,
               volt_level,
               node_id,
               province_name,
               city_name
        from basedata_station
    </sql>

    <select id="selectBasedataStationList" parameterType="BasedataStation" resultMap="BasedataStationResult">
        <include refid="selectBasedataStationVo"/>
        <where>
            <if test="stationName != null  and stationName != ''">and station_name like concat('%', #{stationName},
                '%')
            </if>
            <if test="stationType != null  and stationType != ''">and station_type = #{stationType}</if>
        </where>
    </select>

    <select id="selectBasedataStationByStationId" parameterType="String" resultMap="BasedataStationResult">
        <include refid="selectBasedataStationVo"/>
        where station_id = #{stationId}
    </select>

    <insert id="insertBasedataStation" parameterType="BasedataStation" useGeneratedKeys="true" keyProperty="stationId">
        insert into basedata_station
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationNo != null">station_no,</if>
            <if test="stationName != null">station_name,</if>
            <if test="stationType != null">station_type,</if>
            <if test="description != null">description,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="voltLevel != null">volt_level,</if>
            <if test="nodeId != null">node_id,</if>
            <if test="provinceName != null">province_name,</if>
            <if test="cityName != null">city_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationNo != null">#{stationNo},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="stationType != null">#{stationType},</if>
            <if test="description != null">#{description},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="voltLevel != null">#{voltLevel},</if>
            <if test="nodeId != null">#{nodeId},</if>
            <if test="provinceName != null">#{provinceName},</if>
            <if test="cityName != null">#{cityName},</if>
        </trim>
    </insert>

    <update id="updateBasedataStation" parameterType="BasedataStation">
        update basedata_station
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationNo != null">station_no = #{stationNo},</if>
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="stationType != null">station_type = #{stationType},</if>
            <if test="description != null">description = #{description},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="voltLevel != null">volt_level = #{voltLevel},</if>
            <if test="nodeId != null">node_id = #{nodeId},</if>
            <if test="provinceName != null">province_name = #{provinceName},</if>
            <if test="cityName != null">city_name = #{cityName},</if>
        </trim>
        where station_id = #{stationId}
    </update>

    <delete id="deleteBasedataStationByStationId" parameterType="String">
        delete
        from basedata_station
        where station_id = #{stationId}
    </delete>

    <delete id="deleteBasedataStationByStationIds" parameterType="String">
        delete from basedata_station where station_id in
        <foreach item="stationId" collection="array" open="(" separator="," close=")">
            #{stationId}
        </foreach>
    </delete>

    <select id="getStationTypeByPatrolPointId" parameterType="String" resultMap="BasedataStationResult">
        SELECT c.station_name,
               c.station_type
        FROM basedata_patrolpoint a,
             basedata_device b,
             basedata_station c
        WHERE 1 = 1
          AND a.main_device_id = b.device_id
          AND b.station_no = c.station_no
          AND a.patrol_point_id = #{patrolpointId}
    </select>

    <select id="checkStationNameIsExit" parameterType="String" resultType="int">
        SELECT
        COUNT( 0 )
        FROM
        basedata_station a
        <where>
            <if test="stationName != null  and stationName != ''">and station_name = #{stationName}</if>
            <if test="stationId != null  and stationId != ''">and station_id != #{stationId}</if>
        </where>
        UNION
        SELECT
        COUNT( 0 )
        FROM
        basedata_station a
        <where>
            <if test="stationId != null  and stationId != ''">and station_id != #{stationId}</if>
            <if test="stationNo != null  and stationNo != ''">and station_no = #{stationNo}</if>
        </where>
    </select>

    <select id="getStationNameList" parameterType="String" resultType="map">
        SELECT a.station_id,
               a.station_name
        FROM basedata_station a
    </select>

</mapper>
