Explorar el Código

新增首页统计第一个模块

Administrator hace 2 años
padre
commit
b37ba14a4c

+ 9 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/info/SchedulingConfig.java

@@ -1,5 +1,7 @@
 package com.ruoyi.web.controller.info;
 
+import com.ruoyi.common.utils.DateUtils;
+import com.ruoyi.system.service.IUserNucleicTimeService;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -14,11 +16,18 @@ import org.springframework.scheduling.annotation.Scheduled;
 public class SchedulingConfig {
 
 
+    /**
+     * 核酸时间记录表
+     */
+    private IUserNucleicTimeService userNucleicTimeService;
 
     /**
      * 10分钟执行一次刷新首页数据
      */
     @Scheduled(cron="* 0/10 * * * ? ")    // 10分钟执行一次
     public void scheduler() {
+        //获取首页数据
+        userNucleicTimeService.getIndexData();
+        System.out.println("执行了一次首页数据更新:时间"+ DateUtils.getNowDate());
     }
 }

+ 22 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/UserNucleicTimeMapper.java

@@ -1,5 +1,6 @@
 package com.ruoyi.system.mapper;
 
+import java.util.Date;
 import java.util.List;
 import com.ruoyi.system.domain.UserNucleicTime;
 
@@ -58,4 +59,25 @@ public interface UserNucleicTimeMapper
      * @return 结果
      */
     public int deleteUserNucleicTimeByIds(Long[] ids);
+
+    /**
+     * 获取本月人数
+     * @param monthTime
+     * @return
+     */
+    public int getIndexDataMonth(Date monthTime);
+
+    /**
+     * 获取本周人数
+     * @param weekTime
+     * @return
+     */
+    public int getIndexDataWeek(Date weekTime);
+
+    /**
+     * 获取上周人数
+     * @param weekTime
+     * @return
+     */
+    public int getIndexDataLastWeek(Date weekTime);
 }

+ 6 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/IUserNucleicTimeService.java

@@ -58,4 +58,10 @@ public interface IUserNucleicTimeService
      * @return 结果
      */
     public int deleteUserNucleicTimeById(Long id);
+
+    /**
+     * 查询首页需要的数据
+     * @param
+     */
+    public void getIndexData();
 }

+ 8 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/IndexServiceImpl.java

@@ -4,6 +4,7 @@ import com.ruoyi.common.constant.CacheConstants;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.redis.RedisCache;
 import com.ruoyi.system.domain.SysConfig;
+import com.ruoyi.system.service.IUserNucleicTimeService;
 import com.ruoyi.system.service.IndexService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -19,6 +20,9 @@ public class IndexServiceImpl implements IndexService {
 
     @Autowired
     private RedisCache redisCache;
+
+    @Autowired
+    private IUserNucleicTimeService userNucleicTimeService;
     /**
      * 后台首页统计本月核酸人数,本周人数,对比上周人数
      * @return
@@ -29,6 +33,9 @@ public class IndexServiceImpl implements IndexService {
         if (indexData != null && indexData.size() > 0){
             return AjaxResult.success(indexData);
         }
-        return AjaxResult.error("暂未获取到数据");
+        //运行一下数据
+        userNucleicTimeService.getIndexData();
+        Map<String, Object> indexDataNew = redisCache.getCacheMap(CacheConstants.INDEX_DATA);
+        return AjaxResult.success(indexDataNew);
     }
 }

+ 44 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UserNucleicTimeServiceImpl.java

@@ -1,6 +1,14 @@
 package com.ruoyi.system.service.impl;
 
+import java.text.NumberFormat;
+import java.text.SimpleDateFormat;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+
+import com.ruoyi.common.constant.CacheConstants;
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.utils.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.ruoyi.system.mapper.UserNucleicTimeMapper;
@@ -19,6 +27,9 @@ public class UserNucleicTimeServiceImpl implements IUserNucleicTimeService
     @Autowired
     private UserNucleicTimeMapper userNucleicTimeMapper;
 
+    @Autowired
+    private RedisCache redisCache;
+
     /**
      * 查询导入人员核酸时间记录
      * 
@@ -90,4 +101,37 @@ public class UserNucleicTimeServiceImpl implements IUserNucleicTimeService
     {
         return userNucleicTimeMapper.deleteUserNucleicTimeById(id);
     }
+
+
+    /**
+     * 10分钟执行一次刷新首页数据
+     */
+    @Override
+    public void getIndexData() {
+        //获取本月人数
+        int indexDataMonth = userNucleicTimeMapper.getIndexDataMonth(DateUtils.getNowDate());
+        //获取本周人数
+        int indexDataWeek = userNucleicTimeMapper.getIndexDataWeek(DateUtils.getNowDate());
+        //获取上周周人数
+        int indexDataLastWeek = userNucleicTimeMapper.getIndexDataLastWeek(DateUtils.getNowDate());
+
+        NumberFormat numberformat= NumberFormat.getPercentInstance();
+        numberformat.setMinimumFractionDigits(2);
+        String resultWeek = "0%";
+        String resultLastWeek = "0%";
+        int allCount = indexDataWeek + indexDataLastWeek;
+        if (indexDataWeek > 0 ){
+             resultWeek=numberformat.format((float)indexDataWeek/(float)allCount);
+        }
+        if (indexDataLastWeek > 0 ){
+            resultLastWeek=numberformat.format((float)indexDataLastWeek/(float)allCount);
+        }
+        Map<String,Object> map = new HashMap<>();
+        map.put("indexDataMonth",indexDataMonth);
+        map.put("indexDataWeek",indexDataWeek);
+        map.put("indexDataLastWeek",indexDataLastWeek);
+        map.put("resultWeek",resultWeek);
+        map.put("resultLastWeek",resultLastWeek);
+        redisCache.setCacheMap(CacheConstants.INDEX_DATA,map);
+    }
 }

+ 13 - 0
ruoyi-system/src/main/resources/mapper/system/UserNucleicTimeMapper.xml

@@ -69,4 +69,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             #{id}
         </foreach>
     </delete>
+
+    <select id="getIndexDataMonth"  resultType="java.lang.Integer">
+        select ifnull(count(s1.id_card),0) from (
+        SELECT id_card FROM `user_nucleic_time` where DATE_FORMAT(nucleic_collect_time,'%Y-%m') = DATE_FORMAT(#{monthTime},'%Y-%m') GROUP BY id_card) s1
+    </select>
+    <select id="getIndexDataWeek" resultType="java.lang.Integer">
+    select ifnull(count(s1.id_card),0) from (
+    SELECT id_card FROM `user_nucleic_time` where DATE_FORMAT(nucleic_collect_time,'%x-%v') = DATE_FORMAT(now(),'%x-%v') GROUP BY id_card) s1
+    </select>
+    <select id="getIndexDataLastWeek" resultType="java.lang.Integer">
+                select ifnull(count(s1.id_card),0) from (
+        SELECT id_card FROM `user_nucleic_time` where YEARWEEK(DATE_FORMAT(nucleic_collect_time,'%Y-%m-%d')) = YEARWEEK(DATE_FORMAT(now(),'%Y-%m-%d')) -1 GROUP BY id_card) s1
+    </select>
 </mapper>