|
@@ -7,6 +7,10 @@ import com.mooctest.crowd.domain.repository.CrowdTestProjectRepo;
|
|
|
import com.mooctest.crowd.domain.repository.UserRepo;
|
|
|
import com.mooctest.crowd.domain.util.Converter;
|
|
|
import com.mooctest.crowd.domain.util.FileUtil;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
@@ -14,10 +18,14 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
import java.sql.Timestamp;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import java.util.regex.PatternSyntaxException;
|
|
|
|
|
|
/**
|
|
|
* @author guochao
|
|
@@ -459,18 +467,7 @@ public class CrowdTestProjectController {
|
|
|
@RequestMapping(value = "projectListImportByExcel", method = RequestMethod.POST)
|
|
|
public String projectListImportByExcel(MultipartFile file){
|
|
|
//解析excel
|
|
|
- List<CrowdTestProjectPO> crowdTestProjectPOList = FileUtil.importExcel(file,1,1,CrowdTestProjectPO.class);
|
|
|
-
|
|
|
-
|
|
|
-// List<CrowdTestProjectPO> crowdTestProjectPOList = new ArrayList<>();
|
|
|
-// CrowdTestProjectPO crowdTestProjectPO = new CrowdTestProjectPO();
|
|
|
-// crowdTestProjectPO.setName("途牛APP");
|
|
|
-// crowdTestProjectPO.setPlatform("[1,2]");
|
|
|
-// crowdTestProjectPO.setLinkMan("孙加辉");
|
|
|
-// crowdTestProjectPO.setLinkManMobile("13567908766");
|
|
|
-// crowdTestProjectPO.setType("[1]");
|
|
|
-// crowdTestProjectPO.setDescription("测试项目中存在的bug");
|
|
|
-// crowdTestProjectPOList.add(crowdTestProjectPO);
|
|
|
+ List<CrowdTestProjectPO> crowdTestProjectPOList = FileUtil.importExcel(file,0,1,CrowdTestProjectPO.class);
|
|
|
|
|
|
for(int i = 0; i < crowdTestProjectPOList.size(); i++){
|
|
|
CrowdTestProject crowdTestProject = Converter.convert(CrowdTestProject.class, crowdTestProjectPOList.get(i));
|
|
@@ -480,11 +477,159 @@ public class CrowdTestProjectController {
|
|
|
|
|
|
}
|
|
|
System.out.println("共导入数据【"+crowdTestProjectPOList.size()+"】行");
|
|
|
+
|
|
|
return "success";
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
+ * 通过EXCEL向数据库中导入众包项目文件,并补全项目中任务和报告的数据
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "projectImportByExcel", method = RequestMethod.POST)
|
|
|
+ public String projectImportByExcel(MultipartFile file) {
|
|
|
+ List<String> logList = new ArrayList<>();
|
|
|
+ List<Map<Integer,Object>> recordList = new ArrayList<>();
|
|
|
+ HSSFWorkbook workbook=null;
|
|
|
+ try {
|
|
|
+ workbook = new HSSFWorkbook(file.getInputStream());
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取指定标签页
|
|
|
+ HSSFSheet sheet = workbook.getSheetAt(0);
|
|
|
+ if(sheet==null){
|
|
|
+ throw new RuntimeException("标签页不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ //总行数
|
|
|
+ int rows = sheet.getPhysicalNumberOfRows();
|
|
|
+ if (rows < 2) {
|
|
|
+ throw new RuntimeException(file.getName()+" 内容为空,请重新编辑!");
|
|
|
+ }
|
|
|
+
|
|
|
+ //不为空的单元格数 ,第一行是标题列
|
|
|
+ HSSFRow keyRow = sheet.getRow(0);
|
|
|
+ int cellNumber = keyRow.getPhysicalNumberOfCells();
|
|
|
+
|
|
|
+ //循环遍历,第一行是标题,所以从 1 开始遍历(0表示第一行,1表示第二行)
|
|
|
+ for (int r = 1; r < rows; r++) {
|
|
|
+ Map<Integer,Object> record = new LinkedHashMap<Integer,Object>();//使用HashMap的话,存进去的顺序与取出来的顺序是不一致的,此处需要使用LinkedHashMap
|
|
|
+ HSSFRow row = sheet.getRow(r);
|
|
|
+ if(row == null){
|
|
|
+ logList.add("表中存在错误的单元格,请把与内容无关的单元格置空");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int c = 0; c < cellNumber; c++){ //共cellNumber列
|
|
|
+ HSSFCell cell = row.getCell(c);
|
|
|
+ String cellValue = "";
|
|
|
+ DecimalFormat df = new DecimalFormat("#");
|
|
|
+ //如果单元格不为空
|
|
|
+ if (cell != null) {
|
|
|
+ if(c == 1){
|
|
|
+ String platform =cell.getRichStringCellValue().getString().trim();
|
|
|
+ if(platform.length() == 0){
|
|
|
+ logList.add("第"+(r+1)+"行第"+(c+1)+"列数据为空");
|
|
|
+ }else{
|
|
|
+ if(!isPlatform(platform)){
|
|
|
+ logList.add("第"+(r+1)+"行第"+(c+1)+"列数据不符合要求");
|
|
|
+ }else{
|
|
|
+ String platformSave = "[" + platform + "]";
|
|
|
+ record.put(c,platformSave);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(c == 4){
|
|
|
+ String type =cell.getRichStringCellValue().getString().trim();
|
|
|
+ if(type.length() == 0){
|
|
|
+ logList.add("第"+(r+1)+"行第"+(c+1)+"列数据为空");
|
|
|
+ }else{
|
|
|
+ if(!isType(type)){
|
|
|
+ logList.add("第"+(r+1)+"行第"+(c+1)+"列数据不符合要求");
|
|
|
+ }else{
|
|
|
+ String typeSave = "[" + type + "]";
|
|
|
+ record.put(c,typeSave);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(c == 3){
|
|
|
+ String phone = cell.getRichStringCellValue().getString().trim();
|
|
|
+ if(!isRightPhone(phone)){
|
|
|
+ logList.add("第"+(r+1)+"行第"+(c+1)+"列手机号格式不正确");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ switch (cell.getCellType()) {
|
|
|
+ case HSSFCell.CELL_TYPE_STRING:
|
|
|
+ cellValue =cell.getRichStringCellValue().getString().trim();
|
|
|
+ if(cellValue.indexOf(" ") != -1){
|
|
|
+ logList.add("第"+(r+1)+"行第"+(c+1)+"列数据中存在空格");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case HSSFCell.CELL_TYPE_NUMERIC:
|
|
|
+ cellValue =df.format(cell.getNumericCellValue());
|
|
|
+ break;
|
|
|
+ case HSSFCell.CELL_TYPE_BOOLEAN:
|
|
|
+ cellValue =String.valueOf(cell.getBooleanCellValue()).trim();
|
|
|
+ break;
|
|
|
+ case HSSFCell.CELL_TYPE_FORMULA:
|
|
|
+ cellValue =cell.getCellFormula();
|
|
|
+ break;
|
|
|
+ case HSSFCell.CELL_TYPE_BLANK:
|
|
|
+ logList.add("第"+(r+1)+"行第"+(c+1)+"列数据为空");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ cellValue = "";
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ logList.add("第"+(r+1)+"行第"+(c+1)+"列为空");
|
|
|
+ }
|
|
|
+ record.put(c,cellValue);
|
|
|
+ }
|
|
|
+ recordList.add(record);
|
|
|
+ }
|
|
|
+
|
|
|
+// System.out.println("logList.toString() " + logList.toString());
|
|
|
+// System.out.println("recordList.toString() " + recordList.toString());
|
|
|
+
|
|
|
+ if(logList.size() != 0){
|
|
|
+ String mes = "表格中存在问题,导入失败!";
|
|
|
+ return mes + logList.toString();
|
|
|
+ }else{
|
|
|
+ List<CrowdTestProjectPO> crowdTestProjectPOList = new ArrayList<>();
|
|
|
+ CrowdTestProjectPO crowdTestProjectPO = new CrowdTestProjectPO();
|
|
|
+ for(Map<Integer,Object> map : recordList){
|
|
|
+ crowdTestProjectPO.setName(map.get(0).toString());
|
|
|
+ crowdTestProjectPO.setPlatform(map.get(1).toString());
|
|
|
+ crowdTestProjectPO.setLinkMan(map.get(2).toString());
|
|
|
+ crowdTestProjectPO.setLinkManMobile(map.get(3).toString());
|
|
|
+ crowdTestProjectPO.setType(map.get(4).toString());
|
|
|
+ crowdTestProjectPO.setDescription(map.get(5).toString());
|
|
|
+ crowdTestProjectPOList.add(crowdTestProjectPO);
|
|
|
+ }
|
|
|
+ for(int i = 0; i < crowdTestProjectPOList.size(); i++){
|
|
|
+ CrowdTestProject crowdTestProject = Converter.convert(CrowdTestProject.class, crowdTestProjectPOList.get(i));
|
|
|
+ CrowdTestProject crowdTestProjectResult = crowdTestProject.setTaskAndReportData();
|
|
|
+ //TODO 保存数据库
|
|
|
+ crowdTestProjectRepo.saveCrowdTestProject(crowdTestProjectResult);
|
|
|
+ }
|
|
|
+ return "SUCCESS";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
* 通过表单向数据库中导入众包项目文件,并补全项目中任务和报告的数据
|
|
|
* @return
|
|
|
*/
|
|
@@ -532,4 +677,78 @@ public class CrowdTestProjectController {
|
|
|
Date beforeDate=new Date(date.getTime() - subTime);
|
|
|
return beforeDate;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
|
|
|
+ * 此方法中前三位格式有:
|
|
|
+ * 13+任意数
|
|
|
+ * 15+除4的任意数
|
|
|
+ * 18+除1和4的任意数
|
|
|
+ * 17+除9的任意数
|
|
|
+ * 147
|
|
|
+ */
|
|
|
+ private boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
|
|
|
+ String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
|
|
|
+ Pattern p = Pattern.compile(regExp);
|
|
|
+ Matcher m = p.matcher(str);
|
|
|
+ return m.matches();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 香港手机号码8位数,5|6|8|9开头+7位任意数
|
|
|
+ */
|
|
|
+ private boolean isHKPhoneLegal(String str)throws PatternSyntaxException {
|
|
|
+ String regExp = "^(5|6|8|9)\\d{7}$";
|
|
|
+ Pattern p = Pattern.compile(regExp);
|
|
|
+ Matcher m = p.matcher(str);
|
|
|
+ return m.matches();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 座机号
|
|
|
+ */
|
|
|
+ private boolean isTel(String str)throws PatternSyntaxException {
|
|
|
+ String regExp = "^0[1-9](\\d{1,2}\\-?)\\d{7,8}";
|
|
|
+ Pattern p = Pattern.compile(regExp);
|
|
|
+ Matcher m = p.matcher(str);
|
|
|
+ return m.matches();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断手机号或者座机是正确
|
|
|
+ * @param phone
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean isRightPhone(String phone){
|
|
|
+ if(phone.startsWith("0")){
|
|
|
+ if(!isTel(phone)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ if(!(isChinaPhoneLegal(phone) || isHKPhoneLegal(phone))){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 匹配平台类型 0,1,2 任意 ^[0-2](([,][0-2])?([,][0-2])?)$
|
|
|
+ */
|
|
|
+ private boolean isPlatform(String str)throws PatternSyntaxException {
|
|
|
+ String regExp = "^[0-2](([,][0-2])?([,][0-2])?)$";
|
|
|
+ Pattern p = Pattern.compile(regExp);
|
|
|
+ Matcher m = p.matcher(str);
|
|
|
+ return m.matches();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 匹配项目类型 0,1,2,3,4,5,6 任意 ^[0-6](([,][0-6])?([,][0-6])?([,][0-6])?([,][0-6])?([,][0-6])?([,][0-6])?)$
|
|
|
+ */
|
|
|
+ private boolean isType(String str)throws PatternSyntaxException {
|
|
|
+ String regExp = "^[0-6](([,][0-6])?([,][0-6])?([,][0-6])?([,][0-6])?([,][0-6])?([,][0-6])?)$";
|
|
|
+ Pattern p = Pattern.compile(regExp);
|
|
|
+ Matcher m = p.matcher(str);
|
|
|
+ return m.matches();
|
|
|
+ }
|
|
|
}
|