Browse Source

重构设备详情

zhuoning 3 years ago
parent
commit
94afc2959d

+ 5 - 5
huimv-farm-admin/pom.xml

@@ -81,11 +81,11 @@
             <artifactId>lombok</artifactId>
             <optional>true</optional>
         </dependency>
-        <!--        &lt;!&ndash; JPA &ndash;&gt;-->
-        <!--        <dependency>-->
-        <!--            <groupId>org.springframework.boot</groupId>-->
-        <!--            <artifactId>spring-boot-starter-data-jpa</artifactId>-->
-        <!--        </dependency>-->
+        <!-- JPA -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
+        </dependency>
         <!-- MySQL -->
         <dependency>
             <groupId>mysql</groupId>

+ 77 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/controller/DeviceController.java

@@ -0,0 +1,77 @@
+package com.huimv.admin.device.controller;
+
+import com.huimv.admin.device.service.IDevice;
+import com.huimv.admin.device.utils.DateUtil;
+import com.huimv.common.utils.Result;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.text.ParseException;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@RestController
+@RequestMapping(value = "/farm/device")
+@Slf4j
+public class DeviceController {
+    @Autowired
+    private IDevice iDevice;
+    @Autowired
+    private DateUtil dateUtil;
+
+    //新建设备信息
+    @RequestMapping(value = "/newDevice",method = RequestMethod.GET)
+    public Result saveDevice(@RequestParam(value = "deviceName",required = true) String deviceName, @RequestParam(value = "deviceCode",required = true) String deviceCode, @RequestParam(value = "deviceType",required = true) String deviceType,
+                             @RequestParam(value = "factory",required = true) String factory, @RequestParam(value = "worker",required = true) String worker, @RequestParam(value = "mainParams",required = true) String mainParams,
+                             @RequestParam(value = "state",required = true) Integer state, @RequestParam(value = "record",required = true) String record, @RequestParam(value = "farmId",required = true) Integer farmId){
+        //
+        return iDevice.newDevice(deviceName,deviceCode,deviceType,factory,worker,mainParams,state,record,farmId);
+    }
+
+    //编辑设备信息
+    @RequestMapping(value = "/editDevice",method = RequestMethod.GET)
+    public Result editDevice(@RequestParam(value = "deviceName",required = true) String deviceName, @RequestParam(value = "deviceCode",required = true) String deviceCode, @RequestParam(value = "deviceType",required = true) String deviceType,
+                             @RequestParam(value = "factory",required = true) String factory, @RequestParam(value = "worker",required = true) String worker, @RequestParam(value = "mainParams",required = true) String mainParams,
+                             @RequestParam(value = "state",required = true) Integer state, @RequestParam(value = "record",required = true) String record, @RequestParam(value = "farmId",required = true) Integer farmId,
+                             @RequestParam(value = "id",required = true) Integer id){
+            //
+        return iDevice.editDevice(deviceName,deviceCode,deviceType,factory,worker,mainParams,state,record,farmId,id);
+    }
+
+    //删除设备信息
+    @RequestMapping(value = "/removeDevice",method = RequestMethod.GET)
+    public Result removeDevice(@RequestParam(value = "ids",required = true) String ids, @RequestParam(value = "farmId",required = true) Integer farmId){
+        //
+        return iDevice.removeDevice(ids,farmId);
+    }
+
+    //列表展示设备
+    @RequestMapping(value = "/listDevice",method = RequestMethod.GET)
+    public Result listDevice(@RequestParam(value = "deviceName",required = false) String deviceName, @RequestParam(value = "deviceCode",required = false) String deviceCode, @RequestParam(value = "deviceType",required = false) String deviceType,
+                             @RequestParam(value = "factory",required = false) String factory, @RequestParam(value = "worker",required = false) String worker,
+                             @RequestParam(value = "state",required = false) Integer state, @RequestParam(value = "farmId",required = true) Integer farmId,
+                             @RequestParam(value = "startDate",required = false) String startDate, @RequestParam(value = "endDate",required = false) String endDate,
+                             @RequestParam(value = "pageNo",required = true) Integer pageNo, @RequestParam(value = "pageSize",required = true) Integer pageSize) throws ParseException {
+        //
+        return iDevice.listDevice(deviceName,deviceCode,deviceType,factory,worker,state,farmId,startDate,endDate,pageNo,pageSize);
+    }
+
+    //计算设备总数,设备故障数,故障率
+    @RequestMapping(value = "/getCountDeviceFailureRate",method = RequestMethod.GET)
+    public Result getCountDeviceFailureRate(@RequestParam(value = "farmId",required = true) Integer farmId, @RequestParam(value = "bit",required = true) Integer bit){
+        //
+        return iDevice.getCountDeviceFailureRate(farmId,bit);
+    }
+
+}

+ 188 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/dao/entity/FarmDeviceEntity.java

@@ -0,0 +1,188 @@
+package com.huimv.admin.device.dao.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+@Entity
+@Table(name = "farm_device")
+public class FarmDeviceEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    @Column(name = "device_name")
+    private String deviceName;
+
+    @Column(name = "device_code")
+    private String deviceCode;
+
+    @Column(name = "device_type")
+    private String deviceType;
+
+    @Column(name = "factory")
+    private String factory;
+
+    @Column(name = "worker")
+    private String worker;
+
+    @Column(name = "main_params")
+    private String mainParams;
+
+    @Column(name = "state")
+    private Integer state;
+
+    @Column(name = "record")
+    private String record;
+
+    @Column(name = "last_time")
+    private Timestamp lastTime;
+
+    @Column(name = "up_time")
+    private Timestamp upTime;
+
+    @Column(name = "down_time")
+    private Timestamp downTime;
+
+    @Column(name = "lockin_state")
+    private Integer lockinState;
+
+    @Column(name = "farm_id")
+    private Integer farmId;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setDeviceName(String deviceName) {
+        this.deviceName = deviceName;
+    }
+
+    public String getDeviceName() {
+        return deviceName;
+    }
+
+    public void setDeviceCode(String deviceCode) {
+        this.deviceCode = deviceCode;
+    }
+
+    public String getDeviceCode() {
+        return deviceCode;
+    }
+
+    public void setDeviceType(String deviceType) {
+        this.deviceType = deviceType;
+    }
+
+    public String getDeviceType() {
+        return deviceType;
+    }
+
+    public void setFactory(String factory) {
+        this.factory = factory;
+    }
+
+    public String getFactory() {
+        return factory;
+    }
+
+    public void setWorker(String worker) {
+        this.worker = worker;
+    }
+
+    public String getWorker() {
+        return worker;
+    }
+
+    public void setMainParams(String mainParams) {
+        this.mainParams = mainParams;
+    }
+
+    public String getMainParams() {
+        return mainParams;
+    }
+
+    public void setState(Integer state) {
+        this.state = state;
+    }
+
+    public Integer getState() {
+        return state;
+    }
+
+    public void setRecord(String record) {
+        this.record = record;
+    }
+
+    public String getRecord() {
+        return record;
+    }
+
+    public void setLastTime(Timestamp lastTime) {
+        this.lastTime = lastTime;
+    }
+
+    public Timestamp getLastTime() {
+        return lastTime;
+    }
+
+    public void setUpTime(Timestamp upTime) {
+        this.upTime = upTime;
+    }
+
+    public Timestamp getUpTime() {
+        return upTime;
+    }
+
+    public void setDownTime(Timestamp downTime) {
+        this.downTime = downTime;
+    }
+
+    public Timestamp getDownTime() {
+        return downTime;
+    }
+
+    public void setLockinState(Integer lockinState) {
+        this.lockinState = lockinState;
+    }
+
+    public Integer getLockinState() {
+        return lockinState;
+    }
+
+    public void setFarmId(Integer farmId) {
+        this.farmId = farmId;
+    }
+
+    public Integer getFarmId() {
+        return farmId;
+    }
+
+    @Override
+    public String toString() {
+        return "FarmDeviceEntity{" +
+                "id=" + id + '\'' +
+                "deviceName=" + deviceName + '\'' +
+                "deviceCode=" + deviceCode + '\'' +
+                "deviceType=" + deviceType + '\'' +
+                "factory=" + factory + '\'' +
+                "worker=" + worker + '\'' +
+                "mainParams=" + mainParams + '\'' +
+                "state=" + state + '\'' +
+                "record=" + record + '\'' +
+                "lastTime=" + lastTime + '\'' +
+                "upTime=" + upTime + '\'' +
+                "downTime=" + downTime + '\'' +
+                "lockinState=" + lockinState + '\'' +
+                "farmId=" + farmId + '\'' +
+                '}';
+    }
+}

+ 19 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/dao/repo/FarmDeviceRepo.java

@@ -0,0 +1,19 @@
+package com.huimv.admin.device.dao.repo;
+
+import com.huimv.admin.device.dao.entity.FarmDeviceEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.List;
+
+public interface FarmDeviceRepo extends JpaRepository<FarmDeviceEntity, Integer>, JpaSpecificationExecutor<FarmDeviceEntity> {
+
+    //
+    @Query(nativeQuery = true,value = "SELECT COUNT(*) AS total FROM farm_device WHERE farm_id=?1")
+    List<Object[]> findDeviceTotal(Integer farmId);
+
+    //
+    @Query(nativeQuery = true,value = "SELECT COUNT(*) AS num FROM farm_device WHERE farm_id=?1 AND state=?2")
+    List<Object[]> findFailureDeviceTotal(Integer farmId, Integer failureState);
+}

+ 20 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/service/IDevice.java

@@ -0,0 +1,20 @@
+package com.huimv.admin.device.service;
+
+import com.huimv.common.utils.Result;
+
+public interface IDevice {
+    //
+    Result newDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId);
+
+    //
+    Result editDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId, Integer id);
+
+    //
+    Result removeDevice(String ids, Integer farmId);
+
+    //
+    Result listDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, Integer state, Integer farmId, String startDate, String endDate, Integer pageNo, Integer pageSize);
+
+    //
+    Result getCountDeviceFailureRate(Integer farmId, Integer bit);
+}

+ 209 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/service/impl/DeviceImpl.java

@@ -0,0 +1,209 @@
+package com.huimv.admin.device.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.huimv.admin.device.dao.entity.FarmDeviceEntity;
+import com.huimv.admin.device.dao.repo.FarmDeviceRepo;
+import com.huimv.admin.device.service.IDevice;
+import com.huimv.admin.device.utils.DateUtil;
+import com.huimv.admin.device.utils.MathUtil;
+import com.huimv.common.utils.Result;
+import com.huimv.common.utils.ResultCode;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.stereotype.Service;
+
+import javax.persistence.criteria.Order;
+import javax.persistence.criteria.Predicate;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Service
+public class DeviceImpl implements IDevice {
+    @Autowired
+    private FarmDeviceRepo deviceRepo;
+    @Autowired
+    private DateUtil dateUtil;
+    @Autowired
+    private MathUtil mathUtil;
+
+    /**
+     * @Method : newDevice
+     * @Description : 添加设备
+     * @Params : [deviceName, deviceCode, deviceType, factory, worker, mainParams, state, record, farmId]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/8
+     * @Time : 22:03
+     */
+    @Override
+    public Result newDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId) {
+        FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
+        deviceEntity.setDeviceCode(deviceCode);
+        deviceEntity.setDeviceName(deviceName);
+        deviceEntity.setDeviceType(deviceType);
+        deviceEntity.setFactory(factory);
+        deviceEntity.setFarmId(farmId);
+        deviceEntity.setMainParams(mainParams);
+        deviceEntity.setRecord(record);
+        deviceEntity.setWorker(worker);
+        deviceEntity.setState(state);
+        deviceEntity.setLastTime(new Timestamp(new Date().getTime()));
+        FarmDeviceEntity outFarmDeviceEntity = deviceRepo.saveAndFlush(deviceEntity);
+        return new Result(ResultCode.SUCCESS, outFarmDeviceEntity);
+    }
+
+    /**
+     * @Method : editDevice
+     * @Description : 编辑设备
+     * @Params : [deviceName, deviceCode, deviceType, factory, worker, mainParams, state, record, farmId, id]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/8
+     * @Time : 22:03
+     */
+    @Override
+    public Result editDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, String mainParams, Integer state, String record, Integer farmId, Integer id) {
+
+        Optional<FarmDeviceEntity> optional = deviceRepo.findById(id);
+        if (!optional.isPresent()) {
+            return new Result(10001, "该记录已不存在.", false);
+        }
+        FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
+        deviceEntity.setId(id);
+        deviceEntity.setDeviceCode(deviceCode);
+        deviceEntity.setDeviceName(deviceName);
+        deviceEntity.setDeviceType(deviceType);
+        deviceEntity.setFactory(factory);
+        deviceEntity.setFarmId(farmId);
+        deviceEntity.setMainParams(mainParams);
+        deviceEntity.setRecord(record);
+        deviceEntity.setWorker(worker);
+        deviceEntity.setState(state);
+        deviceEntity.setLastTime(new Timestamp(new Date().getTime()));
+        FarmDeviceEntity outFarmDeviceEntity = deviceRepo.saveAndFlush(deviceEntity);
+        return new Result(ResultCode.SUCCESS, outFarmDeviceEntity);
+    }
+
+    /**
+     * @Method : removeDevice
+     * @Description : 删除设备
+     * @Params : [ids]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/8
+     * @Time : 22:02
+     */
+    @Override
+    public Result removeDevice(String ids, Integer farmId) {
+        String[] idArray = ids.split(",");
+        List idList = new ArrayList<>();
+        for (int a = 0; a < idArray.length; a++) {
+            idList.add(Integer.parseInt(idArray[a]));
+        }
+        deviceRepo.deleteAllByIdInBatch(idList);
+        return new Result(ResultCode.SUCCESS);
+    }
+
+    /**
+     * @Method : listDevice
+     * @Description : 列表查询设备
+     * @Params : [deviceName, deviceCode, deviceType, factory, worker, state, farmId, startDate, endDate, pageNo, pageSize]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/8
+     * @Time : 22:02
+     */
+    @Override
+    public Result listDevice(String deviceName, String deviceCode, String deviceType, String factory, String worker, Integer state, Integer farmId, String startDateText, String endDateText, Integer pageNo, Integer pageSize) {
+        Specification<FarmDeviceEntity> sf = (Specification<FarmDeviceEntity>) (root, criteriaQuery, criteriaBuilder) -> {
+            //
+            List<Predicate> predList = new ArrayList<>();
+            if (null != farmId) {
+                predList.add(criteriaBuilder.equal(root.get("farmId").as(Integer.class), farmId));
+            }
+            if (null != deviceName) {
+                predList.add(criteriaBuilder.equal(root.get("deviceName").as(String.class), deviceName));
+            }
+            if (null != deviceCode) {
+                predList.add(criteriaBuilder.equal(root.get("deviceCode").as(String.class), deviceCode));
+            }
+            if (null != factory) {
+                predList.add(criteriaBuilder.equal(root.get("factory").as(String.class), factory));
+            }
+            if (null != worker) {
+                predList.add(criteriaBuilder.equal(root.get("worker").as(String.class), worker));
+            }
+            if (null != startDateText) {
+                Date startDate = dateUtil.parseDate(startDateText);
+                predList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("lastTime").as(Date.class), startDate));
+            }
+            if (null != endDateText) {
+                Date endDate = dateUtil.parseDatePlus(endDateText);
+                predList.add(criteriaBuilder.lessThanOrEqualTo(root.get("lastTime").as(Date.class), endDate));
+            }
+            if (null != state) {
+                predList.add(criteriaBuilder.equal(root.get("state").as(Integer.class), state));
+            }
+            //
+            Predicate[] pred = new Predicate[predList.size()];
+            Predicate and = criteriaBuilder.and(predList.toArray(pred));
+            criteriaQuery.where(and);
+            //
+            List<Order> orders = new ArrayList<>();
+            orders.add(criteriaBuilder.desc(root.get("id")));
+            return criteriaQuery.orderBy(orders).getRestriction();
+        };
+        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
+        return new Result(ResultCode.SUCCESS, deviceRepo.findAll(sf, pageable));
+    }
+
+    /**
+     * @Method      : getCountDeviceFailureRate
+     * @Description : 计算设备故障率
+     * @Params      : [farmId, bit]
+     * @Return      : com.huimv.common.utils.Result
+     *
+     * @Author      : ZhuoNing
+     * @Date        : 2021/12/8
+     * @Time        : 23:51
+     */
+    @Override
+    public Result getCountDeviceFailureRate(Integer farmId, Integer bit) {
+        if (bit == null || bit == 0) {
+            bit = 2;
+        }
+        //获取设备总数
+        List<Object[]> objList = deviceRepo.findDeviceTotal(farmId);
+        Object[] totalObj = (Object[]) objList.get(0);
+        int total = Integer.parseInt(totalObj[0].toString());
+        //获取有故障设备数
+        Integer failureState = 2;
+        List<Object[]> failureList = deviceRepo.findFailureDeviceTotal(farmId, failureState);
+        Object[] failureObj = (Object[]) failureList.get(0);
+        int num = Integer.parseInt(failureObj[0].toString());
+        if (total == 0) {
+            return new Result(10002, "当前该牧场无任何设备.", false);
+        }
+        String rate = mathUtil.countRate(num, total, bit);
+        JSONObject outJo = new JSONObject();
+        outJo.put("total",total+" 个");
+        outJo.put("num",num+" 个");
+        outJo.put("rate",rate+" %");
+        return new Result(ResultCode.SUCCESS,outJo);
+    }
+
+
+}

+ 321 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/utils/DateUtil.java

@@ -0,0 +1,321 @@
+package com.huimv.admin.device.utils;
+
+import cn.hutool.core.date.DateTime;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import java.sql.Timestamp;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+@Slf4j
+public class DateUtil {
+    /**
+     * 获取今天
+     *
+     * @return String
+     */
+    public String getToday() {
+        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
+    }
+
+    /**
+     * 获取昨天
+     *
+     * @return String
+     */
+    public String getYestoday() {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.DATE, -1);
+        Date time = cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+
+    /**
+     * 获取本月开始日期
+     *
+     * @return String
+     **/
+    public String getMonthStart() {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.MONTH, 0);
+        cal.set(Calendar.DAY_OF_MONTH, 1);
+        Date time = cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+
+    /**
+     * 获取本月最后一天
+     *
+     * @return String
+     **/
+    public String getMonthEnd() {
+        Calendar cal = Calendar.getInstance();
+        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
+        Date time = cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+
+    /**
+     * 获取本周的第一天
+     *
+     * @return String
+     **/
+    public String getWeekStart() {
+        Calendar cal = Calendar.getInstance();
+        cal.add(Calendar.WEEK_OF_MONTH, 0);
+        cal.set(Calendar.DAY_OF_WEEK, 2);
+        Date time = cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+
+    /**
+     * 获取本周的最后一天
+     *
+     * @return String
+     **/
+    public String getWeekEnd() {
+        Calendar cal = Calendar.getInstance();
+        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
+        cal.add(Calendar.DAY_OF_WEEK, 1);
+        Date time = cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+
+    /**
+     * 获取本年的第一天
+     *
+     * @return String
+     **/
+    public String getYearStart() {
+        return new SimpleDateFormat("yyyy").format(new Date()) + "-01-01";
+    }
+
+    /**
+     * 获取本年的最后一天
+     *
+     * @return String
+     **/
+    public String getYearEnd() {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
+        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
+        Date currYearLast = calendar.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast);
+    }
+
+    //Long转换为Date格式
+    public String fromLongToDate(Long time, String format) {
+        SimpleDateFormat sdf = new SimpleDateFormat(format);
+        Date date = new Date(time);
+        return sdf.format(date);
+    }
+
+    public String formatTimestampToDatetime(Timestamp timestamp) {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        return sdf.format(timestamp);
+    }
+
+    public String formatTimestampToDate(Timestamp timestamp) {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.format(timestamp);
+    }
+
+    //格式化本年
+    public String getThisYear() {
+        Calendar cal = Calendar.getInstance();
+        int year = cal.get(Calendar.YEAR);
+        return String.valueOf(year);
+    }
+
+    //格式化本月
+    public String getThisMonth() {
+        Calendar cal = Calendar.getInstance();
+        int month = cal.get(Calendar.MONTH) + 1;
+        if (String.valueOf(month).length() == 1) {
+            return "0" + String.valueOf(month);
+        } else {
+            return String.valueOf(month);
+        }
+    }
+
+    //格式化日期时间
+    public String formatDateTime(String dateText) throws ParseException {
+        if (dateText.indexOf("T") != -1) {
+            dateText = dateText.replace("T", " ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        Date date = sdf.parse(dateText);
+        return sdf.format(date);
+    }
+
+    public String formatDateText(String dateText) throws ParseException {
+        if (dateText.indexOf("T") != -1) {
+            dateText = dateText.replace("T", " ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        Date date = sdf.parse(dateText);
+        return sdf.format(date);
+    }
+
+    public String formatDateText(Date date) throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.format(date);
+    }
+
+    public Date parseDateTime(String dateText) throws ParseException {
+        if (dateText.indexOf("T") != -1) {
+            dateText = dateText.replace("T", " ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        return sdf.parse(dateText);
+    }
+
+    public Date parseDate(String dateText) {
+        if (dateText.indexOf("T") != -1) {
+            dateText = dateText.replace("T", " ");
+        }
+        try {
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+            return sdf.parse(dateText);
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+
+    public Date parseDatePlus(String dateText) {
+        try {
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+            Calendar cd = Calendar.getInstance();
+            cd.setTime(sdf.parse(dateText));
+            cd.add(Calendar.DATE, 1);//增加一天
+            //cd.add(Calendar.MONTH, n);//增加一个月
+            return sdf.parse(sdf.format(cd.getTime()));
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    public Date parseDate2(String dateText) throws ParseException {
+        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", " ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        Date date = sdf.parse(dateText);
+        return date.getTime();
+    }
+
+
+    //
+    public Date getTodayDate() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.parse(sdf.format(new Date()));
+    }
+
+    public String getTodayDateText() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.format(new Date());
+    }
+
+    public String getTodayText() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        return sdf.format(new Date());
+    }
+
+    public String getTodayMissionText() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+        return sdf.format(new Date());
+    }
+
+    public String getStartDateInThisMonth() {
+        DateTime date = cn.hutool.core.date.DateUtil.date();
+        return (cn.hutool.core.date.DateUtil.beginOfMonth(date) + "").substring(0, 10);
+    }
+
+    public String getEndDateInThisMonth() {
+        DateTime date = cn.hutool.core.date.DateUtil.date();
+        return (date + "").substring(0, 10);
+    }
+
+    /**
+     * 获取过去或者未来 任意天内的日期数组
+     *
+     * @param intervals intervals天内
+     * @return 日期数组
+     */
+    public ArrayList<String> test(int intervals) {
+        ArrayList<String> pastDaysList = new ArrayList<>();
+        ArrayList<String> fetureDaysList = new ArrayList<>();
+        for (int i = 0; i < intervals; i++) {
+            pastDaysList.add(getPastDate(i));
+            fetureDaysList.add(getFetureDate(i));
+        }
+        return pastDaysList;
+    }
+
+    /**
+     * 获取过去第几天的日期
+     *
+     * @param past
+     * @return
+     */
+    public String getPastDate(int past) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
+        Date today = calendar.getTime();
+        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+        String result = format.format(today);
+        return result;
+    }
+
+    /**
+     * 获取未来 第 past 天的日期
+     *
+     * @param past
+     * @return
+     */
+    public String getFetureDate(int past) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
+        Date today = calendar.getTime();
+        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+        String result = format.format(today);
+        return result;
+    }
+
+    //重新构建日期
+    public String rebuildDateTime(String text) {
+        return text.substring(0, 4) + "-" + text.substring(4, 6) + "-" + text.substring(6, 8) + " " + text.substring(8, 10) + ":" + text.substring(10, 12) + ":" + text.substring(12, 14);
+    }
+
+    public static void main(String[] args) {
+        DateUtil du = new DateUtil();
+        //
+        du.test1();
+    }
+
+    private void test1() {
+        String text = "20211201104300";
+//        String text = "1234567890abcd";
+        String date = text.substring(0, 4) + "-" + text.substring(4, 6) + "-" + text.substring(6, 8) + " " + text.substring(8, 10) + ":" + text.substring(10, 12) + ":" + text.substring(12, 14);
+        System.out.println("date=" + date);
+    }
+}

+ 68 - 0
huimv-farm-admin/src/main/java/com/huimv/admin/device/utils/MathUtil.java

@@ -0,0 +1,68 @@
+package com.huimv.admin.device.utils;
+
+import org.springframework.stereotype.Component;
+
+import java.text.NumberFormat;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class MathUtil {
+
+    public String formatBit(float num, int bit) {
+        // 创建一个数值格式化对象
+        NumberFormat numberFormat = NumberFormat.getInstance();
+        // 设置精确到小数点后2位
+        numberFormat.setMaximumFractionDigits(bit);
+        return numberFormat.format(num);
+    }
+
+    public String formatBit(float num) {
+        return String.valueOf(num);
+    }
+
+    //计算比率
+    public String countRate(float num, int bit) {
+        // 创建一个数值格式化对象
+        NumberFormat numberFormat = NumberFormat.getInstance();
+        // 设置精确到小数点后2位
+        numberFormat.setMaximumFractionDigits(bit);
+        return numberFormat.format(num * 100);//所占百分比
+    }
+
+    //计算占比
+    public String countRate(float num, float total,int bit) {
+        // 创建一个数值格式化对象
+        NumberFormat numberFormat = NumberFormat.getInstance();
+        // 设置精确到小数点后2位
+        numberFormat.setMaximumFractionDigits(bit);
+        //所占百分比
+        return numberFormat.format((float)  num/ (float)total* 100);
+    }
+
+    //格式化
+    public String format(float num, int bit) {
+        // 创建一个数值格式化对象
+        NumberFormat numberFormat = NumberFormat.getInstance();
+        // 设置精确到小数点后2位
+        numberFormat.setMaximumFractionDigits(bit);
+        return numberFormat.format(num * 100);//所占百分比
+    }
+
+    //格式化
+    public String format(float num, float total,int bit) {
+        // 创建一个数值格式化对象
+        NumberFormat numberFormat = NumberFormat.getInstance();
+        // 设置精确到小数点后2位
+        numberFormat.setMaximumFractionDigits(bit);
+        //所占百分比
+        return numberFormat.format((float)  num/ (float)total* 100);
+    }
+
+}