<?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.xhpc.activity.mapper.XhpcStationDeviceMapper">
  <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcStationDeviceDomain">
    <id column="device_id" jdbcType="BIGINT" property="deviceId" />
    <result column="device_name" jdbcType="VARCHAR" property="deviceName" />
    <result column="device_type" jdbcType="VARCHAR" property="deviceType" />
    <result column="current_type" jdbcType="VARCHAR" property="currentType" />
    <result column="station_id" jdbcType="BIGINT" property="stationId" />
    <result column="parent_device_id" jdbcType="BIGINT" property="parentDeviceId" />
    <result column="brand_model" jdbcType="VARCHAR" property="brandModel" />
    <result column="serial_number" jdbcType="VARCHAR" property="serialNumber" />
    <result column="status" jdbcType="SMALLINT" property="status" />
    <result column="del_flag" jdbcType="SMALLINT" property="delFlag" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
  </resultMap>
  <sql id="Base_Column_List">
    device_id, device_name, device_type, current_type, station_id, brand_model, parent_device_id, serial_number, sorted,
    `status`, del_flag, create_time, create_by, update_time, update_by
  </sql>

  <select id="selectStationGunDeviceBySerialNumber" resultType="com.xhpc.activity.domain.XhpcStationDeviceDomain">
    select
    <include refid="Base_Column_List" />
    from xhpc_station_device
    where serial_number = #{serialNumber}
  </select>

  <select id="selectStationGunDeviceListByParams" resultType="map">
    SELECT
    s.`name` as 'stationName',
    d1.device_id as 'pileId',
    d1.device_name as 'pileName',
    d1.device_type as 'pileDeviceType',
    d1.serial_number as 'pileSerialNumber',
    d1.brand_model as 'pileBrandModel',
    d1.current_type as 'pileCurrentType',
    d1.sorted as 'pileSorted',
    d2.device_id as 'gunId',
    d2.device_name as 'gunName',
    d2.device_type as 'gunDeviceType',
    d2.serial_number as 'gunSerialNumber',
    d2.brand_model as 'gunBrandModel',
    d2.current_type as 'gunCurrentType',
    d2.sorted as 'gunSorted',
    t.terminal_id as 'terminalId',
    t.status as 'status',
    t.work_status as 'workStatus',
    t.charging_pile_id as 'chargingPileId'
    from xhpc_station_device d1
    LEFT JOIN xhpc_work_station s on s.work_station_id=d1.station_id
    LEFT JOIN xhpc_station_device d2 on d2.parent_device_id=d1.device_id
    LEFT JOIN xhpc_terminal t on t.serial_number = d2.serial_number
    WHERE s.del_flag=0 and d1.del_flag=0 and d2.del_flag=0 and d1.device_type='PILE' and t.del_flag=0
    <if test="params.stationName!=null and params.stationName != ''">
      and s.name like concat('%', #{params.stationName}, '%')
    </if>
    <if test="params.stationId!=null and params.stationId != ''">
      and s.work_station_id = #{params.stationId}
    </if>
    <if test="params.terminalName!=null and params.terminalName!=''">
      and d2.device_name like concat('%', #{params.terminalName},'%')
    </if>
    <if test="params.tenantId!=null and params.tenantId!=''">
      and s.tenant_id=#{params.tenantId}
    </if>
    GROUP BY d2.device_id
    order by s.work_station_id, d1.device_type,d1.device_id, d1.sorted, d2.device_type,d2.device_id, d2.sorted
  </select>


  <select id="selectByDeviceId" resultType="java.util.Map">
    select
        d.device_id as 'deviceId',
       d.device_name as 'deviceName',
       d.device_type as 'deviceType',
       d.current_type as 'currentType',
       d.station_id as 'stationId',
       d.brand_model as 'brandModel',
       d.parent_device_id as 'parengDeviceId',
       d.serial_number as 'serialNumber',
       d.sorted as 'sorted',
       d.`status` as 'status',
       d.del_flag as 'delFlag',
       d.create_time as 'createTime',
       d.create_by as 'createBy',
       d.update_time as 'updateTime',
       d.update_by as 'updateBy',
       s.name as 'stationName'
    from xhpc_station_device d
    left join xhpc_work_station s on d.station_id=s.work_station_id
    where device_id = #{deviceId,jdbcType=INTEGER}
  </select>


  <select id="selectStationGunDeviceTreeByParams" resultType="map">
    SELECT
    concat('D_', d1.device_id) as 'id',
    d1.device_name as 'name',
    d1.device_type as 'type',
    d1.serial_number as 'serialNumber',
    d1.brand_model as 'brandModel',
    d1.current_type as 'currentType',
    d1.sorted as 'sorted'
    from xhpc_station_device d1
    LEFT JOIN xhpc_work_station s on s.work_station_id=d1.station_id
    WHERE s.del_flag=0 and d1.del_flag=0
    <if test="params.deviceName != null and params.deviceName != ''">
        and d1.device_name like concat('%', #{params.deviceName}, '%')
    </if>
    <if test="params.stationName!=null and params.stationName != ''">
      and s.name like concat('%', #{params.stationName}, '%')
    </if>
    <if test="params.stationId!=null and params.stationId != ''">
      and d1.station_id =#{params.stationId}
    </if>
    <if test="params.terminalName!=null and params.terminalName!=''">
      and d1.device_name like concat('%', #{params.terminalName},'%')
    </if>
    <if test="params.tenantId!=null and params.tenantId!=''">
      and s.tenant_id=#{params.tenantId}
    </if>
    <choose>
      <when test="params.parentDeviceId!=null and params.parentDeviceId!=''">
        and d1.parent_device_id=#{params.parentDeviceId}
      </when>
        <otherwise>
          and d1.parent_device_id is null
        </otherwise>
    </choose>
    order by d1.station_id, d1.device_type, d1.sorted
  </select>


  <select id="selectListByParent" resultType="map">
    SELECT
    s.`name` as 'stationName',
    d1.device_id as 'deviceId',
    d1.device_name as 'deviceName',
    d1.device_type as 'deviceType',
    d1.serial_number as 'deviceSerialNumber',
    d1.brand_model as 'deviceBrandModel',
    d1.current_type as 'deviceCurrentType',
    d1.sorted as 'deviceSorted'
    from xhpc_station_device d1
    LEFT JOIN xhpc_work_station s on s.work_station_id=d1.station_id
    WHERE s.del_flag=0 and d1.del_flag=0
    <if test="params.deviceName != null and params.deviceName != ''">
      and d1.device_name like concat('%', #{params.deviceName}, '%')
    </if>
    <if test="params.stationName!=null and params.stationName != ''">
      and s.name like concat('%', #{params.stationName}, '%')
    </if>
    <if test="params.terminalName!=null and params.terminalName!=''">
      and d1.device_name like concat('%', #{params.terminalName},'%')
    </if>
    <if test="params.tenantId!=null and params.tenantId!=''">
      and s.tenant_id=#{params.tenantId}
    </if>
    <if test="params.stationId != null and params.stationId !=''">
      and s.work_station_id = #{params.stationId}
    </if>
    <if test="params.deviceType != null and params.deviceType !=''">
      and d1.device_type = #{params.deviceType}
    </if>
    <if test="params.parentId != null and params.parentId !=''">
      and d1.parent_device_id = #{params.parentId}
    </if>
    order by d1.device_type, d1.sorted
  </select>


  <select id="selectStationGunDeviceTreeByParent" resultType="map">
    SELECT
      concat('D_', d1.device_id) as 'id',
    d1.device_name as 'name',
    d1.device_type as 'type',
    d1.serial_number as 'serialNumber',
    d1.brand_model as 'brandModel',
    d1.current_type as 'currentType',
    d1.sorted as 'sorted'
    from xhpc_station_device d1
    WHERE d1.del_flag=0
        and concat('D_', d1.parent_device_id)=#{parentDeviceId}
    order by d1.device_type, d1.sorted
  </select>


  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from xhpc_station_device
    where device_id = #{deviceId,jdbcType=INTEGER}
  </select>

    <update id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    update xhpc_station_device set del_flag = 2
    where device_id = #{deviceId,jdbcType=INTEGER}
  </update>
  <insert id="insert" keyColumn="device_id" keyProperty="deviceId" parameterType="com.xhpc.activity.domain.XhpcStationDeviceDomain" useGeneratedKeys="true">
    insert into xhpc_station_device (device_name, device_type, current_type, station_id,
      parent_device_id, serial_number, brand_model,sorted, `status`,
      del_flag, create_time, create_by, 
      update_time, update_by)
    values (#{deviceName,jdbcType=VARCHAR}, #{deviceType,jdbcType=VARCHAR}, #{currentType,jdbcType=VARCHAR}, #{stationId,jdbcType=BIGINT},
      #{parentDeviceId,jdbcType=BIGINT}, #{serialNumber,jdbcType=VARCHAR}, #{brandModel,jdbcType=VARCHAR},#{sorted, jdbcType=SMALLINT}, #{status,jdbcType=SMALLINT},
      0, SYSDATE(), #{createBy,jdbcType=VARCHAR}, SYSDATE(), #{updateBy,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" keyColumn="device_id" keyProperty="deviceId" parameterType="com.xhpc.activity.domain.XhpcStationDeviceDomain" useGeneratedKeys="true">
    insert into xhpc_station_device
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="deviceName != null">
        device_name,
      </if>
      <if test="deviceType != null">
        device_type,
      </if>
      <if test="currentType != null">
        current_type,
      </if>
      <if test="stationId != null">
        station_id,
      </if>
      <if test="parentDeviceId != null">
        parent_device_id,
      </if>
      <if test="serialNumber != null">
        serial_number,
      </if>
        <if test="brandModel != null">
          brand_model,
        </if>
      <if test="sorted != null">
        `sorted`,
      </if>
      <if test="status != null">
        `status`,
      </if>
        del_flag,
        create_time,
      <if test="createBy != null">
        create_by,
      </if>
        update_time,
      <if test="updateBy != null">
        update_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="deviceName != null">
        #{deviceName,jdbcType=VARCHAR},
      </if>
      <if test="deviceType != null">
        #{deviceType,jdbcType=VARCHAR},
      </if>
      <if test="currentType != null">
        #{currentType,jdbcType=VARCHAR},
      </if>
      <if test="stationId != null">
        #{stationId,jdbcType=BIGINT},
      </if>
      <if test="parentDeviceId != null">
        #{parentDeviceId,jdbcType=BIGINT},
      </if>
      <if test="serialNumber != null">
        #{serialNumber,jdbcType=VARCHAR},
      </if>
        <if test="brandModel != null">
          #{brandModel,jdbcType=VARCHAR},
        </if>
      <if test="sorted != null">
        #{sorted,jdbcType=SMALLINT},
      </if>
      <if test="status != null">
        #{status,jdbcType=SMALLINT},
      </if>
        0,
      sysdate(),
      <if test="createBy != null">
        #{createBy,jdbcType=VARCHAR},
      </if>
      sysdate(),
      <if test="updateBy != null">
        #{updateBy,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.xhpc.activity.domain.XhpcStationDeviceDomain">
    update xhpc_station_device
    <set>
      <if test="deviceName != null">
        device_name = #{deviceName,jdbcType=VARCHAR},
      </if>
      <if test="deviceType != null">
        device_type = #{deviceType,jdbcType=VARCHAR},
      </if>
      <if test="currentType != null">
        current_type = #{currentType,jdbcType=VARCHAR},
      </if>
      <if test="stationId != null">
        station_id = #{stationId,jdbcType=BIGINT},
      </if>
      <if test="parentDeviceId != null">
        parent_device_id = #{parentDeviceId,jdbcType=BIGINT},
      </if>
      <if test="serialNumber != null">
        serial_number = #{serialNumber,jdbcType=VARCHAR},
      </if>
      <if test="brandModel != null">
        brand_model = #{brandModel,jdbcType=VARCHAR},
      </if>
      <if test="sorted != null">
        `sorted` = #{sorted,jdbcType=SMALLINT},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=SMALLINT},
      </if>
        update_time = sysdate(),
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
    </set>
    where device_id = #{deviceId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcStationDeviceDomain">
    update xhpc_station_device
    set device_name = #{deviceName,jdbcType=VARCHAR},
      device_type = #{deviceType,jdbcType=VARCHAR},
        current_type = #{currentType,jdbcType=VARCHAR},
      station_id = #{stationId,jdbcType=BIGINT},
      parent_device_id = #{parentDeviceId,jdbcType=BIGINT},
      serial_number = #{serialNumber,jdbcType=VARCHAR},
        brand_model = #{brandModel,jdbcType=VARCHAR},
        sorted = #{sorted,jdbcType=SMALLINT},
      `status` = #{status,jdbcType=SMALLINT},
      update_time = sysdate(),
      update_by = #{updateBy,jdbcType=VARCHAR}
    where device_id = #{deviceId,jdbcType=INTEGER}
  </update>


  <select id="selectBySerialNumber" resultType="com.xhpc.activity.domain.XhpcStationDeviceDomain">
    select
    <include refid="Base_Column_List" />
    from xhpc_station_device
    where serial_number = #{serialNumber}
  </select>
</mapper>