|
@@ -0,0 +1,216 @@
|
|
|
+package com.macro.mall.tiny.modules.business.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.macro.mall.tiny.common.exception.ApiException;
|
|
|
+import com.macro.mall.tiny.common.exception.Asserts;
|
|
|
+import com.macro.mall.tiny.common.util.CommonUtils;
|
|
|
+import com.macro.mall.tiny.config.UploadConfig;
|
|
|
+import com.macro.mall.tiny.modules.business.enume.DirectoryEnum;
|
|
|
+import com.macro.mall.tiny.modules.business.model.*;
|
|
|
+import com.macro.mall.tiny.modules.business.service.BLineService;
|
|
|
+import com.macro.mall.tiny.modules.business.service.BLineUploadLogService;
|
|
|
+import com.macro.mall.tiny.modules.business.service.BTowerService;
|
|
|
+import com.macro.mall.tiny.modules.ums.model.UmsAdmin;
|
|
|
+import com.macro.mall.tiny.modules.ums.model.UmsRole;
|
|
|
+import com.macro.mall.tiny.modules.ums.service.UmsAdminService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.nio.file.StandardOpenOption;
|
|
|
+import java.security.Principal;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipFile;
|
|
|
+import java.util.zip.ZipInputStream;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author gjs
|
|
|
+ * @description
|
|
|
+ * @date 2021/3/28 17:44
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class AsyncHandler {
|
|
|
+
|
|
|
+ @Value("${system.charset:gbk}")
|
|
|
+ private String charset;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UmsAdminService adminService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BProvinceServiceImpl provinceService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BLineService lineService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BLineUploadLogService lineUploadLogService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BTowerService towerService;
|
|
|
+
|
|
|
+ @Async
|
|
|
+ @Transactional
|
|
|
+ public void handleDocument(Long provinceId, String fileSource, String lineName, Principal principal) {
|
|
|
+ StringBuilder warnMsg = new StringBuilder();
|
|
|
+ try {
|
|
|
+ if (provinceId == null) Asserts.fail("上传失败,省份不能为空,provinceId:" + provinceId);
|
|
|
+ if (principal == null) Asserts.fail("上传失败,未获取到当前用户");
|
|
|
+
|
|
|
+ BProvince province = provinceService.getById(provinceId);
|
|
|
+ String provinceName = province.getProvince();
|
|
|
+
|
|
|
+ String username = principal.getName();
|
|
|
+ UmsAdmin umsAdmin = adminService.getAdminByUsername(username);
|
|
|
+ Long userId = umsAdmin.getId();
|
|
|
+ boolean canCover = false;
|
|
|
+ List<UmsRole> roleList = adminService.getRoleList(umsAdmin.getId());
|
|
|
+ for (UmsRole umsRole : roleList) {
|
|
|
+ if (umsRole.getName().equals("超级管理员")) {
|
|
|
+ canCover = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 查看是否为线路创建者
|
|
|
+ QueryWrapper<BLine> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.lambda().eq(BLine::getName, lineName);
|
|
|
+ BLine line = lineService.getOne(wrapper);
|
|
|
+ if (line == null) {
|
|
|
+ // 如果为空,则创建新线路
|
|
|
+ BLine newLine = new BLine();
|
|
|
+ newLine.setCreatorId(userId);
|
|
|
+ newLine.setName(lineName);
|
|
|
+ newLine.setProvinceId(provinceId);
|
|
|
+ lineService.save(newLine);
|
|
|
+ line = newLine;
|
|
|
+
|
|
|
+ canCover = true;
|
|
|
+ } else if (line.getCreatorId().equals(userId)) {
|
|
|
+ canCover = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 先获取根目录
|
|
|
+ Path rootPath = Paths.get(UploadConfig.powerPath + provinceName);
|
|
|
+ if (!Files.exists(rootPath)) {
|
|
|
+ Files.createDirectories(rootPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取暂存文件Path
|
|
|
+ Path filePath = Paths.get(fileSource);
|
|
|
+
|
|
|
+ // 获取枚举类判断
|
|
|
+ DirectoryEnum[] enums = DirectoryEnum.values();
|
|
|
+ HashMap<String, Boolean> booleanHashMap = new HashMap<>();
|
|
|
+ for (DirectoryEnum directory : enums) {
|
|
|
+ booleanHashMap.put(directory.getName(), false);
|
|
|
+ }
|
|
|
+
|
|
|
+ try (ZipFile zipFile = new ZipFile(filePath.toFile(), Charset.forName(charset))) {
|
|
|
+ // 读取zip流
|
|
|
+ try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(filePath), Charset.forName(charset))) {
|
|
|
+ ZipEntry zipEntry;
|
|
|
+ // 遍历每一个zip项
|
|
|
+ while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
|
|
+ // 获取zip项目名称
|
|
|
+ String entryName = zipEntry.getName();
|
|
|
+
|
|
|
+ // 解析各项路径
|
|
|
+ String[] pathStr = entryName.split("/");
|
|
|
+ if (pathStr.length < 2) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String SecDirectoryName = pathStr[1].charAt(0) == '#' ? pathStr[1].substring(1) : pathStr[1];
|
|
|
+ if (booleanHashMap.get(SecDirectoryName) != null) {
|
|
|
+ booleanHashMap.put(SecDirectoryName, true);
|
|
|
+ } else if ("杆塔信息录入.xlsx".equals(SecDirectoryName) || "杆塔信息录入.xls".equals(SecDirectoryName)) {
|
|
|
+ List<TowerExcelModel> modelArrayList = EasyExcel.read(zipFile.getInputStream(zipEntry), TowerExcelModel.class, null).sheet().doReadSync();
|
|
|
+ BLine finalLine = line;
|
|
|
+ List<BTower> towerList = modelArrayList.stream().map(model -> {
|
|
|
+ BTower tower = new BTower();
|
|
|
+ tower.setName(model.getName());
|
|
|
+ tower.setShape(model.getShape());
|
|
|
+ tower.setSort(model.getSort());
|
|
|
+ tower.setHardwareType(model.getHardware());
|
|
|
+ // 经纬度设置
|
|
|
+ String lonLat = model.getLonLat();
|
|
|
+ String[] lonLats = lonLat.split(",");
|
|
|
+ BigDecimal lon = CommonUtils.Dms2D(lonLats[0]);
|
|
|
+ BigDecimal lat = CommonUtils.Dms2D(lonLats[1]);
|
|
|
+ tower.setLon(lon);
|
|
|
+ tower.setLat(lat);
|
|
|
+ tower.setLineId(finalLine.getId());
|
|
|
+ return tower;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ towerService.saveBatch(towerList);
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ warnMsg.append(entryName).append("第二层目录解析出错;");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 构建绝对路径
|
|
|
+ Path entryFile = rootPath.resolve(entryName);
|
|
|
+ if (zipEntry.isDirectory()) { // 文件夹
|
|
|
+ if (!Files.isDirectory(entryFile)) {
|
|
|
+ Files.createDirectories(entryFile);
|
|
|
+ }
|
|
|
+ } else if (!canCover && Files.exists(entryFile)) {
|
|
|
+ warnMsg.append(entryName).append("已存在,无权覆盖;");
|
|
|
+ } else { // 文件
|
|
|
+ // 读取zip项数据流
|
|
|
+ try (InputStream zipEntryInputStream = zipFile.getInputStream(zipEntry)) {
|
|
|
+ try (OutputStream fileOutputStream = Files.newOutputStream(entryFile, StandardOpenOption.CREATE)) {
|
|
|
+ byte[] buffer = new byte[2048];
|
|
|
+ int length;
|
|
|
+ while ((length = zipEntryInputStream.read(buffer)) != -1) {
|
|
|
+ fileOutputStream.write(buffer, 0, length);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ booleanHashMap.forEach((key, val) -> {
|
|
|
+ if (!val) {
|
|
|
+ warnMsg.append("缺少").append(key).append("文件夹;");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 存储上传记录
|
|
|
+ BLineUploadLog lineUploadLog = new BLineUploadLog();
|
|
|
+ lineUploadLog.setLineId(line.getId());
|
|
|
+ lineUploadLog.setStatus(true);
|
|
|
+ lineUploadLog.setUploaderId(userId);
|
|
|
+ lineUploadLog.setUploadTime(new Date());
|
|
|
+ lineUploadLog.setWarnMsg(warnMsg.toString());
|
|
|
+ lineUploadLogService.save(lineUploadLog);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ log.error("上传文件抛出业务异常", e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("上传文件抛出异常", e);
|
|
|
+ } finally {
|
|
|
+ File file = new File(fileSource);
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|