Lius
昨天 f73f8cb6753f8d8ab2c689b4640cd2688e6f6f62
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
package org.jeecg.modules.mdc.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.mdc.entity.MdcEquipmentDaySchedule;
import org.jeecg.modules.mdc.mapper.MdcEquipmentDayScheduleMapper;
import org.jeecg.modules.mdc.service.IMdcEquipmentDayScheduleService;
import org.jeecg.modules.mdc.vo.MdcEquipmentDayScheduleVo;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @Author: Lius
 * @CreateTime: 2025-02-26
 * @Description: s
 */
@Service
public class MdcEquipmentDayScheduleServiceImpl extends ServiceImpl<MdcEquipmentDayScheduleMapper, MdcEquipmentDaySchedule> implements IMdcEquipmentDayScheduleService {
 
    @Resource
    private IMdcProductionService mdcProductionService;
 
    @Override
    public IPage<MdcEquipmentDaySchedule> pageList(MdcEquipmentDayScheduleVo mdcEquipmentDayScheduleVo, Page<MdcEquipmentDaySchedule> page) {
        IPage<MdcEquipmentDaySchedule> mdcEquipmentDayScheduleIPage = this.baseMapper.pageList(mdcEquipmentDayScheduleVo, page);
        if (mdcEquipmentDayScheduleIPage.getRecords() != null && !mdcEquipmentDayScheduleIPage.getRecords().isEmpty()) {
            MdcProduction mdcProduction = mdcProductionService.getById(mdcEquipmentDayScheduleVo.getProductionId());
            mdcEquipmentDayScheduleIPage.getRecords().forEach(mdcEquipmentDaySchedule -> {
                mdcEquipmentDaySchedule.setWorkshop(mdcProduction.getProductionName());
                BigDecimal batchNum = new BigDecimal(mdcEquipmentDaySchedule.getBatchNum());
                BigDecimal qualifiedQty = new BigDecimal(mdcEquipmentDaySchedule.getQualifiedQty());
                if (batchNum.compareTo(BigDecimal.ZERO) != 0) {
                    mdcEquipmentDaySchedule.setQualifiedRate(qualifiedQty.multiply(new BigDecimal("100")).divide(batchNum, 2, RoundingMode.HALF_UP));
                }
                if (batchNum.compareTo(qualifiedQty) > 0) {
                    mdcEquipmentDaySchedule.setQualifiedStatus("未完成");
                } else {
                    mdcEquipmentDaySchedule.setQualifiedStatus("已完成");
                }
            });
        }
        return mdcEquipmentDayScheduleIPage;
    }
 
    @Override
    public MdcEquipmentDaySchedule selectLast(String equipmentId) {
        return this.baseMapper.selectLast(equipmentId);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateBatchByMesId(List<MdcEquipmentDaySchedule> entityList) {
        if (CollectionUtils.isEmpty(entityList)) {
            return true;
        }
 
        // 按mesId分组处理
        Map<String, MdcEquipmentDaySchedule> mesIdMap = entityList.stream()
                .collect(Collectors.toMap(MdcEquipmentDaySchedule::getMesId,
                        mdcEquipmentDaySchedule -> mdcEquipmentDaySchedule,       // 值:用户对象本身
                        (existing, replacement) -> existing));
 
        // 批量查询已存在的mesId
        List<MdcEquipmentDaySchedule> existList = this.lambdaQuery()
                .in(MdcEquipmentDaySchedule::getMesId, mesIdMap.keySet())
                .list();
 
        // 分离需要新增和更新的数据
        List<MdcEquipmentDaySchedule> toInsert = new ArrayList<>();
        List<MdcEquipmentDaySchedule> toUpdate = new ArrayList<>();
 
        existList.forEach(exist -> {
            MdcEquipmentDaySchedule newData = mesIdMap.get(exist.getMesId());
            // 保留原ID
            BeanUtils.copyProperties(newData, exist);
            toUpdate.add(exist);
            mesIdMap.remove(exist.getMesId());
        });
 
        toInsert.addAll(mesIdMap.values());
 
        // 执行批量操作
        boolean insertResult = toInsert.isEmpty() || this.saveBatch(toInsert);
        boolean updateResult = toUpdate.isEmpty() || this.updateBatchById(toUpdate);
 
        return insertResult && updateResult;
    }
}