<?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.railway.business.baseinfo.mapper.BaseStationMapper">

  <resultMap id="BaseResultMap" type="com.railway.business.baseinfo.domain.vo.BaseStationVo">
    <result column="station_id" property="stationId"/>
    <result column="line_id" property="lineId"/>
    <result column="line_name" property="lineName"/>
    <result column="station_name" property="stationName"/>
    <result column="station_type" property="stationType"/>
    <result column="station_type_text" property="stationTypeText"/>
    <result column="order_num" property="orderNum"/>
    <result column="start_marker" property="startMarker"/>
    <result column="end_marker" property="endMarker"/>
    <result column="del_flag" property="delFlag"/>
    <result column="create_by" property="createBy"/>
    <result column="create_time" property="createTime"/>
    <result column="update_by" property="updateBy"/>
    <result column="update_time" property="updateTime"/>
  </resultMap>

  <sql id="Base_Column_List">
    t.station_id,
    t.line_id,
    t.station_name,
    t.station_type,
    t.order_num,
    t.start_marker,
    t.end_marker,
    t.del_flag,
    t.create_by,
    t.create_time,
    t.update_by,
    t.update_time
  </sql>

  <insert id="insert" parameterType="com.railway.business.baseinfo.domain.BaseStation">
    <selectKey keyProperty="stationId" order="AFTER" resultType="Long">
      select @@IDENTITY as station_id
    </selectKey>
    INSERT INTO base_station
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test='null != stationName'>
        station_name,
      </if>
      <if test='null != lineId'>
        line_id,
      </if>
      <if test='null != stationType'>
        station_type,
      </if>
      <if test='null != orderNum'>
        order_num,
      </if>
      <if test='null != startMarker'>
        start_marker,
      </if>
      <if test='null != endMarker'>
        end_marker,
      </if>
      <if test='null != delFlag'>
        del_flag,
      </if>
      <if test='null != createBy'>
        create_by,
      </if>
      <if test='null != createTime'>
        create_time,
      </if>
      <if test='null != updateBy'>
        update_by,
      </if>
      <if test='null != updateTime'>
        update_time
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test='null != stationName'>
        #{stationName},
      </if>
      <if test='null != lineId'>
        #{lineId},
      </if>
      <if test='null != stationType'>
        #{stationType},
      </if>
      <if test='null != orderNum'>
        #{orderNum},
      </if>
      <if test='null != startMarker'>
        #{startMarker},
      </if>
      <if test='null != endMarker'>
        #{endMarker},
      </if>
      <if test='null != delFlag'>
        #{delFlag},
      </if>
      <if test='null != createBy'>
        #{createBy},
      </if>
      <if test='null != createTime'>
        #{createTime},
      </if>
      <if test='null != updateBy'>
        #{updateBy},
      </if>
      <if test='null != updateTime'>
        #{updateTime}
      </if>
    </trim>
  </insert>

  <delete id="delete">
    UPDATE base_station
    set del_flag='1'
    WHERE station_id = #{stationId}
  </delete>

  <update id="update" parameterType="com.railway.business.baseinfo.domain.BaseStation">
    UPDATE base_station
    <set>
      <if test='null != stationName'>station_name = #{stationName},</if>
      <if test='null != lineId'>line_id = #{lineId},</if>
      <if test='null != stationType'>station_type = #{stationType},</if>
      <if test='null != orderNum'>order_num = #{orderNum},</if>
      <if test='null != startMarker'>start_marker = #{startMarker},</if>
      <if test='null != endMarker'>end_marker = #{endMarker},</if>
      <if test='null != delFlag'>del_flag = #{delFlag},</if>
      <if test='null != updateBy'>update_by = #{updateBy},</if>
      <if test='null != updateTime'>update_time = #{updateTime}</if>
    </set>
    WHERE station_id = #{stationId}
  </update>


  <select id="getInfo" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>, v.line_name, dict.dict_label as station_type_text
    FROM base_station t
    left join sys_dict_data dict on t.station_type = dict.dict_value and dict.dict_type = 'station_type'
    left join base_dept_station v on t.station_id = t1.station_id
    WHERE t.del_flag='0' and t.station_id = #{stationId}

  </select>

  <select id="getList" resultMap="BaseResultMap">
    SELECT DISTINCT
    <include refid="Base_Column_List"/>, v.line_name, dict.dict_label as station_type_text
    FROM base_station t
    left join sys_dict_data dict on t.station_type = dict.dict_value and dict.dict_type = 'station_type'
    left join v_station v on t.station_id = v.station_id
    <where>
      t.del_flag='0'
      <if test="deptId!=null and deptId!=''">
        and  v.dept_id=#{deptId}
      </if>
      <if test="lineId!=null and lineId!=''">
        and  t.line_id=#{lineId}
      </if>
      <if test="stationName!=null and stationName!=''">
        and  t.station_name like concat('%', #{stationName}, '%')
      </if>
      <if test="stationType!=null and stationType!=''">
        and  t.station_type=#{stationType}
      </if>
      <if test="startMarker!=null and startMarker!=''">
        and  t.start_marker <![CDATA[ >= ]]> #{startMarker}
      </if>
      <if test="endMarker!=null and endMarker!=''">
        and  t.end_marker <![CDATA[ <= ]]> #{endMarker}
      </if>
    </where>
  </select>

  <select id="getStationList" resultMap="BaseResultMap">
    SELECT DISTINCT
    <include refid="Base_Column_List"/>, v.line_name, dict.dict_label as station_type_text
    FROM base_station t
    left join sys_dict_data dict on t.station_type = dict.dict_value and dict.dict_type = 'station_type'
    left join v_station v on t.station_id = v.station_id
    <where>
      t.del_flag='0'
      <if test="deptId!=null and deptId!=''">
        and  v.dept_id=#{deptId}
      </if>
      <if test="lineId!=null and lineId!=''">
        and  t.line_id=#{lineId}
      </if>
      <if test="stationName!=null and stationName!=''">
        and  t.station_name like concat('%', #{stationName}, '%')
      </if>
      <if test="stationType!=null and stationType!=''">
        and  t.station_type=#{stationType}
      </if>
      <if test="startMarker!=null and startMarker!=''">
        and  t.start_marker <![CDATA[ >= ]]> #{startMarker}
      </if>
      <if test="endMarker!=null and endMarker!=''">
        and  t.end_marker <![CDATA[ <= ]]> #{endMarker}
      </if>
    </where>
  </select>

</mapper>