jinlin
2024-09-06 3ecb68c427a627ad8e90d8c555655e7724be2d96
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
package com.zt.life.modules.mainPart.taskReliability.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.common.constant.Constant;
import com.zt.common.db.query.QueryFilter;
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.modules.mainPart.basicInfo.dao.XhProductModelDao;
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.basicInfo.service.ParamDataService;
import com.zt.life.modules.mainPart.sysPictureBase.service.SysPictureBaseService;
import com.zt.life.modules.mainPart.taskReliability.dao.ReliabilityAssessDao;
import com.zt.life.modules.mainPart.taskReliability.model.AssessItem;
import com.zt.life.modules.mainPart.taskReliability.model.ReliabilityAssess;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 org.springframework.web.multipart.MultipartFile;
 
import java.util.*;
 
 
/**
 * product_model
 *
 * @author zt generator
 * @since 1.0.0 2024-02-29
 */
@Service
public class ReliabilityAssessService extends BaseService<ReliabilityAssessDao, ReliabilityAssess> {
    public List<Map<String, Object>> importProductExcel(MultipartFile mutFile, String progressId, Long shipId, Long itemId) {
        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<>();
 
        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();
 
            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);
                }
            }
 
            String shipPath = "";
            String xtPath = "";
            String fxtPath = "";
            String sbPath = "";
            String path = "";
            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++) {
                    CacheUtils.put(progressId, "speed", CommonUtils.getPercent(j, num));
                    CacheUtils.put(progressId, "msg", "共" + num + "行,已完成第" + j + "行");
 
                    row1 = j;
                    String pattern = "yyyy-MM-dd";
                    Row row = sheet.getRow(row1);
                   // String sort = ImportUtil.getCellValue(row, 0, pattern); //序号
 
                    String name = ImportUtil.getCellValue(row, 1, pattern); //节点名称
                    String type = ImportUtil.getCellValue(row, 2, pattern);//节点类型
                    String runNum = ImportUtil.getCellValue(row, 3, pattern); //运行时长
                    String runTime = ImportUtil.getCellValue(row, 4, pattern); //运行次数
                    String failNum = ImportUtil.getCellValue(row, 5, pattern); //失败次数
 
                    if (StringUtils.isBlank(name) || StringUtils.isBlank(type))
                        continue;
 
                    Long productId = null;
                    if (type.equals("总体")) {
                        path = shipPath = name;
                        xtPath = "";
                        fxtPath = "";
                        sbPath = "";
                        productId = shipId;
                    }else {
                        if (type.equals("系统")) {
                            if (StringUtils.isNotBlank(shipPath)) {
                                path = xtPath = name;
                                fxtPath = "";
                                sbPath = "";
                            } else {
                                continue;
                            }
                        }
                        if (type.equals("分系统")) {
                            if (StringUtils.isNotBlank(xtPath)) {
                                path = fxtPath = xtPath + "," + name;
                                sbPath = "";
                            } else {
                                continue;
                            }
                        }
                        if (type.equals("设备")) {
                            if ((StringUtils.isNotBlank(xtPath) || StringUtils.isNotBlank(fxtPath))) {
                                path = sbPath = StringUtils.isNotBlank(fxtPath) ? fxtPath + "," + name : xtPath + "," + name;
                            } else {
                                continue;
                            }
                        }
                        if (modelMap.get(path) != null) {
                            productId = modelMap.get(path).getId();
                        }
                    }
 
 
 
                    ReliabilityAssess assess = new ReliabilityAssess();
                    assess.setFailNum(Integer.valueOf(failNum));
                    if (StringUtils.isNotBlank(runNum)){
                        assess.setRunNum(Integer.valueOf(runNum));
                    }
                    if (StringUtils.isNotBlank(runTime)){
                        assess.setRunTimes(Double.valueOf(runTime));
                    }
                    assess.setItemId(itemId);
                    assess.setProductId(productId);
                    this.insert(assess);
                }
            }
        } 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 List<AssessItem> getAssessDataList(Long productId) {
        return baseDao.getAssessDataList(productId);
    }
}