jinlin
2024-11-19 195bb5267a6ece13363303e177fee7d1fa3941aa
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
package com.zt.life.modules.mainPart.basicInfo.service;
 
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.common.constant.Constant;
import com.zt.common.service.BaseService;
import com.zt.common.utils.CacheUtils;
import com.zt.common.utils.CommonUtils;
import com.zt.common.utils.TreeUtils;
import com.zt.common.utils.UUIDUtil;
import com.zt.core.shiro.ImportUtil;
import com.zt.life.export.service.DownloadService;
import com.zt.life.modules.mainPart.basicInfo.dao.XhProductModelDao;
import com.zt.life.modules.mainPart.basicInfo.dto.ProductDto;
import com.zt.life.modules.mainPart.basicInfo.model.ParamData;
import com.zt.life.modules.mainPart.basicInfo.model.ProductImg;
import com.zt.life.modules.mainPart.basicInfo.model.XhProductModel;
import com.zt.life.modules.mainPart.sysPictureBase.service.SysPictureBaseService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zt.common.db.query.QueryFilter;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
 
 
/**
 * product_model
 *
 * @author zt generator
 * @since 1.0.0 2024-02-29
 */
@Service
public class XhProductModelService extends BaseService<XhProductModelDao, XhProductModel> {
    @Autowired
    ParamDataService paramDataService;
    @Autowired
    SysPictureBaseService sysPictureBaseService;
 
    /**
     * 分页查询
     *
     * @param queryFilter
     * @return
     */
    public List<XhProductModel> page(QueryFilter queryFilter) {
        List<XhProductModel> list = baseDao.getList(queryFilter.getQueryParams());
        return list;
    }
 
    /**
     * 删除
     *
     * @param ids
     */
    public void delete(Long[] ids) {
        super.deleteLogic(ids);
    }
 
    public List<XhProductModel> getAllTree(Boolean showXdy, Integer ztShow, Long productId) {
        List<XhProductModel> productList = new ArrayList<>();
        if (showXdy) {
            if (productId != null) {
                productList = baseDao.selectList(new QueryWrapper<XhProductModel>().eq(Constant.TableColumn.IS_DELETE,
                        Constant.Bool.NO).ne("product_type", 5).and(wrapper -> wrapper.eq("ship_id", productId).or().eq("id", productId)).orderByAsc(Constant.TableColumn.SORT));
            } else {
                productList = baseDao.selectList(new QueryWrapper<XhProductModel>().eq(Constant.TableColumn.IS_DELETE,
                        Constant.Bool.NO).ne("product_type", 5).orderByAsc(Constant.TableColumn.SORT));
            }
        } else {
            if (productId == null) {
                if (ztShow == 1) {
                    productList = baseDao.selectList(new QueryWrapper<XhProductModel>().eq(Constant.TableColumn.IS_DELETE,
                            Constant.Bool.NO).lt("product_type", 5).ne("product_type", 10).orderByAsc(Constant.TableColumn.SORT));
                } else if (ztShow == 2) {
                    productList = baseDao.selectList(new QueryWrapper<XhProductModel>().eq(Constant.TableColumn.IS_DELETE,
                            Constant.Bool.NO).lt("product_type", 4).ne("product_type", 10).orderByAsc(Constant.TableColumn.SORT));
                } else if (ztShow == 3) {
                    productList = baseDao.selectList(new QueryWrapper<XhProductModel>().eq(Constant.TableColumn.IS_DELETE,
                            Constant.Bool.NO).lt("product_type", 3).ne("product_type", 10).orderByAsc(Constant.TableColumn.SORT));
                } else if (ztShow == 4) {
                    productList = baseDao.selectList(new QueryWrapper<XhProductModel>().eq(Constant.TableColumn.IS_DELETE,
                            Constant.Bool.NO).lt("product_type", 5).ne("product_type", 1).orderByAsc(Constant.TableColumn.SORT));
                } else if (ztShow == 5) {
                    productList = baseDao.selectList(new QueryWrapper<XhProductModel>().eq(Constant.TableColumn.IS_DELETE,
                            Constant.Bool.NO).gt("product_type", 1).orderByAsc(Constant.TableColumn.SORT));
                }
            } else {
                if (ztShow == 4) {
                    productList = baseDao.selectList(new QueryWrapper<XhProductModel>().eq(Constant.TableColumn.IS_DELETE,
                            Constant.Bool.NO).lt("product_type", 5).ne("product_type", 1).and(wrapper -> wrapper.eq("ship_id", productId).or().eq("id", productId)).orderByAsc(Constant.TableColumn.SORT));
                } else if (ztShow == 5) {
                    List<XhProductModel> dbList = baseDao.getProductListByShip(productId, 2, 5);
                    for (XhProductModel row : dbList) {
                        int sameNum = row.getSameSbNum() == null ? 1 : row.getSameSbNum();
                        for (int i = 1; i <= sameNum; i++) {
                            try {
                                XhProductModel newRow = null;
                                if (sameNum > 1) {
                                    newRow = row.clone();
                                }
                                else
                                    newRow = row;
                                if ("5".equals(newRow.getProductType())){
                                    newRow.setDeviceNo(i);
                                    newRow.setDataId(row.getId().toString() + "-" + i);
                                    if (i>1){
                                        newRow.setName(row.getName() + "-" + i);
                                    }
                                }
                                else{
                                    newRow.setDeviceNo(0);
                                    newRow.setDataId(row.getId().toString());
                                }
                                productList.add(newRow);
                            } catch (CloneNotSupportedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    productList.sort(Comparator.comparing(XhProductModel::getSort, Comparator.naturalOrder())
                            .thenComparing(XhProductModel::getDeviceNo, Comparator.naturalOrder()));
                }
            }
        }
        return TreeUtils.build(productList);
    }
 
    public Integer getNo(Long pid, String productType) {
        if (baseDao.getNo(pid, productType) == null) {
            return 0;
        }
        return baseDao.getNo(pid, productType);
    }
 
    public List<XhProductModel> getProductList(Long shipId, Integer type) {
        List<XhProductModel> ProductList = baseDao.getProductList(shipId, type);
        return TreeUtils.build(ProductList);
    }
 
    public List<ProductImg> getProduct(Long productId) {
        XhProductModel model = this.get(productId);
        Long parentId = model.getPid();
        return baseDao.getProduct(productId, parentId);
    }
 
    public List<XhProductModel> getTaskProductList() {
        return baseDao.getTaskProductList();
    }
 
    public List<Map<String, Object>> importProductExcel(MultipartFile mutFile, String progressId, Long shipId) {
        Map<Long, XhProductModel> imgMap = new HashMap<>();
        Date beginDate = new Date();
        List<Map<String, Object>> list = new ArrayList<>();//存储所有的导入状态
        Map<String, Object> map;//存储每一行的状态
        Integer sum = 0; // 总共的条数
        Integer suc = 0; // 成功的条数
        Integer err = 0; // 失败的条数
        int row1 = 0;
        Map<String, Object> errMap = new HashMap<>();
        Map<String, Integer> reliabType = new HashMap<>();
        reliabType.put("指数分布", 1);
        reliabType.put("二项分布", 3);
        reliabType.put("威布尔分布", 2);
        Map<String, Integer> repairType = new HashMap<>();
        repairType.put("指数分布", 1);
        repairType.put("威布尔分布", 2);
        Map<String, Integer> isOrNot = new HashMap<>();
        isOrNot.put("是", 1);
        isOrNot.put("否", 0);
 
        try {
            // 获取导入文件的后缀名
            String fileName = mutFile.getOriginalFilename();
            Workbook workbook = null;
            //解决excel版本问题
            if (fileName != null && fileName.endsWith(".xls")) {
                workbook = new HSSFWorkbook(mutFile.getInputStream());
            } else if (fileName != null && fileName.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(mutFile.getInputStream());
            }
            assert workbook != null; //断言如果[boolean表达式]为true,则程序继续执行。 如果为false,则程序抛出AssertionError,并终止执行。
            int sheets = workbook.getNumberOfSheets();
 
            Long curXtId = null;
            Long curFxtId = null;
            Long curSbId;
            String curXtName = "";
            String curFxtName = "";
 
            Map<String, XhProductModel> modelMap = new HashMap<>();
            List<XhProductModel> prductList = baseDao.getProductPath(shipId);
 
            for (XhProductModel product : prductList) {
                if (StringUtils.isNotBlank(product.getNamePath())) {
                    modelMap.put(product.getNamePath(), product);
                }
            }
 
            boolean insertXt;
            boolean insertFxt;
            Boolean insertSb;
            for (int i = 0; i < sheets; i++) {
                Sheet sheet = workbook.getSheetAt(i);
                int num = sheet.getLastRowNum(); // 一共有多少行
                String sheetName = sheet.getSheetName(); //获取当前sheet名称
                for (int j = 1; j <= num; j++) {
                    insertXt = false;
                    insertFxt = false;
                    insertSb = false;
                    CacheUtils.put(progressId, "speed", CommonUtils.getPercent(j, num));
                    CacheUtils.put(progressId, "msg", "共" + num + "行,已完成第" + j + "行");
 
                    row1 = j + 1;
                    String pattern = "yyyy-MM-dd";
                    Row row = sheet.getRow(row1);
                    String sort = com.zt.core.shiro.ImportUtil.getCellValue(row, 0, pattern); //序号
 
                    String xt = com.zt.core.shiro.ImportUtil.getCellValue(row, 1, pattern); //系统
                    String fxt = com.zt.core.shiro.ImportUtil.getCellValue(row, 2, pattern);
                    String sb = com.zt.core.shiro.ImportUtil.getCellValue(row, 3, pattern); //设备
                    String sameSbNumStr = com.zt.core.shiro.ImportUtil.getCellValue(row, 4, pattern); //同类设备数量
                    String equipType = com.zt.core.shiro.ImportUtil.getCellValue(row, 5, pattern); //设备类型
                    String reliabDistribType = com.zt.core.shiro.ImportUtil.getCellValue(row, 6, pattern); //可靠性分布类型
                    String basicJoinCompute = com.zt.core.shiro.ImportUtil.getCellValue(row, 7, pattern); //参加计算
                    String basicMtbfRegulateStr = com.zt.core.shiro.ImportUtil.getCellValue(row, 8, pattern); //mtbf
                    String basicMtbfRegulSuccRateStr = com.zt.core.shiro.ImportUtil.getCellValue(row, 9, pattern); //成功率
                    String basicMtbfOperatingRatioStr = com.zt.core.shiro.ImportUtil.getCellValue(row, 10, pattern); //运行比
                    String taskMtbcfRegulateStr = com.zt.core.shiro.ImportUtil.getCellValue(row, 11, pattern); //mtbcf
                    String taskMtbcfOtherParams2Str = com.zt.core.shiro.ImportUtil.getCellValue(row, 12, pattern); //mtbcf其他参数2
                    String taskMtbcfOtherParams3Str = com.zt.core.shiro.ImportUtil.getCellValue(row, 13, pattern); //mtbcf其他参数3
                    String taskMtbcfRegulSuccRateStr = com.zt.core.shiro.ImportUtil.getCellValue(row, 14, pattern); //mtbcf成功率
                    String taskMtbcfOperatingRatioStr = com.zt.core.shiro.ImportUtil.getCellValue(row, 15, pattern); //mtbcf运行比
                    String repairable = com.zt.core.shiro.ImportUtil.getCellValue(row, 16, pattern); //是否可维修
                    String repairDistribType = com.zt.core.shiro.ImportUtil.getCellValue(row, 17, pattern); //维修发布类型
                    String repairMttcrStr = com.zt.core.shiro.ImportUtil.getCellValue(row, 18, pattern); //mttcr
                    String repairMttcrOtherParams2Str = com.zt.core.shiro.ImportUtil.getCellValue(row, 19, pattern); //mttcr其他参数2
                    String repairMttcrOtherParams3Str = com.zt.core.shiro.ImportUtil.getCellValue(row, 20, pattern); //mttcr其他参数3
 
                    Integer sameSbNum = StringUtils.isNotBlank(sameSbNumStr) ? Integer.parseInt(sameSbNumStr) : 1;
                    Double basicMtbfRegulate = StringUtils.isNotBlank(basicMtbfRegulateStr) ? Double.valueOf(basicMtbfRegulateStr) : null;
                    Double basicMtbfRegulSuccRate = StringUtils.isNotBlank(basicMtbfRegulSuccRateStr) ? Double.valueOf(basicMtbfRegulSuccRateStr) : null;
                    Double basicMtbfOperatingRatio = StringUtils.isNotBlank(basicMtbfOperatingRatioStr) ? Double.parseDouble(basicMtbfOperatingRatioStr) : 1.0;
                    Double taskMtbcfRegulate = StringUtils.isNotBlank(taskMtbcfRegulateStr) ? Double.parseDouble(taskMtbcfRegulateStr) : null;
                    Double taskMtbcfOtherParams2 = StringUtils.isNotBlank(taskMtbcfOtherParams2Str) ? Double.parseDouble(taskMtbcfOtherParams2Str) : null;
                    Double taskMtbcfOtherParams3 = StringUtils.isNotBlank(taskMtbcfOtherParams3Str) ? Double.parseDouble(taskMtbcfOtherParams3Str) : null;
                    Double taskMtbcfRegulSuccRate = StringUtils.isNotBlank(taskMtbcfRegulSuccRateStr) ? Double.parseDouble(taskMtbcfRegulSuccRateStr) : null;
                    Double taskMtbcfOperatingRatio = StringUtils.isNotBlank(taskMtbcfOperatingRatioStr) ? Double.parseDouble(taskMtbcfOperatingRatioStr) : 1.0;
                    Double repairMttcr = StringUtils.isNotBlank(repairMttcrStr) ? Double.parseDouble(repairMttcrStr) : null;
                    Double repairMttcrOtherParams2 = StringUtils.isNotBlank(repairMttcrOtherParams2Str) ? Double.parseDouble(repairMttcrOtherParams2Str) : null;
                    Double repairMttcrOtherParams3 = StringUtils.isNotBlank(repairMttcrOtherParams3Str) ? Double.parseDouble(repairMttcrOtherParams3Str) : null;
 
 
                    if (StringUtils.isBlank(sb))
                        continue;
 
                    if (StringUtils.isBlank(xt)) {
                        if (StringUtils.isBlank(curXtName))
                            continue;
                        else
                            xt = curXtName;
                    } else {
                        if (StringUtils.isBlank(curXtName) || !xt.equals(curXtName)) {
                            curXtName = xt;
                            if (modelMap.get(curXtName) == null) {
                                curXtId = UUIDUtil.generateId();
                                insertXt = true;
                            } else {
                                curXtId = modelMap.get(curXtName).getId();
                            }
                            curFxtId = null;
                            curFxtName = "";
                        }
                    }
                    String fxtPath = "";
                    if (StringUtils.isNotBlank(fxt) && !fxt.equals(curFxtName)) {
                        curFxtName = fxt;
                        fxtPath = curXtName + "," + curFxtName;
                        if (modelMap.get(fxtPath) == null) {
                            curFxtId = UUIDUtil.generateId();
                            insertFxt = true;
                        } else {
                            curFxtId = modelMap.get(fxtPath).getId();
                        }
                    }
 
                    Long paramId = null;
                    String sbPath = curXtName + "," + (StringUtils.isBlank(curFxtName) ? "" : curFxtName + ",") + sb;
                    if (modelMap.get(sbPath) == null) {
                        curSbId = UUIDUtil.generateId();
                        insertSb = true;
                    } else {
                        curSbId = modelMap.get(sbPath).getId();
                        paramId = modelMap.get(sbPath).getParamId();
                    }
 
                    if (StringUtils.isEmpty(sb)) {
                        com.zt.life.util.ImportUtil.updateErrMap(errMap, "没有填写设备名称", sheetName, row1);
                        continue;
                    }
 
                    //导入型号产品结果库
                    XhProductModel xhProductModel = new XhProductModel();
 
                    Long defultImg = null;
                    xhProductModel.setShipId(shipId);
 
                    //添加系统
                    if (insertXt) {
                        xhProductModel.setPid(shipId);
                        xhProductModel.setId(curXtId);
                        xhProductModel.setName(xt);
                        xhProductModel.setProductType("3");
                        xhProductModel.setSort(Integer.valueOf(sort));
                        defultImg = sysPictureBaseService.getDefaultImg(3).getId();
                        xhProductModel.setOperatImg(defultImg);
                        xhProductModel.setNamePath(curXtName);
                        this.insert(xhProductModel);
                        imgMap = (Map<Long, XhProductModel>) CacheUtils.get("sysImgCache", "sysImgCache");
                        imgMap.put(xhProductModel.getId(), xhProductModel);
 
                        //导入预计参数
                        ParamData itemEntity = new ParamData();
                        itemEntity.setShipId(shipId);
                        itemEntity.setProductId(curXtId);
                        itemEntity.setBasicUnitNum(1);
                        paramDataService.insert(itemEntity);
                    }
 
 
                    if (insertFxt) {
                        xhProductModel.setPid(curXtId);
                        xhProductModel.setId(curFxtId);
                        xhProductModel.setName(fxt);
                        xhProductModel.setProductType("4");
                        xhProductModel.setSort(Integer.valueOf(sort));
                        defultImg = sysPictureBaseService.getDefaultImg(4).getId();
                        xhProductModel.setOperatImg(defultImg);
                        xhProductModel.setNamePath(fxtPath);
                        this.insert(xhProductModel);
                        imgMap = (Map<Long, XhProductModel>) CacheUtils.get("sysImgCache", "sysImgCache");
                        imgMap.put(xhProductModel.getId(), xhProductModel);
 
                        //导入预计参数
                        ParamData itemEntity = new ParamData();
                        itemEntity.setShipId(shipId);
                        itemEntity.setProductId(curFxtId);
                        itemEntity.setBasicUnitNum(1);
                        paramDataService.insert(itemEntity);
                    }
 
                    xhProductModel.setPid(curFxtId == null ? curXtId : curFxtId);
                    xhProductModel.setId(curSbId);
                    xhProductModel.setName(sb);
                    xhProductModel.setSameSbNum(sameSbNum);
                    xhProductModel.setEquipType(equipType);
                    xhProductModel.setProductType("5");
                    xhProductModel.setSort(Integer.valueOf(sort));
                    defultImg = sysPictureBaseService.getDefaultImg(5).getId();
                    xhProductModel.setOperatImg(defultImg);
                    xhProductModel.setNamePath(sbPath);
                    if (insertSb) {
                        this.insert(xhProductModel);
                        imgMap = (Map<Long, XhProductModel>) CacheUtils.get("sysImgCache", "sysImgCache");
                        imgMap.put(xhProductModel.getId(), xhProductModel);
                    } else {
                        this.update(xhProductModel);
                        imgMap = (Map<Long, XhProductModel>) CacheUtils.get("sysImgCache", "sysImgCache");
                        imgMap.put(xhProductModel.getId(), xhProductModel);
                    }
 
                    //导入预计参数
                    ParamData itemEntity = new ParamData();
                    //绑定设备ID
                    itemEntity.setShipId(shipId);
                    itemEntity.setProductId(curSbId);
                    itemEntity.setBasicUnitNum(sameSbNum);
                    itemEntity.setReliabDistribType(reliabType.get(reliabDistribType));
                    itemEntity.setBasicJoinCompute(isOrNot.get(basicJoinCompute));
                    itemEntity.setBasicMtbfRegulate(basicMtbfRegulate);
                    itemEntity.setBasicMtbfRegulSuccRate(basicMtbfRegulSuccRate);
                    itemEntity.setBasicMtbfOperatingRatio(basicMtbfOperatingRatio);
                    itemEntity.setTaskMtbcfRegulate(taskMtbcfRegulate);
                    itemEntity.setTaskMtbcfOtherParams2(taskMtbcfOtherParams2);
                    itemEntity.setTaskMtbcfOtherParams3(taskMtbcfOtherParams3);
                    itemEntity.setTaskMtbcfRegulSuccRate(taskMtbcfRegulSuccRate);
                    itemEntity.setTaskMtbcfOperatingRatio(taskMtbcfOperatingRatio);
                    itemEntity.setRepairable(isOrNot.get(repairable));
                    itemEntity.setRepairDistribType(repairType.get(repairDistribType));
                    itemEntity.setRepairMttcr(repairMttcr);
                    itemEntity.setRepairMttcrOtherParams2(repairMttcrOtherParams2);
                    itemEntity.setRepairMttcrOtherParams3(repairMttcrOtherParams3);
 
                    if (modelMap.get(sbPath) == null) {
                        paramDataService.insert(itemEntity);
                    } else {
                        itemEntity.setId(paramId);
                        paramDataService.update(itemEntity);
                    }
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
            ImportUtil.updateErrMap(errMap, "导入异常" + e.getMessage(), "", row1);
            //err++;
        }
 
        Date nowDate = new Date();
        String msg = "产品模型导入时间:" + CommonUtils.getDatePoor(nowDate, beginDate) + "\r\n";
        System.out.println(msg);
        suc = sum - err;
        map = new HashMap<>();
        map.put("sum", sum + ""); // 所有导入的条数
        map.put("suc", suc + ""); // 成功的条数
        map.put("err", err + ""); // 失败的条数
        list.add(map);
        list.add(errMap);
        return list;
    }
 
    public void deleteByShipId(Long shipId) {
        baseDao.deleteByShipId(shipId);
    }
 
    private List<XhProductModel> getByShipId(Long shipId) {
        return baseDao.getByShipId(shipId);
    }
 
    public void deleteByPid(Long id) {
        baseDao.deleteByPid(id);
    }
 
    public Long[] getByPid(Long id) {
        return baseDao.getByPid(id);
    }
 
    public void exportDataExcel(HttpServletRequest request, HttpServletResponse response, Long shipId, String name) {
        Map<Integer, String> reliabType = new HashMap<>();
        reliabType.put(1, "指数分布");
        reliabType.put(3, "二项分布");
        reliabType.put(2, "威布尔分布");
        Map<Integer, String> repairType = new HashMap<>();
        repairType.put(1, "指数分布");
        repairType.put(2, "威布尔分布");
        Map<Integer, String> isOrNot = new HashMap<>();
        isOrNot.put(1, "是");
        isOrNot.put(0, "否");
 
        List<ProductDto> data = baseDao.getProductByShip(shipId);
        data = TreeUtils.build(data);
        List<ProductDto> List = new ArrayList<>();
        this.writeList(data, List);
        // 读取模板文件
        try (InputStream inputStream = getClass().getResourceAsStream("/产品结构树导出模板.xlsx")) {
            Workbook workbook = new XSSFWorkbook(inputStream);
            // 获取第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            List.sort((o1, o2) -> {
                String[] parentList1 = o1.getNamePath().split(",");
                String[] parentList2 = o2.getNamePath().split(",");
                String xt1 = parentList1.length > 0 ? parentList1[0] : "";
                String xt2 = parentList2.length > 0 ? parentList2[0] : "";
 
                // 首先比较xt值
                int xtCompare = xt1.compareTo(xt2);
                if (xtCompare != 0) {
                    return xtCompare;
                }
 
                // 如果xt相同,比较parentList的长度,确保parentList.length = 2的数据排在前面
                return Integer.compare(parentList1.length, parentList2.length);
            });
            int num = 0;
            String xt = "";
            String fxt = "";
            String sb = "";
            for (int i = 0; i < List.size(); i++) {
                num = num + 1;
                String[] parentList = List.get(i).getNamePath().split(",");
                Row row = sheet.createRow(sheet.getLastRowNum() + 1); // 创建新行
                Cell cell = row.createCell(0);
                cell.setCellValue(num);
                if (!xt.equals(parentList[0])) {
                    xt = parentList[0];
                    cell = row.createCell(1);
                    cell.setCellValue(xt);
                }
                if (parentList.length > 2) {
                    if (!fxt.equals(parentList[1])) {
                        fxt = parentList[1];
                        cell = row.createCell(2);
                        cell.setCellValue(fxt);
                    }
                    if (!sb.equals(parentList[2])) {
                        sb = parentList[2];
                        cell = row.createCell(3);
                        cell.setCellValue(sb);
                    }
                } else {
                    if (!sb.equals(parentList[1])) {
                        sb = parentList[1];
                        cell = row.createCell(3);
                        cell.setCellValue(sb);
                    }
                }
                cell = row.createCell(4);
                cell.setCellValue(List.get(i).getSameSbNum());
 
                cell = row.createCell(5);
                if (StringUtils.isNotBlank(List.get(i).getEquipType())) {
                    cell.setCellValue(List.get(i).getEquipType());
                }
                cell = row.createCell(6);
                String type = reliabType.get(List.get(i).getReliabDistribType());
                cell.setCellValue(type);
                cell = row.createCell(7);
                String isNot = isOrNot.get(List.get(i).getBasicJoinCompute());
                cell.setCellValue(isNot);
                cell = row.createCell(8);
                if (List.get(i).getBasicMtbfRegulate() != null) {
                    cell.setCellValue(List.get(i).getBasicMtbfRegulate());
                }
                cell = row.createCell(9);
                if (List.get(i).getBasicMtbfRegulSuccRate() != null) {
                    cell.setCellValue(List.get(i).getBasicMtbfRegulSuccRate());
                }
                cell = row.createCell(10);
                if (List.get(i).getBasicMtbfOperatingRatio() != null) {
                    cell.setCellValue(List.get(i).getBasicMtbfOperatingRatio());
                } else {
                    cell.setCellValue(1.0);
                }
                cell = row.createCell(11);
                if (List.get(i).getTaskMtbcfRegulate() != null) {
                    cell.setCellValue(List.get(i).getTaskMtbcfRegulate());
                }
                cell = row.createCell(12);
                if (List.get(i).getTaskMtbcfOtherParams2() != null) {
                    cell.setCellValue(List.get(i).getTaskMtbcfOtherParams2());
                }
                cell = row.createCell(13);
                if (List.get(i).getTaskMtbcfOtherParams3() != null) {
                    cell.setCellValue(List.get(i).getTaskMtbcfOtherParams3());
                }
                cell = row.createCell(14);
                if (List.get(i).getTaskMtbcfRegulSuccRate() != null) {
                    cell.setCellValue(List.get(i).getTaskMtbcfRegulSuccRate());
                }
                cell = row.createCell(15);
                if (List.get(i).getTaskMtbcfOperatingRatio() != null) {
                    cell.setCellValue(List.get(i).getTaskMtbcfOperatingRatio());
                } else {
                    cell.setCellValue(1.0);
                }
                cell = row.createCell(16);
                if (List.get(i).getRepairable() != null) {
                    isNot = isOrNot.get(List.get(i).getRepairable());
                    cell.setCellValue(isNot);
                }
                cell = row.createCell(17);
                if (List.get(i).getRepairDistribType() != null) {
                    type = repairType.get(List.get(i).getRepairDistribType());
                    cell.setCellValue(type);
                }
                cell = row.createCell(18);
                if (List.get(i).getRepairMttcr() != null) {
                    cell.setCellValue(List.get(i).getRepairMttcr());
                }
                cell = row.createCell(19);
                if (List.get(i).getRepairMttcrOtherParams2() != null) {
                    cell.setCellValue(List.get(i).getRepairMttcrOtherParams2());
                }
                cell = row.createCell(20);
                if (List.get(i).getRepairMttcrOtherParams3() != null) {
                    cell.setCellValue(List.get(i).getRepairMttcrOtherParams3());
                }
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            String encodedFilename = DownloadService.getNameEncoder(request, name + "_产品结构树.xlsx");
            response.addHeader("Content-Disposition", "attachment;filename=" + encodedFilename);
            workbook.write(response.getOutputStream());
 
        } catch (
                IOException e) {
            e.printStackTrace();
        }
 
    }
 
    private void writeList(List<ProductDto> data, List<ProductDto> List) {
        for (ProductDto dto : data) {
            List.add(dto);
            writeList(dto.getChildren(), List);
        }
    }
 
    public void refreshCache() {
        List<XhProductModel> productList = baseDao.getProductListAll();
        Map<Long, XhProductModel> imgMap = new HashMap<>();
        for (XhProductModel product : productList) {
            imgMap.put(product.getId(), product);
        }
        CacheUtils.put("sysImgCache", "sysImgCache", imgMap);
    }
 
    public void getImg(HttpServletResponse response, Long id) {
        Map<Long, XhProductModel> imgMap = new HashMap<>();
        imgMap = (Map<Long, XhProductModel>) CacheUtils.get("sysImgCache", "sysImgCache");
        XhProductModel product = imgMap.get(id);
        sysPictureBaseService.getSvgImage(response, product.getOperatImg());
    }
 
    public void insertProduct(XhProductModel xhProductModel) {
        Integer no = this.getNo(xhProductModel.getPid(), xhProductModel.getProductType());
        if (xhProductModel.getSort() == null || xhProductModel.getSort() < no) {
            xhProductModel.setSort(no + 1);
        }
        if (xhProductModel.getProductType().equals("2")) {
            Long id = UUIDUtil.generateId();
            xhProductModel.setId(id);
            xhProductModel.setShipId(id);
 
            ParamData data = new ParamData();
            data.setShipId(id);
            data.setProductId(id);
 
            paramDataService.insert(data);
        } else {
            xhProductModel.setNamePath(xhProductModel.getNamePath() + "," + xhProductModel.getName());
            this.insert(xhProductModel);
            ParamData data = new ParamData();
            data.setShipId(xhProductModel.getShipId());
            data.setProductId(xhProductModel.getId());
            paramDataService.insert(data);
        }
        Map<Long, XhProductModel> imgMap = new HashMap<>();
        imgMap = (Map<Long, XhProductModel>) CacheUtils.get("sysImgCache", "sysImgCache");
        imgMap.put(xhProductModel.getId(), xhProductModel);
    }
 
    public void updateProduct(XhProductModel xhProductModel) {
        XhProductModel oldXh = this.get(xhProductModel.getId());
        if (oldXh != null) {
            if (!oldXh.getPid().equals(xhProductModel.getPid())) {
                XhProductModel parentXh = this.get(xhProductModel.getPid());
                if (parentXh.getProductType().equals("3")) {
                    xhProductModel.setNamePath(parentXh.getName() + "," + xhProductModel.getName());
                } else if (parentXh.getProductType().equals("4")) {
                    xhProductModel.setNamePath(parentXh.getNamePath() + "," + xhProductModel.getName());
                }
            }
        }
        this.update(xhProductModel);
        Map<Long, XhProductModel> imgMap = new HashMap<>();
        imgMap = (Map<Long, XhProductModel>) CacheUtils.get("sysImgCache", "sysImgCache");
        imgMap.put(xhProductModel.getId(), xhProductModel);
    }
}