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

  <resultMap id="BaseResultMap" type="com.railway.business.baseinfo.domain.BusSdtz">
    <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="sdmc" property="sdmc"/>
    <result column="lcgh" property="lcgh"/>
    <result column="start_marker" property="startMarker"/>
    <result column="end_marker" property="endMarker"/>
    <result column="center_marker" property="centerMarker"/>
    <result column="sdcd" property="sdcd"/>
    <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.id,
    v.dept_id,
    v.dept_name,
    v.line_id,
    v.line_name,
    v.station_name as station_name,
    v.station_id,
    t.dept_station_id,
    t.sdmc,
    t.lcgh,
    t.start_marker,
    t.end_marker,
    t.center_marker,
    t.sdcd,
    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.BusSdtz">
    <selectKey keyProperty="id" order="AFTER" resultType="Long">
      select @@IDENTITY as id
    </selectKey>
    INSERT INTO base_sdtz
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test='null != deptStationId'>
        dept_station_id,
      </if>
      <if test='null != sdmc'>
        sdmc,
      </if>
      <if test='null != lcgh'>
        lcgh,
      </if>
      <if test='null != startMarker'>
        start_marker,
      </if>
      <if test='null != endMarker'>
        end_marker,
      </if>
      <if test='null != centerMarker'>
        center_marker,
      </if>
      <if test='null != sdcd'>
        sdcd,
      </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 != sdmc'>
        #{sdmc},
      </if>
      <if test='null != lcgh'>
        #{lcgh},
      </if>
      <if test='null != startMarker'>
        #{startMarker},
      </if>
      <if test='null != endMarker'>
        #{endMarker},
      </if>
      <if test='null != centerMarker'>
        #{centerMarker},
      </if>
      <if test='null != sdcd'>
        #{sdcd},
      </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_sdtz
    set del_flag='1'
    WHERE id = #{id}
  </delete>

  <update id="update" parameterType="com.railway.business.baseinfo.domain.BusSdtz">
    UPDATE base_sdtz
    <set>
      <if test='null != deptStationId'>dept_station_id = #{deptStationId},</if>
      <if test='null != sdmc'>sdmc = #{sdmc},</if>
      <if test='null != lcgh'>lcgh = #{lcgh},</if>
      <if test='null != startMarker'>start_marker = #{startMarker},</if>
      <if test='null != endMarker'>end_marker = #{endMarker},</if>
      <if test='null != centerMarker'>center_marker = #{centerMarker},</if>
      <if test='null != sdcd'>sdcd = #{sdcd},</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
    <include refid="Base_Column_List"/>
    FROM base_sdtz t
    LEFT JOIN v_station v on t.dept_station_id = v.dept_station_id
    WHERE t.del_flag='0' and t.id = #{id}

  </select>

  <select id="getList" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM base_sdtz t
    LEFT JOIN v_station v on t.dept_station_id = v.dept_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 v.line_id=#{lineId}
      </if>
      <if test="stationId!=null and stationId!=''">
        and v.station_id=#{stationId}
      </if>
      <if test="sdmc!=null and sdmc!=''">
        and t.sdmc=#{sdmc}
      </if>
      <if test="lcgh!=null and lcgh!=''">
        and t.lcgh=#{lcgh}
      </if>
      <if test="startMarker!=null and startMarker!=''">
        and t.start_marker=#{startMarker}
      </if>
      <if test="endMarker!=null and endMarker!=''">
        and t.end_marker=#{endMarker}
      </if>
      <if test="centerMarker!=null and centerMarker!=''">
        and t.center_marker=#{centerMarker}
      </if>
      <if test="sdcd!=null and sdcd!=''">
        and t.sdcd=#{sdcd}
      </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="smartQuery" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    from base_sdtz t
    LEFT JOIN v_station v on t.dept_station_id = v.dept_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 v.line_id=#{lineId}
      </if>
      <if test="stationId!=null and stationId!=''">
        and v.station_id=#{stationId}
      </if>

      <if test="marker!=null and marker!=''">
        AND (
        (#{marker} + 0.0) - (#{pillarArea} + 0.0) BETWEEN (t.start_marker + 0.0) and (t.end_marker + 0.0)
        or
        (#{marker} + 0.0) + (#{pillarArea} + 0.0) BETWEEN (t.start_marker + 0.0) and (t.end_marker + 0.0)
        or
        (#{marker} + 0.0) BETWEEN (t.start_marker + 0.0) and (t.end_marker + 0.0)
        or
          (
            (t.start_marker + 0.0) &gt;= (#{marker} + 0.0) - (#{pillarArea} + 0.0)
            and
            (t.start_marker + 0.0) &lt;= (#{marker} + 0.0) + (#{pillarArea} + 0.0)
          )
        )
      </if>

    </where>
  </select>

</mapper>