<?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.steerinfo.ems.tcm0318.mapper.TCm0318Mapper">
  <resultMap id="BaseResultMap" type="com.steerinfo.ems.tcm0318.model.TCm0318">
    <id column="RECORDID" jdbcType="VARCHAR" property="recordid" />
    <result column="ANALYST" jdbcType="VARCHAR" property="analyst" />
    <result column="DATADATE" jdbcType="VARCHAR" property="datadate" />
    <result column="DATASTATE" jdbcType="VARCHAR" property="datastate" />
    <result column="DETECTIONDEPARTMENT" jdbcType="VARCHAR" property="detectiondepartment" />
    <result column="MONITORDATE" jdbcType="VARCHAR" property="monitordate" />
    <result column="MONITORINGVALUE" jdbcType="DECIMAL" property="monitoringvalue" />
    <result column="NOTE" jdbcType="VARCHAR" property="note" />
    <result column="PLANID" jdbcType="VARCHAR" property="planid" />
    <result column="POWERMONITORNAME" jdbcType="VARCHAR" property="powermonitorname" />
    <result column="SITEM" jdbcType="VARCHAR" property="sitem" />
    <result column="SYSTEMNAME" jdbcType="VARCHAR" property="systemname" />
    <result column="UNIT" jdbcType="VARCHAR" property="unit" />
    <result column="BATCHNO" jdbcType="VARCHAR" property="batchno" />
    <result column="TIMEGRANID" jdbcType="VARCHAR" property="timegranid" />
    <result column="ITEMID" jdbcType="VARCHAR" property="itemid" />
    <result column="INSERT_TIME" jdbcType="VARCHAR" property="inserttime" />
    <result column="QTYPE" jdbcType="VARCHAR" property="qtype" />
    <result column="BANBIE" jdbcType="VARCHAR" property="banbie" />
    <result column="BANCI" jdbcType="VARCHAR" property="banci" />
    <result column="LSH" jdbcType="VARCHAR" property="lsh" />
    <result column="PASSRATE" jdbcType="VARCHAR" property="passrate" />
    <result column="PASSNUM" jdbcType="VARCHAR" property="passnum" />
    <result column="TOTALNUM" jdbcType="VARCHAR" property="totalnum" />
    <result column="DATASOURCE" jdbcType="VARCHAR" property="datasource" />
  </resultMap>

  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select RECORDID, ANALYST, DATADATE, DATASTATE, DETECTIONDEPARTMENT, MONITORDATE,
    MONITORINGVALUE, NOTE, PLANID, POWERMONITORNAME, SITEM, SYSTEMNAME, UNIT, BATCHNO,
    TIMEGRANID, INSERT_TIME, DATASOURCE
    from T_CM_0318
    where RECORDID = #{recordid,jdbcType=VARCHAR}
	ORDER BY MONITORDATE, DATADATE
  </select>

  <!-- 水质监测日月数据录入界面查询 -->
  <select id="selectByParameters" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    select RECORDID, ANALYST, DATADATE, DATASTATE, DETECTIONDEPARTMENT, MONITORDATE,
    MONITORINGVALUE, NOTE, PLANID, POWERMONITORNAME, SITEM, SYSTEMNAME, UNIT, BATCHNO,
    TIMEGRANID, INSERT_TIME, DATASOURCE
    from T_CM_0318
    where DATASOURCE = 'MANUAL'
        <if test="detectiondepartment != null and detectiondepartment != ''">
            and DETECTIONDEPARTMENT = #{detectiondepartment}
        </if>
        <if test="planid != null and planid != ''">
            and PLANID = #{planid}
        </if>
        <if test="powermonitorname != null and powermonitorname != ''">
            and POWERMONITORNAME = #{powermonitorname}
        </if>
        <if test="sitem != null and sitem != ''">
            and SITEM = #{sitem}
        </if>
        <if test="systemname != null and systemname != ''">
            and SYSTEMNAME = #{systemname}
        </if>
        <if test="timegranid != null and timegranid != ''">
            and TIMEGRANID = #{timegranid, jdbcType=VARCHAR}
        </if>
        <if test="monitordate != null and monitordate != ''">
            and MONITORDATE = #{monitordate, jdbcType=VARCHAR}
        </if>
    ORDER BY MONITORDATE, DATADATE
  </select>

<!-- 水质监测数据查询界面查询 -->
  <select id="dataQuery" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    select RECORDID, ANALYST, DATADATE, DATASTATE, DETECTIONDEPARTMENT, MONITORDATE,
    MONITORINGVALUE, NOTE, PLANID, POWERMONITORNAME, SITEM, SYSTEMNAME, UNIT, BATCHNO,
    TIMEGRANID, INSERT_TIME
    from T_CM_0318
    <where>
      <if test="analyst != null and analyst != ''">
        and ANALYST like '%${analyst, jdbcType=VARCHAR}%'
      </if>
      <if test="detectiondepartment != null and detectiondepartment != ''">
        and DETECTIONDEPARTMENT = #{detectiondepartment, jdbcType=VARCHAR}
      </if>
      <if test="powermonitorname != null and powermonitorname != ''">
        and POWERMONITORNAME = #{powermonitorname, jdbcType=VARCHAR}
      </if>
      <if test="sitem != null and sitem != ''">
          and SITEM = #{sitem}
      </if>
      <if test="systemname != null and systemname != ''">
        and SYSTEMNAME = #{systemname, jdbcType=VARCHAR}
      </if>
      <if test="datasource != null and datasource != ''">
        DATASOURCE = #{datasource,jdbcType=VARCHAR}
      </if>
      <if test="timegranid != null and timegranid != ''">
        and TIMEGRANID = #{timegranid, jdbcType=VARCHAR}
      </if>
      <if test="monitordate != null and monitordate != ''">
        and MONITORDATE &gt;= #{monitordate, jdbcType=VARCHAR}
      </if>
      <if test="monitordatee != null and monitordatee != ''">
        and MONITORDATE &lt;= #{monitordatee, jdbcType=VARCHAR}
      </if>
    </where>
    ORDER BY MONITORDATE, DATADATE
  </select>

  <!-- 水质历史数据对比分析界面查询 -->
  <select id="datacomparison" parameterType="java.util.HashMap" resultType="Map">
    select t.UNIT, t.DETECTIONDEPARTMENT, t.POWERMONITORNAME, t.SYSTEMNAME, t.MONITORINGVALUE,
    t.MONITORDATE, t.SITEM, t.DATADATE, t2.MAXVALUE, t2.MINVALUE
    from
    T_CM_0318 t,
    (select SITEM, max(MONITORINGVALUE) as MAXVALUE, min(MONITORINGVALUE) as MINVALUE from T_CM_0318 
    where MONITORDATE like '${monitordate}%'
    <if test="detectiondepartment != null and detectiondepartment != ''">
      and DETECTIONDEPARTMENT = #{detectiondepartment, jdbcType=VARCHAR}
    </if>
    <if test="powermonitorname != null and powermonitorname != ''">
      and POWERMONITORNAME = #{powermonitorname, jdbcType=VARCHAR}
    </if>
    <if test="systemname != null and systemname != ''">
      and SYSTEMNAME = #{systemname, jdbcType=VARCHAR}
    </if>
    <if test="datasource != null and datasource != ''">
      and DATASOURCE = #{datasource,jdbcType=VARCHAR}
    </if>
    <if test="timegranid != null and timegranid !='' ">
      and TIMEGRANID = #{timegranid, jdbcType=VARCHAR}
    </if>
    group by SITEM) t2
    where
    t.SITEM = t2.SITEM
    and MONITORDATE like '${monitordate}%'
    <if test="detectiondepartment != null and detectiondepartment != ''">
      and DETECTIONDEPARTMENT = #{detectiondepartment, jdbcType=VARCHAR}
    </if>
    <if test="powermonitorname != null and powermonitorname != ''">
      and POWERMONITORNAME = #{powermonitorname, jdbcType=VARCHAR}
    </if>
    <if test="systemname != null and systemname != ''">
      and SYSTEMNAME = #{systemname, jdbcType=VARCHAR}
    </if>
    <if test="sitem != null and sitem != ''">
      and t.SITEM = #{sitem, jdbcType=VARCHAR}
    </if>
    <if test="datasource != null and datasource != ''">
      and DATASOURCE = #{datasource,jdbcType=VARCHAR}
    </if>
    <if test="timegranid != null and timegranid !='' ">
      and TIMEGRANID = #{timegranid, jdbcType=VARCHAR}
    </if>
    union 
    select t.UNIT, t.DETECTIONDEPARTMENT, t.POWERMONITORNAME, t.SYSTEMNAME, t.MONITORINGVALUE,
    t.MONITORDATE, t.SITEM, t.DATADATE, t2.MAXVALUE, t2.MINVALUE
    from
    T_CM_0318 t,
    (select SITEM, max(MONITORINGVALUE) as MAXVALUE, min(MONITORINGVALUE) as MINVALUE from T_CM_0318 
    where MONITORDATE like '${monitordatee}%'
    <if test="detectiondepartment != null and detectiondepartment != ''">
      and DETECTIONDEPARTMENT = #{detectiondepartment, jdbcType=VARCHAR}
    </if>
    <if test="powermonitorname != null and powermonitorname != ''">
      and POWERMONITORNAME = #{powermonitorname, jdbcType=VARCHAR}
    </if>
    <if test="systemname != null and systemname != ''">
      and SYSTEMNAME = #{systemname, jdbcType=VARCHAR}
    </if>
    <if test="datasource != null and datasource != ''">
      and DATASOURCE = #{datasource,jdbcType=VARCHAR}
    </if>
    <if test="timegranid != null and timegranid !='' ">
      and TIMEGRANID = #{timegranid, jdbcType=VARCHAR}
    </if>
    group by SITEM ) t2
    where
    t.SITEM = t2.SITEM
    and MONITORDATE like '${monitordatee}%'
    <if test="detectiondepartment != null and detectiondepartment != ''">
      and DETECTIONDEPARTMENT = #{detectiondepartment, jdbcType=VARCHAR}
    </if>
    <if test="powermonitorname != null and powermonitorname != ''">
      and POWERMONITORNAME = #{powermonitorname, jdbcType=VARCHAR}
    </if>
    <if test="systemname != null and systemname != ''">
      and SYSTEMNAME = #{systemname, jdbcType=VARCHAR}
    </if>
    <if test="sitem != null and sitem != ''">
      and t.SITEM = #{sitem, jdbcType=VARCHAR}
    </if>
    <if test="datasource != null and datasource != ''">
      and DATASOURCE = #{datasource,jdbcType=VARCHAR}
    </if>
    <if test="timegranid != null and timegranid !='' ">
      and TIMEGRANID = #{timegranid, jdbcType=VARCHAR}
    </if>
	ORDER BY MONITORDATE, DATADATE
  </select>

  <!-- 水质质量考核指标统计界面查询 -->
  <select id="qualityassessment" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    SELECT
    sum( CASE WHEN a.MONITORINGVALUE &lt;= c.UPPERLIMIT AND a.MONITORINGVALUE &gt;= c.LOWERLIMIT THEN 1 ELSE 0 END ) AS PASSNUM,
    count( a.SITEM ) AS TOTALNUM,
    round( 100 * sum( CASE WHEN a.MONITORINGVALUE &lt;= c.UPPERLIMIT AND a.MONITORINGVALUE &gt;= c.LOWERLIMIT THEN 1 ELSE 0 END ) / count( a.SITEM ), 2 ) AS PASSRATE,
    a.DETECTIONDEPARTMENT,
    a.POWERMONITORNAME,
    a.SYSTEMNAME
    FROM T_CM_0318 a, T_CM_0311 b, T_CM_0313 c
    WHERE
    b.ITEMCODE = a.SITEM
    and b.CHECKSTATE = 'Check'
    and a.DETECTIONDEPARTMENT = c.POWERDETECDEPARTMENT
    and a.SYSTEMNAME = c.SYSTEMNAME
    and a.POWERMONITORNAME = c.POWERMONITORNAME
    and a.SITEM = c.ITEMNAME
    <if test="monitordate != null and monitordate != ''">
      and instr( a.MONITORDATE, #{monitordate, jdbcType=VARCHAR}) &gt; 0
    </if>
    <if test="detectiondepartment != null and detectiondepartment != ''">
      and a.DETECTIONDEPARTMENT = #{detectiondepartment, jdbcType=VARCHAR}
    </if>
    <if test="systemname != null and systemname != ''">
      and a.SYSTEMNAME = #{systemname, jdbcType=VARCHAR}
    </if>
    <if test="datasource != null and datasource != ''">
      a.DATASOURCE = #{datasource,jdbcType=VARCHAR}
    </if>
    GROUP BY a.DETECTIONDEPARTMENT, a.POWERMONITORNAME, a.SYSTEMNAME
  </select>

  <insert id="insert" parameterType="com.steerinfo.ems.tcm0318.model.TCm0318">
    insert into T_CM_0318
    (RECORDID, PLANID, DETECTIONDEPARTMENT, POWERMONITORNAME, SITEM, SYSTEMNAME, UNIT, TIMEGRANID,
    ANALYST, INSERT_TIME, MONITORDATE, DATADATE, MONITORINGVALUE, DATASOURCE)
    values ( #{recordid, jdbcType=DECIMAL}, #{planid, jdbcType=VARCHAR}, #{detectiondepartment, jdbcType=VARCHAR},
    #{powermonitorname, jdbcType=VARCHAR}, #{sitem, jdbcType=VARCHAR}, #{systemname, jdbcType=VARCHAR}, #{unit, jdbcType=VARCHAR},
    #{timegranid, jdbcType=VARCHAR}, #{analyst, jdbcType=VARCHAR}, #{inserttime, jdbcType=VARCHAR}, #{monitordate, jdbcType=VARCHAR},
    #{datadate, jdbcType=VARCHAR}, #{monitoringvalue, jdbcType=VARCHAR}, #{datasource, jdbcType=VARCHAR}
    )
  </insert>

  <delete id="deleteBySelectiveElement" parameterType="java.util.HashMap">
    delete from T_CM_0318
    where DATASOURCE = 'MES'
    <if test="datadate != null and datadate != ''">
      and DATADATE = #{datadate}
    </if>
    <if test="sitem != null and sitem != ''">
      and SITEM = #{sitem}
    </if>
    <if test="systemname != null and systemname != ''">
      and SYSTEMNAME = #{systemname}
    </if>
  </delete>

  <select id="getByCode" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    select * from T_CM_0318
    where 1=1
    <if test="datasource != null and datasource != ''">
        and DATASOURCE = #{datasource,jdbcType=VARCHAR}
    </if>
    <if test="datadate != null and datadate != ''">
        and DATADATE = #{datadate}
    </if>
    <if test="sitem != null and sitem != ''">
        and SITEM = #{sitem}
    </if>
    <if test="systemname != null and systemname != ''">
        and SYSTEMNAME = #{systemname}
    </if>
  </select>

  <update id="updateByPrimaryKeySelective" parameterType="com.steerinfo.ems.tcm0318.model.TCm0318">
    update T_CM_0318
    <set>
      <if test="analyst != null">
        ANALYST = #{analyst,jdbcType=VARCHAR},
      </if>
      <if test="datadate != null">
        DATADATE = #{datadate,jdbcType=VARCHAR},
      </if>
      <if test="datastate != null">
        DATASTATE = #{datastate,jdbcType=VARCHAR},
      </if>
      <if test="monitordate != null">
        MONITORDATE = #{monitordate,jdbcType=VARCHAR},
      </if>
      <if test="monitoringvalue != null">
        MONITORINGVALUE = #{monitoringvalue,jdbcType=DECIMAL},
      </if>
      <if test="note != null">
        NOTE = #{note,jdbcType=VARCHAR},
      </if>
    </set>
    where RECORDID = #{recordid,jdbcType=VARCHAR}
  </update>
</mapper>