<?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.AdjustRecordDao">
  <sql id="Base_Column_List">
    t.WELLNAME, RECORD_DATE, t.UNITCODE, UNIT, FINISH_DATE, TUBE_TYPE_ID, FAILURE_REASON_ID,
    CREATED_ON, MODIFIED_ON,
    (select flag_name from sys_flag where category_code = 'TUBE_TYPE' and flag_code = t.TUBE_TYPE_ID) TUBE_TYPE_NAME,
    (select flag_name from sys_flag where category_code = 'ADJUST_FAILURE_REASON' and flag_code = t.FAILURE_REASON_ID) FAILURE_REASON_NAME
  </sql>
  <select id="selectList" parameterType="UnitBlockWellPageParam" resultType="AdjustRecord">
    select
    <include refid="Base_Column_List" />
    , su.unitname,
    (select blockname from sys_blockinfo where blockcode = wb.blockcode) blockname,
    (select unitname from sys_unitinfo where unitcode = su.p_unitcode) areaname
    from CON_ADJUST_RECORD t, water_well_basic wb, sys_unitinfo su
    where t.wellname = wb.wellname
    and wb.unitcode = su.unitcode
    <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>
    order by t.wellname, t.RECORD_DATE
  </select>
  <select id="selectByPrimaryKey" parameterType="AdjustRecord" resultType="AdjustRecord">
    select 
    <include refid="Base_Column_List" />
    from CON_ADJUST_RECORD t
    where WELLNAME = #{wellname,jdbcType=VARCHAR}
      and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="AdjustRecord">
    delete from CON_ADJUST_RECORD
    where WELLNAME = #{wellname,jdbcType=VARCHAR}
      and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </delete>
  <insert id="insert" parameterType="AdjustRecord">
    insert into CON_ADJUST_RECORD (WELLNAME, RECORD_DATE, UNITCODE, 
      UNIT, FINISH_DATE, TUBE_TYPE_ID, 
      FAILURE_REASON_ID, CREATED_ON, MODIFIED_ON
      )
    values (#{wellname,jdbcType=VARCHAR}, #{recordDate,jdbcType=TIMESTAMP}, #{unitcode,jdbcType=VARCHAR}, 
      #{unit,jdbcType=VARCHAR}, #{finishDate,jdbcType=TIMESTAMP}, #{tubeTypeId,jdbcType=VARCHAR}, 
      #{failureReasonId,jdbcType=VARCHAR}, #{createdOn,jdbcType=TIMESTAMP}, #{modifiedOn,jdbcType=TIMESTAMP}
      )
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="AdjustRecord">
    update CON_ADJUST_RECORD
    <set>
      <if test="unitcode != null">
        UNITCODE = #{unitcode,jdbcType=VARCHAR},
      </if>
      <if test="unit != null">
        UNIT = #{unit,jdbcType=VARCHAR},
      </if>
      <if test="finishDate != null">
        FINISH_DATE = #{finishDate,jdbcType=TIMESTAMP},
      </if>
      <if test="tubeTypeId != null">
        TUBE_TYPE_ID = #{tubeTypeId,jdbcType=VARCHAR},
      </if>
      <if test="failureReasonId != null">
        FAILURE_REASON_ID = #{failureReasonId,jdbcType=VARCHAR},
      </if>
      <if test="createdOn != null">
        CREATED_ON = #{createdOn,jdbcType=TIMESTAMP},
      </if>
      <if test="modifiedOn != null">
        MODIFIED_ON = #{modifiedOn,jdbcType=TIMESTAMP},
      </if>
    </set>
    where WELLNAME = #{wellname,jdbcType=VARCHAR}
      and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </update>
  <update id="updateByPrimaryKey" parameterType="AdjustRecord">
    update CON_ADJUST_RECORD
    set UNITCODE = #{unitcode,jdbcType=VARCHAR},
      UNIT = #{unit,jdbcType=VARCHAR},
      FINISH_DATE = #{finishDate,jdbcType=TIMESTAMP},
      TUBE_TYPE_ID = #{tubeTypeId,jdbcType=VARCHAR},
      FAILURE_REASON_ID = #{failureReasonId,jdbcType=VARCHAR},
      CREATED_ON = #{createdOn,jdbcType=TIMESTAMP},
      MODIFIED_ON = #{modifiedOn,jdbcType=TIMESTAMP}
    where WELLNAME = #{wellname,jdbcType=VARCHAR}
      and RECORD_DATE = #{recordDate,jdbcType=TIMESTAMP}
  </update>
</mapper>