吉安感知网项目-后端
xiebin
2026-01-19 4e48664942bbefe70699ca43d25cbbd56ecc8daa
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 *      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.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.sxkj.common.utils.DateUtils;
import org.sxkj.fw.device.dto.FwDeviceMaintainRecordDTO;
import org.sxkj.fw.device.entity.FwDeviceMaintainPlanEntity;
import org.sxkj.fw.device.entity.FwDeviceMaintainRecordEntity;
import org.sxkj.fw.device.excel.FwDeviceMaintainRecordExcel;
import org.sxkj.fw.device.mapper.FwDeviceMaintainRecordMapper;
import org.sxkj.fw.device.service.IFwDeviceMaintainPlanService;
import org.sxkj.fw.device.service.IFwDeviceMaintainRecordService;
import org.sxkj.fw.device.vo.FwDeviceMaintainRecordVO;
 
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
 
/**
 * 设备维护记录表 服务实现类
 *
 * @author lw
 * @since 2026-01-08
 */
@Service
@AllArgsConstructor
public class FwDeviceMaintainRecordServiceImpl extends BaseServiceImpl<FwDeviceMaintainRecordMapper, FwDeviceMaintainRecordEntity> implements IFwDeviceMaintainRecordService {
 
    private final IFwDeviceMaintainPlanService fwDeviceMaintainPlanService;
 
    @Override
    public FwDeviceMaintainRecordVO selectLastOne(Long planId) {
        return baseMapper.selectLastOne(planId);
    }
 
    @Override
    public IPage<FwDeviceMaintainRecordVO> selectFwDeviceMaintainRecordPage(IPage<FwDeviceMaintainRecordVO> page, FwDeviceMaintainRecordDTO fwDeviceMaintainRecord) {
        return page.setRecords(baseMapper.selectFwDeviceMaintainRecordPage(page, fwDeviceMaintainRecord));
    }
 
    @Override
    public boolean saveOrUpdate(FwDeviceMaintainRecordEntity entity) {
        // 维护时间
        Date maintainTime = entity.getMaintainTime();
 
        // 查询计划的规则
        FwDeviceMaintainPlanEntity fwDeviceMaintainPlan = fwDeviceMaintainPlanService.getById(entity.getPlanId());
        if (null == fwDeviceMaintainPlan) {
            return false;
        }
 
        // 验证维护时间是否符合计划周期
        boolean isInCycle = validateMaintainTimeInCycle(maintainTime, fwDeviceMaintainPlan);
 
        // 在当前周期内修改计划维修状态
        if (isInCycle) {
            updatePlanMaintainStatus(fwDeviceMaintainPlan, maintainTime);
        }
 
        return super.saveOrUpdate(entity);
    }
 
    @Override
    public List<FwDeviceMaintainRecordExcel> exportFwDeviceMaintainRecord(Wrapper<FwDeviceMaintainRecordEntity> queryWrapper) {
        List<FwDeviceMaintainRecordExcel> fwDeviceMaintainRecordList = baseMapper.exportFwDeviceMaintainRecord(queryWrapper);
        return fwDeviceMaintainRecordList;
    }
 
    // region 维修计划状态更新相关
 
    /**
     * 验证维护时间是否在计划周期内
     */
    private boolean validateMaintainTimeInCycle(Date maintainTime, FwDeviceMaintainPlanEntity plan) {
        String planCycleType = plan.getPlanCycleType();
        List<String> planCycleValue = plan.getPlanCycleValue();
 
        for (String ruleValue : planCycleValue) {
            boolean isValid = false;
 
            switch (planCycleType) {
                case "1": // 年度规则
                    isValid = validateAnnualCycle(maintainTime, ruleValue);
                    break;
                case "2": // 月度规则
                    isValid = validateMonthlyCycle(maintainTime, ruleValue);
                    break;
                case "3": // 周度规则
                    isValid = validateWeeklyCycle(maintainTime, ruleValue);
                    break;
            }
 
            if (isValid) {
                return true;
            }
        }
        return false;
    }
 
    /**
     * 验证年度周期
     */
    private boolean validateAnnualCycle(Date maintainTime, String ruleValue) {
        String[] parts = ruleValue.split("月");
        int ruleMonth = Integer.parseInt(parts[0]);
        int ruleDay = Integer.parseInt(parts[1].replace("号", ""));
 
        LocalDateTime[] cycleBounds = DateUtils.calculateAnnualCycle(new Date(), ruleMonth, ruleDay);
        return DateUtils.isMaintainTimeInCycle(maintainTime, cycleBounds[0], cycleBounds[1]);
    }
 
    /**
     * 验证月度周期
     */
    private boolean validateMonthlyCycle(Date maintainTime, String ruleValue) {
        int ruleDay = Integer.parseInt(ruleValue.replace("号", ""));
 
        LocalDateTime[] cycleBounds = DateUtils.calculateMonthlyCycle(new Date(), ruleDay);
        return DateUtils.isMaintainTimeInCycle(maintainTime, cycleBounds[0], cycleBounds[1]);
    }
 
    /**
     * 验证周度周期
     */
    private boolean validateWeeklyCycle(Date maintainTime, String ruleValue) {
        int ruleDayOfWeek = DateUtils.getDayOfWeekNumber(ruleValue);
 
        LocalDateTime[] cycleBounds = DateUtils.calculateWeeklyCycle(new Date(), ruleDayOfWeek);
//        String dateStart = cycleBounds[0].atZone(ZoneId.of("Asia/Shanghai")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        String dateEnd = cycleBounds[1].atZone(ZoneId.of("Asia/Shanghai")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        String dateMaintain = maintainTime.toInstant().atZone(ZoneId.of("Asia/Shanghai")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//
//        log.debug("维护时间: " + dateMaintain + ", 周期开始: " + dateStart + ", 周期结束: " + dateEnd);
        return DateUtils.isMaintainTimeInCycle(maintainTime, cycleBounds[0], cycleBounds[1]);
    }
 
    /**
     * 更新计划维护状态
     */
    private void updatePlanMaintainStatus(FwDeviceMaintainPlanEntity plan, Date maintainTime) {
        FwDeviceMaintainPlanEntity updateEntity = new FwDeviceMaintainPlanEntity();
        updateEntity.setId(plan.getId());
        updateEntity.setMaintainStatus(1); // 维护状态
        updateEntity.setLastMaintainTime(maintainTime); // 最后一次维护时间
        fwDeviceMaintainPlanService.updateById(updateEntity);
    }
 
    // endregion 维修计划状态更新相关
 
}