新火炬后端单体项目初始化代码
cuilei
8 天以前 7fc3a69a9592fde25971055ff0c98eea98188805
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
package org.jeecg.modules.mes.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.base.entity.Factory;
import org.jeecg.modules.base.entity.Shift;
import org.jeecg.modules.base.entity.ShiftGroup;
import org.jeecg.modules.base.service.IFactoryService;
import org.jeecg.modules.base.service.IShiftGroupService;
import org.jeecg.modules.base.service.IShiftService;
import org.jeecg.modules.mes.dto.MesProductionWorkScheduleRequest;
import org.jeecg.modules.mes.service.IMesProductionWorkOrderService;
import org.jeecg.modules.mes.entity.MesProductionWorkOrder;
import org.jeecg.modules.mes.mapper.MesProductionWorkOrderMapper;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * @Description: 排产工单
 * @Author: jeecg-boot
 * @Date:   2025-07-04
 * @Version: V1.0
 */
@Service
public class MesProductionWorkOrderServiceImpl extends ServiceImpl<MesProductionWorkOrderMapper, MesProductionWorkOrder> implements IMesProductionWorkOrderService {
    @Autowired
    private IShiftService shiftService;
    @Autowired
    private IShiftGroupService shiftGroupService;
    @Autowired
    private IFactoryService factoryService;
 
    @Override
    public List<MesProductionWorkOrder> schedule(MesProductionWorkScheduleRequest request) {
        //查询起止日期范围内的排产计划,先排除
        //查询该产线下所有的班次
        Map<String, ShiftGroup> shiftGroupMap = shiftGroupService.list(new LambdaQueryWrapper<ShiftGroup>()
                        .eq(ShiftGroup::getFactoryId, request.getFactoryId())
                        .eq(ShiftGroup::getDelFlag, CommonConstant.DEL_FLAG_0))
                .stream().collect(Collectors.toMap(ShiftGroup::getShiftId, v1 -> v1, (v1, v2) -> v1));
        Factory factory = factoryService.getById(request.getFactoryId());
        Map<String, Shift> shiftNameMap = new HashMap<>();
        List<Shift> shifts = shiftService.list(new LambdaQueryWrapper<Shift>()
                .in(Shift::getId, shiftGroupMap.keySet()));
        shifts.forEach(shift -> shiftNameMap.put(shift.getId(), shift));
 
        LocalDate startDate = request.getStartDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endDate = request.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        // 使用日期范围进行遍历处理
        List<LocalDate> dateRange = Stream.iterate(startDate, date -> date.plusDays(1))
                .limit(ChronoUnit.DAYS.between(startDate, endDate) + 1)
                .collect(Collectors.toList());
        List<MesProductionWorkOrder> newProductionWorkOrderList = CollectionUtil.newArrayList();
 
        for (LocalDate date : dateRange) {
            for (String shiftId : shiftGroupMap.keySet()) {
                Date workOrderDate = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
                //查询该产线、班次在该日期下是否有排产计划
                Optional<MesProductionWorkOrder> optional = list(new LambdaQueryWrapper<MesProductionWorkOrder>()
                        .eq(MesProductionWorkOrder::getWorkOrderDate, workOrderDate)
                        .eq(MesProductionWorkOrder::getFactoryId, request.getFactoryId())
                        .eq(MesProductionWorkOrder::getShiftId, shiftId))
                        .stream().findAny();
                if (!optional.isPresent()) {
                    ShiftGroup shiftGroup = shiftGroupMap.get(shiftId);
                    //没有,生成新排产计划
                    MesProductionWorkOrder mesProductionWorkOrder = new MesProductionWorkOrder()
                            .setFactoryId(factory.getId())
                            .setFactoryCode(factory.getFactoryCode())
                            .setFactoryName(factory.getFactoryName())
                            .setShiftId(shiftId)
                            .setShiftCode(shiftNameMap.get(shiftId).getShiftCode())
                            .setShiftName(shiftNameMap.get(shiftId).getShiftName())
                            .setGroupId(shiftGroup.getId())
                            .setGroupName(shiftGroup.getGroupName())
                            .setWorkOrderDate(workOrderDate);
 
                    newProductionWorkOrderList.add(mesProductionWorkOrder);
                }
            }
        }
        //如果为空,默认给一条,用于手动新增时表格的初始化
        if (newProductionWorkOrderList.isEmpty()) {
            MesProductionWorkOrder mesProductionWorkOrder = new MesProductionWorkOrder()
                    .setFactoryId(factory.getId())
                    .setFactoryCode(factory.getFactoryCode())
                    .setFactoryName(factory.getFactoryName());
            newProductionWorkOrderList.add(mesProductionWorkOrder);
        }
        return newProductionWorkOrderList;
    }
}