/*
|
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
|
*
|
* Redistribution and use in source and binary forms, with or without
|
* modification, are permitted provided that the following conditions are met:
|
*
|
* Redistributions of source code must retain the above copyright notice,
|
* this list of conditions and the following disclaimer.
|
* Redistributions in binary form must reproduce the above copyright
|
* notice, this list of conditions and the following disclaimer in the
|
* documentation and/or other materials provided with the distribution.
|
* Neither the name of the dreamlu.net developer nor the names of its
|
* contributors may be used to endorse or promote products derived from
|
* this software without specific prior written permission.
|
* Author: Chill 庄骞 (smallchill@163.com)
|
*/
|
package org.sxkj.fw.device.wrapper;
|
|
import org.springblade.core.mp.support.BaseEntityWrapper;
|
import org.springblade.core.tool.utils.BeanUtil;
|
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;
|
|
/**
|
* 设备表 包装类,返回视图层所需的字段
|
*
|
* @author aix
|
* @since 2026-01-08
|
*/
|
public class FwDeviceWrapper extends BaseEntityWrapper<FwDeviceEntity, FwDeviceVO> {
|
|
public static FwDeviceWrapper build() {
|
return new FwDeviceWrapper();
|
}
|
|
@Override
|
public FwDeviceVO entityVO(FwDeviceEntity fwDevice) {
|
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));
|
}
|
|
}
|