zhanghl
2025-04-23 61d2cc8a474fd42f1afb50761fe8de87ce891048
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
package com.aps.core.service.impl;
 
import java.util.*;
import java.util.stream.Collectors;
 
import com.alibaba.fastjson.JSONObject;
import com.aps.common.core.utils.DateUtils;
import com.aps.common.core.utils.bean.BeanUtils;
import com.aps.common.core.web.domain.AjaxResult;
import com.aps.common.security.utils.SecurityUtils;
import com.aps.core.domain.*;
import com.aps.core.mapper.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aps.core.service.IApsPlateProcessShopStatService;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * 钣金车间统计Service业务层处理
 *
 * @author zhl
 * @date 2025-04-23
 */
@Slf4j
@Service
public class ApsPlateProcessShopStatServiceImpl implements IApsPlateProcessShopStatService {
    @Autowired
    private ApsPlateProcessShopStatMapper apsPlateProcessShopStatMapper;
    @Resource
    private ApsPlateProcessStatMapper apsPlateProcessStatMapper;
 
    @Resource
    private ApsShopMapper shopMapper;
 
    @Resource
    private ApsStandardProcessMapper standardProcessMapper;
 
    @Resource
    private ApsPlatePlanMapper apsPlatePlanMapper;
 
 
    /**
     * 查询钣金车间统计
     *
     * @param id 钣金车间统计主键
     * @return 钣金车间统计
     */
    @Override
    public ApsPlateProcessShopStat selectApsPlateProcessShopStatById(Long id) {
        return apsPlateProcessShopStatMapper.selectApsPlateProcessShopStatById(id);
    }
 
    /**
     * 查询钣金车间统计列表
     *
     * @param apsPlateProcessShopStat 钣金车间统计
     * @return 钣金车间统计
     */
    @Override
    public List<ApsPlateProcessShopStat> selectApsPlateProcessShopStatList(ApsPlateProcessShopStat apsPlateProcessShopStat) {
        return apsPlateProcessShopStatMapper.selectApsPlateProcessShopStatList(apsPlateProcessShopStat);
    }
 
    /**
     * 新增钣金车间统计
     *
     * @param apsPlateProcessShopStat 钣金车间统计
     * @return 结果
     */
    @Override
    public int insertApsPlateProcessShopStat(ApsPlateProcessShopStat apsPlateProcessShopStat) {
        apsPlateProcessShopStat.setCreateTime(DateUtils.getNowDate());
        return apsPlateProcessShopStatMapper.insertApsPlateProcessShopStat(apsPlateProcessShopStat);
    }
 
    /**
     * 修改钣金车间统计
     *
     * @param apsPlateProcessShopStat 钣金车间统计
     * @return 结果
     */
    @Override
    public int updateApsPlateProcessShopStat(ApsPlateProcessShopStat apsPlateProcessShopStat) {
        apsPlateProcessShopStat.setUpdateTime(DateUtils.getNowDate());
        return apsPlateProcessShopStatMapper.updateApsPlateProcessShopStat(apsPlateProcessShopStat);
    }
 
    /**
     * 批量删除钣金车间统计
     *
     * @param ids 需要删除的钣金车间统计主键
     * @return 结果
     */
    @Override
    public int deleteApsPlateProcessShopStatByIds(Long[] ids) {
        return apsPlateProcessShopStatMapper.deleteApsPlateProcessShopStatByIds(ids);
    }
 
    /**
     * 删除钣金车间统计信息
     *
     * @param id 钣金车间统计主键
     * @return 结果
     */
    @Override
    public int deleteApsPlateProcessShopStatById(Long id) {
        return apsPlateProcessShopStatMapper.deleteApsPlateProcessShopStatById(id);
    }
 
    /**
     * 保存钣金车间统计
     */
    @Transactional
    @Override
    public void saveShopStat() {
        try {
            // 开始之前先删除所有历史数据
            apsPlateProcessShopStatMapper.deleteAll();
 
            // 定义该功能使用数据源为南通的工厂
            final String plant = "FORTUNA";
 
            // 查询相关数据
            ApsPlatePlan platePlan = new ApsPlatePlan();
            platePlan.setPlant(plant);
            List<ApsPlatePlan> planList = apsPlatePlanMapper.selectApsPlatePlanList(platePlan);
 
            List<ApsPlateProcessStat> statList = apsPlateProcessStatMapper.selectApsPlateProcessStatList(new ApsPlateProcessStat());
 
            ApsShop apsShop = new ApsShop();
            apsShop.setPlantCode(plant);
            List<ApsShop> shopList = shopMapper.selectApsShopList(apsShop);
 
            ApsStandardProcess process = new ApsStandardProcess();
            process.setPlant(plant);
            List<ApsStandardProcess> shopProcesses = standardProcessMapper.selectApsStandardProcessList(process);
            if (planList.isEmpty() || shopList.isEmpty() || shopProcesses.isEmpty()) {
                log.warn("计划列表、车间列表或工序列表为空,无法生成统计信息");
                return;
            }
            // 构建车间名称到工序名称的映射
            Map<String, List<String>> shopToProcessNames = shopProcesses.stream()
                    .collect(Collectors.groupingBy(ApsStandardProcess::getWorkShop,
                            Collectors.mapping(ApsStandardProcess::getProcessName, Collectors.toList())
                    ));
            // 批量插入统计数据
            List<ApsPlateProcessShopStat> statsToInsert = new ArrayList<>();
            for (ApsPlatePlan plan : planList) {
                for (ApsShop shop : shopList) {
                    ApsPlateProcessShopStat stat = createShopStat(plan, shop, shopToProcessNames, statList);
                    statsToInsert.add(stat);
                }
            }
            // 批量插入以提高性能
            if (!statsToInsert.isEmpty()) {
                int batchSize = 1000;
                for (int i = 0; i < statsToInsert.size(); i += batchSize) {
                    int end = Math.min(i + batchSize, statsToInsert.size());
                    List<ApsPlateProcessShopStat> batch = statsToInsert.subList(i, end);
                    apsPlateProcessShopStatMapper.batchInsert(batch);
                }
            }
        } catch (Exception e) {
            log.error("保存钣金车间统计时发生异常", e);
            throw new RuntimeException("保存钣金车间统计失败", e);
        }
    }
 
    /**
     * 创建单个车间统计对象
     */
    private ApsPlateProcessShopStat createShopStat(ApsPlatePlan plan, ApsShop shop,
                                                   Map<String, List<String>> shopToProcessNames, List<ApsPlateProcessStat> statList) {
        String shopName = shop.getShopName();
        ApsPlateProcessShopStat stat = new ApsPlateProcessShopStat();
        stat.setDocNo(plan.getDocumentNumber());
        stat.setShopCode(shop.getShopCode());
        stat.setShopName(shopName);
        stat.setDelFlag("0");
        stat.setCreateBy(SecurityUtils.getUsername());
        stat.setCreateTime(DateUtils.getNowDate());
 
        try {
 
 
            List<String> processNames = shopToProcessNames.getOrDefault(shopName, Collections.emptyList());
            if (!processNames.isEmpty()) {
                // 根据工序名称,查询该工单下所有的工序信息
                List<ApsPlateProcessStat> processStats = statList.stream()
                        .filter(x -> processNames.contains(x.getProcessName()) && x.getWorkOrderNo().equals(plan.getDocumentNumber())
                            &&x.getProcessPlanStartDay()!=null&&x.getProcessPlanEndDay()!=null
                        ).toList();
                if (!processStats.isEmpty()) {
                    // 取出工单下工序的最小开始时间和最大结束时间
                    List<Date> startDayList = processStats.stream().map(ApsPlateProcessStat::getProcessPlanStartDay).filter(Objects::nonNull).toList();
                    if (!startDayList.isEmpty()) {
                        if (startDayList.size() == 1) {
                            stat.setPlanStartDate(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", startDayList.get(0)));
                        } else {
                            Optional<Date> minStartDay = processStats.stream()
                                    .map(ApsPlateProcessStat::getProcessPlanStartDay)
                                    .min(Comparator.naturalOrder());
                            Date date = minStartDay.get();
                            stat.setPlanStartDate(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", date));
                        }
 
 
                    }
                    List<Date> endDayList = processStats.stream().map(ApsPlateProcessStat::getProcessPlanEndDay).filter(Objects::nonNull).toList();
                    if (!endDayList.isEmpty()) {
                        if (endDayList.size() == 1) {
                            stat.setPlanEndDate(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", endDayList.get(0)));
                        } else {
                            Optional<Date> maxEndDay = processStats.stream()
                                    .map(ApsPlateProcessStat::getProcessPlanEndDay)
                                    .max(Comparator.naturalOrder());
                            Date date = maxEndDay.get();
                            stat.setPlanEndDate(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", date));
                        }
 
                    }
 
                }
            }
        } catch (Exception e) {
            log.error("computer error:"+ JSONObject.toJSONString(stat));
            throw new RuntimeException("保存钣金车间统计失败", e);
        }
        return stat;
    }
 
    @Override
    public AjaxResult getShopPlanStat() {
        // 提取硬编码值为常量
        final String PLANT_CODE = "FORTUNA";
 
        // 初始化对象
        ApsPlatePlan platePlan = new ApsPlatePlan();
        platePlan.setPlant(PLANT_CODE);
 
        ApsShop apsShop = new ApsShop();
        apsShop.setPlantCode(PLANT_CODE);
 
        // 获取车间列表并处理空值
        List<String> shopList = Optional.ofNullable(shopMapper.selectApsShopList(apsShop))
                .orElse(Collections.emptyList())
                .stream()
                .map(ApsShop::getShopName)
                .toList();
 
        // 获取计划列表和状态列表
        List<ApsPlatePlan> planList = Optional.ofNullable(apsPlatePlanMapper.selectApsPlatePlanList(platePlan))
                .orElse(Collections.emptyList());
 
        List<ApsPlateProcessShopStat> shopStates = Optional.ofNullable(apsPlateProcessShopStatMapper.selectApsPlateProcessShopStatList(new ApsPlateProcessShopStat()))
                .orElse(Collections.emptyList());
 
        // 提前对 shopStates 按 docNo 分组,减少重复流操作
        Map<String, List<ApsPlateProcessShopStat>> shopStatesByDocNo = shopStates.stream()
                .collect(Collectors.groupingBy(ApsPlateProcessShopStat::getDocNo));
 
        // 构建结果列表
        List<ApsPlateProcessShopPlanStat> shopPlanStats = planList.stream()
                .map(plan -> {
                    ApsPlateProcessShopPlanStat shopPlanStat = new ApsPlateProcessShopPlanStat();
                    BeanUtils.copyProperties(plan, shopPlanStat); // 确保目标对象是合法的单个对象实例
 
                    // 根据 docNo 获取对应的 shopStatList
                    List<ApsPlateProcessShopStat> shopStatList = shopStatesByDocNo.getOrDefault(plan.getDocumentNumber(), Collections.emptyList());
                    shopPlanStat.setDeptPlans(shopStatList);
 
                    return shopPlanStat;
                })
                .toList();
 
        // 构建返回结果
        AjaxResult success = AjaxResult.success(shopPlanStats);
        success.put("shopNames", shopList);
        return success;
    }
 
 
}