李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
package com.chinaztt.mes.plan.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.basic.entity.Part;
import com.chinaztt.mes.basic.mapper.PartMapper;
import com.chinaztt.mes.basic.service.DiscToolService;
import com.chinaztt.mes.plan.dto.OperationTaskProduceDTO;
import com.chinaztt.mes.plan.entity.CustomerOrder;
import com.chinaztt.mes.plan.entity.CustomerOrderProduceTask;
import com.chinaztt.mes.plan.entity.OperationTaskProduce;
import com.chinaztt.mes.plan.excel.OperationTaskProduceData;
import com.chinaztt.mes.plan.mapper.CustomerOrderMapper;
import com.chinaztt.mes.plan.mapper.CustomerOrderProduceTaskMapper;
import com.chinaztt.mes.plan.mapper.OperationTaskProduceMapper;
import com.chinaztt.mes.plan.service.OperationTaskProduceService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 工单产出批次
 *
 * @author xucg
 * @date 2022-07-20
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class OperationTaskProduceServiceImpl extends ServiceImpl<OperationTaskProduceMapper, OperationTaskProduce> implements OperationTaskProduceService {
 
    public static final String SUBMIT_TYPE_MERGE = "01merge";
    public static final String SUBMIT_TYPE_SPLIT = "02split";
 
    private CustomerOrderMapper customerOrderMapper;
    private CustomerOrderProduceTaskMapper customerOrderProduceTaskMapper;
    private PartMapper partMapper;
    private DiscToolService discToolService;
 
    @Override
    public IPage<OperationTaskProduceDTO> getPage(Page page, QueryWrapper<OperationTaskProduceDTO> gen) {
        gen.getExpression().getOrderBy().removeAll(gen.getExpression().getOrderBy());
        return this.baseMapper.getPage(page, gen, null);
    }
 
    /**
     * 导入excel数据
     *
     * @param list
     */
    @Override
    public void importExcel(LinkedList<OperationTaskProduceData> list) {
        //查询当前时间 生成SN号需要
        DateTimeFormatter snFormat = DateTimeFormatter.ofPattern("yyMMdd");
        String snDate = snFormat.format(LocalDateTime.now());
        //查询最新的SN号码
        String otcCustomerOrderNo = list.get(0).getOtcCustomerOrderNo();
        String number = baseMapper.getLastSnNumber(otcCustomerOrderNo.substring(otcCustomerOrderNo.length() - 8) + "%");
        //号码的处理
        int num = 0;
        if (StrUtil.isNotBlank(number)) {
            num = Integer.parseInt(number.substring(8));
        }
        // < 客户订单号, List< 段长数据 >>
        LinkedHashMap<String, List<OperationTaskProduceData>> map = list.stream().collect(Collectors.groupingBy(OperationTaskProduceData::getOtcLineNo,
                LinkedHashMap::new, Collectors.toList()));
        for (String otcLineNo : map.keySet()) {
            List<CustomerOrder> customerOrders = customerOrderMapper.selectList(Wrappers.<CustomerOrder>lambdaQuery()
                    .eq(CustomerOrder::getOtcLineNo, otcLineNo));
            if (customerOrders.size() != 1) {
                throw new RuntimeException("OTC行项号重复或不存在相关OTC行项号的客户订单");
            }
            CustomerOrder customerOrder = customerOrders.get(0);
            if (null == customerOrder.getBuyQtyDue()){
                throw new RuntimeException("当前订单行缺少库存数量,请先判断是否查询销售件号");
            }
            List<OperationTaskProduceData> dataList = map.get(otcLineNo);
            List<OperationTaskProduce> insertList = new ArrayList<>();
            BigDecimal total = BigDecimal.ZERO;
            // 算出已经导入的数据并求和段长
            List<OperationTaskProduce> operationTaskProduceListcheck = baseMapper.selectList(Wrappers.<OperationTaskProduce>lambdaQuery()
                    .eq(OperationTaskProduce::getCustomerOrderId, customerOrder.getId()));
            if (CollectionUtil.isNotEmpty(operationTaskProduceListcheck)) {
                total = total.add(operationTaskProduceListcheck.stream().map(OperationTaskProduce::getQty).reduce(BigDecimal.ZERO, BigDecimal::add));
            }
            for (OperationTaskProduceData operationTaskProduceData : dataList) {
                OperationTaskProduce operationTaskProduce = new OperationTaskProduce();
                operationTaskProduce.setOtcCustomerOrderNo(operationTaskProduceData.getOtcCustomerOrderNo());
                operationTaskProduce.setOtcLineNo(operationTaskProduceData.getOtcLineNo());
                // operationTaskProduce.setCustomerPartSpec(operationTaskProduceData.getCustomerPartSpec());
                operationTaskProduce.setSnNo(otcCustomerOrderNo.substring(otcCustomerOrderNo.length() - 8) + String.format("%03d", num + 1));
                operationTaskProduce.setSelfNo(operationTaskProduceData.getSelfNo());
                operationTaskProduce.setPrint(operationTaskProduceData.getPrint());
                operationTaskProduce.setSection(operationTaskProduceData.getSection());
                operationTaskProduce.setBegining(operationTaskProduceData.getBegining());
                operationTaskProduce.setEnding(operationTaskProduceData.getEnding());
                operationTaskProduce.setDirection(operationTaskProduceData.getDirection());
                operationTaskProduce.setPurpose(operationTaskProduceData.getPurpose());
                operationTaskProduce.setOtcLineNo(customerOrder.getOtcLineNo());
                operationTaskProduce.setEndClassification(operationTaskProduceData.getEndClassification());
                operationTaskProduce.setCompany(operationTaskProduceData.getCompany());
                operationTaskProduce.setCrccNumber(operationTaskProduceData.getCrccNumber());
                operationTaskProduce.setCustomerOrderId(customerOrder.getId());
                operationTaskProduce.setQty(operationTaskProduceData.getQty().divide(new BigDecimal(1000), 6,
                        RoundingMode.HALF_UP));
                operationTaskProduce.setFinishedProductSpecification(operationTaskProduceData.getFinishedProductSpecification());
                operationTaskProduce.setYmd(operationTaskProduceData.getYmd());
                operationTaskProduce.setMeterMark(operationTaskProduceData.getMeterMark());
                operationTaskProduce.setOrderUploadRemark(operationTaskProduceData.getOrderUploadRemark());
                operationTaskProduce.setIsGenerateTask(false);
                operationTaskProduce.setSort(num + 1);
                total = total.add(operationTaskProduceData.getQty().divide(new BigDecimal(1000), 6, RoundingMode.HALF_UP));
                insertList.add(operationTaskProduce);
                num++;
            }
            if (customerOrder.getBuyQtyDue().compareTo(total) != 0) {
                throw new RuntimeException("客户订单号" + customerOrder.getCustomerOrderNo() + " 订单行号 " + customerOrder.getOtcLineNo() + " 数量不一致");
            }
            saveBatch(insertList);
 
        }
    }
 
    /**
     * 导入模板下载
     *
     * @param response
     * @throws IOException
     */
    @Override
    public void export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("工单批次模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        try {
            //新建ExcelWriter
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            //获取sheet0对象
            WriteSheet mainSheet = EasyExcel.writerSheet(0, "导入模板").head(OperationTaskProduceData.class).build();
            //向sheet0写入数据 传入空list这样只导出表头
            List<OperationTaskProduceData> operationTaskProduceDataArrayList = new ArrayList<>();
            excelWriter.write(operationTaskProduceDataArrayList, mainSheet);
            //关闭流
            excelWriter.finish();
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        }
    }
 
    /**
     * 根据id list查询
     *
     * @param ids
     * @return
     */
    @Override
    public Map<Long, List<OperationTaskProduce>> getByIds(List<Long> ids, String idType) {
        Map<Long, List<OperationTaskProduce>> map = new HashMap<>();
        for (Long id : ids) {
            List<OperationTaskProduce> operationTaskProduceList = new ArrayList<>();
            switch (idType) {
                case "customer":
                    operationTaskProduceList = baseMapper.selectList(Wrappers.<OperationTaskProduce>lambdaQuery()
                            .eq(OperationTaskProduce::getCustomerOrderId, id));
                    map.put(id, operationTaskProduceList);
                    break;
                case "mps":
                    operationTaskProduceList = baseMapper.selectList(Wrappers.<OperationTaskProduce>lambdaQuery()
                            .eq(OperationTaskProduce::getMpsId, id));
                    map.put(id, operationTaskProduceList);
                    break;
                case "mo":
                    operationTaskProduceList = baseMapper.selectList(Wrappers.<OperationTaskProduce>lambdaQuery()
                            .eq(OperationTaskProduce::getMoId, id));
                    map.put(id, operationTaskProduceList);
                    break;
                case "operationTask":
                    operationTaskProduceList = baseMapper.selectList(Wrappers.<OperationTaskProduce>lambdaQuery()
                            .eq(OperationTaskProduce::getOperationTaskId, id));
                    break;
            }
            map.put(id, operationTaskProduceList);
        }
        return map;
    }
 
    @Override
    public boolean submit(List<OperationTaskProduceDTO> taskProduces, String type) {
        if (CollectionUtil.isEmpty(taskProduces)) {
            throw new RuntimeException("请选择需要提交的数据");
        }
        List<Long> ids = taskProduces.stream().map(OperationTaskProduceDTO::getId).collect(Collectors.toList());
        synchronized (ids.toString().intern()) {
            List<OperationTaskProduce> operationTaskProduceList = this.baseMapper.selectBatchIds(ids);
            if (CollectionUtil.isNotEmpty(operationTaskProduceList)) {
                // 判断是否存在已生成工单
                for (OperationTaskProduce operationTaskProduce : operationTaskProduceList) {
                    if (operationTaskProduce.getIsGenerateTask() != null && operationTaskProduce.getIsGenerateTask()) {
                        throw new RuntimeException("工单已生成,不能合并提交");
                    }
                }
 
                if (SUBMIT_TYPE_MERGE.equals(type)) {
                    // 判断是否存在销售订单工艺文件不一致
                    List<CustomerOrder> customerOrders  = customerOrderMapper.selectBatchIds(operationTaskProduceList.stream()
                            .map(OperationTaskProduce::getCustomerOrderId).collect(Collectors.toList()));
                    Long count = customerOrders.stream().map(CustomerOrder::getTechnologyDocumentId).distinct().count();
                    if (count > 1) {
                        throw new RuntimeException("存在销售订单工艺文件不一致,不能合并提交");
                    }
 
                    long partCount = taskProduces.stream().map(OperationTaskProduceDTO::getPartId).distinct().count();
                    if (partCount > 1) {
                        throw new RuntimeException("存在零件不一致,不能合并提交");
                    }
                }
 
                String mergeNoPrefix = "H" + operationTaskProduceList.get(0).getOtcCustomerOrderNo().substring(operationTaskProduceList.get(0).getOtcCustomerOrderNo().length() - 8);
                String lastMergeNo = this.customerOrderProduceTaskMapper.selectLastMergeNo(mergeNoPrefix + "%");
                //号码的处理
                int num = 0;
                if (StrUtil.isNotBlank(lastMergeNo)) {
                    num = Integer.parseInt(lastMergeNo.substring(lastMergeNo.length() - 3));
                }
 
                String mergeNo = mergeNoPrefix + String.format("%03d", num + 1);
 
                for (OperationTaskProduce operationTaskProduce : operationTaskProduceList) {
                    CustomerOrderProduceTask produceTask = new CustomerOrderProduceTask();
                    BeanUtil.copyProperties(operationTaskProduce, produceTask);
 
                    taskProduces.stream().filter(taskProduce -> taskProduce.getId().equals(operationTaskProduce.getId())).findFirst().ifPresent(taskProduce -> {
                        produceTask.setOperationTaskProduceId(taskProduce.getId());
                        produceTask.setPartId(taskProduce.getPartId());
                        produceTask.setPartNo(taskProduce.getPartNo());
                        produceTask.setPartName(taskProduce.getPartName());
                        produceTask.setCustomerName(taskProduce.getCustomerName());
                        produceTask.setTechnologyDocumentId(taskProduce.getTechnologyDocumentId());
                    });
 
                    // 合并编号
                    if (SUBMIT_TYPE_MERGE.equals(type)) {
                        produceTask.setMergeSnNo(mergeNo);
                    } else {
                        produceTask.setMergeSnNo(operationTaskProduce.getSnNo());
                    }
 
                    produceTask.setStatus(CustomerOrderProduceTask.STATUS_DRAFT);
                    this.customerOrderProduceTaskMapper.insert(produceTask);
                }
            }
            return this.update(Wrappers.<OperationTaskProduce>lambdaUpdate()
                    .set(OperationTaskProduce::getIsGenerateTask, true)
                    .in(OperationTaskProduce::getId, ids));
        }
    }
 
    @Override
    public boolean batchUpdate(List<OperationTaskProduce> operationTaskProduces) {
        if (CollectionUtil.isEmpty(operationTaskProduces)) {
            throw new RuntimeException("请选择需要修改的数据");
        }
        Integer count = this.baseMapper.selectCount(Wrappers.<OperationTaskProduce>lambdaQuery()
                .in(OperationTaskProduce::getId, operationTaskProduces.stream().map(OperationTaskProduce::getId).collect(Collectors.toList()))
                .eq(OperationTaskProduce::getIsGenerateTask, true));
        if (count > 0) {
            throw new RuntimeException("存在已生成工单的数据,不能修改");
        }
        for (OperationTaskProduce operationTaskProduce : operationTaskProduces) {
            if (StrUtil.isNotBlank(operationTaskProduce.getDiscToolMeasurement())) {
                discToolService.checkMeasurementFormat(operationTaskProduce.getDiscToolMeasurement());
            }
        }
        return this.updateBatchById(operationTaskProduces);
    }
 
    @Override
    public void exportData(HttpServletResponse response, String otcCustomerOrderNo) throws IOException {
        //新建ExcelWriter
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
 
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("产出批次明细导出", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //获取sheet0对象
            WriteSheet mainSheet = EasyExcel.writerSheet(0, "产出批次明细导出").head(OperationTaskProduceDTO.class).build();
            //向sheet0写入数据 传入空list这样只导出表头
            List<OperationTaskProduceDTO> operationTaskProduceDataArrayList = this.baseMapper.getListByOrderNo(otcCustomerOrderNo);
            excelWriter.write(operationTaskProduceDataArrayList, mainSheet);
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }
 
}