zhanghl
2025-05-06 04d2803720568c1e7227e205d0f27cf9b83a2f00
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
package com.aps.core.service.impl;
 
import com.aps.common.core.utils.DateUtils;
import com.aps.common.core.utils.StringUtils;
import com.aps.common.core.utils.uuid.IdUtils;
import com.aps.common.security.utils.SecurityUtils;
import com.aps.core.domain.ApsWeldSeam;
import com.aps.core.domain.ApsWeldSeamStatistics;
import com.aps.core.domain.ApsWeldSeamTemp;
import com.aps.core.mapper.ApsWeldSeamMapper;
import com.aps.core.mapper.ApsWeldSeamStatisticsMapper;
import com.aps.core.mapper.ApsWeldSeamTempMapper;
import com.aps.core.service.IApsWeldSeamService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 焊缝Service业务层处理
 *
 * @author wwj
 * @date 2025-04-09
 */
@Service
public class ApsWeldSeamServiceImpl implements IApsWeldSeamService {
    @Autowired
    private ApsWeldSeamMapper apsWeldSeamMapper;
    @Autowired
    private ApsWeldSeamTempMapper apsWeldSeamTempMapper;
    @Autowired
    private ApsWeldSeamStatisticsMapper apsWeldSeamStatisticsMapper;
 
    /**
     * 查询焊缝
     *
     * @param id 焊缝主键
     * @return 焊缝
     */
    @Override
    public ApsWeldSeam selectApsWeldSeamById(String id) {
        return apsWeldSeamMapper.selectApsWeldSeamById(id);
    }
 
    /**
     * 查询焊缝列表
     *
     * @param apsWeldSeam 焊缝
     * @return 焊缝
     */
    @Override
    public List<ApsWeldSeam> selectApsWeldSeamList(ApsWeldSeam apsWeldSeam) {
        return apsWeldSeamMapper.selectApsWeldSeamList(apsWeldSeam);
    }
 
    /**
     * 新增焊缝
     *
     * @param apsWeldSeam 焊缝
     * @return 结果
     */
    @Override
    public int insertApsWeldSeam(ApsWeldSeam apsWeldSeam) {
        apsWeldSeam.setId(IdUtils.fastUUID());
        apsWeldSeam.setCreateTime(DateUtils.getNowDate());
        return apsWeldSeamMapper.insertApsWeldSeam(apsWeldSeam);
    }
 
    /**
     * 修改焊缝
     *
     * @param apsWeldSeam 焊缝
     * @return 结果
     */
    @Override
    public int updateApsWeldSeam(ApsWeldSeam apsWeldSeam) {
        return apsWeldSeamMapper.updateApsWeldSeam(apsWeldSeam);
    }
 
    /**
     * 批量删除焊缝
     *
     * @param ids 需要删除的焊缝主键
     * @return 结果
     */
    @Override
    public int deleteApsWeldSeamByIds(String[] ids) {
        return apsWeldSeamMapper.deleteApsWeldSeamByIds(ids);
    }
 
    /**
     * 删除焊缝信息
     *
     * @param id 焊缝主键
     * @return 结果
     */
    @Override
    public int deleteApsWeldSeamById(String id) {
        return apsWeldSeamMapper.deleteApsWeldSeamById(id);
    }
 
    @Transactional
    @Override
    public int confirmWeldSeam(ApsWeldSeamTemp apsWeldSeamTemp) {
        //查询临时表数据
        List<ApsWeldSeamTemp> apsWeldSeamTemps = apsWeldSeamTempMapper.selectApsWeldSeamTempList(apsWeldSeamTemp);
        int count = 0;
        String[] ids = new String[apsWeldSeamTemps.size()];
        apsWeldSeamMapper.removeLastBatch();
        for (int i = 0; i < apsWeldSeamTemps.size(); i++) {
            //记录临时表id
            ids[i] = apsWeldSeamTemps.get(i).getId();
            ApsWeldSeam apsWeldSeam = new ApsWeldSeam();
            BeanUtils.copyProperties(apsWeldSeamTemps.get(i), apsWeldSeam);
            //通过工单号查询是否存在数据
            /*ApsWeldSeam apsWeldSeam1 = apsWeldSeamMapper.selectApsWeldSeamByWorkOrderNo(apsWeldSeam.getWorkOrderNo());
            //当工单号存在则更新数据否则插入数据
            if (apsWeldSeam1!=null&&apsWeldSeam1.getId()!=null) {
                apsWeldSeam.setId(apsWeldSeam1.getId());
                apsWeldSeamMapper.updateApsWeldSeam(apsWeldSeam);
            } else {*/
                //插入正式表,并记录
            apsWeldSeam.setId(IdUtils.fastUUID());
            apsWeldSeamMapper.insertApsWeldSeam(apsWeldSeam);
            //}
            count++;
        }
        //插入数量与临时表查询一直则删除临时表数据
        if (count == apsWeldSeamTemps.size()) {
            apsWeldSeamTempMapper.deleteApsWeldSeamTempByIds(ids);
        }
        /*删除旧数据*/
        apsWeldSeamStatisticsMapper.removeLastBatch();
        //插入焊缝统计表
        List<ApsWeldSeamStatistics> apsWeldSeamStatistics = apsWeldSeamMapper.weldSeamStat(apsWeldSeamTemps);
 
        List<Long> years = apsWeldSeamStatistics.stream().map(ApsWeldSeamStatistics::getYear).distinct().toList();
        List<Long> months = apsWeldSeamStatistics.stream().map(ApsWeldSeamStatistics::getMonth).distinct().toList();
        List<String> bases = apsWeldSeamStatistics.stream().map(ApsWeldSeamStatistics::getProductionBase).distinct().toList();
        /*工单类型:订单*/
        String WorkOrderType_Order="0";
        /*工单类型:预测*/
        String WorkOrderType_Prediction="1";
        /*类型:气体*/
        String Classification_Gas="0";
        /*类型:管路*/
        String Classification_Piping="1";
        for (Long year : years) {
            for (Long month : months) {
                for (String basis : bases) {
                    List<ApsWeldSeamStatistics> list = apsWeldSeamStatistics.stream()
                            .filter(x -> x.getYear().equals(year) && x.getMonth().equals(month) && x.getProductionBase().equals(basis)).toList();
 
                    long gasOrderRequirement = list.stream().filter(x -> x.getWorkOrderType().equals(WorkOrderType_Order) && x.getClassification().equals(Classification_Gas))
                            .mapToLong(ApsWeldSeamStatistics::getTotal).sum();
                    long gasPredictionRequirement = list.stream().filter(x -> x.getWorkOrderType().equals(WorkOrderType_Prediction) && x.getClassification().equals(Classification_Gas))
                            .mapToLong(ApsWeldSeamStatistics::getTotal).sum();
                    long pipingOrderRequirement = list.stream().filter(x -> x.getWorkOrderType().equals(WorkOrderType_Order) && x.getClassification().equals(Classification_Piping))
                            .mapToLong(ApsWeldSeamStatistics::getTotal).sum();
                    long pipingPredictionRequirement = list.stream().filter(x -> x.getWorkOrderType().equals(WorkOrderType_Prediction) && x.getClassification().equals(Classification_Piping))
                            .mapToLong(ApsWeldSeamStatistics::getTotal).sum();
                    long total = gasOrderRequirement + gasPredictionRequirement + pipingOrderRequirement + pipingPredictionRequirement;
                    if(total>0) {
                        ApsWeldSeamStatistics seamStatistics = new ApsWeldSeamStatistics();
                        seamStatistics.setId(IdUtils.fastSimpleUUID());
                        seamStatistics.setYear(year);
                        seamStatistics.setMonth(month);
                        seamStatistics.setProductionBase(basis);
                        seamStatistics.setPipingOrderRequirement(pipingOrderRequirement);
                        seamStatistics.setPipingPredictionRequirement(pipingPredictionRequirement);
                        seamStatistics.setGasOrderRequirement(gasOrderRequirement);
                        seamStatistics.setGasPredictionRequirement(gasPredictionRequirement);
                        seamStatistics.setTotal(total);
                        seamStatistics.setCreateTime(DateUtils.getNowDate());
                        seamStatistics.setCreateBy(SecurityUtils.getUsername());
                        apsWeldSeamStatisticsMapper.insertApsWeldSeamStatistics(seamStatistics);
                    }
                }
            }
 
        }
 
 
        /*for (int i = 0; i < apsWeldSeamStatistics.size(); i++) {
            List<ApsWeldSeamStatistics> apsWeldSeamStatistics1 = apsWeldSeamStatisticsMapper.selectApsWeldSeamStatisticsList(apsWeldSeamStatistics.get(i));
            if (apsWeldSeamStatistics1.size() > 0) {
 
                ApsWeldSeamStatistics apsWeldSeamStatistics2 = apsWeldSeamStatistics1.get(0);
 
                apsWeldSeamStatistics2.setPipingOrderRequirement(apsWeldSeamStatistics.get(i).getPipingOrderRequirement());
                apsWeldSeamStatistics2.setGasOrderRequirement(apsWeldSeamStatistics.get(i).getGasOrderRequirement());
                apsWeldSeamStatistics2.setPipingPredictionRequirement(apsWeldSeamStatistics.get(i).getPipingPredictionRequirement());
                apsWeldSeamStatistics2.setGasPredictionRequirement(apsWeldSeamStatistics.get(i).getGasPredictionRequirement());
 
                //预留紧急订单产出是否为空
                if (apsWeldSeamStatistics2.getReserveEmergencyOrderOutput() != null) {
                    apsWeldSeamStatistics2.setTotal(apsWeldSeamStatistics2.getPipingOrderRequirement() + apsWeldSeamStatistics2.getPipingPredictionRequirement() + apsWeldSeamStatistics2.getGasOrderRequirement() + apsWeldSeamStatistics2.getGasPredictionRequirement() + apsWeldSeamStatistics2.getReserveEmergencyOrderOutput());
                    //天数不为空计算需求日焊缝
                    if(apsWeldSeamStatistics2.getDays()!=null){
                        // 转换为 double 类型进行除法运算
                        double result = (double) apsWeldSeamStatistics2.getTotal() / apsWeldSeamStatistics2.getDays();
                        // 四舍五入
                        long roundedResult = Math.round(result);
                        apsWeldSeamStatistics2.setRequirementDayWeldSeam(roundedResult);
                        //判断生产日焊缝不为空计算是否满足
                        if(apsWeldSeamStatistics2.getProductionDayWeldSeam()!=null){
                            if(apsWeldSeamStatistics2.getProductionDayWeldSeam()>=apsWeldSeamStatistics2.getRequirementDayWeldSeam()){
                                apsWeldSeamStatistics2.setIsSatisfy("是");
                            }else{
                                apsWeldSeamStatistics2.setIsSatisfy("否");
                            }
                        }
                    }
                }
                apsWeldSeamStatisticsMapper.updateApsWeldSeamStatistics(apsWeldSeamStatistics2);
            }else {
                apsWeldSeamStatistics.get(i).setId(IdUtils.fastUUID());
                apsWeldSeamStatistics.get(i).setCreateTime(DateUtils.getNowDate());
                apsWeldSeamStatisticsMapper.insertApsWeldSeamStatistics(apsWeldSeamStatistics.get(i));
            }
 
        }*/
        return 1;
    }
 
    @Override
    public List<List<String>> weldSeamEchartsList(ApsWeldSeam apsWeldSeam) {
 
        List<ApsWeldSeam> queryDataList = new ArrayList<>();
        Map<String, Object> params = apsWeldSeam.getParams();
        if(ObjectUtils.isNotEmpty(params)){
            String startDay = params.get("startDay").toString();
            String endDay = params.get("endDay").toString();
            List<String> dateList = getMonthBetweenDate(startDay,endDay);
            dateList.forEach(item->{
                ApsWeldSeam weldSeam = new ApsWeldSeam();
                String queryYear = item.substring(0,4);
                String queryMonth = item.substring(6,7);
                weldSeam.setProduceYear(Long.valueOf(queryYear));
                weldSeam.setProduceMonth(Long.valueOf(queryMonth));
                queryDataList.add(weldSeam);
            });
        }
        List<Map> mapList =apsWeldSeamMapper.weldSeamEcharts(queryDataList);
        Set monthSet=mapList.stream().map(e->e.get("produce_month")).collect(Collectors.toSet());
        List monthList = new ArrayList(monthSet);
        Collections.sort(monthList);
 
        List<List<String>> echartsList = new ArrayList<>();
 
        Set<String> titleSet = new HashSet<>();
        mapList.forEach(item->{
            StringBuilder sb = new StringBuilder();
            sb.append(item.get("production_base"));
            sb.append("-");
            sb.append(item.get("work_order_name"));
            sb.append("-");
            sb.append(item.get("classification_name"));
            titleSet.add(sb.toString());
        });
        List<String> title = new ArrayList<>();
        title.add("product");
        title.addAll(titleSet);
        echartsList.add(title);
        monthList.forEach(month->{
            List<String> table = new ArrayList<>();
            table.add(month+"月");
            for (int i=1;i<title.size();i++){
                for(int j=0;j<mapList.size();j++){
                    Map value =mapList.get(j);
                    StringBuilder sb = new StringBuilder();
                    sb.append(value.get("production_base"));
                    sb.append("-");
                    sb.append(value.get("work_order_name"));
                    sb.append("-");
                    sb.append(value.get("classification_name"));
                    if(sb.toString().equals(title.get(i)) && month==value.get("produce_month")){
                        table.add(value.get("total_weld_seam").toString());
                    }
                }
            }
            echartsList.add(table);
        });
        return echartsList;
    }
 
    public static List<String> getMonthBetweenDate(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // 声明保存日期集合
        List<String> list = new ArrayList<>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
 
            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
 
                // 把日期添加到集合
                list.add(sdf.format(startDate));
 
                // 设置日期
                calendar.setTime(startDate);
 
                //把月数增加 1
                calendar.add(Calendar.MONTH, 1);
 
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}