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

  <resultMap id="BaseResultMap" type="com.railway.business.baseinfo.domain.BusPrevent">
    <result column="prevent_id" property="preventId"/>
    <result column="prevent_type" property="preventType"/>
    <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="xingbie" property="xingbie"/>
    <result column="xingbie_text" property="xingbieText"/>
    <result column="station_id" property="stationId"/>
    <result column="station_name" property="stationName"/>
    <result column="dept_station_id" property="deptStationId"/>
    <result column="pillar_code" property="pillarCode"/>
    <result column="marker" property="marker"/>
    <result column="risk_source" property="riskSource"/>
    <result column="measures" property="measures"/>
    <result column="remark" property="remark"/>
    <result column="fill_date" property="fillDate"/>
    <result column="fill_user" property="fillUser"/>
    <result column="pillar_area" property="pillarArea"/>
    <result column="pillar_count" property="pillarCount"/>
    <result column="line_type" property="lineType"/>
    <result column="rust_position" property="rustPosition"/>
    <result column="pillar_type" property="pillarType"/>
    <result column="terrain" property="terrain"/>
    <result column="iftransform" property="iftransform"/>
    <result column="specification" property="specification"/>
    <result column="display_num" property="displayNum"/>
    <result column="count" property="count"/>
    <result column="distance_horizontal" property="distanceHorizontal"/>
    <result column="distance_vertical" property="distanceVertical"/>
    <result column="kind" property="kind"/>
    <result column="severity" property="severity"/>
    <result column="property_rights" property="propertyRights"/>
    <result column="inquirer" property="inquirer"/>
    <result column="start_marker" property="startMarker"/>
    <result column="end_marker" property="endMarker"/>
  </resultMap>

<!--  <sql id="Base_Column_List">
    prevent_id,
    prevent_type,
    dept_id,
    dept_name,
    line_id,
    line_name
    xingbie,
    station_id,
    station_name,
    pillar_code,
    marker,
    risk_source,
    measures,
    remark,
    fill_date,
    fill_user,
    pillar_area,
    pillar_count,
    line_type,
    rust_position,
    pillar_type,
    terrain,
    iftransform,
    specification,
    display_num,
    count,
    distance_horizontal,
    distance_vertical,
    kind,
    severity,
    property_rights.
    inquirer
  </sql>-->


  <select id="smartQuery" resultMap="BaseResultMap">
    SELECT
    v.prevent_id,
    v.prevent_type,
    v1.dept_id,
    v1.dept_name,
    v1.line_id,
    v1.line_name,
    v.xingbie,
    dict.dict_label xingbie_text,
    v1.station_id,
    v1.station_name,
    v.pillar_code,
    v.marker,
    v.risk_source,
    v.measures,
    v.remark,
    v.fill_date,
    v.fill_user,
    v.pillar_area,
    v.pillar_count,
    v.line_type,
    v.rust_position,
    v.pillar_type,
    v.terrain,
    v.iftransform,
    v.specification,
    v.display_num,
    v.count,
    v.distance_horizontal,
    v.distance_vertical,
    v.kind,
    v.severity,
    v.property_rights,
    v.inquirer,
    v.start_marker,
    v.end_marker
    FROM
    v_prevent v
    left join v_station v1 on v.dept_station_id = v1.dept_station_id
    LEFT JOIN sys_dict_data dict ON (v.xingbie = dict.dict_value AND dict.dict_type = 'xingbie')
    <where>
      <if test="deptId!=null and deptId!=''">
        and v1.dept_id=#{deptId}
      </if>
      <if test="lineId!=null and lineId!=''">
        and v1.line_id=#{lineId}
      </if>
      <if test="xingbie!=null and xingbie!=''">
        and (v.xingbie=#{xingbie} or v.xingbie is null or v.xingbie='')
      </if>
      <if test="stationId!=null and stationId!=''">
        and v1.station_id=#{stationId}
      </if>
<!--      <if test="pillarCode!=null and pillarCode!=''">
        and v.pillar_code=#{pillarCode}
      </if>-->
      <if test="marker != null and marker != ''">
        AND (
        (#{marker} + 0.0) - (#{pillarArea} + 0.0) BETWEEN (v.start_marker + 0.0) and (v.end_marker + 0.0)
        or
        (#{marker} + 0.0) + (#{pillarArea} + 0.0) BETWEEN (v.start_marker + 0.0) and (v.end_marker + 0.0)
        or
        (#{marker} + 0.0) BETWEEN (v.start_marker + 0.0) and (v.end_marker + 0.0)
        or
        (
        (v.start_marker + 0.0) &gt;= (#{marker} + 0.0) - (#{pillarArea} + 0.0)
        and
        (v.start_marker + 0.0) &lt;= (#{marker} + 0.0) + (#{pillarArea} + 0.0)
        )
        )
      </if>
    </where>
  </select>
</mapper>