web导入能力实现

This commit is contained in:
wangliwen 2022-06-16 15:24:35 +08:00
parent 6b5f35e283
commit 4b7bb257ba
2 changed files with 113 additions and 57 deletions

View File

@ -1,5 +1,6 @@
package io.renren.modules.resource.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.annotation.LogOperation;
@ -10,6 +11,7 @@ import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.modules.resource.dto.ResourceDTO;
import io.renren.modules.resource.excel.ResourceExcelImportListener;
import io.renren.modules.resource.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@ -22,14 +24,20 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
/**
* 资源表
@ -72,12 +80,20 @@ public class ResourceController {
@Value("${zsk.catalogIds}")
private String[] catalogIds;
@Value("${resource.path}")
private String uploadPath;
@Autowired
private ResourceService resourceService;
@Autowired
private RestTemplate restTemplate;
@Autowired
private JdbcTemplate jdbcTemplate;
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
private static final Logger logger = LoggerFactory.getLogger(ResourceController.class);
@GetMapping("/page")
@ -186,6 +202,40 @@ public class ResourceController {
return new Result().ok(dto.getId() == null ? "" : dto.getId());
}
@PostMapping("/importResource")
@ApiOperation("导入")
@LogOperation("导入")
@ApiImplicitParams({
@ApiImplicitParam(name = "source", value = "请求来源", paramType = "string", dataType = "string")
})
public Result importResource(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) {
List<Map<String, Object>> dept =
jdbcTemplate.queryForList("SELECT id,`name` FROM sys_dept");
logger.info("上传文件:" + uploadFile.getOriginalFilename());
String format = sdf.format(new Date());
File folder = new File(uploadPath + "upload" + File.separator + format);
logger.info(uploadPath + format);
if (!folder.isDirectory()) {
folder.mkdirs();
}
// 对上传的文件重命名避免文件重名
String oldName = uploadFile.getOriginalFilename();
String newName = UUID.randomUUID().toString()
+ oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
// 文件保存
File file = new File(folder, newName);
uploadFile.transferTo(file);
EasyExcel.read(file, new ResourceExcelImportListener(0, dept, resourceService)).sheet(0).headRowNumber(6).doRead();
EasyExcel.read(file, new ResourceExcelImportListener(1, dept, resourceService)).sheet(1).headRowNumber(5).doRead();
EasyExcel.read(file, new ResourceExcelImportListener(2, dept, resourceService)).sheet(2).headRowNumber(5).doRead();
EasyExcel.read(file, new ResourceExcelImportListener(3, dept, resourceService)).sheet(3).headRowNumber(6).doRead();
} catch (IOException e) {
return new Result<String>().error(e.getMessage());
}
return new Result().ok(LocalDateTime.now().toString());
}
@PutMapping("/update")
@ApiOperation("修改")
@LogOperation("修改")

View File

@ -30,6 +30,12 @@ public class ResourceExcelImportListener extends AnalysisEventListener<Map<Integ
Map<Integer, String> headMap = null;
public ResourceExcelImportListener(int sheet, List<Map<String, Object>> deptList, ResourceService resourceService) {
this.sheet = sheet;
this.deptList = deptList;
this.resourceService = resourceService;
}
/**
* When analysis one row trigger invoke function.
*
@ -110,202 +116,202 @@ public class ResourceExcelImportListener extends AnalysisEventListener<Map<Integ
switch (sheet) {
case 0: {
resourceDTO.setType("应用资源");
resourceDTO.setName(date.get(1));
resourceDTO.setName(date.get(0));
AttrEntity attrEntity1 = new AttrEntity();
attrEntity1.setAttrType("应用状态");
attrEntity1.setAttrValue(date.get(2));
attrEntity1.setAttrValue(date.get(1));
infoList.add(attrEntity1); // 应用状态
AttrEntity attrEntity2 = new AttrEntity();
attrEntity2.setAttrType("应用类型");
attrEntity2.setAttrValue(date.get(3));
attrEntity2.setAttrValue(date.get(2));
infoList.add(attrEntity2); // 应用类型
AttrEntity attrEntity3 = new AttrEntity();
attrEntity3.setAttrType("应用领域");
attrEntity3.setAttrValue(date.get(4));
attrEntity3.setAttrValue(date.get(3));
infoList.add(attrEntity3); // 应用领域
resourceDTO.setDescription(date.get(5)); // 描述
resourceDTO.setDescription(date.get(4)); // 描述
AttrEntity attrEntity4 = new AttrEntity();
attrEntity4.setAttrType("发布端");
attrEntity4.setAttrValue(date.get(6));
attrEntity4.setAttrValue(date.get(5));
infoList.add(attrEntity4); // 发布端
resourceDTO.setDeptName(date.get(7));
resourceDTO.setDeptContacts(date.get(8)); // 部门联系人
resourceDTO.setDeptPhone(date.get(9)); // 部门联系人电话
resourceDTO.setDeptName(date.get(6));
resourceDTO.setDeptContacts(date.get(7)); // 部门联系人
resourceDTO.setDeptPhone(date.get(8)); // 部门联系人电话
AttrEntity attrEntity5 = new AttrEntity();
attrEntity5.setAttrType("应用展示视频");
attrEntity5.setAttrValue(date.get(10));
attrEntity5.setAttrValue(date.get(9));
infoList.add(attrEntity5); // 应用展示视频
AttrEntity attrEntity6 = new AttrEntity();
attrEntity6.setAttrType("应用图片");
attrEntity6.setAttrValue(date.get(11));
attrEntity6.setAttrValue(date.get(10));
infoList.add(attrEntity6); // 应用图片
AttrEntity attrEntity7 = new AttrEntity();
attrEntity7.setAttrType("功能介绍");
attrEntity7.setAttrValue(date.get(12));
attrEntity7.setAttrValue(date.get(11));
infoList.add(attrEntity7); // 功能介绍
AttrEntity attrEntity8 = new AttrEntity();
attrEntity8.setAttrType("部署区域");
attrEntity8.setAttrValue(date.get(13));
attrEntity8.setAttrValue(date.get(12));
infoList.add(attrEntity8); // 部署区域
AttrEntity attrEntity9 = new AttrEntity();
attrEntity9.setAttrType("是否统一登录");
attrEntity9.setAttrValue(date.get(14));
attrEntity9.setAttrValue(date.get(13));
infoList.add(attrEntity9); // 是否统一登录
resourceDTO.setLink(date.get(15)); // 访问地址
resourceDTO.setShareCondition(date.get(16)); // 共享条件
resourceDTO.setShareMode(date.get(17)); // 共享类型
resourceDTO.setLink(date.get(14)); // 访问地址
resourceDTO.setShareCondition(date.get(15)); // 共享条件
resourceDTO.setShareMode(date.get(16)); // 共享类型
resourceDTO.setShareType(null);
AttrEntity attrEntity10 = new AttrEntity();
attrEntity10.setAttrType("等保定级");
attrEntity10.setAttrValue(date.get(18));
attrEntity10.setAttrValue(date.get(17));
infoList.add(attrEntity10); // 等保定级
AttrEntity attrEntity11 = new AttrEntity();
attrEntity11.setAttrType("是否等保备案");
attrEntity11.setAttrValue(date.get(19));
attrEntity11.setAttrValue(date.get(18));
infoList.add(attrEntity11); // 是否等保备案
AttrEntity attrEntity12 = new AttrEntity();
attrEntity12.setAttrType("服务商");
attrEntity12.setAttrValue(date.get(20));
attrEntity12.setAttrValue(date.get(19));
infoList.add(attrEntity12); // 服务商
AttrEntity attrEntity13 = new AttrEntity();
attrEntity13.setAttrType("服务商联系人");
attrEntity13.setAttrValue(date.get(21));
attrEntity13.setAttrValue(date.get(20));
infoList.add(attrEntity13); // 服务商联系人
AttrEntity attrEntity14 = new AttrEntity();
attrEntity14.setAttrType("服务商联系电话");
attrEntity14.setAttrValue(date.get(22));
attrEntity14.setAttrValue(date.get(21));
infoList.add(attrEntity14); // 服务商联系人
AttrEntity attrEntity15 = new AttrEntity();
attrEntity15.setAttrType("常见问题");
attrEntity15.setAttrValue(date.get(23));
attrEntity15.setAttrValue(date.get(22));
infoList.add(attrEntity15); // 常见问题
}
break;
case 1: {
resourceDTO.setType("组件服务");
resourceDTO.setName(date.get(1));
resourceDTO.setName(date.get(0));
AttrEntity attrEntity1 = new AttrEntity();
attrEntity1.setAttrType("算法类别");
attrEntity1.setAttrValue(date.get(2));
attrEntity1.setAttrValue(date.get(1));
infoList.add(attrEntity1); // 算法类别
AttrEntity attrEntity2 = new AttrEntity();
attrEntity2.setAttrType("是否免批");
attrEntity2.setAttrValue(date.get(3));
attrEntity2.setAttrValue(date.get(2));
infoList.add(attrEntity2); // 算法类别
AttrEntity attrEntity3 = new AttrEntity();
attrEntity3.setAttrType("使用方式");
attrEntity3.setAttrValue(date.get(4));
attrEntity3.setAttrValue(date.get(3));
infoList.add(attrEntity3); // 使用方式
AttrEntity attrEntity4 = new AttrEntity();
attrEntity4.setAttrType("部署位置");
attrEntity4.setAttrValue(date.get(5));
attrEntity4.setAttrValue(date.get(4));
infoList.add(attrEntity4); // 部署位置
AttrEntity attrEntity5 = new AttrEntity();
attrEntity5.setAttrType("应用领域");
attrEntity5.setAttrValue(date.get(6));
attrEntity5.setAttrValue(date.get(5));
infoList.add(attrEntity5); // 应用领域
resourceDTO.setShareCondition(date.get(7)); // 共享条件
resourceDTO.setShareMode(date.get(8)); // 共享类型
resourceDTO.setShareCondition(date.get(6)); // 共享条件
resourceDTO.setShareMode(date.get(7)); // 共享类型
resourceDTO.setShareType(null);
resourceDTO.setDescription(date.get(9));
resourceDTO.setDescription(date.get(8));
AttrEntity attrEntity6 = new AttrEntity();
attrEntity6.setAttrType("算法介绍视频");
attrEntity6.setAttrValue(date.get(10));
attrEntity6.setAttrValue(date.get(9));
infoList.add(attrEntity5); // 应用领域
AttrEntity attrEntity7 = new AttrEntity();
attrEntity7.setAttrType("服务商");
attrEntity7.setAttrValue(date.get(11));
attrEntity7.setAttrValue(date.get(10));
infoList.add(attrEntity7); // 服务商
AttrEntity attrEntity8 = new AttrEntity();
attrEntity8.setAttrType("服务商联系人");
attrEntity8.setAttrValue(date.get(12));
attrEntity8.setAttrValue(date.get(11));
infoList.add(attrEntity8); // 服务商联系人
AttrEntity attrEntity9 = new AttrEntity();
attrEntity9.setAttrType("服务商联系电话");
attrEntity9.setAttrValue(date.get(13));
attrEntity9.setAttrValue(date.get(12));
infoList.add(attrEntity9); // 服务商联系人
AttrEntity attrEntity10 = new AttrEntity();
attrEntity10.setAttrType("关联应用");
attrEntity10.setAttrValue(date.get(14));
attrEntity10.setAttrValue(date.get(13));
infoList.add(attrEntity10); // 关联应用
AttrEntity attrEntity11 = new AttrEntity();
attrEntity11.setAttrType("算法优势");
attrEntity11.setAttrValue(date.get(15));
attrEntity11.setAttrValue(date.get(14));
infoList.add(attrEntity11); // 算法优势
AttrEntity attrEntity12 = new AttrEntity();
attrEntity12.setAttrType("应用场景");
attrEntity12.setAttrValue(date.get(16));
attrEntity12.setAttrValue(date.get(15));
infoList.add(attrEntity12); // 应用场景
resourceDTO.setLink(date.get(17));
resourceDTO.setApiMethodType(date.get(18));
resourceDTO.setLink(date.get(16));
resourceDTO.setApiMethodType(date.get(17));
AttrEntity attrEntity13 = new AttrEntity();
attrEntity13.setAttrType("技术文档");
attrEntity13.setAttrValue(date.get(19));
attrEntity13.setAttrValue(date.get(18));
infoList.add(attrEntity13); // 技术文档
AttrEntity attrEntity14 = new AttrEntity();
attrEntity14.setAttrType("试用地址");
attrEntity14.setAttrValue(date.get(20));
attrEntity14.setAttrValue(date.get(19));
infoList.add(attrEntity14); // 试用地址
AttrEntity attrEntity15 = new AttrEntity();
attrEntity15.setAttrType("是否收费");
attrEntity15.setAttrValue(date.get(21));
attrEntity15.setAttrValue(date.get(20));
infoList.add(attrEntity15); // 是否收费
AttrEntity attrEntity16 = new AttrEntity();
attrEntity16.setAttrType("计费标准信息");
attrEntity16.setAttrValue(date.get(22));
attrEntity16.setAttrValue(date.get(21));
infoList.add(attrEntity16); // 计费标准信息
AttrEntity attrEntity17 = new AttrEntity();
attrEntity17.setAttrType("常见问题");
attrEntity17.setAttrValue(date.get(23));
attrEntity17.setAttrValue(date.get(22));
infoList.add(attrEntity17); // 常见问题
}
break;
case 2: {
resourceDTO.setType("知识库");
resourceDTO.setName(date.get(1));
resourceDTO.setLink(date.get(2));
resourceDTO.setDescription(date.get(3));
resourceDTO.setName(date.get(0));
resourceDTO.setLink(date.get(1));
resourceDTO.setDescription(date.get(2));
Date createDate = new Date(Long.parseLong(date.get(5)));
Date createDate = new Date(Long.parseLong(date.get(4)));
resourceDTO.setCreateDate(createDate);
AttrEntity attrEntity = new AttrEntity();
@ -324,13 +330,13 @@ public class ResourceExcelImportListener extends AnalysisEventListener<Map<Integ
}
case 3: {
resourceDTO.setType("基础设施");
resourceDTO.setName(date.get(1));
resourceDTO.setName(date.get(0));
this.headMap.keySet().stream().skip(1).forEach(key -> { // 表头后1个
AttrEntity attrEntity = new AttrEntity();
attrEntity.setAttrType(this.headMap.get(key));
attrEntity.setAttrValue(date.get(key));
infoList.add(attrEntity);
AttrEntity attrEntity_ = new AttrEntity();
attrEntity_.setAttrType(this.headMap.get(key));
attrEntity_.setAttrValue(date.get(key));
infoList.add(attrEntity_);
});
}