Преглед изворни кода

新建设备在线统计记录/图表和在线状态图表显示

zhuoning пре 3 година
родитељ
комит
8bfb387bc3

+ 3 - 0
huimv-eartag2-platform/huimv-eartag2-common/src/main/java/com/huimv/eartag2/common/dao/repo/EartagDeviceOnlineRepo.java

@@ -15,4 +15,7 @@ public interface EartagDeviceOnlineRepo extends JpaRepository<EartagDeviceOnline
 
     @Query(nativeQuery = true,value = "SELECT * FROM eartag_device_online WHERE farm_id=?1 AND DATE_FORMAT(add_date,'%Y-%m-%d')=DATE_FORMAT(?2,'%Y-%m-%d') ORDER BY add_date DESC")
     List<EartagDeviceOnlineEntity> getDeviceOnlineByFarmIdAndCreateDate(String farmId, String todayDate);
+
+    @Query(nativeQuery = true,value = "SELECT * FROM eartag_device_online WHERE device_code=?1 AND add_date BETWEEN DATE_FORMAT(?2,'%Y-%m-%d') AND DATE_FORMAT(?3,'%Y-%m-%d') ORDER BY add_date ASC")
+    List<EartagDeviceOnlineEntity> getDeviceOnlineByDeviceCode(String deviceCode, String pastDate, String todayDate);
 }

+ 5 - 4
huimv-eartag2-platform/huimv-eartag2-common/src/main/java/com/huimv/eartag2/common/utils/BizConst.java

@@ -36,10 +36,11 @@ public class BizConst {
     //牧场总状态表+牧场id
     public final static String FARM_ALL_STATUS_PREFIX = "farm_all_status_";
 
-    public final static Integer CODE_ONLINE_NO_EXIST = 10001;
-    public final static String MSG_ONLINE_NO_EXIST = "今天无在线设备.";
+    public final static Integer CODE_DEVICE_ONLINE_NO_EXIST = 10001;
+    public final static String MSG_DEVICE_ONLINE_NO_EXIST = "今天无在线设备.";
+
+    public final static Integer CODE_DEVICE_REGISTER_NO_EXIST = 10002;
+    public final static String MSG_DEVICE_REGISTER_NO_EXIST = "该设备的注册信息不存在.";
 
-    public final static Integer CODE_REGISTER_NO_EXIST = 10002;
-    public final static String MSG_REGISTER_NO_EXIST = "该设备不存在.";
 
 }

+ 75 - 1
huimv-eartag2-platform/huimv-eartag2-common/src/main/java/com/huimv/eartag2/common/utils/DateUtil.java

@@ -10,6 +10,7 @@ import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.List;
 
 /**
  * @Project : huimv.shiwan
@@ -67,6 +68,14 @@ public class DateUtil {
         return sdf.parse(dateText);
     }
 
+    public Date parseDate(String dateText) throws ParseException {
+        if(dateText.indexOf("T") != -1){
+            dateText = dateText.replace("T"," ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.parse(dateText);
+    }
+
     public Long parseDateTimeLong(String dateText) throws ParseException {
         if(dateText.indexOf("T") != -1){
             dateText = dateText.replace("T"," ");
@@ -159,6 +168,61 @@ public class DateUtil {
     }
 
     /**
+     * @Method      : getPastIntervals
+     * @Description : 返回过去N天日期(倒排序)
+     * @Params      : [intervals]
+     * @Return      : java.util.ArrayList<java.lang.String>
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2022/3/7       
+     * @Time        : 15:27
+     */
+    public ArrayList<String> getPastIntervals(int intervals ) {
+        ArrayList<String> pastDaysList = new ArrayList<>();
+        for (int i = 0; i <intervals; i++) {
+            pastDaysList.add(getPastDate(i));
+        }
+        return pastDaysList;
+    }
+
+    /**
+     * @Method      : getPastIntervalsASC
+     * @Description : 正排序
+     * @Params      : [intervals]
+     * @Return      : java.util.ArrayList<java.lang.String>
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2022/3/7       
+     * @Time        : 16:33
+     */
+    public ArrayList<String> getPastIntervalsASC(int intervals ) {
+        ArrayList<String> pastDaysList = new ArrayList<>();
+        for (int i = intervals-1; i >=0; i--) {
+            pastDaysList.add(getPastDate(i));
+        }
+        return pastDaysList;
+    }
+
+    /**
+     * @Method      : getFetureIntervals
+     * @Description : 获取未来N天日期
+     * @Params      : [intervals]
+     * @Return      : java.util.ArrayList<java.lang.String>
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2022/3/7       
+     * @Time        : 15:28
+     */
+    public ArrayList<String> getFetureIntervals(int intervals ) {
+        ArrayList<String> fetureDaysList = new ArrayList<>();
+        for (int i = 0; i <intervals; i++) {
+            fetureDaysList.add(getFetureDate(i));
+        }
+        return fetureDaysList;
+    }
+
+
+    /**
      * 获取过去第几天的日期
      *
      * @param past
@@ -195,7 +259,17 @@ public class DateUtil {
     public static void main(String[] args){
         DateUtil du = new DateUtil();
         //
-        du.test1();
+//        du.test1();
+        List<String> pastList = du.getPastIntervalsASC(5);
+        for(String pastDate:pastList){
+            System.out.println("pastDate>>"+pastDate);
+        }
+
+        List<String> fetureList = du.getFetureIntervals(5);
+        for(String fetureDate:fetureList){
+            System.out.println("fetureDate>>"+fetureDate);
+        }
+
     }
 
     private void test1() {

+ 46 - 0
huimv-eartag2-platform/huimv-eartag2-manage/src/main/java/com/huimv/eartag2/manage/controller/DeviceController.java

@@ -106,4 +106,50 @@ public class DeviceController {
         return deviceService.getDeviceRegisterByDeviceCode(deviceCode);
     }
 
+    /**
+     * @Method      : listManyDaysOnlineByDeviceCode
+     * @Description : 查询单个设备N天在线记录-showMode:1(记录) ,2(图表)
+     * @Params      : [deviceCode]
+     * @Return      : com.huimv.eartag2.common.utils.Result
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2022/3/7       
+     * @Time        : 14:07
+     */
+    @RequestMapping(value = "/listDeviceOnlineCountByDeviceCode",method = RequestMethod.GET)
+    public Result listDeviceOnlineCountByDeviceCode(@RequestParam(value = "deviceCode",required = true) String deviceCode,@RequestParam(value = "pastDays",required = true) Integer pastDays,@RequestParam(value = "showMode",required = true) Integer showMode) throws ParseException {
+        log.info("<listManyDaysOnlineByDeviceCode>输入参数 deviceCode>>"+deviceCode);
+        log.info("<listManyDaysOnlineByDeviceCode>输入参数 pastDays>>"+pastDays);
+        log.info("<listManyDaysOnlineByDeviceCode>输入参数 showMode>>"+showMode);
+        if(pastDays == null){
+            pastDays = 7;
+        }
+        if(showMode == null){
+            showMode = 1;
+        }
+        //查询单个设备N天在线记录
+        return deviceService.listDeviceOnlineCountByDeviceCode(deviceCode,pastDays,showMode);
+    }
+
+    /**
+     * @Method      : listDeviceOnlineStatusByDeviceCode
+     * @Description : 显示设备在线状态
+     * @Params      : [deviceCode, pastDays]
+     * @Return      : com.huimv.eartag2.common.utils.Result
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2022/3/7       
+     * @Time        : 15:44
+     */
+    @RequestMapping(value = "/listDeviceOnlineStatusByDeviceCode",method = RequestMethod.GET)
+    public Result listDeviceOnlineStatusByDeviceCode(@RequestParam(value = "deviceCode",required = true) String deviceCode,@RequestParam(value = "pastDays",required = true) Integer pastDays) throws ParseException {
+        log.info("<listManyDaysOnlineByDeviceCode>输入参数 deviceCode>>"+deviceCode);
+        log.info("<listManyDaysOnlineByDeviceCode>输入参数 pastDays>>"+pastDays);
+        if(pastDays == null){
+            pastDays = 7;
+        }
+        //查询单个设备N天在线记录
+        return deviceService.listDeviceOnlineStatusByDeviceCode(deviceCode,pastDays);
+    }
+
 }

+ 6 - 0
huimv-eartag2-platform/huimv-eartag2-manage/src/main/java/com/huimv/eartag2/manage/service/IDeviceService.java

@@ -16,4 +16,10 @@ public interface IDeviceService {
 
     //
     Result getDeviceRegisterByDeviceCode(String deviceCode);
+
+    //查询单个设备N天在线记录
+    Result listDeviceOnlineCountByDeviceCode(String deviceCode, Integer pastDays,Integer showMode) throws ParseException;
+
+    //查询设备在线状态
+    Result listDeviceOnlineStatusByDeviceCode(String deviceCode, Integer pastDays) throws ParseException;
 }

+ 77 - 6
huimv-eartag2-platform/huimv-eartag2-manage/src/main/java/com/huimv/eartag2/manage/service/impl/DeviceServiceImpl.java

@@ -1,5 +1,6 @@
 package com.huimv.eartag2.manage.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.huimv.eartag2.common.dao.entity.EartagDeviceOnlineEntity;
@@ -10,7 +11,6 @@ import com.huimv.eartag2.common.dao.repo.EartagDeviceRegisterRepo;
 import com.huimv.eartag2.common.dao.repo.EartagEartagRegisterRepo;
 import com.huimv.eartag2.common.dao.repo.EartarFarmAllStatusRepo;
 //import com.huimv.eartag2.common.service.IDevice;
-import com.huimv.eartag2.common.service.impl.DeviceImpl;
 import com.huimv.eartag2.common.utils.BizConst;
 import com.huimv.eartag2.common.utils.DateUtil;
 import com.huimv.eartag2.common.utils.Result;
@@ -20,10 +20,8 @@ import com.huimv.eartag2.manage.service.IDeviceService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
-import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.sql.Date;
 import java.sql.Timestamp;
@@ -56,6 +54,79 @@ public class DeviceServiceImpl implements IDeviceService {
     private EartagDeviceOnlineRepo deviceOnlineRepo;
 
 
+    @Override
+    public Result listDeviceOnlineStatusByDeviceCode(String deviceCode, Integer pastDays) throws ParseException {
+        DateUtil du = new DateUtil();
+        DateUtil dateUtil = new DateUtil();
+        String pastDate = dateUtil.getPastDate(pastDays);
+        String todayDate = dateUtil.getTodayDateText();
+        //
+        List<EartagDeviceOnlineEntity> deviceOnlineEntityList = deviceOnlineRepo.getDeviceOnlineByDeviceCode(deviceCode,pastDate,todayDate);
+        if(deviceOnlineEntityList.size()>0){
+            List<String> pastDateList = du.getPastIntervalsASC(pastDays);
+            JSONArray dataJa = new JSONArray();
+            for(String pastDayDate:pastDateList){
+                int compareInt = du.parseDate(pastDayDate).compareTo(du.getTodayDate());
+                JSONObject newJo = new JSONObject();
+                dataJa.add(newJo);
+                newJo.put("time",pastDayDate);
+                boolean ieFound = false;
+                for(EartagDeviceOnlineEntity deviceOnlineEntity:deviceOnlineEntityList)
+                {
+                    int compareTo = du.parseDate(pastDayDate).compareTo(deviceOnlineEntity.getAddDate());
+//                    System.out.println("compareTo>>"+compareTo+" "+du.formatDateText(deviceOnlineEntity.getAddDate()));
+                    if(compareTo == 0){
+                        newJo.put("value",1);
+                        ieFound = true;
+                        break;
+                    }
+                }
+                if(!ieFound){
+                    newJo.put("value",0);
+                }
+            }
+            return new Result(ResultCode.SUCCESS,dataJa);
+        }else{
+            return new Result(BizConst.CODE_DEVICE_ONLINE_NO_EXIST,BizConst.MSG_DEVICE_ONLINE_NO_EXIST,false);
+        }
+    }
+
+    /**
+     * @Method      : listDeviceOnlineByDeviceCode
+     * @Description : 查询单个设备N天在线记录
+     * @Params      : [deviceCode]
+     * @Return      : com.huimv.eartag2.common.utils.Result
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2022/3/7
+     * @Time        : 14:08
+     */
+    @Override
+    public Result listDeviceOnlineCountByDeviceCode(String deviceCode, Integer pastDays,Integer showMode) throws ParseException {
+        DateUtil dateUtil = new DateUtil();
+        String pastDate = dateUtil.getPastDate(pastDays);
+        String todayDate = dateUtil.getTodayDateText();
+        //
+        List<EartagDeviceOnlineEntity> deviceOnlineEntityList = deviceOnlineRepo.getDeviceOnlineByDeviceCode(deviceCode,pastDate,todayDate);
+        if(deviceOnlineEntityList.size()>0){
+            if(showMode == 1){
+                return new Result(ResultCode.SUCCESS,JSONObject.toJSONString(deviceOnlineEntityList));
+            }else{
+                JSONArray dataJa = new JSONArray();
+                for(EartagDeviceOnlineEntity deviceOnlineEntity:deviceOnlineEntityList)
+                {
+                    JSONObject newJo = new JSONObject();
+                    dataJa.add(newJo);
+                    newJo.put("time",dateUtil.formatDateText(deviceOnlineEntity.getAddDate()));
+                    newJo.put("value",deviceOnlineEntity.getEartagTotal());
+                }
+                return new Result(ResultCode.SUCCESS,dataJa.toJSONString());
+            }
+        }else{
+            return new Result(BizConst.CODE_DEVICE_ONLINE_NO_EXIST,BizConst.MSG_DEVICE_ONLINE_NO_EXIST,false);
+        }
+    }
+
     /**
      * @Method      : getDeviceRegisterByDeviceCode
      * @Description : 通过设备编号显示设备注册信息
@@ -71,8 +142,8 @@ public class DeviceServiceImpl implements IDeviceService {
         //
         EartagDeviceRegisterEntity deviceRegisterEntity = deviceRegisterRepo.getDeviceRegister(deviceCode);
         if(deviceRegisterEntity == null){
-            log.error(BizConst.MSG_REGISTER_NO_EXIST);
-            return new Result(BizConst.CODE_REGISTER_NO_EXIST,BizConst.MSG_REGISTER_NO_EXIST,false);
+            log.error(BizConst.MSG_DEVICE_REGISTER_NO_EXIST);
+            return new Result(BizConst.CODE_DEVICE_REGISTER_NO_EXIST,BizConst.MSG_DEVICE_REGISTER_NO_EXIST,false);
         }else{
             return new Result(ResultCode.SUCCESS,JSONObject.toJSONString(deviceRegisterEntity));
         }
@@ -117,7 +188,7 @@ public class DeviceServiceImpl implements IDeviceService {
         //
         List<EartagDeviceOnlineEntity> deviceOnlineEntityList = deviceOnlineRepo.getDeviceOnlineByFarmIdAndCreateDate(farmId,todayDate);
         if(deviceOnlineEntityList.size()==0){
-            return new Result(BizConst.CODE_ONLINE_NO_EXIST,BizConst.MSG_ONLINE_NO_EXIST,false);
+            return new Result(BizConst.CODE_DEVICE_ONLINE_NO_EXIST,BizConst.MSG_DEVICE_ONLINE_NO_EXIST,false);
         }else{
 //            for(EartagDeviceOnlineEntity deviceOnlineEntity:deviceOnlineEntityList){
 //                deviceOnlineEntity.setHeartbeatFirstTime(du.formatDatetimeText());