<?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.LessMeasureSummaryDao">
  <sql id="Base_Column_List">
    imms.UNITCODE, "MONTH", MEASURE_ID, WELL, "SECTION", WATER, SUMMARY_DATE, su.unitname
  </sql>

  <select id="selectUnit" parameterType="UnitMonthParam" resultType="LessMeasureSummary">
    select
    <include refid="Base_Column_List" />
    from INDI_LESS_MEASURE_SUMMARY imms, V_SYS_UNITINFO su
    where "MONTH" = #{month,jdbcType=CHAR}
    and imms.UNITCODE = su.UNITCODE
    and su.unitcode= #{unitcode,jdbcType=VARCHAR}
    order by su.unitorder
  </select>

  <select id="selectChildren" parameterType="UnitMonthParam" resultType="LessMeasureSummary">
    select
    <include refid="Base_Column_List" />
    from INDI_LESS_MEASURE_SUMMARY imms, V_SYS_UNITINFO su
    where "MONTH" = #{month,jdbcType=CHAR}
    and imms.UNITCODE = su.UNITCODE
    and su.p_unitcode= #{unitcode,jdbcType=VARCHAR}
    order by su.unitorder
  </select>

  <select id="selectByPrimaryKey" parameterType="LessMeasureSummary" resultType="LessMeasureSummary">
    select
    <include refid="Base_Column_List" />
    from INDI_LESS_MEASURE_SUMMARY imms, V_SYS_UNITINFO su
    where imms.UNITCODE = #{unitcode,jdbcType=VARCHAR}
    and "MONTH" = #{month,jdbcType=CHAR}
    and MEASURE_ID = #{measureId,jdbcType=VARCHAR}
    and imms.UNITCODE = su.UNITCODE
  </select>

  <delete id="deleteByMonth" parameterType="String">
    delete from INDI_LESS_MEASURE_SUMMARY
    where "MONTH" = #{value,jdbcType=CHAR}
  </delete>

  <delete id="deleteByPrimaryKey" parameterType="LessMeasureSummary">
    delete from INDI_LESS_MEASURE_SUMMARY
    where UNITCODE = #{unitcode,jdbcType=VARCHAR}
      and "MONTH" = #{month,jdbcType=CHAR}
      and MEASURE_ID = #{measureId,jdbcType=VARCHAR}
  </delete>

  <sql id="Insert_Sql">
    insert into INDI_LESS_MEASURE_SUMMARY (UNITCODE, "MONTH", MEASURE_ID,
      WELL, "SECTION", WATER,
      SUMMARY_DATE)
  </sql>

  <insert id="summaryStation" parameterType="String">
    <include refid="Insert_Sql" />
    select unitcode,
    max(month) month,
    nvl(measure_id, 'all') measure_id,
    count(distinct wellname) well,
    count(1) section,
    abs(sum(diff_water)) water,
    sysdate
    from indi_well_section_month
    where month = #{value}
    and EVALUATE = 'LESS'
    and measure_id is not null
    and apply = '1'
    group by unitcode, rollup(measure_id)
  </insert>

  <sql id="Summary_Parent_Sql">
    <include refid="Insert_Sql" />
    select b.p_unitcode, max(a.month), a.measure_id,
    sum(WELL), sum(SECTION), sum(WATER), sysdate
    from indi_less_measure_summary a, sys_unitinfo b
    where a.unitcode = b.unitcode
    and a.month = #{month}
  </sql>

  <insert id="summaryArea" parameterType="String">
    <include refid="Summary_Parent_Sql" />
    and b.unitlevel = 4
    group by b.p_unitcode, a.measure_id
  </insert>

  <insert id="summaryCompany" parameterType="String">
    <include refid="Summary_Parent_Sql" />
    and b.unitlevel = 3
    group by b.p_unitcode, a.measure_id
  </insert>

  <insert id="summaryGroup" parameterType="String">
    <include refid="Summary_Parent_Sql" />
    and b.unitlevel = 2
    group by b.p_unitcode, a.measure_id
  </insert>

  <insert id="insert" parameterType="LessMeasureSummary">
    <include refid="Insert_Sql" />
    values (#{unitcode,jdbcType=VARCHAR}, #{month,jdbcType=CHAR}, #{measureId,jdbcType=VARCHAR},
      #{well,jdbcType=DECIMAL}, #{section,jdbcType=DECIMAL}, #{water,jdbcType=DECIMAL},
      #{summaryDate,jdbcType=TIMESTAMP})
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="LessMeasureSummary">
    update INDI_LESS_MEASURE_SUMMARY
    <set>
      <if test="well != null">
        WELL = #{well,jdbcType=DECIMAL},
      </if>
      <if test="section != null">
        "SECTION" = #{section,jdbcType=DECIMAL},
      </if>
      <if test="water != null">
        WATER = #{water,jdbcType=DECIMAL},
      </if>
      <if test="summaryDate != null">
        SUMMARY_DATE = #{summaryDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where UNITCODE = #{unitcode,jdbcType=VARCHAR}
      and "MONTH" = #{month,jdbcType=CHAR}
      and MEASURE_ID = #{measureId,jdbcType=VARCHAR}
  </update>

  <update id="updateByPrimaryKey" parameterType="LessMeasureSummary">
    update INDI_LESS_MEASURE_SUMMARY
    set WELL = #{well,jdbcType=DECIMAL},
      "SECTION" = #{section,jdbcType=DECIMAL},
      WATER = #{water,jdbcType=DECIMAL},
      SUMMARY_DATE = #{summaryDate,jdbcType=TIMESTAMP}
    where UNITCODE = #{unitcode,jdbcType=VARCHAR}
      and "MONTH" = #{month,jdbcType=CHAR}
      and MEASURE_ID = #{measureId,jdbcType=VARCHAR}
  </update>
</mapper>
