zhuo
2025-02-22 88b4f398111e6ca81efa7dee369112020dd38557
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.ruoyi.device.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.basic.mapper.StructureItemParameterMapper;
import com.ruoyi.basic.service.ProductService;
import com.ruoyi.device.mapper.DeviceMapper;
import com.ruoyi.device.mapper.DeviceMetricRecordMapper;
import com.ruoyi.device.pojo.DeviceMetricRecord;
import com.ruoyi.device.service.QrShowService;
import com.ruoyi.inspect.mapper.InsOrderMapper;
import com.ruoyi.inspect.service.InsOrderService;
import com.ruoyi.system.service.CustomService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;
 
@Service
public class QrShowServiceImpl implements QrShowService {
 
 
 
    @Autowired
    private DeviceMetricRecordMapper deviceMetricRecordMapper;
 
 
 
    /**
     * 计算启用时长
     * @param activationDate
     * @return
     */
    public double calcUsedYears(LocalDateTime activationDate) {
        if(Objects.isNull(activationDate)){
            return 0;
        }
        BigDecimal defDays = BigDecimal.valueOf(365);
        BigDecimal usedDays = BigDecimal.valueOf(Duration.between(activationDate,LocalDateTime.now()).toDays());
        return usedDays.divide(defDays,2,RoundingMode.HALF_UP).setScale(2,RoundingMode.HALF_UP).doubleValue();
    }
 
    /**
     * 计算距离下次校准日期的天数百分比
     * @param startDate
     * @param endDate
     * @return
     */
    public double calcDeviceNextCheckRatio(Date startDate, Date endDate){
        if(Objects.isNull(startDate) || Objects.isNull(endDate)){
            return 0;
        }
        LocalDateTime startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime endLocalDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        long totalDays = Duration.between(startLocalDate, endLocalDate).toDays();
        long usedDays = Duration.between(startLocalDate, LocalDateTime.now()).toDays();
        BigDecimal calcVal = BigDecimal.valueOf(usedDays).divide(BigDecimal.valueOf(totalDays),2,RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP);
        if(calcVal.compareTo(BigDecimal.ZERO)<0){
            calcVal = BigDecimal.ZERO;
        }else if(calcVal.compareTo(BigDecimal.valueOf(100))>0){
            calcVal = BigDecimal.valueOf(100);
        }
        return calcVal.doubleValue();
    }
 
    /**
     * 查询设备校准/核查记录
     * @param deviceId
     * @param type
     * @return
     */
    public DeviceMetricRecord getDeviceMetricRecord(int deviceId, String type){
        return Optional.ofNullable(
                deviceMetricRecordMapper.selectOne(Wrappers.<DeviceMetricRecord>lambdaQuery()
                .eq(DeviceMetricRecord::getDeviceId, deviceId)
                .eq(DeviceMetricRecord::getType, type)
                .orderByDesc(DeviceMetricRecord::getCreateTime)
                .last("limit 1"))).orElse(new DeviceMetricRecord());
    }
 
    /**
     * 格式化日期
     * @return
     */
    public String formatDate(Date date,String formatter){
        if(Objects.isNull(date)){
            return "";
        }
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime.format(DateTimeFormatter.ofPattern(formatter));
    }
    /**
     * 格式化日期
     * @return
     */
    public String formatDate(LocalDate date,String formatter){
        if(Objects.isNull(date)){
            return "";
        }
        return date.format(DateTimeFormatter.ofPattern(formatter));
    }
 
 
}