云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package cn.iocoder.yudao.module.mes.service.dv.metering;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.api.config.ConfigApi;
import cn.iocoder.yudao.module.mes.controller.admin.dv.metering.vo.MesDvMeteringPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.metering.vo.MesDvMeteringSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.metering.MesDvMeteringDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dv.metering.MesDvMeteringMapper;
import cn.iocoder.yudao.module.mes.service.dv.meteringcheck.MesDvMeteringCheckService;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDate;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.DV_METERING_CODE_DUPLICATE;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.DV_METERING_HAS_CHECK_RECORD;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.DV_METERING_NOT_EXISTS;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.DV_METERING_IMPORT_LIST_IS_EMPTY;
import cn.iocoder.yudao.module.mes.controller.admin.dv.metering.vo.MesDvMeteringImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.metering.vo.MesDvMeteringImportRespVO;
import cn.iocoder.yudao.module.mes.enums.dv.MesDvMeteringStatusEnum;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * MES 计量器具 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesDvMeteringServiceImpl implements MesDvMeteringService {
 
    /**
     * 提醒天数配置 KEY(infra_config)
     */
    private static final String REMIND_DAYS_CONFIG_KEY = "mes.dv-metering.remind-days";
    /**
     * 提醒天数默认值
     */
    private static final int DEFAULT_REMIND_DAYS = 30;
 
    @Resource
    private MesDvMeteringMapper meteringMapper;
    @Resource
    @Lazy // 延迟加载,避免与计量检测记录 Service 循环依赖
    private MesDvMeteringCheckService meteringCheckService;
    @Resource
    private MesDvMeteringRemindService meteringRemindService;
    @Resource
    private ConfigApi configApi;
 
    @Override
    public Long createMetering(MesDvMeteringSaveReqVO createReqVO) {
        // 校验编码唯一
        validateMeteringCodeUnique(null, createReqVO.getCode());
 
        // 插入
        MesDvMeteringDO metering = BeanUtils.toBean(createReqVO, MesDvMeteringDO.class);
        meteringMapper.insert(metering);
        return metering.getId();
    }
 
    @Override
    public void updateMetering(MesDvMeteringSaveReqVO updateReqVO) {
        // 校验存在
        validateMeteringExists(updateReqVO.getId());
        // 校验编码唯一
        validateMeteringCodeUnique(updateReqVO.getId(), updateReqVO.getCode());
 
        // 更新
        MesDvMeteringDO updateObj = BeanUtils.toBean(updateReqVO, MesDvMeteringDO.class);
        meteringMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteMetering(Long id) {
        // 校验存在
        validateMeteringExists(id);
        // 校验无关联检测记录
        if (meteringCheckService.getMeteringCheckCountByMeteringId(id) > 0) {
            throw exception(DV_METERING_HAS_CHECK_RECORD);
        }
 
        // 删除
        meteringMapper.deleteById(id);
    }
 
    @Override
    public void validateMeteringExists(Long id) {
        if (meteringMapper.selectById(id) == null) {
            throw exception(DV_METERING_NOT_EXISTS);
        }
    }
 
    private void validateMeteringCodeUnique(Long id, String code) {
        if (StrUtil.isBlank(code)) {
            return;
        }
        MesDvMeteringDO metering = meteringMapper.selectByCode(code);
        if (metering == null) {
            return;
        }
        if (ObjUtil.notEqual(metering.getId(), id)) {
            throw exception(DV_METERING_CODE_DUPLICATE);
        }
    }
 
    @Override
    public MesDvMeteringDO getMetering(Long id) {
        return meteringMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesDvMeteringDO> getMeteringPage(MesDvMeteringPageReqVO pageReqVO) {
        // 1. 到期状态转换为日期范围,走 DB 过滤
        Integer expireStatus = pageReqVO.getExpireStatus();
        if (expireStatus != null) {
            LocalDate today = LocalDate.now();
            int remindDays = getRemindDays();
            switch (expireStatus) {
                case 1 -> { // 已过期:到期日 < 今天
                    pageReqVO.setNextCheckDateEnd(today.minusDays(1));
                }
                case 2 -> { // 即将到期:到期日在 [今天, 今天+提醒天数]
                    pageReqVO.setNextCheckDateBegin(today);
                    pageReqVO.setNextCheckDateEnd(today.plusDays(remindDays));
                }
                case 3 -> { // 正常:到期日 > 今天+提醒天数
                    pageReqVO.setNextCheckDateBegin(today.plusDays(remindDays + 1));
                }
                default -> {
                }
            }
        }
        // 2. 查询
        PageResult<MesDvMeteringDO> pageResult = meteringMapper.selectPage(pageReqVO);
        // 3. 懒触发到期提醒站内信(best-effort,不影响查询)
        meteringRemindService.sendExpireRemind();
        return pageResult;
    }
 
    @Override
    public int getRemindDays() {
        String value = configApi.getConfigValueByKey(REMIND_DAYS_CONFIG_KEY);
        if (StrUtil.isBlank(value)) {
            return DEFAULT_REMIND_DAYS;
        }
        try {
            return Integer.parseInt(value.trim());
        } catch (NumberFormatException ex) {
            return DEFAULT_REMIND_DAYS;
        }
    }
 
    @Override
    public void updateMeteringCheckInfo(Long meteringId, LocalDate lastCheckDate, LocalDate nextCheckDate) {
        meteringMapper.updateById(new MesDvMeteringDO()
                .setId(meteringId)
                .setLastCheckDate(lastCheckDate)
                .setNextCheckDate(nextCheckDate));
    }
 
    @Override
    public List<MesDvMeteringDO> getMeteringList() {
        return meteringMapper.selectList();
    }
 
    @Override
    public List<MesDvMeteringDO> getMeteringList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return meteringMapper.selectByIds(ids);
    }
 
 
    @Override
    public MesDvMeteringImportRespVO importMeteringList(List<MesDvMeteringImportExcelVO> importList, boolean updateSupport) {
        if (CollUtil.isEmpty(importList)) {
            throw exception(DV_METERING_IMPORT_LIST_IS_EMPTY);
        }
        MesDvMeteringImportRespVO respVO = MesDvMeteringImportRespVO.builder()
                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
                .failureCodes(new LinkedHashMap<>()).build();
        AtomicInteger index = new AtomicInteger(1);
        importList.forEach(importItem -> {
            int currentIndex = index.getAndIncrement();
            String key = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行");
            if (StrUtil.isBlank(importItem.getCode())) {
                respVO.getFailureCodes().put(key, "计量器具编码不能为空");
                return;
            }
            try {
                MesDvMeteringSaveReqVO saveReqVO = BeanUtils.toBean(importItem, MesDvMeteringSaveReqVO.class);
                saveReqVO.setCategory(ObjUtil.defaultIfNull(importItem.getCategory(), 1));
                saveReqVO.setCheckCycleCount(ObjUtil.defaultIfNull(importItem.getCheckCycleCount(), 12));
                saveReqVO.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), MesDvMeteringStatusEnum.IN_USE.getStatus()));
                MesDvMeteringDO exist = meteringMapper.selectByCode(importItem.getCode());
                if (exist == null) {
                    createMetering(saveReqVO);
                    respVO.getCreateCodes().add(saveReqVO.getCode());
                } else if (updateSupport) {
                    saveReqVO.setId(exist.getId());
                    updateMetering(saveReqVO);
                    respVO.getUpdateCodes().add(saveReqVO.getCode());
                } else {
                    respVO.getFailureCodes().put(key, "计量器具编码已存在:" + importItem.getCode());
                }
            } catch (ServiceException e) {
                respVO.getFailureCodes().put(key, e.getMessage());
            }
        });
        return respVO;
    }
 
}