广丰卷烟厂数采质量分析系统
baoshiwei
17 小时以前 d143af7023cfd4a0ced6f0ecf04ae3b3a06fd1dc
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package org.dromara.qa.md.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.qa.md.domain.bo.BatchCalibrateBo;
import org.dromara.qa.md.domain.bo.BatchConfigBo;
import org.dromara.qa.md.domain.CalibrationRecord;
import org.dromara.qa.md.domain.WeighingBox;
import org.dromara.qa.md.mapper.WeighingBoxMapper;
import org.dromara.qa.md.service.ICalibrationRecordService;
import org.dromara.qa.md.service.IWeighingBoxService;
import org.dromara.qa.md.service.INotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
/**
 * 称重盒子服务实现
 * 
 * @author ruoyi
 * @date 2026-04-09
 */
@Service
public class WeighingBoxServiceImpl extends ServiceImpl<WeighingBoxMapper, WeighingBox> implements IWeighingBoxService
{
    @Autowired
    private WeighingBoxMapper weighingBoxMapper;
 
    @Autowired
    private ICalibrationRecordService calibrationRecordService;
 
    @Autowired
    private INotificationService notificationService;
 
    @Override
    public TableDataInfo<WeighingBox> queryPageList(WeighingBox weighingBox, PageQuery pageQuery) {
        // 检查是否需要按校准状态过滤
        if (weighingBox.getCalibStatus() != null && !weighingBox.getCalibStatus().isEmpty()) {
            // 使用带校准状态过滤的查询
            long total = weighingBoxMapper.selectWeighingBoxCountWithCalibStatus(weighingBox);
            List<WeighingBox> records = weighingBoxMapper.selectWeighingBoxListWithCalibStatus(weighingBox);
            
            // 手动分页
            int pageNum = pageQuery.getPageNum();
            int pageSize = pageQuery.getPageSize();
            int start = (pageNum - 1) * pageSize;
            int end = Math.min(start + pageSize, records.size());
            List<WeighingBox> pageRecords = start < records.size() ? records.subList(start, end) : new ArrayList<>();
 
            // 计算每个盒子的校准状态(保持与前端一致)
            for (WeighingBox box : pageRecords) {
                calculateCalibStatus(box);
            }
            
            // 构建返回结果
            TableDataInfo<WeighingBox> tableDataInfo = new TableDataInfo<>();
            tableDataInfo.setRows(pageRecords);
            tableDataInfo.setTotal(total);
            tableDataInfo.setCode(200);
            tableDataInfo.setMsg("查询成功");
            return tableDataInfo;
        } else {
            // 使用常规分页查询
            Page<WeighingBox> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
            
            // 构建查询条件
            LambdaQueryWrapper<WeighingBox> queryWrapper = new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<>();
            queryWrapper.eq(WeighingBox::getDelFlag, 0);
            
            if (weighingBox.getName() != null && !weighingBox.getName().isEmpty()) {
                queryWrapper.like(WeighingBox::getName, weighingBox.getName())
                        .or().like(WeighingBox::getCode, weighingBox.getName())
                        .or().like(WeighingBox::getDescription, weighingBox.getName())
                        .or().like(WeighingBox::getLocation, weighingBox.getName());
            }
            if (weighingBox.getActiveStatus() != null) {
                queryWrapper.eq(WeighingBox::getActiveStatus, weighingBox.getActiveStatus());
            }
            
            queryWrapper.orderByDesc(WeighingBox::getCreateTime);
            
            // 执行分页查询
            Page<WeighingBox> resultPage = baseMapper.selectPage(page, queryWrapper);
            
            // 计算每个盒子的校准状态
            for (WeighingBox box : resultPage.getRecords()) {
                calculateCalibStatus(box);
            }
            
            return TableDataInfo.build(resultPage);
        }
    }
 
    @Override
    public List<WeighingBox> selectWeighingBoxList(WeighingBox weighingBox)
    {
        List<WeighingBox> list = weighingBoxMapper.selectWeighingBoxList(weighingBox);
        for (WeighingBox box : list)
        {
            calculateCalibStatus(box);
        }
        return list;
    }
 
    @Override
    public int insertWeighingBox(WeighingBox weighingBox)
    {
        // 计算下次校准日期
        if (weighingBox.getLastCalibDate() != null && weighingBox.getCalibCycleDays() != null)
        {
            Date nextCalibDate = DateUtils.addDays(weighingBox.getLastCalibDate(), weighingBox.getCalibCycleDays());
            weighingBox.setNextCalibDate(nextCalibDate);
        }
        return baseMapper.insert(weighingBox);
    }
 
    @Override
    public int updateWeighingBox(WeighingBox weighingBox)
    {
        // 计算下次校准日期
        if (weighingBox.getLastCalibDate() != null && weighingBox.getCalibCycleDays() != null)
        {
            Date nextCalibDate = DateUtils.addDays(weighingBox.getLastCalibDate(), weighingBox.getCalibCycleDays());
            weighingBox.setNextCalibDate(nextCalibDate);
        }
        return baseMapper.updateById(weighingBox);
    }
 
    @Override
    public int deleteWeighingBoxByIds(Long[] boxIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(boxIds));
    }
 
    @Override
    @Transactional
    public int calibrate(Long boxId, Date calibDate, BigDecimal actualWeight, String note)
    {
        WeighingBox box = baseMapper.selectById(boxId);
        if (box == null)
        {
            return 0;
        }
 
        // 保存校准前的状态
        Date prevCalibDate = box.getLastCalibDate();
 
        // 更新盒子信息
        box.setLastCalibDate(calibDate);
        box.setNextCalibDate(DateUtils.addDays(calibDate, box.getCalibCycleDays()));
        baseMapper.updateById(box);
 
        // 计算偏差
        BigDecimal deviation = null;
        BigDecimal deviationPct = null;
        if (actualWeight != null)
        {
            deviation = actualWeight.subtract(box.getWeight());
            if (box.getWeight().compareTo(BigDecimal.ZERO) > 0)
            {
                deviationPct = deviation.divide(box.getWeight(), 3, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
            }
        }
 
        // 创建校准记录
        CalibrationRecord record = new CalibrationRecord();
        record.setTargetType("weighing_box");
        record.setTargetId(boxId);
        record.setTargetCode(box.getCode());
        record.setTargetName(box.getName());
        record.setCalibDate(calibDate);
        record.setCalibCycleDays(box.getCalibCycleDays());
        record.setStandardWeight(box.getWeight());
        record.setActualWeight(actualWeight);
        record.setDeviation(deviation);
        record.setDeviationPct(deviationPct);
        record.setPrevCalibDate(prevCalibDate);
        record.setNextCalibDate(box.getNextCalibDate());
        record.setOperator("admin"); // 实际应该从当前用户获取
        record.setNote(note);
        record.setCreateTime(new Date());
        calibrationRecordService.save(record);
 
        return 1;
    }
 
    @Override
    @Transactional
    public Map<String, Object> batchCalibrate(BatchCalibrateBo batchCalibrateDTO)
    {
        List<Long> boxIds = batchCalibrateDTO.getBoxIds();
        Date calibDate = batchCalibrateDTO.getCalibDate();
        String note = batchCalibrateDTO.getNote();
        List<BatchCalibrateBo.CalibrateItem> items = batchCalibrateDTO.getItems();
 
        Map<Long, BigDecimal> actualWeightMap = new HashMap<>();
        if (items != null)
        {
            for (BatchCalibrateBo.CalibrateItem item : items)
            {
                actualWeightMap.put(item.getBoxId(), item.getActualWeight());
            }
        }
 
        String batchId = UUID.randomUUID().toString();
        List<CalibrationRecord> records = new ArrayList<>();
        List<Map<String, Object>> results = new ArrayList<>();
 
        for (Long boxId : boxIds)
        {
            WeighingBox box = baseMapper.selectById(boxId);
            if (box == null)
            {
                continue;
            }
 
            // 保存校准前的状态
            Date prevCalibDate = box.getLastCalibDate();
 
            // 更新盒子信息
            box.setLastCalibDate(calibDate);
            box.setNextCalibDate(DateUtils.addDays(calibDate, box.getCalibCycleDays()));
            baseMapper.updateById(box);
 
            // 计算偏差
            BigDecimal actualWeight = actualWeightMap.get(boxId);
            BigDecimal deviation = null;
            BigDecimal deviationPct = null;
            if (actualWeight != null)
            {
                deviation = actualWeight.subtract(box.getWeight());
                if (box.getWeight().compareTo(BigDecimal.ZERO) > 0)
                {
                    deviationPct = deviation.divide(box.getWeight(), 3, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
                }
            }
 
            // 创建校准记录
            CalibrationRecord record = new CalibrationRecord();
            record.setTargetType("weighing_box");
            record.setTargetId(boxId);
            record.setTargetCode(box.getCode());
            record.setTargetName(box.getName());
            record.setCalibDate(calibDate);
            record.setCalibCycleDays(box.getCalibCycleDays());
            record.setStandardWeight(box.getWeight());
            record.setActualWeight(actualWeight);
            record.setDeviation(deviation);
            record.setDeviationPct(deviationPct);
            record.setPrevCalibDate(prevCalibDate);
            record.setNextCalibDate(box.getNextCalibDate());
            record.setBatchId(batchId);
            record.setOperator("admin"); // 实际应该从当前用户获取
            record.setNote(note);
            record.setCreateTime(new Date());
            records.add(record);
 
            // 准备返回结果
            Map<String, Object> result = new HashMap<>();
            result.put("boxId", boxId);
            result.put("nextCalibDate", box.getNextCalibDate());
            results.add(result);
        }
 
        // 批量插入校准记录
        if (!records.isEmpty())
        {
            calibrationRecordService.batchInsert(records);
        }
 
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("successCount", boxIds.size());
        resultMap.put("failCount", 0);
        resultMap.put("results", results);
        return resultMap;
    }
 
    @Override
    @Transactional
    public int batchConfig(BatchConfigBo batchConfigDTO)
    {
        Integer calibCycleDays = batchConfigDTO.getCalibCycleDays();
        Integer remindDays = batchConfigDTO.getRemindDays();
        String applyScope = batchConfigDTO.getApplyScope();
        List<Long> boxIds = batchConfigDTO.getBoxIds();
 
        List<Long> targetBoxIds = new ArrayList<>();
        if ("all".equals(applyScope))
        {
            // 所有已启用的盒子
            List<WeighingBox> boxes = weighingBoxMapper.selectActiveWeighingBoxes();
            for (WeighingBox box : boxes)
            {
                targetBoxIds.add(box.getId());
            }
        }
        else if ("selected".equals(applyScope) && boxIds != null)
        {
            targetBoxIds.addAll(boxIds);
        }
 
        if (targetBoxIds.isEmpty())
        {
            return 0;
        }
 
        // 批量更新校准配置
        int result = weighingBoxMapper.batchUpdateCalibConfig(targetBoxIds, calibCycleDays, remindDays);
 
        // 重新计算下次校准日期
        for (Long boxId : targetBoxIds)
        {
            WeighingBox box = baseMapper.selectById(boxId);
            if (box.getLastCalibDate() != null)
            {
                box.setNextCalibDate(DateUtils.addDays(box.getLastCalibDate(), calibCycleDays));
                baseMapper.updateById(box);
            }
        }
 
        return result;
    }
 
    @Override
    public int batchUpdateStatus(List<Long> boxIds, Integer activeStatus)
    {
        return weighingBoxMapper.batchUpdateStatus(boxIds, activeStatus);
    }
 
    @Override
    public Map<String, Object> copyBox(Long sourceId, Integer count)
    {
        WeighingBox sourceBox = baseMapper.selectById(sourceId);
        if (sourceBox == null)
        {
            throw new RuntimeException("源盒子不存在");
        }
        Date nextCalibDate = null;
        List<Long> newIds = new ArrayList<>();
        List<String> newCodes = new ArrayList<>();
// 计算下次校准日期
        if ( sourceBox.getCalibCycleDays() != null)
        {
            nextCalibDate = DateUtils.addDays(new Date(), sourceBox.getCalibCycleDays());
        }
        for (int i = 1; i <= count; i++)
        {
            WeighingBox newBox = new WeighingBox();
            newBox.setName(sourceBox.getName() + "-" + (char)('a' + i - 1));
            newBox.setCode(sourceBox.getCode() + "-" + (char)('a' + i - 1));
            newBox.setWeight(sourceBox.getWeight());
            newBox.setUnit(sourceBox.getUnit());
            newBox.setLocation(sourceBox.getLocation());
            newBox.setCalibCycleDays(sourceBox.getCalibCycleDays());
            newBox.setRemindDays(sourceBox.getRemindDays());
            newBox.setActiveStatus(sourceBox.getActiveStatus());
            newBox.setDescription(sourceBox.getDescription());
            newBox.setLastCalibDate(new Date());
            newBox.setNextCalibDate(nextCalibDate);
            newBox.setCreateBy("admin"); // 实际应该从当前用户获取
            newBox.setCreateTime(new Date());
            baseMapper.insert(newBox);
            newIds.add(newBox.getId());
            newCodes.add(newBox.getCode());
        }
 
        Map<String, Object> result = new HashMap<>();
        result.put("ids", newIds);
        result.put("codes", newCodes);
        return result;
    }
 
    @Override
    public Map<String, Integer> getStatistics()
    {
        Map<String, Integer> statistics = new HashMap<>();
        statistics.put("total", Math.toIntExact(baseMapper.selectCount(null)));
        
        // 已启用
        int activeCount = Math.toIntExact(baseMapper.selectCount(new LambdaQueryWrapper<WeighingBox>()
                .eq(WeighingBox::getActiveStatus, 1)
                .eq(WeighingBox::getDelFlag, 0)));
        statistics.put("active", activeCount);
        
        // 已停用
        int inactiveCount = Math.toIntExact(baseMapper.selectCount(new LambdaQueryWrapper<WeighingBox>()
                .eq(WeighingBox::getActiveStatus, 0)
                .eq(WeighingBox::getDelFlag, 0)));
        statistics.put("inactive", inactiveCount);
 
        // 校准状态统计
        List<WeighingBox> boxes = baseMapper.selectList(new LambdaQueryWrapper<WeighingBox>()
            .eq(WeighingBox::getDelFlag, 0)
            .eq(WeighingBox::getActiveStatus, 1));
        
        int normal = 0, warning = 0, overdue = 0, unset = 0;
        for (WeighingBox box : boxes)
        {
            calculateCalibStatus(box);
            String status = box.getCalibStatus();
            if ("normal".equals(status)) normal++;
            else if ("warning".equals(status)) warning++;
            else if ("overdue".equals(status)) overdue++;
            else if ("unset".equals(status)) unset++;
        }
        
        statistics.put("normal", normal);
        statistics.put("warning", warning);
        statistics.put("overdue", overdue);
        statistics.put("unset", unset);
 
        return statistics;
    }
 
    @Override
    public WeighingBox calculateCalibStatus(WeighingBox weighingBox)
    {
        Date nextCalibDate = weighingBox.getNextCalibDate();
        if (nextCalibDate == null)
        {
            weighingBox.setCalibStatus("unset");
            weighingBox.setCalibDaysLeft(null);
            return weighingBox;
        }
 
        long daysLeft = DateUtils.getTimeDifference(new Date(), nextCalibDate, TimeUnit.DAYS);
        weighingBox.setCalibDaysLeft((int) daysLeft);
 
        if (daysLeft < 0)
        {
            weighingBox.setCalibStatus("overdue");
        }
        else if (daysLeft <= weighingBox.getRemindDays())
        {
            weighingBox.setCalibStatus("warning");
        }
        else
        {
            weighingBox.setCalibStatus("normal");
        }
 
        return weighingBox;
    }
}