<?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.zcxk.rmcp.core.dao.MeterReadRecordMapper">
  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.MeterReadRecord">
    <!--@mbg.generated-->
    <!--@Table rmcp_meter_read_record-->
    <id column="id" property="id" />
    <result column="read_date" property="readDate" />
    <result column="tenant_id" property="tenantId" />
    <result column="category_id" property="categoryId" />
    <result column="product_id" property="productId" />
    <result column="device_id" property="deviceId" />
    <result column="community_id" property="communityId" />
    <result column="company_org_id" property="companyOrgId" />
    <result column="dept_org_id" property="deptOrgId" />
    <result column="concentrator_id" property="concentratorId" />
    <result column="collector_id" property="collectorId" />
    <result column="read_time" property="readTime" />
    <result column="read_status" property="readStatus" />
    <result column="read_data" property="readData" />
    <result column="last_valid" property="lastValid" />
    <result column="last_cost" property="lastCost" />
    <result column="status" property="status" />
    <result column="create_date" property="createDate" />
    <result column="update_date" property="updateDate" />
    <result column="create_by" property="createBy" />
    <result column="update_by" property="updateBy" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, read_date, tenant_id, category_id, product_id, device_id, community_id, company_org_id, 
    dept_org_id, concentrator_id, collector_id, read_time, read_status, read_data, last_valid, 
    last_cost, `status`, create_date, update_date, create_by, update_by
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.MeterReadRecord" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_meter_read_record
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="readDate != null">
        read_date,
      </if>
      <if test="tenantId != null">
        tenant_id,
      </if>
      <if test="categoryId != null">
        category_id,
      </if>
      <if test="productId != null">
        product_id,
      </if>
      <if test="deviceId != null">
        device_id,
      </if>
      <if test="communityId != null">
        community_id,
      </if>
      <if test="companyOrgId != null">
        company_org_id,
      </if>
      <if test="deptOrgId != null">
        dept_org_id,
      </if>
      <if test="concentratorId != null">
        concentrator_id,
      </if>
      <if test="collectorId != null">
        collector_id,
      </if>
      <if test="readTime != null">
        read_time,
      </if>
      <if test="readStatus != null">
        read_status,
      </if>
      <if test="readData != null">
        read_data,
      </if>
      <if test="lastValid != null">
        last_valid,
      </if>
      <if test="lastCost != null">
        last_cost,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="updateDate != null">
        update_date,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="readDate != null">
        #{readDate},
      </if>
      <if test="tenantId != null">
        #{tenantId},
      </if>
      <if test="categoryId != null">
        #{categoryId},
      </if>
      <if test="productId != null">
        #{productId},
      </if>
      <if test="deviceId != null">
        #{deviceId},
      </if>
      <if test="communityId != null">
        #{communityId},
      </if>
      <if test="companyOrgId != null">
        #{companyOrgId},
      </if>
      <if test="deptOrgId != null">
        #{deptOrgId},
      </if>
      <if test="concentratorId != null">
        #{concentratorId},
      </if>
      <if test="collectorId != null">
        #{collectorId},
      </if>
      <if test="readTime != null">
        #{readTime},
      </if>
      <if test="readStatus != null">
        #{readStatus},
      </if>
      <if test="readData != null">
        #{readData},
      </if>
      <if test="lastValid != null">
        #{lastValid},
      </if>
      <if test="lastCost != null">
        #{lastCost},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="createDate != null">
        #{createDate},
      </if>
      <if test="updateDate != null">
        #{updateDate},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.rmcp.core.entity.MeterReadRecord">
    <!--@mbg.generated-->
    update rmcp_meter_read_record
    <set>
      <if test="readDate != null">
        read_date = #{readDate},
      </if>
      <if test="tenantId != null">
        tenant_id = #{tenantId},
      </if>
      <if test="categoryId != null">
        category_id = #{categoryId},
      </if>
      <if test="productId != null">
        product_id = #{productId},
      </if>
      <if test="deviceId != null">
        device_id = #{deviceId},
      </if>
      <if test="communityId != null">
        community_id = #{communityId},
      </if>
      <if test="companyOrgId != null">
        company_org_id = #{companyOrgId},
      </if>
      <if test="deptOrgId != null">
        dept_org_id = #{deptOrgId},
      </if>
      <if test="concentratorId != null">
        concentrator_id = #{concentratorId},
      </if>
      <if test="collectorId != null">
        collector_id = #{collectorId},
      </if>
      <if test="readTime != null">
        read_time = #{readTime},
      </if>
      <if test="readStatus != null">
        read_status = #{readStatus},
      </if>
      <if test="readData != null">
        read_data = #{readData},
      </if>
      <if test="lastValid != null">
        last_valid = #{lastValid},
      </if>
      <if test="lastCost != null">
        last_cost = #{lastCost},
      </if>
      <if test="status != null">
        `status` = #{status},
      </if>
      <if test="createDate != null">
        create_date = #{createDate},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate},
      </if>
      <if test="createBy != null">
        create_by = #{createBy},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy},
      </if>
    </set>
    where id = #{id}
  </update>
  <insert id="batchInsert" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_meter_read_record
    (id,read_date, tenant_id, category_id, product_id, device_id, community_id, company_org_id,
      dept_org_id, concentrator_id, collector_id, read_time, read_status, read_data, 
      last_valid, last_cost, `status`, create_date, update_date, create_by, update_by
      )
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.id},#{item.readDate}, #{item.tenantId}, #{item.categoryId}, #{item.productId}, #{item.deviceId},
        #{item.communityId}, #{item.companyOrgId}, #{item.deptOrgId}, #{item.concentratorId}, 
        #{item.collectorId}, #{item.readTime}, #{item.readStatus}, #{item.readData}, #{item.lastValid}, 
        #{item.lastCost}, #{item.status}, #{item.createDate}, #{item.updateDate}, #{item.createBy}, 
        #{item.updateBy})
    </foreach>
  </insert>

  <insert id="todayReadRateJobHandler">
    insert into rmcp_stat_meter_read_rate_by_community_today (stat_day,tenant_id,category_id,company_org_id,dept_org_id,community_id,device_count,read_times,real_read_times,un_read_times,read_rate,create_date,update_date)
    select
            DATE_FORMAT( #{readDate}, '%Y%m%d' ),
            tenant_id,
            category_id,
            company_org_id,
            dept_org_id,
            community_id,
            count(distinct id) as device_count, -- 设备总数
            count(distinct id) as read_times,   -- 应抄总数
            0 as real_read_times, -- 实抄总数
            count(distinct id) as un_read_times,   -- 未抄总数
            0 as read_rate ,-- 抄表率
            now(),
            now()
    from
            rmcp_device
    where
            `status` = 1
    group by tenant_id,category_id,company_org_id,dept_org_id,community_id;
  </insert>

  <insert id="yesterdayReadRate">
    insert into rmcp_stat_meter_read_rate_by_community_yesterday (stat_day,tenant_id,category_id,company_org_id,dept_org_id,community_id,device_count,read_times,real_read_times,un_read_times,read_rate,create_date,update_date)
    select
            DATE_FORMAT( #{readDate}, '%Y%m%d' ),
            tenant_id,
            category_id,
            company_org_id,
            dept_org_id,
            community_id,
            count(distinct device_id) as device_count, -- 设备总数
            count(1) as read_times,   -- 应抄总数
            SUM(IF(read_status = 2, 1, 0)) as real_read_times, -- 实抄总数
            SUM(IF(read_status = 1, 1, 0)) as un_read_times,   -- 未抄总数
            FORMAT(SUM(IF(read_status = 2, 1, 0))/ COUNT(1)*100, 2) as read_rate ,-- 抄表率
            now(),
            now()
    from
            rmcp_meter_read_record
    where
            read_date = DATE_FORMAT( #{readDate}, '%Y%m%d' )
    group by tenant_id,category_id,company_org_id,dept_org_id,community_id;
  </insert>

  <insert id="recent7daysReadRate">
    insert into rmcp_stat_meter_read_rate_by_community_7day (stat_day,tenant_id,category_id,company_org_id,dept_org_id,community_id,device_count,read_times,real_read_times,un_read_times,read_rate,create_date,update_date)
    select
            DATE_FORMAT( #{readDate}, '%Y%m%d' ),
            tenant_id,
            category_id,
            company_org_id,
            dept_org_id,
            community_id,
            count(distinct device_id) as device_count, -- 设备总数
            count(1) as read_times,   -- 应抄总数
            SUM(IF(read_status = 2, 1, 0)) as real_read_times, -- 实抄总数
            SUM(IF(read_status = 1, 1, 0)) as un_read_times,   -- 未抄总数
            FORMAT(SUM(IF(read_status = 2, 1, 0))/ COUNT(1)*100, 2) as read_rate ,-- 抄表率
            now(),
            now()
    from
            rmcp_meter_read_record
    where

            read_date <![CDATA[ < ]]> DATE_FORMAT( #{readDate}, '%Y%m%d' )
            and read_date <![CDATA[ >= ]]> DATE_FORMAT( date_add(#{readDate}, interval -7 day), '%Y%m%d' )
    group by tenant_id,category_id,company_org_id,dept_org_id,community_id;
  </insert>

  <insert id="lastMonthReadRate">
    insert into rmcp_stat_meter_read_rate_by_community_month (stat_day,tenant_id,category_id,company_org_id,dept_org_id,community_id,device_count,read_times,real_read_times,un_read_times,read_rate,create_date,update_date)
            select
            DATE_FORMAT( date_add(#{readDate}, interval -1 MONTH), '%Y%m' ),
            tenant_id,
            category_id,
            company_org_id,
            dept_org_id,
            community_id,
            count(distinct device_id) as device_count, -- 设备总数
            count(1) as read_times,   -- 应抄总数
            SUM(IF(read_status = 2, 1, 0)) as real_read_times, -- 实抄总数
            SUM(IF(read_status = 1, 1, 0)) as un_read_times,   -- 未抄总数
            FORMAT(SUM(IF(read_status = 2, 1, 0))/ COUNT(1)*100, 2) as read_rate ,-- 抄表率
            now(),
            now()
    from
            rmcp_meter_read_record
    where
    period_diff(date_format(#{readDate} , '%Y%m') , date_format(`read_date`, '%Y%m')) =1
    group by tenant_id,category_id,company_org_id,dept_org_id,community_id;
  </insert>

  <insert id="updateTodayReadRate">
    update rmcp_stat_meter_read_rate_by_community_today
    set real_read_times = real_read_times + 1 ,
        un_read_times = un_read_times - 1,
        read_rate = FORMAT(real_read_times / read_times*100, 2)
    WHERE stat_day = DATE_FORMAT( #{readDate}, '%Y%m%d' ) and tenant_id = #{device.tenantId} and category_id = #{device.categoryId}
    and company_org_id = #{device.companyOrgId} and dept_org_id = #{device.deptOrgId} and community_id = #{device.communityId}
  </insert>
</mapper>