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

  <resultMap id="BaseResultMap" type="com.railway.business.baseinfo.domain.BaseSubstation">
    <result column="substation_id" property="substationId"/>
    <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="work_area" property="workArea"/>
    <result column="substation_name" property="substationName"/>
    <result column="order_num" property="orderNum"/>
    <result column="marker" property="marker"/>
    <result column="longitude" property="longitude"/>
    <result column="latitude" property="latitude"/>
    <result column="substation_group" property="substationGroup"/>
    <result column="substation_duty" property="substationDuty"/>
    <result column="substation_state" property="substationState"/>
    <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.substation_id,
    t.dept_id,
    v.dept_name,
    t.line_id,
    v.line_name,
    t.work_area,
    t.substation_name,
    t.order_num,
    t.marker,
    t.longitude,
    t.latitude,
    t.substation_group,
    t.substation_duty,
    t.substation_state,
    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.BaseSubstation">
    INSERT INTO base_substation
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test='null != deptId'>
        dept_id,
      </if>
      <if test='null != lineId'>
        line_id,
      </if>
      <if test='null != workArea'>
        work_area,
      </if>
      <if test='null != substationName'>
        substation_name,
      </if>
      <if test='null != orderNum'>
        order_num,
      </if>
      <if test='null != marker'>
        marker,
      </if>
      <if test='null != longitude'>
        longitude,
      </if>
      <if test='null != latitude'>
        latitude,
      </if>
      <if test='null != substationGroup'>
        substation_group,
      </if>
      <if test='null != substationDuty'>
        substation_duty,
      </if>
      <if test='null != substationState'>
        substation_state,
      </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 != deptId'>
        #{deptId},
      </if>
      <if test='null != lineId'>
        #{lineId},
      </if>
      <if test='null != workArea'>
        #{workArea},
      </if>
      <if test='null != substationName'>
        #{substationName},
      </if>
      <if test='null != orderNum'>
        #{orderNum},
      </if>
      <if test='null != marker'>
        #{marker},
      </if>
      <if test='null != longitude'>
        #{longitude},
      </if>
      <if test='null != latitude'>
        #{latitude},
      </if>
      <if test='null != substationGroup'>
        #{substationGroup},
      </if>
      <if test='null != substationDuty'>
        #{substationDuty},
      </if>
      <if test='null != substationState'>
        #{substationState},
      </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_substation
    set del_flag='1'
    WHERE substation_id = #{substationId}
  </delete>

  <update id="update" parameterType="com.railway.business.baseinfo.domain.BaseSubstation">
    UPDATE base_substation
    <set>
      <if test='null != deptId'>dept_id = #{deptId},</if>
      <if test='null != lineId'>line_id = #{lineId},</if>
      <if test='null != workArea'>work_area = #{workArea},</if>
      <if test='null != substationName'>substation_name = #{substationName},</if>
      <if test='null != orderNum'>order_num = #{orderNum},</if>
      <if test='null != marker'>marker = #{marker},</if>
      <if test='null != longitude'>longitude = #{longitude},</if>
      <if test='null != latitude'>latitude = #{latitude},</if>
      <if test='null != substationGroup'>substation_group = #{substationGroup},</if>
      <if test='null != substationDuty'>substation_duty = #{substationDuty},</if>
      <if test='null != substationState'>substation_state = #{substationState},</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 substation_id = #{substationId}
  </update>


  <select id="getInfo" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM base_substation t
    LEFT JOIN v_substation v on t.substation_id = v.substation_id
    WHERE t.del_flag='0' and t.substation_id = #{substationId}
  </select>

  <select id="getList" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM base_substation t
    LEFT JOIN v_substation v on t.substation_id = v.substation_id
    <where>
      t.del_flag='0'
      <if test="deptId!=null and deptId!=''">
        and t.dept_id=#{deptId}
      </if>
      <if test="deptName!=null and deptName!=''">
        and v.dept_name like concat('%', #{deptName}, '%')
      </if>
      <if test="lineId!=null and lineId!=''">
        and t.line_id=#{lineId}
      </if>
      <if test="lineName!=null and lineName!=''">
        and v.line_name like concat('%', #{lineName}, '%')
      </if>
      <if test="workArea!=null and workArea!=''">
        and t.work_area=#{workArea}
      </if>
      <if test="substationName!=null and substationName!=''">
        and t.substation_name like concat('%', #{substationName}, '%')
      </if>
      <if test="orderNum!=null and orderNum!=''">
        and t.order_num=#{orderNum}
      </if>
      <if test="marker!=null and marker!=''">
        and t.marker=#{marker}
      </if>
      <if test="longitude!=null and longitude!=''">
        and t.longitude=#{longitude}
      </if>
      <if test="latitude!=null and latitude!=''">
        and t.latitude=#{latitude}
      </if>
      <if test="substationGroup!=null and substationGroup!=''">
        and t.substation_group=#{substationGroup}
      </if>
      <if test="substationDuty!=null and substationDuty!=''">
        and t.substation_duty=#{substationDuty}
      </if>
      <if test="substationState!=null and substationState!=''">
        and t.substation_state=#{substationState}
      </if>
      <if test="delFlag!=null and delFlag!=''">
        and t.del_flag=#{delFlag}
      </if>
    </where>
  </select>

  <select id="getDeptSubList" resultType="com.railway.business.baseinfo.domain.vo.BaseDeptSubstationVo">
    SELECT
      bdsu.substation_id AS substationId,
      bsu.substation_name AS substationName,
      bdsu.line_id AS lineId,
      bl.line_name AS lineName
    FROM
      base_dept_substation bdsu
        LEFT JOIN base_substation bsu ON bdsu.substation_id = bsu.substation_id
        LEFT JOIN base_line bl on bdsu.line_id = bl.line_id
    <where>
      <if test="lineId!=null and lineId!=''">
        bdsu.line_id = #{lineId}
      </if>
    </where>
    ORDER BY bdsu.line_id
  </select>

</mapper>