<?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.lightinit.hsdatashow.mapper.StWaterRiverMapper">
  <resultMap id="BaseResultMap" type="com.lightinit.hsdatashow.entity.StWaterRiver">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="river_type" jdbcType="VARCHAR" property="riverType" />
    <result column="river_name" jdbcType="VARCHAR" property="riverName" />
    <result column="river_code" jdbcType="VARCHAR" property="riverCode" />
    <result column="river_description" jdbcType="VARCHAR" property="riverDescription" />
    <result column="river_length" jdbcType="DECIMAL" property="riverLength" />
    <result column="start_stop_point" jdbcType="VARCHAR" property="startStopPoint" />
    <result column="flow_through" jdbcType="VARCHAR" property="flowThrough" />
    <result column="river_impoundage" jdbcType="DECIMAL" property="riverImpoundage" />
    <result column="river_chief" jdbcType="VARCHAR" property="riverChief" />
    <result column="water_level_normal" jdbcType="DECIMAL" property="waterLevelNormal" />
    <result column="water_level_warning" jdbcType="DECIMAL" property="waterLevelWarning" />
    <result column="water_level_dead" jdbcType="DECIMAL" property="waterLevelDead" />
    <result column="water_quality" jdbcType="VARCHAR" property="waterQuality" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="created_by" jdbcType="VARCHAR" property="createdBy" />
    <result column="deleted_at" jdbcType="TIMESTAMP" property="deletedAt" />
    <result column="deleted_by" jdbcType="VARCHAR" property="deletedBy" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, river_type, river_name, river_code, river_description, river_length, start_stop_point, 
    flow_through, river_impoundage, river_chief, water_level_normal, water_level_warning, 
    water_level_dead, water_quality, remark, created_at, created_by, deleted_at, deleted_by
  </sql>
  <select id="selectByExample" parameterType="com.lightinit.hsdatashow.entity.StWaterRiverExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from st_water_river
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from st_water_river
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from st_water_river
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.lightinit.hsdatashow.entity.StWaterRiverExample">
    delete from st_water_river
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.lightinit.hsdatashow.entity.StWaterRiver">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into st_water_river (river_type, river_name, river_code, 
      river_description, river_length, start_stop_point, 
      flow_through, river_impoundage, river_chief, 
      water_level_normal, water_level_warning, water_level_dead, 
      water_quality, remark, created_at, 
      created_by, deleted_at, deleted_by
      )
    values (#{riverType,jdbcType=VARCHAR}, #{riverName,jdbcType=VARCHAR}, #{riverCode,jdbcType=VARCHAR}, 
      #{riverDescription,jdbcType=VARCHAR}, #{riverLength,jdbcType=DECIMAL}, #{startStopPoint,jdbcType=VARCHAR}, 
      #{flowThrough,jdbcType=VARCHAR}, #{riverImpoundage,jdbcType=DECIMAL}, #{riverChief,jdbcType=VARCHAR}, 
      #{waterLevelNormal,jdbcType=DECIMAL}, #{waterLevelWarning,jdbcType=DECIMAL}, #{waterLevelDead,jdbcType=DECIMAL}, 
      #{waterQuality,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{createdAt,jdbcType=TIMESTAMP}, 
      #{createdBy,jdbcType=VARCHAR}, #{deletedAt,jdbcType=TIMESTAMP}, #{deletedBy,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.lightinit.hsdatashow.entity.StWaterRiver">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into st_water_river
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="riverType != null">
        river_type,
      </if>
      <if test="riverName != null">
        river_name,
      </if>
      <if test="riverCode != null">
        river_code,
      </if>
      <if test="riverDescription != null">
        river_description,
      </if>
      <if test="riverLength != null">
        river_length,
      </if>
      <if test="startStopPoint != null">
        start_stop_point,
      </if>
      <if test="flowThrough != null">
        flow_through,
      </if>
      <if test="riverImpoundage != null">
        river_impoundage,
      </if>
      <if test="riverChief != null">
        river_chief,
      </if>
      <if test="waterLevelNormal != null">
        water_level_normal,
      </if>
      <if test="waterLevelWarning != null">
        water_level_warning,
      </if>
      <if test="waterLevelDead != null">
        water_level_dead,
      </if>
      <if test="waterQuality != null">
        water_quality,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="createdAt != null">
        created_at,
      </if>
      <if test="createdBy != null">
        created_by,
      </if>
      <if test="deletedAt != null">
        deleted_at,
      </if>
      <if test="deletedBy != null">
        deleted_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="riverType != null">
        #{riverType,jdbcType=VARCHAR},
      </if>
      <if test="riverName != null">
        #{riverName,jdbcType=VARCHAR},
      </if>
      <if test="riverCode != null">
        #{riverCode,jdbcType=VARCHAR},
      </if>
      <if test="riverDescription != null">
        #{riverDescription,jdbcType=VARCHAR},
      </if>
      <if test="riverLength != null">
        #{riverLength,jdbcType=DECIMAL},
      </if>
      <if test="startStopPoint != null">
        #{startStopPoint,jdbcType=VARCHAR},
      </if>
      <if test="flowThrough != null">
        #{flowThrough,jdbcType=VARCHAR},
      </if>
      <if test="riverImpoundage != null">
        #{riverImpoundage,jdbcType=DECIMAL},
      </if>
      <if test="riverChief != null">
        #{riverChief,jdbcType=VARCHAR},
      </if>
      <if test="waterLevelNormal != null">
        #{waterLevelNormal,jdbcType=DECIMAL},
      </if>
      <if test="waterLevelWarning != null">
        #{waterLevelWarning,jdbcType=DECIMAL},
      </if>
      <if test="waterLevelDead != null">
        #{waterLevelDead,jdbcType=DECIMAL},
      </if>
      <if test="waterQuality != null">
        #{waterQuality,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="createdAt != null">
        #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        #{createdBy,jdbcType=VARCHAR},
      </if>
      <if test="deletedAt != null">
        #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        #{deletedBy,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.lightinit.hsdatashow.entity.StWaterRiverExample" resultType="java.lang.Long">
    select count(*) from st_water_river
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update st_water_river
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.riverType != null">
        river_type = #{record.riverType,jdbcType=VARCHAR},
      </if>
      <if test="record.riverName != null">
        river_name = #{record.riverName,jdbcType=VARCHAR},
      </if>
      <if test="record.riverCode != null">
        river_code = #{record.riverCode,jdbcType=VARCHAR},
      </if>
      <if test="record.riverDescription != null">
        river_description = #{record.riverDescription,jdbcType=VARCHAR},
      </if>
      <if test="record.riverLength != null">
        river_length = #{record.riverLength,jdbcType=DECIMAL},
      </if>
      <if test="record.startStopPoint != null">
        start_stop_point = #{record.startStopPoint,jdbcType=VARCHAR},
      </if>
      <if test="record.flowThrough != null">
        flow_through = #{record.flowThrough,jdbcType=VARCHAR},
      </if>
      <if test="record.riverImpoundage != null">
        river_impoundage = #{record.riverImpoundage,jdbcType=DECIMAL},
      </if>
      <if test="record.riverChief != null">
        river_chief = #{record.riverChief,jdbcType=VARCHAR},
      </if>
      <if test="record.waterLevelNormal != null">
        water_level_normal = #{record.waterLevelNormal,jdbcType=DECIMAL},
      </if>
      <if test="record.waterLevelWarning != null">
        water_level_warning = #{record.waterLevelWarning,jdbcType=DECIMAL},
      </if>
      <if test="record.waterLevelDead != null">
        water_level_dead = #{record.waterLevelDead,jdbcType=DECIMAL},
      </if>
      <if test="record.waterQuality != null">
        water_quality = #{record.waterQuality,jdbcType=VARCHAR},
      </if>
      <if test="record.remark != null">
        remark = #{record.remark,jdbcType=VARCHAR},
      </if>
      <if test="record.createdAt != null">
        created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.createdBy != null">
        created_by = #{record.createdBy,jdbcType=VARCHAR},
      </if>
      <if test="record.deletedAt != null">
        deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="record.deletedBy != null">
        deleted_by = #{record.deletedBy,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update st_water_river
    set id = #{record.id,jdbcType=BIGINT},
      river_type = #{record.riverType,jdbcType=VARCHAR},
      river_name = #{record.riverName,jdbcType=VARCHAR},
      river_code = #{record.riverCode,jdbcType=VARCHAR},
      river_description = #{record.riverDescription,jdbcType=VARCHAR},
      river_length = #{record.riverLength,jdbcType=DECIMAL},
      start_stop_point = #{record.startStopPoint,jdbcType=VARCHAR},
      flow_through = #{record.flowThrough,jdbcType=VARCHAR},
      river_impoundage = #{record.riverImpoundage,jdbcType=DECIMAL},
      river_chief = #{record.riverChief,jdbcType=VARCHAR},
      water_level_normal = #{record.waterLevelNormal,jdbcType=DECIMAL},
      water_level_warning = #{record.waterLevelWarning,jdbcType=DECIMAL},
      water_level_dead = #{record.waterLevelDead,jdbcType=DECIMAL},
      water_quality = #{record.waterQuality,jdbcType=VARCHAR},
      remark = #{record.remark,jdbcType=VARCHAR},
      created_at = #{record.createdAt,jdbcType=TIMESTAMP},
      created_by = #{record.createdBy,jdbcType=VARCHAR},
      deleted_at = #{record.deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{record.deletedBy,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.lightinit.hsdatashow.entity.StWaterRiver">
    update st_water_river
    <set>
      <if test="riverType != null">
        river_type = #{riverType,jdbcType=VARCHAR},
      </if>
      <if test="riverName != null">
        river_name = #{riverName,jdbcType=VARCHAR},
      </if>
      <if test="riverCode != null">
        river_code = #{riverCode,jdbcType=VARCHAR},
      </if>
      <if test="riverDescription != null">
        river_description = #{riverDescription,jdbcType=VARCHAR},
      </if>
      <if test="riverLength != null">
        river_length = #{riverLength,jdbcType=DECIMAL},
      </if>
      <if test="startStopPoint != null">
        start_stop_point = #{startStopPoint,jdbcType=VARCHAR},
      </if>
      <if test="flowThrough != null">
        flow_through = #{flowThrough,jdbcType=VARCHAR},
      </if>
      <if test="riverImpoundage != null">
        river_impoundage = #{riverImpoundage,jdbcType=DECIMAL},
      </if>
      <if test="riverChief != null">
        river_chief = #{riverChief,jdbcType=VARCHAR},
      </if>
      <if test="waterLevelNormal != null">
        water_level_normal = #{waterLevelNormal,jdbcType=DECIMAL},
      </if>
      <if test="waterLevelWarning != null">
        water_level_warning = #{waterLevelWarning,jdbcType=DECIMAL},
      </if>
      <if test="waterLevelDead != null">
        water_level_dead = #{waterLevelDead,jdbcType=DECIMAL},
      </if>
      <if test="waterQuality != null">
        water_quality = #{waterQuality,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="createdAt != null">
        created_at = #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="createdBy != null">
        created_by = #{createdBy,jdbcType=VARCHAR},
      </if>
      <if test="deletedAt != null">
        deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      </if>
      <if test="deletedBy != null">
        deleted_by = #{deletedBy,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lightinit.hsdatashow.entity.StWaterRiver">
    update st_water_river
    set river_type = #{riverType,jdbcType=VARCHAR},
      river_name = #{riverName,jdbcType=VARCHAR},
      river_code = #{riverCode,jdbcType=VARCHAR},
      river_description = #{riverDescription,jdbcType=VARCHAR},
      river_length = #{riverLength,jdbcType=DECIMAL},
      start_stop_point = #{startStopPoint,jdbcType=VARCHAR},
      flow_through = #{flowThrough,jdbcType=VARCHAR},
      river_impoundage = #{riverImpoundage,jdbcType=DECIMAL},
      river_chief = #{riverChief,jdbcType=VARCHAR},
      water_level_normal = #{waterLevelNormal,jdbcType=DECIMAL},
      water_level_warning = #{waterLevelWarning,jdbcType=DECIMAL},
      water_level_dead = #{waterLevelDead,jdbcType=DECIMAL},
      water_quality = #{waterQuality,jdbcType=VARCHAR},
      remark = #{remark,jdbcType=VARCHAR},
      created_at = #{createdAt,jdbcType=TIMESTAMP},
      created_by = #{createdBy,jdbcType=VARCHAR},
      deleted_at = #{deletedAt,jdbcType=TIMESTAMP},
      deleted_by = #{deletedBy,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="selectAllList" resultType="com.lightinit.hsdatashow.model.topic.water.WaterAllListReturnModel">
     SELECT swr.river_name riverName,swr.river_length riverLength,swr.start_stop_point startStopPoint,swr.`river_impoundage` riverImpoundage,
     swr.`river_chief` riverChief,swr.`water_level_normal` waterLevelNormal,swr.`water_level_warning` waterLevelWarning,swr.water_quality waterQuality,
     swr.flow_through flowThrough,swrwl.water_level waterLevel
     from st_water_river swr
     join st_water_river_water_level swrwl
     on swrwl.river_id = swr.id
     INNER JOIN
    (SELECT river_id,MAX(capture_time) AS maxtime
    FROM st_water_river_water_level
    GROUP BY river_id
    ) AS A
    on swrwl.capture_time = A.maxtime and A.river_id = swrwl.river_id
  </select>
  <select id="getRiverCount" resultType="java.lang.Integer">
     SELECT count(*)
     from st_water_river swr
     join st_water_river_water_level swrwl
     on swrwl.river_id = swr.id
     INNER JOIN
    (SELECT river_id,MAX(capture_time) AS maxtime
    FROM st_water_river_water_level
    GROUP BY river_id
    ) AS A
    on swrwl.capture_time = A.maxtime and A.river_id = swrwl.river_id
  </select>
  <select id="getShangyueJiangyu" resultType="java.math.BigDecimal">
     select sum(swr.rainfall) from
     st_water_rainfall swr
     join region r
     on r.id = swr.region_id
    where PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( swr.capture_time, '%Y%m' ) ) =1
  </select>
  <select id="getShangyueYongshui" resultType="java.math.BigDecimal">
    select sum(swc.consumption) value from
       st_water_consumption swc
       join region r
       on r.id = swc.region_id
          where PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( swc.capture_time, '%Y%m' ) ) =1
  </select>
  <select id="getRiverLength" resultType="java.math.BigDecimal">
    SELECT sum(swr.river_length)
     from st_water_river swr
     join st_water_river_water_level swrwl
     on swrwl.river_id = swr.id
     INNER JOIN
    (SELECT river_id,MAX(capture_time) AS maxtime
    FROM st_water_river_water_level
    GROUP BY river_id
    ) AS A
    on swrwl.capture_time = A.maxtime and A.river_id = swrwl.river_id
  </select>
  <select id="getHlXs" resultType="java.math.BigDecimal">
     SELECT sum(swr.river_impoundage)
     from st_water_river swr
     join st_water_river_water_level swrwl
     on swrwl.river_id = swr.id
     INNER JOIN
    (SELECT river_id,MAX(capture_time) AS maxtime
    FROM st_water_river_water_level
    GROUP BY river_id
    ) AS A
    on swrwl.capture_time = A.maxtime and A.river_id = swrwl.river_id
  </select>
</mapper>