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

  <resultMap id="BaseResultMap" type="com.railway.business.baseinfo.domain.BusFjxgmd">
    <result column="id" property="id"/>
    <result column="dept_id" property="deptId"/>
    <result column="dept_name" property="deptName"/>
    <result column="line_id" property="lineId"/>
    <result column="line_name" property="lineName"/>
    <result column="station_id" property="stationId"/>
    <result column="station_name" property="stationName"/>
    <result column="dept_station_id" property="deptStationId"/>
    <result column="xingbie" property="xingbie"/>
    <result column="fjlx" property="fjlx"/>
    <result column="qsglb" property="qsglb"/>
    <result column="qsgh" property="qsgh"/>
    <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"/>
    <result column="dept_name" property="deptName"/>
    <result column="line_name" property="lineName"/>
    <result column="xingbie_text" property="xingbieText"/>
    <result column="fjlx_text" property="fjlxText"/>
    <result column="pillar_area" property="pillarArea"/>
  </resultMap>

  <sql id="Base_Column_List">
    t.id,
    t.dept_station_id,
    t.xingbie,
    t.fjlx,
    t.qsglb,
    t.qsgh,
    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.BusFjxgmd">
    <selectKey keyProperty="id" order="AFTER" resultType="Long">
      select @@IDENTITY as id
    </selectKey>
    INSERT INTO base_fjxgmd
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test='null != deptStationId'>
        dept_station_id,
      </if>
      <if test='null != xingbie'>
        xingbie,
      </if>
      <if test='null != fjlx'>
        fjlx,
      </if>
      <if test='null != qsglb'>
        qsglb,
      </if>
      <if test='null != qsgh'>
        qsgh,
      </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 != deptStationId'>
        #{deptStationId},
      </if>
      <if test='null != xingbie'>
        #{xingbie},
      </if>
      <if test='null != fjlx'>
        #{fjlx},
      </if>
      <if test='null != qsglb'>
        #{qsglb},
      </if>
      <if test='null != qsgh'>
        #{qsgh},
      </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_fjxgmd
    set del_flag='1'
    WHERE id = #{id}
  </delete>

  <update id="update" parameterType="com.railway.business.baseinfo.domain.BusFjxgmd">
    UPDATE base_fjxgmd
    <set>
      <if test='null != deptStationId'>dept_station_id = #{deptStationId},</if>
      <if test='null != xingbie'>xingbie = #{xingbie},</if>
      <if test='null != fjlx'>fjlx = #{fjlx},</if>
      <if test='null != qsglb'>qsglb = #{qsglb},</if>
      <if test='null != qsgh'>qsgh = #{qsgh},</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 id = #{id}
  </update>


  <select id="getInfo" resultMap="BaseResultMap">
    SELECT
    DISTINCT
    v.dept_name,
    v.line_name,
    v.station_name,
    v.station_id,
    v.dept_id,
    v.line_id,
    dict1.dict_label as xingbie_text,
    dict2.dict_label as fjlx_text,
    <include refid="Base_Column_List"/>
    FROM
    base_fjxgmd t
    LEFT JOIN v_station v on t.dept_station_id = v.dept_station_id
    LEFT JOIN sys_dict_data dict1 ON (t.xingbie = dict1.dict_value AND dict1.dict_type = 'xingbie')
    LEFT JOIN sys_dict_data dict2 ON (t.fjlx = dict2.dict_value AND dict2.dict_type = 'fjlx')
    WHERE t.del_flag = '0'
      and dict1.status =  '0'
      and dict2.status =  '0'
      and t.id = #{id}
  </select>

  <select id="getList" resultMap="BaseResultMap">
    SELECT
      DISTINCT
      v.dept_name,
      v.line_name,
      v.station_name,
      v.station_id,
      v.dept_id,
      v.line_id,
      dict1.dict_label as xingbie_text,
      dict2.dict_label as fjlx_text,
    <include refid="Base_Column_List"/>
    FROM
        base_fjxgmd t
    LEFT JOIN v_station v on t.dept_station_id = v.dept_station_id
    LEFT JOIN sys_dict_data dict1 ON (t.xingbie = dict1.dict_value AND dict1.dict_type = 'xingbie')
    LEFT JOIN sys_dict_data dict2 ON (t.fjlx = dict2.dict_value AND dict2.dict_type = 'fjlx')
    <where>
      t.del_flag = '0'
      and dict1.status =  '0'
      and dict2.status =  '0'
      <if test="deptId!=null and deptId!=''">
        and v.dept_id=#{deptId}
      </if>
      <if test="lineId!=null and lineId!=''">
        and v.line_id=#{lineId}
      </if>
      <if test="stationId!=null and stationId!=''">
        and v.station_id=#{stationId}
      </if>
      <if test="xingbie!=null and xingbie!=''">
        and t.xingbie=#{xingbie}
      </if>
      <if test="fjlx!=null and fjlx!=''">
        and t.fjlx=#{fjlx}
      </if>
      <if test="qsglb!=null and qsglb!=''">
        and t.qsglb=#{qsglb}
      </if>
      <if test="qsgh!=null and qsgh!=''">
        and t.qsgh=#{qsgh}
      </if>
      <if test="pillarArea!=null and pillarArea!=''">
        and t.qsglb BETWEEN (t.qsglb + 0.0) - (#{pillarArea} + 0.0) AND (t.qsglb + 0.0) + (#{pillarArea} + 0.0)
      </if>
      <if test="delFlag!=null and delFlag!=''">
        and t.del_flag=#{delFlag}
      </if>
      <if test="createBy!=null and createBy!=''">
        and t.create_by=#{createBy}
      </if>
      <if test="createTime!=null">
        and DATE_FORMAT(t.create_time, '%Y-%m-%d') = #{createTime}
      </if>
      <if test="updateBy!=null and updateBy!=''">
        and t.update_by=#{updateBy}
      </if>
      <if test="updateTime!=null">
        and DATE_FORMAT(t.update_time, '%Y-%m-%d') = #{updateTime}
      </if>
    </where>
  </select>

  <select id="getListByZz" resultMap="BaseResultMap">

      SELECT
        DISTINCT
        a.id,
        v.dept_id,
        v.dept_name,
        v.line_name,
        v.station_name,
        a.qsgh,
        a.qsglb,
        v.station_id,
        a.xingbie,
        a.fjlx,
        dict1.dict_label as xingbie_text,
        dict2.dict_label AS fjlx_text,
        ${query.pillarArea} AS pillar_area
      FROM base_fjxgmd a
      LEFT JOIN v_station v ON a.dept_station_id = v.dept_station_id
      LEFT JOIN sys_dict_data dict1 ON (a.xingbie = dict1.dict_value AND dict1.dict_type = 'xingbie')
      LEFT JOIN sys_dict_data dict2 ON (a.fjlx = dict2.dict_value AND dict2.dict_type = 'fjlx')
      WHERE a.del_flag = '0'
            and dict1.status =  '0'
            and dict2.status =  '0'
      <if test="query.lineId != null and query.lineId != ''">
        AND v.line_id = #{query.lineId}
      </if>
      <if test="query.xingbie != null and query.xingbie != '' and query.xingbie != 'sxx'">
        AND a.xingbie = #{query.xingbie}
      </if>
      <if test="query.marker != null and query.marker != ''">
        AND (a.qsglb + 0.0) BETWEEN (#{query.marker} + 0.0) - (#{query.pillarArea} + 0.0)
        AND (#{query.marker} + 0.0) + (#{query.pillarArea} + 0.0)
      </if>
      ORDER BY (a.qsglb + 0)
  </select>

</mapper>