<?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.AbnormalWellDao">
  <sql id="Base_Column_List">
    t.WELLNAME, RECORD_DATE, t.UNITCODE, OPEN_TIME, LAST_OPEN_TIME, TRUNK_PRESSURE, OIL_PRESSURE,
    PLAN_DAILY_WATER, DAILY_WATER, OIL_PRESSURE_DIFF, WATER_DIFF, WATER_DIFF_RATE, TYPE_ID, 
    REASON_ID, CLOSE_REASON, CREATED_ON, MODIFIED_ON, CASING_PRESSURE,
    (select flag_name from sys_flag where category_code = 'ABNORMAL_REASON' and flag_code = t.REASON_ID) REASON_NAME
  </sql>
  <select id="selectList" parameterType="AbnormalWellParam" resultType="AbnormalWell">
    select
    <include refid="Base_Column_List" />
    , su.unitname, su.fullname,
    (select blockname from sys_blockinfo where blockcode = wb.blockcode) blockname,
    (select unitname from sys_unitinfo where unitcode = su.p_unitcode) areaname
    from CON_ABNORMAL_WELL t, water_well_basic wb, sys_unitinfo su
    where t.wellname = wb.wellname
    and wb.unitcode = su.unitcode
    and RECORD_DATE = #{testdate}
    <if test="unitcode != null and unitcode != ''">
      and su.unitcode in (select unitcode from sys_unitinfo start with unitcode = #{unitcode} connect by prior unitcode = p_unitcode)
    </if>
    <if test="blockcode != null and blockcode != ''">
      and wb.blockcode = #{blockcode}
    </if>
    <if test="wellname != null and wellname != ''">
      and wb.wellname = #{wellname}
    </if>
    <if test="typeId != null and typeId != ''">
      and (t.TYPE_ID = #{typeId} or t.TYPE_ID = 'BOTH')
    </if>
    <if test="reasonId != null and reasonId != ''">
      and t.REASON_ID = #{reasonId}
    </if>
    <if test="oilPressureDiff != null and oilPressureDiff > 0">
      and t.OIL_PRESSURE_DIFF >= #{oilPressureDiff}
    </if>
    <if test="oilPressureDiff != null and oilPressureDiff &lt; 0">
      and t.OIL_PRESSURE_DIFF &lt;= #{oilPressureDiff}
    </if>
    <if test="waterDiff != null and waterDiff > 0">
      and t.WATER_DIFF >= #{waterDiff}
    </if>
    <if test="waterDiff != null and waterDiff &lt; 0">
      and t.WATER_DIFF &lt;= #{waterDiff}
    </if>
    <if test="waterDiffRate != null and waterDiffRate > 0">
      and t.WATER_DIFF_RATE >= #{waterDiffRate}
    </if>
    <if test="waterDiffRate != null and waterDiffRate &lt; 0">
      and t.WATER_DIFF_RATE &lt;= #{waterDiffRate}
    </if>
    <if test="orderColumn != null and orderColumn != ''">
      order by ${orderColumn} ${order} nulls last
    </if>
    <if test="orderColumn == null or orderColumn == ''">
      order by t.wellname
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="AbnormalWell" resultType="AbnormalWell">
    select 
    <include refid="Base_Column_List" />
    from CON_ABNORMAL_WELL t
    where WELLNAME = #{wellname,jdbcType=VARCHAR}
      and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="AbnormalWell">
    delete from CON_ABNORMAL_WELL
    where WELLNAME = #{wellname,jdbcType=VARCHAR}
      and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </delete>
  <delete id="deleteByDate" parameterType="Date">
    delete from CON_ABNORMAL_WELL
    where RECORD_DATE = #{value,jdbcType=TIMESTAMP}
  </delete>
  <insert id="insert" parameterType="AbnormalWell">
    insert into CON_ABNORMAL_WELL (WELLNAME, RECORD_DATE, UNITCODE, 
      OPEN_TIME, LAST_OPEN_TIME, TRUNK_PRESSURE, 
      OIL_PRESSURE, PLAN_DAILY_WATER, DAILY_WATER, 
      OIL_PRESSURE_DIFF, WATER_DIFF, WATER_DIFF_RATE, 
      TYPE_ID, REASON_ID, CLOSE_REASON, 
      CREATED_ON, MODIFIED_ON, CASING_PRESSURE)
    values (#{wellname,jdbcType=VARCHAR}, #{recordDate,jdbcType=TIMESTAMP}, #{unitcode,jdbcType=VARCHAR}, 
      #{openTime,jdbcType=DECIMAL}, #{lastOpenTime,jdbcType=DECIMAL}, #{trunkPressure,jdbcType=DECIMAL}, 
      #{oilPressure,jdbcType=DECIMAL}, #{planDailyWater,jdbcType=DECIMAL}, #{dailyWater,jdbcType=DECIMAL}, 
      #{oilPressureDiff,jdbcType=DECIMAL}, #{waterDiff,jdbcType=DECIMAL}, #{waterDiffRate,jdbcType=DECIMAL}, 
      #{typeId,jdbcType=VARCHAR}, #{reasonId,jdbcType=VARCHAR}, #{closeReason,jdbcType=VARCHAR}, 
      #{createdOn,jdbcType=TIMESTAMP}, #{modifiedOn,jdbcType=TIMESTAMP}, #{casingPressure,jdbcType=DECIMAL})
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="AbnormalWell">
    update CON_ABNORMAL_WELL
    <set>
      <if test="unitcode != null">
        UNITCODE = #{unitcode,jdbcType=VARCHAR},
      </if>
      <if test="openTime != null">
        OPEN_TIME = #{openTime,jdbcType=DECIMAL},
      </if>
      <if test="lastOpenTime != null">
        LAST_OPEN_TIME = #{lastOpenTime,jdbcType=DECIMAL},
      </if>
      <if test="trunkPressure != null">
        TRUNK_PRESSURE = #{trunkPressure,jdbcType=DECIMAL},
      </if>
      <if test="oilPressure != null">
        OIL_PRESSURE = #{oilPressure,jdbcType=DECIMAL},
      </if>
      <if test="planDailyWater != null">
        PLAN_DAILY_WATER = #{planDailyWater,jdbcType=DECIMAL},
      </if>
      <if test="dailyWater != null">
        DAILY_WATER = #{dailyWater,jdbcType=DECIMAL},
      </if>
      <if test="oilPressureDiff != null">
        OIL_PRESSURE_DIFF = #{oilPressureDiff,jdbcType=DECIMAL},
      </if>
      <if test="waterDiff != null">
        WATER_DIFF = #{waterDiff,jdbcType=DECIMAL},
      </if>
      <if test="waterDiffRate != null">
        WATER_DIFF_RATE = #{waterDiffRate,jdbcType=DECIMAL},
      </if>
      <if test="typeId != null">
        TYPE_ID = #{typeId,jdbcType=VARCHAR},
      </if>
      <if test="reasonId != null">
        REASON_ID = #{reasonId,jdbcType=VARCHAR},
      </if>
      <if test="closeReason != null">
        CLOSE_REASON = #{closeReason,jdbcType=VARCHAR},
      </if>
      <if test="createdOn != null">
        CREATED_ON = #{createdOn,jdbcType=TIMESTAMP},
      </if>
      <if test="modifiedOn != null">
        MODIFIED_ON = #{modifiedOn,jdbcType=TIMESTAMP},
      </if>
      <if test="casingPressure != null">
        CASING_PRESSURE = #{casingPressure,jdbcType=DECIMAL},
      </if>
    </set>
    where WELLNAME = #{wellname,jdbcType=VARCHAR}
      and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </update>
  <update id="updateByPrimaryKey" parameterType="AbnormalWell">
    update CON_ABNORMAL_WELL
    set UNITCODE = #{unitcode,jdbcType=VARCHAR},
      OPEN_TIME = #{openTime,jdbcType=DECIMAL},
      LAST_OPEN_TIME = #{lastOpenTime,jdbcType=DECIMAL},
      TRUNK_PRESSURE = #{trunkPressure,jdbcType=DECIMAL},
      OIL_PRESSURE = #{oilPressure,jdbcType=DECIMAL},
      PLAN_DAILY_WATER = #{planDailyWater,jdbcType=DECIMAL},
      DAILY_WATER = #{dailyWater,jdbcType=DECIMAL},
      OIL_PRESSURE_DIFF = #{oilPressureDiff,jdbcType=DECIMAL},
      WATER_DIFF = #{waterDiff,jdbcType=DECIMAL},
      WATER_DIFF_RATE = #{waterDiffRate,jdbcType=DECIMAL},
      TYPE_ID = #{typeId,jdbcType=VARCHAR},
      REASON_ID = #{reasonId,jdbcType=VARCHAR},
      CLOSE_REASON = #{closeReason,jdbcType=VARCHAR},
      CREATED_ON = #{createdOn,jdbcType=TIMESTAMP},
      MODIFIED_ON = #{modifiedOn,jdbcType=TIMESTAMP},
      CASING_PRESSURE = #{casingPressure,jdbcType=DECIMAL}
    where WELLNAME = #{wellname,jdbcType=VARCHAR}
      and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </update>
</mapper>