Procházet zdrojové kódy

Merge remote-tracking branch 'origin/20210716' into 20210716

lihui001 před 3 roky
rodič
revize
a3e108f84d

+ 104 - 33
zoniot-rmcp/zoniot-rmcp-core/src/main/java/com/zcxk/rmcp/core/mapper/MeterReadRecordMapper.xml

@@ -291,51 +291,122 @@
   </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' ),
+    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' ),
+        tmp.tenant_id,
+        tmp.category_id,
+        tmp.company_org_id,
+        tmp.dept_org_id,
+        tmp.community_id,
+        count(tmp.device_id) as device_count,
+        count(1) as read_times,
+        SUM(IF(tmp.real_read_times != 0 ,1,0)) as real_read_times,
+        SUM(IF(tmp.real_read_times = 0 ,1,0)) as un_read_times,
+        FORMAT(SUM(IF(tmp.real_read_times != 0 ,1,0))/ COUNT(1)*100, 2) as read_rate ,-- 抄表率
+        now(),
+        now()
+    FROM
+    (
+        SELECT
             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
+            device_id,
+            SUM( IF ( read_status = 2, 1, 0 ) ) AS real_read_times
+        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' ),
+        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,
-            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
+            device_id
+    ) tmp
+    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' ),
+          tmp.tenant_id,
+          tmp.category_id,
+          tmp.company_org_id,
+          tmp.dept_org_id,
+          tmp.community_id,
+          count(tmp.device_id) as device_count,
+          count(1) as read_times,
+          SUM(IF(tmp.real_read_times != 0 ,1,0)) as real_read_times,
+          SUM(IF(tmp.real_read_times = 0 ,1,0)) as un_read_times,
+          FORMAT(SUM(IF(tmp.real_read_times != 0 ,1,0))/ COUNT(1)*100, 2) as read_rate ,-- 抄表率
+          now(),
+          now()
+      FROM
+      (
+          SELECT
+              tenant_id,
+              category_id,
+              company_org_id,
+              dept_org_id,
+              community_id,
+              device_id,
+              SUM( IF ( read_status = 2, 1, 0 ) ) AS real_read_times
+          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;
+          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,
+              device_id
+      ) tmp
+      GROUP BY
+      tenant_id,
+      category_id,
+      company_org_id,
+      dept_org_id,
+      community_id
   </insert>
 
   <insert id="updateTodayReadRate">