吉安感知网项目-后端
linwei
20 hours ago 08ddfad4530a014094c06fe68b8c2d9a0753403a
drone-service/drone-fw/src/main/java/org/sxkj/fw/device/wrapper/FwDeviceWrapper.java
@@ -21,6 +21,9 @@
import org.sxkj.fw.device.dto.FwDeviceDTO;
import org.sxkj.fw.device.entity.FwDeviceEntity;
import org.sxkj.fw.device.vo.FwDeviceVO;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
/**
@@ -37,12 +40,51 @@
   @Override
   public FwDeviceVO entityVO(FwDeviceEntity fwDevice) {
      return Objects.requireNonNull(BeanUtil.copy(fwDevice, FwDeviceVO.class));
      FwDeviceVO vo = Objects.requireNonNull(BeanUtil.copy(fwDevice, FwDeviceVO.class));
      // 计算维护状态
      vo.setMaintenanceStatus(calculateMaintenanceStatus(fwDevice.getUseDate(), fwDevice.getServiceLife()));
      return vo;
   }
   /**
    * 计算维护状态
    *
    * @param useDate 使用日期
    * @param serviceLife 使用年限(单位:年)
    * @return 维护状态:0-否,1-是(一个月内要到使用年限或已过期)
    */
   private Integer calculateMaintenanceStatus(Date useDate, Integer serviceLife) {
      // 1. 参数校验:如果使用日期或使用年限为空,返回0
      if (useDate == null || serviceLife == null || serviceLife <= 0) {
         return 0;
      }
      // 2. 计算到期日期:使用日期 + 使用年限
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(useDate);
      calendar.add(Calendar.YEAR, serviceLife);
      Date expiryDate = calendar.getTime();
      // 3. 计算当前日期
      Date currentDate = new Date();
      // 4. 如果当前日期已经超过到期日期,返回1(已过期)
      if (currentDate.after(expiryDate)) {
         return 1;
      }
      // 5. 计算距离到期日期的时间差(毫秒)
      long diffInMillis = expiryDate.getTime() - currentDate.getTime();
      // 6. 一个月的毫秒数(按30天计算)
      long oneMonthInMillis = 30L * 24 * 60 * 60 * 1000;
      // 7. 如果距离到期日期在一个月内,返回1;否则返回0
      return diffInMillis <= oneMonthInMillis ? 1 : 0;
   }
   public FwDeviceEntity entityDTO(FwDeviceDTO fwDevice) {
      return Objects.requireNonNull(BeanUtil.copy(fwDevice, FwDeviceEntity.class));
   }
}