zss
2024-12-30 97bb7a8832281eafe0ef947ea095258d355e52f5
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package com.yuanchu.mom.service.impl;
 
import cn.hutool.poi.excel.ExcelUtil;
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.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.dto.*;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.StandardProductListMapper;
import com.yuanchu.mom.mapper.StandardTreeMapper;
import com.yuanchu.mom.pojo.*;
import com.yuanchu.mom.service.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
 
/**
 * @author Administrator
 * @description 针对表【standard_tree(标准树)】的数据库操作Service实现
 * @createDate 2024-03-01 15:06:44
 */
@Service
@AllArgsConstructor
public class StandardTreeServiceImpl extends ServiceImpl<StandardTreeMapper, StandardTree>
        implements StandardTreeService {
 
    private StandardTreeMapper standardTreeMapper;
 
    private StandardMethodListService standardMethodListService;
 
    private StandardProductListMapper standardProductListMapper;
 
    private StandardProductListService standardProductListService;
 
    private StandardTemplateService standardTemplateService;
 
    private EnumService enumService;
 
    @Override
    public List<FactoryDto> selectStandardTreeList() {
        List<FactoryDto> factoryDtos = standardTreeMapper.selectStandardTreeList();
        for (FactoryDto factoryDto : factoryDtos) {
            for (LaboratoryDto laboratoryDto : factoryDto.getChildren()) {
                for (SampleTypeDto sampleTypeDto : laboratoryDto.getChildren()) {
                    if (sampleTypeDto.getChildren().size() == 0) {
                        sampleTypeDto.setChildren(standardTreeMapper.getStandardTree3(sampleTypeDto.getValue()));
                    }
                }
            }
        }
        return factoryDtos;
    }
 
    @Override
    public List<SampleTypeDto> selectStandardTreeList2() {
        List<SampleTypeDto> sampleTypeDtos= new ArrayList<>();
        List<FactoryDto> factoryDtos = standardTreeMapper.selectStandardTreeList();
        for (FactoryDto factoryDto : factoryDtos) {
            for (LaboratoryDto laboratoryDto : factoryDto.getChildren()) {
                for (SampleTypeDto sampleTypeDto : laboratoryDto.getChildren()) {
                    if (sampleTypeDto.getChildren().size() == 0) {
                        sampleTypeDto.setChildren(standardTreeMapper.getStandardTree3(sampleTypeDto.getValue()));
                    }
                    sampleTypeDtos.add(sampleTypeDto);
                }
            }
        }
        return sampleTypeDtos;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addStandardTree(StandardTree standardTree) {
        StandardTree tree = standardTreeMapper.selectOne(Wrappers.<StandardTree>lambdaQuery()
                .eq(StandardTree::getFactory, standardTree.getFactory())
                .eq(StandardTree::getLaboratory, standardTree.getLaboratory())
                .eq(StandardTree::getSampleType, standardTree.getSampleType())
                .eq(StandardTree::getSample, standardTree.getSample())
                .eq(StandardTree::getModel, standardTree.getModel()));
        if (tree != null) {
            throw new ErrorException("该型号已存在");
        }
        return standardTreeMapper.insert(standardTree);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delStandardTree(String tree) {
        String[] trees = tree.split(" - ");
        switch (trees.length) {
            case 5:
                if (trees[3] == null) {
                    standardTreeMapper.delete(Wrappers.<StandardTree>lambdaUpdate().eq(StandardTree::getFactory, trees[0]).eq(StandardTree::getLaboratory, trees[1]).eq(StandardTree::getSampleType, trees[2]).isNull(StandardTree::getSample).eq(StandardTree::getModel, trees[4]));
                } else {
                    standardTreeMapper.delete(Wrappers.<StandardTree>lambdaUpdate().eq(StandardTree::getFactory, trees[0]).eq(StandardTree::getLaboratory, trees[1]).eq(StandardTree::getSampleType, trees[2]).eq(StandardTree::getSample, trees[3]).eq(StandardTree::getModel, trees[4]));
                }
                break;
            /*case 4:
                standardTreeMapper.delete(Wrappers.<StandardTree>lambdaUpdate().eq(StandardTree::getFactory, trees[0]).eq(StandardTree::getLaboratory, trees[1]).eq(StandardTree::getSampleType, trees[2]).eq(StandardTree::getSample, trees[3]));
                break;
            case 3:
                standardTreeMapper.delete(Wrappers.<StandardTree>lambdaUpdate().eq(StandardTree::getFactory, trees[0]).eq(StandardTree::getLaboratory, trees[1]).eq(StandardTree::getSampleType, trees[2]));
                break;
            case 2:
                standardTreeMapper.delete(Wrappers.<StandardTree>lambdaUpdate().eq(StandardTree::getFactory, trees[0]).eq(StandardTree::getLaboratory, trees[1]));
                break;
            case 1:
                standardTreeMapper.delete(Wrappers.<StandardTree>lambdaUpdate().eq(StandardTree::getFactory, trees[0]));
                break;*/
        }
        return 1;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addStandardProduct(String ids, String tree) {
        String[] trees = tree.split(" - ");
        JSONArray jsonArray = JSON.parseArray(ids);
        for (Object o : jsonArray) {
            StandardProductList standardProductList = standardTreeMapper.selectStandardProductById(Integer.parseInt("" + o));
            standardProductList.setFactory(trees[0]);
            try {
                standardProductList.setLaboratory(trees[1]);
            } catch (Exception e) {
            }
            try {
                standardProductList.setSampleType(trees[2]);
            } catch (Exception e) {
            }
            try {
                standardProductList.setSample(trees[3]);
            } catch (Exception e) {
            }
            try {
                standardProductList.setModel(trees[4]);
            } catch (Exception e) {
            }
            standardProductListMapper.insert(standardProductList);
        }
        return 1;
    }
 
    @Override
    public List<SampleTypeDto> getStandardTree2() {
        return standardTreeMapper.getStandardTree2();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upStandardProducts(Map<String, Object> product) {
        List<Integer> ids = JSON.parseArray(product.get("ids") + "");
        StandardProductList productList = JSON.parseObject(JSON.toJSONString(product.get("standardProductList")), StandardProductList.class);
        if (productList.getMethodS() != null) {
            standardProductListMapper.update(productList, Wrappers.<StandardProductList>lambdaUpdate().in(StandardProductList::getId, ids).like(StandardProductList::getMethod, productList.getMethodS()));
            return 1;
        }
        standardProductListMapper.update(productList, Wrappers.<StandardProductList>lambdaUpdate().in(StandardProductList::getId, ids));
        return 1;
    }
 
    @Override
    public List<StandardTree> getStandTreeBySampleType(String laboratory, String sampleType) {
        return standardTreeMapper.selectList(Wrappers.<StandardTree>lambdaQuery()
                .eq(StandardTree::getLaboratory, laboratory)
                .eq(StandardTree::getSampleType, sampleType)
                .select(StandardTree::getModel, StandardTree::getSample));
    }
 
    /**
     * 河口检测中心导入
     * @param file
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inExcelOfTree(MultipartFile file) {
        InputStream inputStream;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<StandardProductList> lists = new ArrayList<>();
        AtomicReference<String> laboratory = new AtomicReference<>();
        ExcelUtil.readBySax(inputStream, 0, (i, l, list) -> {
            StandardProductList str = new StandardProductList();
            if (i == 0 && l == 1) {
                laboratory.set(standardTreeMapper.getLaboratory(list.get(1) + ""));
                if (laboratory.get() == null) {
                    throw new ErrorException("检验对象不存在:" + list.get(1));
                }
            }
            if (i == 0 && l >= 1) {
                Integer standardMethodId = standardMethodListService.getStandardMethodId(list.get(0) + "");
                if (standardMethodId == null) {
                    throw new ErrorException("标准编号不存在:" + list.get(0));
                }
                str.setStandardMethodListId(standardMethodId);
                str.setSampleType(list.get(1) + "");
                if (list.get(2) != null) {
                    String sample = baseMapper.selSample(list.get(2) + "");
                    if (sample == null) {
                        throw new ErrorException("样品不存在:" + list.get(2));
                    }
                    str.setSample(list.get(2) + "");
                } else {
                    str.setSample(null);
                }
                if (list.get(3) != null) {
                    str.setModel(list.get(3) + "");
                    Long aLong = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
                            .eq(StandardTree::getModel, str.getModel())
                            .eq(StandardTree::getSampleType, list.get(1) + ""));
                    if (aLong == 0) {
                        StandardTree standardTree = new StandardTree();
                        standardTree.setFactory("中天科技检测中心");
                        standardTree.setLaboratory(laboratory.get());
                        standardTree.setSampleType(str.getSampleType());
                        standardTree.setSample(str.getSample());
                        standardTree.setModel(str.getModel());
                        standardTreeMapper.insert(standardTree);
                    }
                } else {
                    str.setModel(null);
                }
                str.setInspectionItem(list.get(4) + "");
                if (list.get(6) == null) {
                    str.setInspectionItemSubclass("");
                } else {
                    str.setInspectionItemSubclass(list.get(6).toString());
                }
                StandardProductList db_str;
                try {
                    db_str = standardProductListMapper.getOne(str.getStandardMethodListId(), str.getInspectionItem(), str.getSample(), str.getInspectionItemSubclass(), str.getModel(), str.getInspectionItemClass());
                } catch (Exception e) {
                    throw new ErrorException("重复查询:" + str.getInspectionItem() + " " + str.getInspectionItemSubclass());
                }
                if (ObjectUtils.isNotEmpty(db_str)) {
                    str.setId(db_str.getId());
                    str.setStructureItemParameterId(db_str.getStructureItemParameterId());
                }
                if (list.get(8) != null) {
                    str.setMethodS(list.get(8) + "");
                }
 
                if (list.get(11) == null) {
                    str.setTell(null);
                } else {
                    str.setTell(list.get(11).toString());
                }
 
                if (list.get(12) == null) {
                    str.setAsk(null);
                } else {
                    str.setAsk(list.get(12).toString());
                }
 
                if (list.get(13) == null) {
                    str.setPrice(null);
                } else {
                    str.setPrice((list.get(13).toString()));
                }
 
                if (list.get(14) == null) {
                    str.setManHour(null);
                } else {
                    str.setManHour(list.get(14).toString());
                }
 
                StandardTemplate standTempIdByName = standardTemplateService.getStandTempIdByName(String.valueOf(list.get(21)));
                if (standTempIdByName != null) {
                    str.setTemplateId(standTempIdByName.getId());
                } else {
                    throw new ErrorException("模板不存在:" + list.get(21));
                }
                str.setFactory("中天科技检测中心");
                str.setLaboratory(laboratory.get());
                str.setState(1);
                str.setTree(str.getFactory() + " - " + str.getLaboratory() + " - " + str.getSampleType() + " - " + str.getSample() + " - " + (str.getModel() == null ? "" : str.getModel()));
                try {
                    str.setStructureItemParameterId(standardTreeMapper.getStructureItemParameterId("\"" + str.getSampleType() + "\"", str.getInspectionItem(), str.getInspectionItemSubclass(), str.getInspectionItemClass()));
                } catch (Exception e) {
                    str.setStructureItemParameterId(standardTreeMapper.getStructureItemParameterId("\"" + str.getSampleType() + "\",\"" + str.getSample() + "\"", str.getInspectionItem(), str.getInspectionItemSubclass(), str.getInspectionItemClass()));
                }
                if (str.getStructureItemParameterId() == null) {
                    throw new ErrorException("检验项目不存在:" + str.getInspectionItem() + " " + str.getInspectionItemSubclass());
                }
                lists.add(str);
            }
        });
        lists.forEach(a -> {
            if (a.getId() != null) {
                standardProductListMapper.updateById(a);
            } else {
                standardProductListMapper.insert(a);
            }
        });
    }
 
    @Override
    public void importWorkstationExcel(MultipartFile file) {
        InputStream inputStream;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<StandardProductList> lists = new ArrayList<>();
        AtomicReference<String> laboratory = new AtomicReference<>();
        ExcelUtil.readBySax(inputStream, 0, (i, l, list) -> {
            StandardProductList str = new StandardProductList();
            if (i == 0 && l == 1) {
                laboratory.set(standardTreeMapper.getLaboratory(list.get(1) + ""));
                if (laboratory.get() == null) {
                    throw new ErrorException("检验对象不存在:" + list.get(1));
                }
            }
            if (i == 0 && l >= 1) {
                Integer standardMethodId = standardMethodListService.getStandardMethodId(list.get(0) + "");
                if (standardMethodId == null) {
                    throw new ErrorException("标准编号不存在:" + list.get(0));
                }
                str.setStandardMethodListId(standardMethodId);
                str.setSampleType(list.get(1) + "");
                if (list.get(2) != null) {
                    String sample = baseMapper.selSample(list.get(2) + "");
                    if (sample == null) {
                        throw new ErrorException("样品不存在:" + list.get(2));
                    }
                    str.setSample(list.get(2) + "");
                } else {
                    str.setSample(null);
                }
                if (list.get(3) != null) {
                    str.setModel(list.get(3) + "");
                    Long aLong = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
                            .eq(StandardTree::getModel, str.getModel())
                            .eq(StandardTree::getSampleType, list.get(1) + ""));
                    if (aLong == 0) {
                        StandardTree standardTree = new StandardTree();
                        standardTree.setFactory("中天科技检测中心");
                        standardTree.setLaboratory(laboratory.get());
                        standardTree.setSampleType(str.getSampleType());
                        standardTree.setSample(str.getSample());
                        standardTree.setModel(str.getModel());
                        standardTreeMapper.insert(standardTree);
                    }
                } else {
                    str.setModel(null);
                }
                str.setInspectionItemClass(list.get(4).toString());
                str.setInspectionItemClassEn(list.get(5).toString());
                // 开始加2
                str.setInspectionItem(list.get(6) + "");
                if (list.get(8) == null) {
                    str.setInspectionItemSubclass("");
                } else {
                    str.setInspectionItemSubclass(list.get(8).toString());
                }
                StandardProductList db_str;
                try {
                    db_str = standardProductListMapper.getOne(str.getStandardMethodListId(), str.getInspectionItem(), str.getSample(), str.getInspectionItemSubclass(), str.getModel(), str.getInspectionItemClass());
                } catch (Exception e) {
                    throw new ErrorException("重复查询:" + str.getInspectionItem() + " " + str.getInspectionItemSubclass());
                }
                if (ObjectUtils.isNotEmpty(db_str)) {
                    str.setId(db_str.getId());
                    str.setStructureItemParameterId(db_str.getStructureItemParameterId());
                }
                if (list.get(10) != null) {
                    str.setMethodS(list.get(10) + "");
                }
                if (list.get(13) == null) {
                    str.setTell(null);
                } else {
                    str.setTell(list.get(13).toString());
                }
 
                if (list.get(14) == null) {
                    str.setAsk(null);
                } else {
                    str.setAsk(list.get(14).toString());
                }
 
                if (list.get(15) == null) {
                    str.setPrice(null);
                } else {
                    str.setPrice((list.get(15).toString()));
                }
 
                if (list.get(16) == null) {
                    str.setManHour(null);
                } else {
                    str.setManHour(list.get(16).toString());
                }
 
                StandardTemplate standTempIdByName = standardTemplateService.getStandTempIdByName(String.valueOf(list.get(23)));
                if (standTempIdByName != null) {
                    str.setTemplateId(standTempIdByName.getId());
                } else {
                    throw new ErrorException("模板不存在:" + list.get(23));
                }
                str.setFactory("中天科技检测中心");
                str.setLaboratory(laboratory.get());
                str.setState(1);
                str.setTree(str.getFactory() + " - " + str.getLaboratory() + " - " + str.getSampleType() + " - " + str.getSample() + " - " + (str.getModel() == null ? "" : str.getModel()));
                try {
                    str.setStructureItemParameterId(standardTreeMapper.getStructureItemParameterId("\"" + str.getSampleType() + "\"", str.getInspectionItem(), str.getInspectionItemSubclass(), str.getInspectionItemClass()));
                } catch (Exception e) {
                    str.setStructureItemParameterId(standardTreeMapper.getStructureItemParameterId("\"" + str.getSampleType() + "\",\"" + str.getSample() + "\"", str.getInspectionItem(), str.getInspectionItemSubclass(), str.getInspectionItemClass()));
                }
                if (str.getStructureItemParameterId() == null) {
                    throw new ErrorException("检验项目不存在:" + str.getInspectionItem() + " " + str.getInspectionItemSubclass());
                }
                lists.add(str);
            }
        });
        lists.forEach(a -> {
            if (a.getId() != null) {
                standardProductListMapper.updateById(a);
            } else {
                standardProductListMapper.insert(a);
            }
        });
    }
 
    //导出
    @Override
    public void exportStandTree(HttpServletResponse response) throws IOException  {
        //查询标准库数据,按照产品大类分类
        List<standardTreeDto> standardTreeDtos = standardTreeMapper.exportStandTree();
        Map<String, List<standardTreeDto>> collect = standardTreeDtos.stream().collect(Collectors.groupingBy(standardTreeDto -> standardTreeDto.getSampleType()));
        List<Map.Entry<String, List<standardTreeDto>>> entryList = new ArrayList<>(collect.entrySet());
        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();
            for (int i = 0; i < collect.size(); i++) {
                Map.Entry<String, List<standardTreeDto>> entry = entryList.get(i);
                WriteSheet mainSheet = EasyExcel.writerSheet(i, entry.getKey()+"标准库导出").head(standardTreeDto.class).build();
                excelWriter.write(entry.getValue(), mainSheet);
            }
            // 关闭流
            excelWriter.finish();
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        }
    }
 
    @Override
    public void resetTreeOfPrice(String tree, Integer standardId) {
        standardProductListMapper.update(null, Wrappers.<StandardProductList>lambdaUpdate().like(StandardProductList::getTree, tree).eq(StandardProductList::getStandardMethodListId, standardId).set(StandardProductList::getPrice, null));
    }
 
    @Override
    public void resetTreeOfHour(String tree, Integer standardId) {
        standardProductListMapper.update(null, Wrappers.<StandardProductList>lambdaUpdate().like(StandardProductList::getTree, tree).eq(StandardProductList::getStandardMethodListId, standardId).set(StandardProductList::getManHour, null));
    }
 
    @Override
    public void resetTreeOfAsk(String tree, Integer standardId) {
        standardProductListMapper.update(null, Wrappers.<StandardProductList>lambdaUpdate().like(StandardProductList::getTree, tree).eq(StandardProductList::getStandardMethodListId, standardId)
                .set(StandardProductList::getAsk, null)
                .set(StandardProductList::getTell, null)
                .set(StandardProductList::getSection, null)
                .set(StandardProductList::getPrice, null)
                .set(StandardProductList::getManHour, null));
    }
}