<?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="cn.com.victorysoft.business.indicator.dao.PumpDeviceDao">
  <sql id="Base_Column_List">
    ipd.STATION_NAME,ipd.PUMP_ID,ipd.RECORD_DATE,ipd.UNITCODE,ipd.PUMP_TYPE,ipd.RUN_TIME,ipd.RUN_STATUS,
    ipd.PRESSURE_IN,ipd.PRESSURE_OUT,ipd.PRESSURE_BALANCE,ipd.VOLUME_HOUR,
    ipd.VOLUME_DAY,ipd.EFFECTIVE_POWER,ipd.VOLTAGE,ipd.ELECTRIC_CURRENT,ipd.POWER_FACTOR,ipd.POWER_IN,
    ipd.POWER_OUT,ipd.EFFICIENCY,ipd.PUMP_EFFICIENCY,ipd.PRESSURE_SPLIT,
    ipd.POWER_USED,ipd.POWER_USED10,ipd.CREATE_ON,sp.name pump_name
  </sql>

  <select id="selectList" parameterType="PumpDeviceParam" resultType="PumpDevice">
    select
    <include refid="Base_Column_List"/>
    from INDI_PUMP_DEVICE ipd
    left join SYS_PUMP sp on ipd.PUMP_ID = sp.ID
    where ipd.unitcode in (select unitcode
    from SYS_UNITINFO
    start with UNITCODE = #{unitcode}
    connect by prior UNITCODE = P_UNITCODE)
    and RECORD_DATE >= #{recordDate,jdbcType=TIMESTAMP}
    and ipd.RECORD_DATE &lt; #{recordDate,jdbcType=TIMESTAMP} + 1
    <if test="pumpId != null and pumpId != ''">
      and ipd.PUMP_ID = #{pumpId}
    </if>
    <if test="stationName != null and stationName != ''">
      and ipd.STATION_NAME= #{stationName}
    </if>
    order by ipd.RUN_STATUS desc nulls last, sp.ID
  </select>

  <insert id="insert" parameterType="PumpDevice">
    insert into INDI_PUMP_DEVICE
    (STATION_NAME,PUMP_ID,RECORD_DATE,UNITCODE,PUMP_TYPE,RUN_TIME,RUN_STATUS,PRESSURE_IN,PRESSURE_OUT,PRESSURE_BALANCE,VOLUME_HOUR,VOLUME_DAY,EFFECTIVE_POWER,
    VOLTAGE,ELECTRIC_CURRENT,POWER_FACTOR,POWER_IN,POWER_OUT,EFFICIENCY,PUMP_EFFICIENCY,PRESSURE_SPLIT,POWER_USED,POWER_USED10,CREATE_ON)
    values
    (#{stationName,jdbcType=VARCHAR},#{pumpId,jdbcType=VARCHAR},#{recordDate,jdbcType=TIMESTAMP},
    #{unitcode,jdbcType=VARCHAR},#{pumpType,jdbcType=VARCHAR},#{runTime,jdbcType=DOUBLE},#{runStatus,jdbcType=INTEGER},
    #{pressureIn,jdbcType=DOUBLE},#{pressureOut,jdbcType=DOUBLE},#{pressureBalance,jdbcType=DOUBLE},
    #{volumeHour,jdbcType=DOUBLE},#{volumeDay,jdbcType=DOUBLE},#{effectivePower,jdbcType=DOUBLE},
    #{voltage,jdbcType=DOUBLE},#{electricCurrent,jdbcType=DOUBLE},#{powerFactor,jdbcType=DOUBLE},
    #{powerIn,jdbcType=DOUBLE},#{powerOut,jdbcType=DOUBLE},#{efficiency,jdbcType=DOUBLE},
    #{pumpEfficiency,jdbcType=DOUBLE},#{pressureSplit,jdbcType=DOUBLE},#{powerUsed,jdbcType=DOUBLE},
    #{powerUsed10,jdbcType=DOUBLE},#{createOn,jdbcType=TIMESTAMP})
  </insert>

  <select id="selectPumpDevice" parameterType="PumpDevice" resultType="PumpDevice">
    select STATION_NAME,PUMP_ID,RECORD_DATE,UNITCODE,PUMP_TYPE,RUN_TIME,RUN_STATUS,PRESSURE_IN,PRESSURE_OUT,PRESSURE_BALANCE,VOLUME_HOUR,VOLUME_DAY,EFFECTIVE_POWER,
    VOLTAGE,ELECTRIC_CURRENT,POWER_FACTOR,POWER_IN,POWER_OUT,EFFICIENCY,PUMP_EFFICIENCY,PRESSURE_SPLIT,POWER_USED,POWER_USED10,CREATE_ON
    from INDI_PUMP_DEVICE
    where PUMP_ID = #{pumpId,jdbcType=VARCHAR}
    and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </select>

  <select id="selectWaterStations" parameterType="WaterStationParam" resultType="WaterStation">
    select
    distinct station_name NAME, AREA_CODE
    from sys_pump ss
    where AREA_CODE in (select unitcode
                        from SYS_UNITINFO
                               start with UNITCODE = #{areaCode}
    connect by prior UNITCODE = P_UNITCODE)
    order by AREA_CODE, station_name
  </select>

  <select id="selectPump" parameterType="PumpParam" resultType="Pump">
    select
    ID,NAME,ORDER_NUM,AREA_CODE,STATION_NAME,PUMP_TYPE, EFFICIENCY
    from sys_pump ss
    <if test="stationName != null and stationName != ''">
        where ss.STATION_NAME = #{stationName}
    </if>
    order by ORDER_NUM
  </select>

  <update id="updateData" parameterType="PumpDevice">
    update INDI_PUMP_DEVICE
    <set>
      <if test="runTime != null">
        RUN_TIME = #{runTime,jdbcType=DOUBLE},
      </if>
      <if test="runStatus != null">
        RUN_STATUS = #{runStatus,jdbcType=INTEGER},
      </if>
      <if test="pressureIn != null">
        PRESSURE_IN = #{pressureIn,jdbcType=DOUBLE},
      </if>
      <if test="pressureOut != null">
        PRESSURE_OUT = #{pressureOut,jdbcType=DOUBLE},
      </if>
      <if test="pressureBalance != null">
        PRESSURE_BALANCE = #{pressureBalance,jdbcType=DOUBLE},
      </if>
      <if test="volumeHour != null">
        VOLUME_HOUR = #{volumeHour,jdbcType=DOUBLE},
      </if>
      <if test="volumeDay != null">
        VOLUME_DAY = #{volumeDay,jdbcType=DOUBLE},
      </if>
      <if test="effectivePower != null">
        EFFECTIVE_POWER = #{effectivePower,jdbcType=DOUBLE},
      </if>
      <if test="voltage != null">
        VOLTAGE = #{voltage,jdbcType=DOUBLE},
      </if>
      <if test="electricCurrent != null">
        ELECTRIC_CURRENT = #{electricCurrent,jdbcType=DOUBLE},
      </if>
      <if test="powerFactor != null">
        POWER_FACTOR = #{powerFactor,jdbcType=DOUBLE},
      </if>
      <if test="powerIn != null">
        POWER_IN = #{powerIn,jdbcType=DOUBLE},
      </if>
      <if test="powerOut != null">
        POWER_OUT = #{powerOut,jdbcType=DOUBLE},
      </if>
      <if test="efficiency != null">
        EFFICIENCY = #{efficiency,jdbcType=DOUBLE},
      </if>
      <if test="pumpEfficiency != null">
        PUMP_EFFICIENCY = #{pumpEfficiency,jdbcType=DOUBLE},
      </if>
      <if test="pressureSplit != null">
        PRESSURE_SPLIT = #{pressureSplit,jdbcType=DOUBLE},
      </if>
      <if test="powerUsed != null">
        POWER_USED = #{powerUsed,jdbcType=DOUBLE},
      </if>
      <if test="powerUsed10 != null">
        POWER_USED10 = #{powerUsed10,jdbcType=DOUBLE},
      </if>
    </set>
    where PUMP_ID = #{pumpId,jdbcType=VARCHAR}
    and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </update>
</mapper>
