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
 
package com.example.erp.service.pp;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.erp.entity.pp.FlowCard;
import com.example.erp.entity.sd.OrderGlassDetail;
import com.example.erp.entity.sd.OrderProcessDetail;
import com.example.erp.mapper.pp.FlowCardMapper;
import com.example.erp.mapper.sd.OrderGlassDetailMapper;
import com.example.erp.mapper.sd.OrderProcessDetailMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.Map;
 
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
 
 
import java.sql.Date;
import java.util.*;
 
import static com.example.erp.service.sd.OrderService.getOrderProcessDetails;
 
@Service
@DS("pp")
public class FlowCardService {
    final
    FlowCardMapper flowCardMapper;
    final
    OrderGlassDetailMapper orderGlassDetailMapper;
    final
    OrderProcessDetailMapper orderProcessDetailMapper;
 
    public FlowCardService(FlowCardMapper flowCardMapper, OrderGlassDetailMapper orderGlassDetailMapper, OrderProcessDetailMapper orderProcessDetailMapper) {
        this.flowCardMapper = flowCardMapper;
        this.orderGlassDetailMapper = orderGlassDetailMapper;
        this.orderProcessDetailMapper = orderProcessDetailMapper;
    }
 
    //流程卡管理查询
    public Map<String, Object> selectProcessCard(Integer pageNum, Integer pageSize, Date selectTime1, Date selectTime2, FlowCard flowCard) {
        Integer offset = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.selectFlowCard(offset, pageSize, selectTime1, selectTime2, flowCard));
        map.put("total", flowCardMapper.getPageTotal(offset, pageSize, selectTime1, selectTime2, flowCard));
        return map;
    }
 
    //分架查询
    public Map<String, Object> selectAddProcess(Date selectTime1, Date selectTime2, FlowCard flowCard) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.selectFlowCardMp(selectTime1, selectTime2, flowCard));
        return map;
    }
 
    //分架明细查询
    public Map<String, Object> detailsSelectSv(String orderId, FlowCard flowCard) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.detailsSelectMp(orderId, flowCard));
        return map;
    }
 
    //删除流程卡
    public Boolean deleteFlowCardSv(String orderId, String processId) {
        if (!orderId.isEmpty() && !processId.isEmpty()) {
            //判断该流程卡是否报工
            Integer count = flowCardMapper.reportingWorkCount(processId);
            if (count == 0) {
 
                //修改分架状态
                flowCardMapper.updateDeleteState(orderId, processId);
                //删除报工流程明细表数据
                flowCardMapper.deleteReportingWork(processId);
                //删除分架明细
                flowCardMapper.deleteFlowCardMp(orderId, processId);
                //判断该订单流程卡是否全部删除
                Integer flowNumber = flowCardMapper.selectFlowCardCount(orderId);
                if (flowNumber == 0) {
                    //修改订单表分架状态为0,全部删除
                    flowCardMapper.updateProcessingCard(orderId, 0);
                } else {
                    //修改订单表分架状态为1,删除部分
                    flowCardMapper.updateProcessingCard(orderId, 1);
                }
                return true;
            } else {
                return false;
            }
 
        } else {
            return false;
        }
    }
 
    //分架新增明细查询
    public Map<String, Object> selectNoCardSv(String orderId, String productionId, FlowCard flowCard) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.selectNoCardMp(orderId, productionId, flowCard));
        return map;
    }
 
 
    //修改排版状态
    public Boolean updateLayoutStatusSv(String processId, Integer state) {
 
        if (!processId.isEmpty()) {
//            Integer Status = flowCardMapper.selectLayoutStatus(processId);
 
            flowCardMapper.updateLayoutStatusMp(processId, state);
 
 
            return true;
        } else {
            return false;
        }
    }
 
    //保存流程卡数据
    public Boolean addFlowCardSv(Map<String, Object> object) {
        String userName = "";
        if (object.get("userName") != null) {
            userName = object.get("userName").toString();
        }
 
        String productionId = "";
        if (object.get("productionId") != null) {
            productionId = object.get("productionId").toString();
        }
 
        List<FlowCard> FlowCardList = JSONArray.parseArray(JSONObject.toJSONString(object.get("flowCard")), FlowCard.class);
        if (!FlowCardList.isEmpty()) {
            for (FlowCard flowCard : FlowCardList) {
                //查询每个序号的层数
                Integer layer = flowCardMapper.selectLayer(productionId, flowCard.getOrderNumber());
                //添加流程卡数据
                flowCardMapper.addFlowCardMp(flowCard.getProcessId(), flowCard.getOrderNumber(), flowCard.getLandingSequence(), flowCard.getQuantity(), productionId, userName, layer);
                //修改分架状态,将状态改为1
                flowCardMapper.updateFlowState(productionId, flowCard.getOrderNumber());
                //查询该订单未分架数量
                Integer FlowCount = flowCardMapper.selectFlowCount(productionId);
                if (FlowCount == 0) {
                    //修改订单表分架状态为2
                    flowCardMapper.updateProcessingCard(productionId, 2);
                } else {
                    //修改订单表分架状态为1,未全部分架完成
                    flowCardMapper.updateProcessingCard(productionId, 1);
                }
                //查询订单小片表获取工艺传入小片工艺表
                List<OrderGlassDetail> orderGlassDetailList =
                        orderGlassDetailMapper.selectList(
                                new QueryWrapper<OrderGlassDetail>()
                                        .eq("order_id", flowCard.getProcessId().substring(0, 10))
                                        .eq("order_number", flowCard.getOrderNumber())
                        );
                List<OrderProcessDetail> orderProcessDetailList = getOrderProcessDetails(orderGlassDetailList);
                orderProcessDetailList.forEach(
                        orderGlassDetail -> orderGlassDetail.setProcessId(flowCard.getProcessId()));
 
 
                //赋值订单工艺表
                orderProcessDetailMapper.insertOrderProcessDetail(orderProcessDetailList);
            }
 
            return true;
        } else {
            return false;
        }
    }
 
    public Map<String, Object> selectSchedulingSv(String selectTime1, String selectTime2, String orderId, String processes, Integer state, FlowCard flowCard) {
        Map<String, Object> map = new HashMap<>();
        if (state == 2) {//已排产
            map.put("data", flowCardMapper.selectOkSchedulingMp(selectTime1, selectTime2, orderId, processes, flowCard));
 
        } else if (state == 1) {//未排产
            map.put("data", flowCardMapper.selectNoSchedulingMp(selectTime1, selectTime2, orderId, processes, flowCard));
 
        }
 
        return map;
    }
 
 
    //首次查询排版数据
    public Map<String, Object> selectLastScheduling(String selectTime1, String selectTime2, FlowCard flowCard) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.selectLastSchedulingMp(selectTime1, selectTime2, flowCard));
        return map;
    }
 
    public Object flowCardDetailSv(String processId, FlowCard flowCard) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.flowCardDetailMp(processId, flowCard));
        return map;
    }
 
    public Object selectPrintFlowCardSv(Date selectTime1, Date selectTime2, String orderId, String project, FlowCard flowCard) {
        if ("null".equals(orderId)) {
            orderId = "";
        }
        if ("null".equals(project)) {
            project = "";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.selectPrintFlowCardMp(selectTime1, selectTime2, orderId, project, flowCard));
        return map;
    }
 
    public Object selectPrintFlowCard(Date selectTime1, Date selectTime2) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.selectPrintFlowCard(selectTime1, selectTime2));
        return map;
    }
 
    public Object selectPrintSv(String orderId, FlowCard flowCard) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.selectPrintMp(orderId, flowCard));
        map.put("type", flowCardMapper.selectType());
        return map;
    }
 
    private static Map<String, Object> parseJson(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    public Map<String, Object> getSelectPrintingSv(Map<String, Object> object) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();//最终结果
        List<FlowCard> flowCardList = JSONArray.parseArray(JSONObject.toJSONString(object.get("printList")), FlowCard.class);
        if (!flowCardList.isEmpty()) {
            for (FlowCard flowCard : flowCardList) {
                Map<String, Object> itemmap = new HashMap<>();
                //流程卡表头表尾数据
                itemmap.put("detail", flowCardMapper.getPrimaryList(flowCard.getProcessId(), flowCard.getTechnologyNumber()));
                itemmap.put("numberList", flowCardMapper.getGlassNumber(flowCard.getTechnologyNumber(),flowCard.getProcessId()));
 
                //流程卡明细数据
                List<Map<String, Object>> detailList = flowCardMapper.getDetailList(flowCard.getProcessId(), flowCard.getTechnologyNumber());
                List<Map<String, Object>> glassList = flowCardMapper.getGlassNumber(flowCard.getTechnologyNumber(),flowCard.getProcessId());
                //工艺流程
                List<Map<String, Object>> processList = flowCardMapper.getProcessList(flowCard.getProcessId(), flowCard.getTechnologyNumber());
                itemmap.put("detailList", detailList);
                itemmap.put("processList", processList);
                list.add(itemmap);
 
            }
        }
        map.put("data", list);
        return map;
    }
 
    public Boolean updateComposingSv(Map<String, Object> object) {
        List<FlowCard> flowCardList = JSONArray.parseArray(JSONObject.toJSONString(object.get("composing")), FlowCard.class);
        if (!flowCardList.isEmpty()) {
            for (FlowCard flowCard : flowCardList) {
                flowCardMapper.updateComposing(flowCard.getProcessId());
            }
            return true;
        }
        else {
            return false;
 
        }
    }
 
    public Map<String, Object> getSelectPrintLabelSv(String projectNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.getPrintLabel(projectNo));
        return map;
    }
 
    public Map<String, Object> getSelectPrintLabelSv1(Map<String, Object> object) {
        Map<String, Object> map = new HashMap<>();
 
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();//最终结果
            List<FlowCard> flowCardList = JSONArray.parseArray(JSONObject.toJSONString(object.get("printList")), FlowCard.class);
            if (!flowCardList.isEmpty()) {
                for (FlowCard flowCard : flowCardList) {
                    // Integer count=flowCardMapper.getPrintLabelCount(flowCard.getProcessId(), flowCard.getTechnologyNumber());
                    Map<String, Object> itemmap = new HashMap<>();
                    // for (int i = 0; i < count; i++) {
                    itemmap.put("data", flowCardMapper.getPrintLabel1(flowCard.getProcessId(), flowCard.getTechnologyNumber()));
                    list.add(itemmap);
                    //}
 
                }
            }
            map.put("data", list);
 
        return map;
    }
 
    public Map<String, Object> printFlowCardDetailsSv(String processId, String technologyNumber, FlowCard flowCard) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.printFlowCardDetailsMp(processId, technologyNumber, flowCard));
        return map;
    }
 
    public Boolean printSortSv(Map<String, Object> object) {
        List<FlowCard> FlowCardList = JSONArray.parseArray(JSONObject.toJSONString(object.get("flowCard")), FlowCard.class);
        if (!FlowCardList.isEmpty()) {
            for (FlowCard flowCard : FlowCardList) {
                flowCardMapper.printSortMp(flowCard.getProcessId(),flowCard.getOrderNumber(),flowCard.getTechnologyNumber(),flowCard.getSort());
            }
        }
        return true;
    }
 
 
    public Map<String, Object> getSelectPrintCustomLabelSv(String type, Integer lableType, Map<String, Object> object) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();//最终结果
        List<FlowCard> flowCardList = JSONArray.parseArray(JSONObject.toJSONString(object.get("printList")), FlowCard.class);
        if (!flowCardList.isEmpty()) {
            Set<String> processedProcessIds = new HashSet<>();  // 用来存放已处理过的 processId
 
            if (lableType != 2){
                for (FlowCard flowCard : flowCardList) {
                    String processId = flowCard.getProcessId();
 
                    // 检查是否已经处理过该 processId,如果处理过则跳过
                    if (processedProcessIds.contains(processId)) {
                        continue;
                    }
                    Map<String, Object> itemmap = new HashMap<>();
                    itemmap.put("data", flowCardMapper.getPrintCustomData(flowCard.getProcessId(),flowCard.getTechnologyNumber()));
                    list.add(itemmap);
 
                    // 将该 processId 加入已处理集合
                    processedProcessIds.add(processId);
                }
            }
            else{
                for (FlowCard flowCard : flowCardList) {
                    Map<String, Object> itemmap = new HashMap<>();
                    itemmap.put("data", flowCardMapper.getPrintCustomData(flowCard.getProcessId(),flowCard.getTechnologyNumber()));
                    list.add(itemmap);
                }
            }
        }
        map.put("data", list);
        map.put("title", flowCardMapper.getPrintTitle(type));
        return map;
    }
 
    public Object getCustomLabelDetailSv(String name, String form, String id, FlowCard flowCard) {
        Map<String, Object> map = new HashMap<>();
        map.put("data", flowCardMapper.getCustomLabelDetailMp(name, form,id, flowCard));
        return map;
    }
 
    public Boolean updatePrintStateSv(Integer printState, Map<String, Object> object) {
        List<FlowCard> flowCardList = JSONArray.parseArray(JSONObject.toJSONString(object.get("printList")), FlowCard.class);
        if (!flowCardList.isEmpty()) {
            for (FlowCard flowCard : flowCardList) {
                // 更新打印状态
                flowCardMapper.updatePrintStateMp(printState,flowCard.getProcessId(),flowCard.getTechnologyNumber());
            }
            return true;
        } else {
            return false;
        }
 
    }
}