<?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.StWaterConsumerMapper">
  <resultMap id="BaseResultMap" type="com.lightinit.hsdatashow.entity.StWaterConsumer">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="region_id" jdbcType="BIGINT" property="regionId" />
    <result column="region_code" jdbcType="VARCHAR" property="regionCode" />
    <result column="consumer_type" jdbcType="VARCHAR" property="consumerType" />
    <result column="consumer_industry" jdbcType="VARCHAR" property="consumerIndustry" />
    <result column="consumer_name" jdbcType="VARCHAR" property="consumerName" />
    <result column="consumer_code" jdbcType="VARCHAR" property="consumerCode" />
    <result column="consumer_description" jdbcType="VARCHAR" property="consumerDescription" />
    <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, region_id, region_code, consumer_type, consumer_industry, consumer_name, consumer_code, 
    consumer_description, remark, created_at, created_by, deleted_at, deleted_by
  </sql>
  <select id="selectByExample" parameterType="com.lightinit.hsdatashow.entity.StWaterConsumerExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from st_water_consumer
    <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_consumer
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from st_water_consumer
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.lightinit.hsdatashow.entity.StWaterConsumerExample">
    delete from st_water_consumer
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.lightinit.hsdatashow.entity.StWaterConsumer">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into st_water_consumer (region_id, region_code, consumer_type, 
      consumer_industry, consumer_name, consumer_code, 
      consumer_description, remark, created_at, 
      created_by, deleted_at, deleted_by
      )
    values (#{regionId,jdbcType=BIGINT}, #{regionCode,jdbcType=VARCHAR}, #{consumerType,jdbcType=VARCHAR}, 
      #{consumerIndustry,jdbcType=VARCHAR}, #{consumerName,jdbcType=VARCHAR}, #{consumerCode,jdbcType=VARCHAR}, 
      #{consumerDescription,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.StWaterConsumer">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into st_water_consumer
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="regionId != null">
        region_id,
      </if>
      <if test="regionCode != null">
        region_code,
      </if>
      <if test="consumerType != null">
        consumer_type,
      </if>
      <if test="consumerIndustry != null">
        consumer_industry,
      </if>
      <if test="consumerName != null">
        consumer_name,
      </if>
      <if test="consumerCode != null">
        consumer_code,
      </if>
      <if test="consumerDescription != null">
        consumer_description,
      </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="regionId != null">
        #{regionId,jdbcType=BIGINT},
      </if>
      <if test="regionCode != null">
        #{regionCode,jdbcType=VARCHAR},
      </if>
      <if test="consumerType != null">
        #{consumerType,jdbcType=VARCHAR},
      </if>
      <if test="consumerIndustry != null">
        #{consumerIndustry,jdbcType=VARCHAR},
      </if>
      <if test="consumerName != null">
        #{consumerName,jdbcType=VARCHAR},
      </if>
      <if test="consumerCode != null">
        #{consumerCode,jdbcType=VARCHAR},
      </if>
      <if test="consumerDescription != null">
        #{consumerDescription,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.StWaterConsumerExample" resultType="java.lang.Long">
    select count(*) from st_water_consumer
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update st_water_consumer
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.regionId != null">
        region_id = #{record.regionId,jdbcType=BIGINT},
      </if>
      <if test="record.regionCode != null">
        region_code = #{record.regionCode,jdbcType=VARCHAR},
      </if>
      <if test="record.consumerType != null">
        consumer_type = #{record.consumerType,jdbcType=VARCHAR},
      </if>
      <if test="record.consumerIndustry != null">
        consumer_industry = #{record.consumerIndustry,jdbcType=VARCHAR},
      </if>
      <if test="record.consumerName != null">
        consumer_name = #{record.consumerName,jdbcType=VARCHAR},
      </if>
      <if test="record.consumerCode != null">
        consumer_code = #{record.consumerCode,jdbcType=VARCHAR},
      </if>
      <if test="record.consumerDescription != null">
        consumer_description = #{record.consumerDescription,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_consumer
    set id = #{record.id,jdbcType=BIGINT},
      region_id = #{record.regionId,jdbcType=BIGINT},
      region_code = #{record.regionCode,jdbcType=VARCHAR},
      consumer_type = #{record.consumerType,jdbcType=VARCHAR},
      consumer_industry = #{record.consumerIndustry,jdbcType=VARCHAR},
      consumer_name = #{record.consumerName,jdbcType=VARCHAR},
      consumer_code = #{record.consumerCode,jdbcType=VARCHAR},
      consumer_description = #{record.consumerDescription,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.StWaterConsumer">
    update st_water_consumer
    <set>
      <if test="regionId != null">
        region_id = #{regionId,jdbcType=BIGINT},
      </if>
      <if test="regionCode != null">
        region_code = #{regionCode,jdbcType=VARCHAR},
      </if>
      <if test="consumerType != null">
        consumer_type = #{consumerType,jdbcType=VARCHAR},
      </if>
      <if test="consumerIndustry != null">
        consumer_industry = #{consumerIndustry,jdbcType=VARCHAR},
      </if>
      <if test="consumerName != null">
        consumer_name = #{consumerName,jdbcType=VARCHAR},
      </if>
      <if test="consumerCode != null">
        consumer_code = #{consumerCode,jdbcType=VARCHAR},
      </if>
      <if test="consumerDescription != null">
        consumer_description = #{consumerDescription,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.StWaterConsumer">
    update st_water_consumer
    set region_id = #{regionId,jdbcType=BIGINT},
      region_code = #{regionCode,jdbcType=VARCHAR},
      consumer_type = #{consumerType,jdbcType=VARCHAR},
      consumer_industry = #{consumerIndustry,jdbcType=VARCHAR},
      consumer_name = #{consumerName,jdbcType=VARCHAR},
      consumer_code = #{consumerCode,jdbcType=VARCHAR},
      consumer_description = #{consumerDescription,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="getWaterConsumer" resultType="com.lightinit.hsdatashow.model.topic.water.NameAndValueModel">
      SELECT '用户用水' NAME, COUNT(*) VALUE FROM `st_water_consumer` wc
      JOIN region r ON r.`id` = wc.`region_id`
      <where>
        <if test="regionName !='' and regionName !=null">
          AND r.`region_name` = #{regionName}
        </if>
      </where>
  </select>

  <select id="getWaterUser" resultType="com.lightinit.hsdatashow.model.topic.water.RegionAndConsumption">
      SELECT r.id,r.region_name name,COUNT(region_id) value ,SUM(swcc.consumption) consumption
      FROM `st_water_consumer` swc JOIN region r ON swc.region_id = r.id
      JOIN `st_water_consumer_consumption` swcc ON swc.id = swcc.consumer_id
      <where>
        r.parent_id= 2
        <if test="month>0">
          and swcc.capture_month = #{month}
        </if>
        <if test="year>0">
          and swcc.capture_year = #{year}
        </if>
      </where>
      group by r.region_name
  </select>

    <select id="getAreaWaterUser" resultType="com.lightinit.hsdatashow.model.topic.water.RegionAndConsumption">
        SELECT r.id,r.region_name name,COUNT(region_id) value ,SUM(swcc.consumption) consumption,swc.consumer_name
        FROM `st_water_consumer` swc JOIN region r ON swc.region_id = r.id
        JOIN `st_water_consumer_consumption` swcc ON swc.id = swcc.consumer_id
        <where>
            r.parent_id= 2
            <if test="month>0">
                and swcc.capture_month = #{month}
            </if>
            <if test="year>0">
                and swcc.capture_year = #{year}
            </if>
            <if test="regionName!='' and regionName !=null">
                and r.region_name = #{regionName}
            </if>
        </where>
        group by r.region_name
    </select>

    <select id="getWaterUserConstitute" resultType="com.lightinit.hsdatashow.model.topic.water.NameAndValueModel">
        SELECT swc.`consumer_type` NAME ,COUNT(*) VALUE FROM
          `st_water_consumer` swc
          GROUP BY swc.`consumer_type`
    </select>
  <select id="getYesterdayUsers" resultType="com.lightinit.hsdatashow.model.topic.water.NameAndValueModel">
    SELECT swc.`consumption_type` name,swc.consumption value
    FROM `st_water_consumption` swc JOIN region r ON swc.`region_id` = r.id
    <where>
      <if test="year>0">
        swc.capture_year = #{year}
      </if>
      <if test="month>0">
        AND swc.capture_month = #{month}
      </if>
      <if test="day>0">
        AND swc.capture_day = #{day}
      </if>
    </where>
    GROUP BY swc.`consumption_type`
  </select>
  <select id="getYesterdayQXYSGC" resultType="com.lightinit.hsdatashow.model.topic.water.NameAndValueModel">
    SELECT r.region_name name,swc.consumption value
    FROM `st_water_consumption` swc JOIN region r ON swc.`region_id` = r.id
    <where>
      <if test="year>0">
        swc.capture_year = #{year}
      </if>
      <if test="month>0">
        AND swc.capture_month = #{month}
      </if>
      <if test="day>0">
        AND swc.capture_day = #{day}
      </if>
    </where>
    GROUP BY r.region_name
  </select>
  <select id="getYesterdayGHYYS" resultType="com.lightinit.hsdatashow.model.topic.water.STChartModel">
      SELECT swc.`consumer_type` name, SUM(swcc.`consumption`) value
      FROM `st_water_consumer` swc
      JOIN `st_water_consumer_consumption`swcc ON swc.id = swcc.`consumer_id`
      JOIN region r ON swc.`region_id` = r.id
      <where>
        <if test="year>0">
          swcc.capture_year = #{year}
        </if>
        <if test="month>0">
          AND swcc.capture_month = #{month}
        </if>
        <if test="day>0">
          AND swcc.capture_day = #{day}
        </if>
      </where>
      GROUP BY swc.`consumer_type`
  </select>

  <select id="getMonthYS" resultType="com.lightinit.hsdatashow.model.topic.water.NameAndValueModel">
    SELECT swc.`consumption_type` name,SUM(swc.consumption) VALUE
    FROM `st_water_consumption` swc JOIN region r ON swc.`region_id` = r.id
    <where>
      <if test="year>0">
        swc.capture_year = #{year}
      </if>
      <if test="month>0">
        AND swc.capture_month = #{month}
      </if>
    </where>
    GROUP BY swc.`consumption_type`
  </select>
  <select id="getMonthQXYSGC" resultType="com.lightinit.hsdatashow.model.topic.water.NameAndValueModel">
    SELECT r.region_name name,SUM(swc.consumption) VALUE
    FROM `st_water_consumption` swc JOIN region r ON swc.`region_id` = r.id
    <where>
      <if test="year>0">
        swc.capture_year = #{year}
      </if>
      <if test="month>0">
        AND swc.capture_month = #{month}
      </if>
    </where>
    GROUP BY r.region_name
  </select>
  <select id="getMonthGHYYS" resultType="com.lightinit.hsdatashow.model.topic.water.STChartModel">
    SELECT swc.`consumer_type` name, SUM(swcc.`consumption`) value
    FROM `st_water_consumer` swc
    JOIN `st_water_consumer_consumption`swcc ON swc.id = swcc.`consumer_id`
    JOIN region r ON swc.`region_id` = r.id
    <where>
      <if test="year>0">
        swcc.capture_year = #{year}
      </if>
      <if test="month>0">
        AND swcc.capture_month = #{month}
      </if>
    </where>
    GROUP BY swc.`consumer_type`
  </select>
</mapper>