Przeglądaj źródła

Merge branch 'master' of http://10.1.235.20:3000/asiainfo/ebc.git

konghl 4 lat temu
rodzic
commit
5b25048c26

+ 6 - 0
location-rescue-service/pom.xml

@ -113,6 +113,12 @@
113 113
            <version>1.2</version>
114 114
        </dependency>
115 115
116
        <dependency>
117
            <groupId>org.apache.poi</groupId>
118
            <artifactId>poi-ooxml</artifactId>
119
            <version>3.6</version>
120
        </dependency>
121
116 122
        <!--	<dependency>
117 123
                     <groupId>com.ai.ipu</groupId>
118 124
                     <artifactId>ipu-data</artifactId>

+ 41 - 11
location-rescue-service/src/main/java/com/ai/bss/location/rescue/controller/EquipmentManageController.java

@ -1,17 +1,27 @@
1 1
package com.ai.bss.location.rescue.controller;
2 2
3
import java.io.InputStream;
4
import java.util.ArrayList;
3 5
import java.util.HashMap;
4 6
import java.util.List;
5 7
import java.util.Map;
6 8
7 9
import javax.servlet.http.HttpServletRequest;
8 10
11
import com.ai.bss.location.rescue.util.ImportExcelUtil;
12
13
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
14
import org.apache.poi.ss.usermodel.Cell;
15
import org.apache.poi.ss.usermodel.Sheet;
16
17
import org.apache.poi.ss.usermodel.Workbook;
9 18
import org.slf4j.Logger;
10 19
import org.slf4j.LoggerFactory;
11 20
import org.springframework.beans.factory.annotation.Autowired;
12 21
import org.springframework.stereotype.Controller;
13 22
import org.springframework.web.bind.annotation.RequestBody;
14 23
import org.springframework.web.bind.annotation.RequestMapping;
24
import org.springframework.web.bind.annotation.RequestParam;
15 25
import org.springframework.web.bind.annotation.ResponseBody;
16 26
17 27
import com.ai.abc.api.model.CommonRequest;
@ -21,6 +31,7 @@ import com.ai.bss.location.rescue.service.interfaces.EquipmentManageService;
21 31
import com.ai.bss.location.rescue.util.EbcConstant;
22 32
import com.ai.bss.work.tool.model.ResourceTool;
23 33
import com.ai.bss.work.tool.model.ResourceToolType;
34
import org.springframework.web.multipart.MultipartFile;
24 35
25 36
/**
26 37
 * 设备管理
@ -95,18 +106,37 @@ public class EquipmentManageController {
95 106
    }
96 107
97 108
    /**
98
	 * 获取所有不可绑定的设备
99
	 */
100
	@ResponseBody
101
	@RequestMapping("/queryAllEquipmentNotBind")
102
	public CommonResponse<List<Map<String, Object>>> queryAllEquipmentNotBind(HttpServletRequest request) throws Exception {
103
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
104
		//TODO 组织ID 权限控制
105
		//JSONObject userInfo = new LoginUserInfoUtil().getUserInfo(request);
109
     * 获取所有不可绑定的设备
110
     */
111
    @ResponseBody
112
    @RequestMapping("/queryAllEquipmentNotBind")
113
    public CommonResponse<List<Map<String, Object>>> queryAllEquipmentNotBind(HttpServletRequest request) throws Exception {
114
        HashMap<String, Object> dataMap = new HashMap<String, Object>();
115
        //TODO 组织ID 权限控制
116
        //JSONObject userInfo = new LoginUserInfoUtil().getUserInfo(request);
106 117
        //dataMap.put("workOrgId", userInfo.get("USER_ORG_ID").toString());
107
		dataMap.put("workOrgId", "0000");
118
        dataMap.put("workOrgId", "0000");
108 119
        dataMap.put("isAllowBindTerminal", EbcConstant.TOOL_BIND_REJECT);
109
		return equipmentManageService.queryAllEquipmentNotBind(dataMap);
110
	}
120
        return equipmentManageService.queryAllEquipmentNotBind(dataMap);
121
    }
111 122
123
    /**
124
     * 批量导入设备
125
     * @return
126
     */
127
    @ResponseBody
128
    @RequestMapping("/importEquipmentInfos")
129
    public CommonResponse<Object> importEquipmentInfos(@RequestParam("file") MultipartFile file) throws Exception {
130
        if (file.isEmpty()) {
131
            return CommonResponse.fail("0","文件为空");
132
        }
133
134
        CommonResponse<Object> result=equipmentManageService.importEquipmentInfos(file);
135
136
        //获取数据
137
        List<List<Object>> olist = ImportExcelUtil.getListByExcel(file.getInputStream(), file.getOriginalFilename());
138
        CommonResponse<Object> ok = CommonResponse.ok(olist);
139
        return ok;
140
    }
112 141
}
142

+ 108 - 0
location-rescue-service/src/main/java/com/ai/bss/location/rescue/service/impl/EquipmentManageServiceImpl.java

@ -1,9 +1,16 @@
1 1
package com.ai.bss.location.rescue.service.impl;
2 2
3
import java.util.ArrayList;
3 4
import java.util.HashMap;
4 5
import java.util.List;
5 6
import java.util.Map;
6 7
8
import com.ai.bss.location.rescue.util.ImportExcelUtil;
9
import com.ai.ipu.basic.string.StringUtil;
10
import org.apache.poi.ss.usermodel.Cell;
11
import org.apache.poi.ss.usermodel.Row;
12
import org.apache.poi.ss.usermodel.Sheet;
13
import org.apache.poi.ss.usermodel.Workbook;
7 14
import org.slf4j.Logger;
8 15
import org.slf4j.LoggerFactory;
9 16
import org.springframework.beans.factory.annotation.Autowired;
@ -18,6 +25,7 @@ import com.ai.bss.work.tool.model.ResourceTool;
18 25
import com.ai.bss.work.tool.model.ResourceToolType;
19 26
import com.ai.bss.work.tool.service.api.ResourceToolCommand;
20 27
import com.ai.bss.work.tool.service.api.ResourceToolQuery;
28
import org.springframework.web.multipart.MultipartFile;
21 29
22 30
@Service
23 31
public class EquipmentManageServiceImpl<EquipmentManageDao> implements EquipmentManageService {
@ -69,4 +77,104 @@ public class EquipmentManageServiceImpl<EquipmentManageDao> implements Equipment
69 77
		return CommonResponse.ok(response.getData().getData());
70 78
	}
71 79
80
	@Override
81
	public CommonResponse<Object> importEquipmentInfos(MultipartFile file) {
82
		//校验结果
83
		boolean reqFlag = true;
84
		//回复消息
85
		String reqMsg = "";
86
		//报错消息数
87
		Integer error = 0;
88
89
		List<List<Object>> list = null;
90
		//创建Excel工作薄
91
		try {
92
			Workbook work = ImportExcelUtil.getWorkbook(file.getInputStream(),file.getOriginalFilename());
93
			if(null == work){
94
				throw new Exception("创建Excel工作薄为空!");
95
			}
96
			Sheet sheet = null;
97
			Row row = null;
98
			Cell cell = null;
99
100
			list = new ArrayList<List<Object>>();
101
			//遍历Excel中所有的sheet
102
			for (int i = 0; i < work.getNumberOfSheets(); i++) {
103
				sheet = work.getSheetAt(i);
104
105
				if(sheet==null){continue;}
106
107
				//遍历当前sheet中的所有行
108
				for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum()+1; j++) {
109
					row = sheet.getRow(j);
110
					if(row==null||row.getFirstCellNum()==j){continue;}
111
112
					//遍历所有的列
113
					List<Object> li = new ArrayList<Object>();
114
					for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
115
						cell = row.getCell(y);
116
						//效验参数 j是行 y是列
117
						if (j < 4 && "".equals(row.getCell(y).getStringCellValue())) { //----这里判断必填项(前4列)
118
							reqFlag = false;
119
							reqMsg += "第" + (i + 1) + "行错误,错误信息:" + "必填项缺漏";
120
							reqMsg += "<br>";
121
							error++;
122
							break;
123
						}
124
						String cellString = row.getCell(y).getStringCellValue();
125
						cellString = cellString.trim();
126
						switch (j) {
127
							/*case 1: {
128
								//进行校验处理,例如手机号
129
								if (!StringUtil.isMobileNo(cellString)) {
130
									reqFlag = false;
131
									reqMsg += "第" + (i + 1) + "行错误,错误信息:" + "联系电话有误";
132
									reqMsg += "<br>";
133
									error++;
134
								} else {
135
									map.put(j, cellString);
136
								}
137
								break;
138
							}
139
							case 2: { ###  break; }
140
							case 3: { ###  break; }
141
							//无需校验,归入default
142
							default: {
143
								map.put(j, cell.getContents());
144
							}*/
145
						}
146
						//将结果放到list里
147
						li.add(ImportExcelUtil.getCellValue(cell));
148
					}
149
					list.add(li);
150
				}
151
				if (reqFlag) {//校验通过
152
					//mapList.add(map);
153
				}
154
			}
155
		} catch (Exception e) {
156
			e.printStackTrace();
157
		}
158
		//效验通过插入数据库
159
		//入库
160
	/*	try {
161
			if (mapList.size() > 0 && reqFlag) {
162
				//此处try,catch应优化为事务处理maplist实现全记录成功或失败
163
				result.setSuccess("提交成功");
164
			} else {
165
				if (error > 10) {//设置要显示的错误数
166
					int index = StringUtil.getIndex(reqMsg, 10, "<br>");
167
					reqMsg = reqMsg.substring(0, index + 4);
168
					reqMsg += "未显示错误数:" + (error - 10) + "条";
169
					reqMsg += "<br>";
170
				}
171
				result.setFailure(reqMsg);
172
			}
173
		} catch (Exception e) {
174
			result.setFailure("入库错误,请联系管理员!");
175
			e.printStackTrace();
176
		}*/
177
		return null;
178
	}
179
72 180
}

+ 3 - 0
location-rescue-service/src/main/java/com/ai/bss/location/rescue/service/interfaces/EquipmentManageService.java

@ -9,6 +9,7 @@ import com.ai.abc.api.model.CommonResponse;
9 9
import com.ai.bss.components.common.model.PageBean;
10 10
import com.ai.bss.work.tool.model.ResourceTool;
11 11
import com.ai.bss.work.tool.model.ResourceToolType;
12
import org.springframework.web.multipart.MultipartFile;
12 13
13 14
public interface EquipmentManageService {
14 15
@ -23,4 +24,6 @@ public interface EquipmentManageService {
23 24
    CommonResponse<List<ResourceToolType>> loadAllResourceToolType(CommonRequest commonRequest);
24 25
25 26
    CommonResponse<List<Map<String, Object>>> queryAllEquipmentNotBind(HashMap<String, Object> dataMap) throws Exception;
27
28
    CommonResponse<Object> importEquipmentInfos(MultipartFile file);
26 29
}

+ 119 - 0
location-rescue-service/src/main/java/com/ai/bss/location/rescue/util/ImportExcelUtil.java

@ -0,0 +1,119 @@
1
package com.ai.bss.location.rescue.util;
2
3
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
4
import org.apache.poi.ss.usermodel.*;
5
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
6
7
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.text.DecimalFormat;
11
import java.text.SimpleDateFormat;
12
import java.util.ArrayList;
13
import java.util.List;
14
15
16
public class ImportExcelUtil {
17
18
    private final static String excel2003L =".xls";    //2003- 版本的excel
19
    private final static String excel2007U =".xlsx";   //2007+ 版本的excel
20
21
    /**
22
     * 描述:获取IO流中的数据,组装成List<List<Object>>对象
23
     * @param in,fileName
24
     * @return
25
     * @throws IOException
26
     */
27
    public static List<List<Object>> getListByExcel(InputStream in,String fileName) throws Exception{
28
        List<List<Object>> list = null;
29
30
        //创建Excel工作薄
31
        Workbook work = ImportExcelUtil.getWorkbook(in,fileName);
32
        if(null == work){
33
            throw new Exception("创建Excel工作薄为空!");
34
        }
35
        Sheet sheet = null;
36
        Row row = null;
37
        Cell cell = null;
38
39
        list = new ArrayList<List<Object>>();
40
        //遍历Excel中所有的sheet
41
        for (int i = 0; i < work.getNumberOfSheets(); i++) {
42
            sheet = work.getSheetAt(i);
43
            if(sheet==null){continue;}
44
45
            //遍历当前sheet中的所有行
46
            for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum()+1; j++) {
47
                row = sheet.getRow(j);
48
                if(row==null||row.getFirstCellNum()==j){continue;}
49
50
                //遍历所有的列
51
                List<Object> li = new ArrayList<Object>();
52
                for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
53
                    cell = row.getCell(y);
54
                    li.add(ImportExcelUtil.getCellValue(cell));
55
                }
56
                list.add(li);
57
            }
58
        }
59
        return list;
60
    }
61
62
    /**
63
     * 描述:根据文件后缀,自适应上传文件的版本
64
     * @param inStr,fileName
65
     * @return
66
     * @throws Exception
67
     */
68
    public static Workbook getWorkbook(InputStream inStr, String fileName) throws Exception{
69
        Workbook wb = null;
70
        String fileType = fileName.substring(fileName.lastIndexOf("."));
71
        if(excel2003L.equals(fileType)){
72
            wb = new HSSFWorkbook(inStr);  //2003-
73
        }else if(excel2007U.equals(fileType)){
74
            wb = new XSSFWorkbook(inStr);  //2007+
75
        }else{
76
            throw new Exception("解析的文件格式有误!");
77
        }
78
        return wb;
79
    }
80
81
    /**
82
     * 描述:对表格中数值进行格式化
83
     * @param cell
84
     * @return
85
     */
86
    public  static Object getCellValue(Cell cell){
87
        Object value = null;
88
        //DecimalFormat df = new DecimalFormat("0");  //格式化number String字符
89
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");  //日期格式化
90
        DecimalFormat df2 = new DecimalFormat("0.00");  //格式化数字
91
92
        switch (cell.getCellType()) {
93
            case Cell.CELL_TYPE_STRING:
94
                value = cell.getRichStringCellValue().getString();
95
                break;
96
            case Cell.CELL_TYPE_NUMERIC:
97
                if("General".equals(cell.getCellStyle().getDataFormatString())){
98
                    //value = df.format(cell.getNumericCellValue());
99
                    value = cell.getNumericCellValue();
100
                }else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
101
                    value = sdf.format(cell.getDateCellValue());
102
                }else{
103
                    value = df2.format(cell.getNumericCellValue());
104
                }
105
                break;
106
            case Cell.CELL_TYPE_BOOLEAN:
107
                value = cell.getBooleanCellValue();
108
                break;
109
            case Cell.CELL_TYPE_BLANK:
110
                value = "";
111
                break;
112
            default:
113
                break;
114
        }
115
        return value;
116
    }
117
118
119
}