Przeglądaj źródła

站点导入功能

yanliming 4 lat temu
rodzic
commit
c7c74b4269

+ 138 - 0
web/src/main/java/com/jpsoft/bus/modules/bus/controller/StationInfoController.java

@@ -1,20 +1,34 @@
 package com.jpsoft.bus.modules.bus.controller;
 
 import com.github.pagehelper.Page;
+import com.jpsoft.bus.config.OSSConfig;
+import com.jpsoft.bus.modules.bus.entity.CompanyInfo;
+import com.jpsoft.bus.modules.bus.entity.RouteInfo;
 import com.jpsoft.bus.modules.common.dto.MessageResult;
 import com.jpsoft.bus.modules.common.dto.Sort;
 import com.jpsoft.bus.modules.bus.entity.StationInfo;
 import com.jpsoft.bus.modules.bus.service.StationInfoService;
+import com.jpsoft.bus.modules.common.utils.OSSUtil;
+import com.jpsoft.bus.modules.common.utils.POIUtils;
 import com.jpsoft.bus.modules.common.utils.PojoUtils;
+import com.jpsoft.bus.modules.sys.entity.DataDictionary;
+import com.jpsoft.bus.modules.sys.service.DataDictionaryService;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
@@ -27,9 +41,15 @@ import java.util.*;
 public class StationInfoController {
     private Logger logger = LoggerFactory.getLogger(getClass());
 
+    @Autowired
+    private OSSConfig ossConfig;
+
     @Autowired
     private StationInfoService stationInfoService;
 
+    @Autowired
+    private DataDictionaryService dataDictionaryService;
+
     @ApiOperation(value="创建空记录")
     @GetMapping("create")
     public MessageResult<StationInfo> create(){
@@ -225,4 +245,122 @@ public class StationInfoController {
 
         return msgResult;
     }
+
+
+    @ApiOperation(value="导入站点")
+    @PostMapping("importXls")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "uploadFile",value = "上传文件", required = true,paramType="form", dataType = "__file")
+    })
+    public MessageResult<String> importXls(MultipartFile uploadFile,String routeId,
+                                           @RequestAttribute String subject){
+        MessageResult<String> msgResult = new MessageResult<>();
+
+        try {
+            POIUtils poiUtils = new POIUtils(uploadFile.getInputStream());
+            int sheetIndex = 0;
+            Sheet sheet1 = poiUtils.getSheetAt(sheetIndex);
+
+            int affectCount = 0;
+            int failCount = 0;
+            int validateColIndex = 10;
+
+            for(int rowIndex=1 ; rowIndex<=sheet1.getLastRowNum(); rowIndex++){
+                try {
+                    String name = (String)poiUtils.getCellValue(sheetIndex,rowIndex,0).toString().replace(" ","");
+                    String classifyName = (String)poiUtils.getCellValue(sheetIndex,rowIndex,1).toString().replace(" ","");
+                    String longitudeAndLatitude = (String)poiUtils.getCellValue(sheetIndex,rowIndex,2).toString().replace(" ","");
+
+                    String longitude = "";
+                    String Latitude = "";
+
+                    if(StringUtils.isEmpty(classifyName)){
+                        break;
+                    }
+
+                    List<DataDictionary> list = dataDictionaryService.findByCatalogName("站点类型");
+
+                    Integer classify = 0;
+
+                    for (DataDictionary dd:list) {
+                        if(dd.getName().equals(classifyName)){
+                            classify = Integer.parseInt(dd.getValue());
+                            break;
+                        }
+                    }
+
+                    if (classify == 0) {
+                        sheet1.getRow(rowIndex).createCell(validateColIndex).setCellValue("不存在的站点类型!");
+                        failCount++;
+                        continue;
+                    }
+
+                    if(StringUtils.isNotEmpty(longitudeAndLatitude)){
+                        String[] longitudeAndLatitudeArray = longitudeAndLatitude.split(",");
+                        if(longitudeAndLatitudeArray.length == 2){
+                            longitude = longitudeAndLatitudeArray[0];
+                            Latitude =  longitudeAndLatitudeArray[1];
+                        }
+                        else{
+                            sheet1.getRow(rowIndex).createCell(validateColIndex).setCellValue("经纬度数据不合法!");
+                            failCount++;
+                            continue;
+                        }
+                    }
+
+                    StationInfo stationInfo = new StationInfo();
+                    stationInfo.setId(UUID.randomUUID().toString());
+                    stationInfo.setName(name);
+                    stationInfo.setClassify(classify);
+                    stationInfo.setLongitude(longitude);
+                    stationInfo.setLatitude(Latitude);
+                    stationInfo.setRouteId(routeId);
+
+                    stationInfo.setDelFlag(false);
+                    stationInfo.setCreateBy(subject);
+                    stationInfo.setCreateTime(new Date());
+                    stationInfoService.insert(stationInfo);
+
+                    affectCount++;
+
+                }
+                catch(Exception innerEx){
+                    logger.error(innerEx.getMessage(),innerEx);
+                }
+            }
+
+            if (failCount>0){
+                //有导入失败的记录
+                msgResult.setResult(false);
+                msgResult.setMessage("数据成功导入" + affectCount + "条,有" + failCount + "条数据未导入成功,错误原因请查看报表。");
+
+                //todo 只保留错误数据的sheet
+                Workbook wb = poiUtils.exportErrorXls(sheetIndex,validateColIndex,1 + affectCount + failCount);
+
+                //todo 将wb保存到oss
+                ByteArrayOutputStream output = new ByteArrayOutputStream();
+                wb.write(output);
+
+                byte[] buffer = output.toByteArray();
+                ByteArrayInputStream input = new ByteArrayInputStream(buffer);
+
+                String downloadUrl = OSSUtil.upload(ossConfig,"import","error.xls",input);
+
+                //todo 返回导入失败报表下载链接
+                msgResult.setData(downloadUrl);
+            }
+            else{
+                msgResult.setResult(true);
+                msgResult.setMessage("数据成功导入" + affectCount + "条");
+            }
+        }
+        catch(Exception ex){
+            logger.error(ex.getMessage(),ex);
+
+            msgResult.setResult(false);
+            msgResult.setMessage(ex.getMessage());
+        }
+
+        return msgResult;
+    }
 }