Browse Source

增加旅游资源导入导出和模版

dev
luyan 2 years ago
parent
commit
ff4d0ac4eb
  1. 34
      epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/IcPublicServiceController.java
  2. 38
      epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/excel/handler/IcPublicServiceExcelImportListener.java
  3. 2
      epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcPublicServiceService.java
  4. 17
      epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/IcPublicServiceServiceImpl.java
  5. BIN
      epmet-module/gov-org/gov-org-server/src/main/resources/excel/tourism_resource_template.xlsx
  6. 2
      epmet-module/gov-org/gov-org-server/src/main/resources/mapper/IcPublicServiceDao.xml

34
epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/IcPublicServiceController.java

@ -36,6 +36,7 @@ import com.epmet.utils.ImportTaskUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -45,6 +46,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -67,6 +69,8 @@ import java.util.UUID;
@Slf4j @Slf4j
public class IcPublicServiceController implements ResultDataResolver { public class IcPublicServiceController implements ResultDataResolver {
private static final String CATEGORY_TYPE = "tourism_resource";
@Autowired @Autowired
private IcPublicServiceService icPublicServiceService; private IcPublicServiceService icPublicServiceService;
@ -118,13 +122,20 @@ public class IcPublicServiceController implements ResultDataResolver {
} }
@RequestMapping(value = "download", method = {RequestMethod.GET, RequestMethod.POST}) @RequestMapping(value = "download", method = {RequestMethod.GET, RequestMethod.POST})
public void downloadTemplate(HttpServletResponse response) throws IOException { public void downloadTemplate(HttpServletResponse response, @PathParam("categoryType") String categoryType) throws IOException {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
response.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition"); response.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition");
String fileName;
if (StringUtils.isNotEmpty(categoryType) && categoryType.equals(CATEGORY_TYPE)) {
response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("旅游资源导入模板", "UTF-8") + ".xlsx");
fileName = "excel/tourism_resource_template.xlsx";
} else {
response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("公共服务导入模板", "UTF-8") + ".xlsx"); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("公共服务导入模板", "UTF-8") + ".xlsx");
fileName = "excel/public_service_template.xlsx";
InputStream is = this.getClass().getClassLoader().getResourceAsStream("excel/public_service_template.xlsx"); }
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName);
try { try {
ServletOutputStream os = response.getOutputStream(); ServletOutputStream os = response.getOutputStream();
IOUtils.copy(is, os); IOUtils.copy(is, os);
@ -144,7 +155,8 @@ public class IcPublicServiceController implements ResultDataResolver {
formDTO.setPageNo(NumConstant.ONE); formDTO.setPageNo(NumConstant.ONE);
formDTO.setPageSize(NumConstant.TEN_THOUSAND); formDTO.setPageSize(NumConstant.TEN_THOUSAND);
try { try {
String fileName = "公共服务管理" + DateUtils.format(new Date()) + ".xlsx"; String fileName = formDTO.getCategoryType().equals(CATEGORY_TYPE) ? "旅游资源管理" + DateUtils.format(new Date()) + ".xlsx" :
"公共服务管理" + DateUtils.format(new Date()) + ".xlsx";
// 头的策略 // 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle(); WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 背景设置为红色 // 背景设置为红色
@ -184,18 +196,22 @@ public class IcPublicServiceController implements ResultDataResolver {
} }
@PostMapping("import") @PostMapping("import")
public Result importExcel(MultipartFile file) { public Result importExcel(MultipartFile file, String categoryType) {
// 1.暂存文件 // 1.暂存文件
String originalFilename = file.getOriginalFilename(); String originalFilename = file.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf(".")); String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
Path fileSavePath; Path fileSavePath;
try { try {
Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_public_service", "import"); Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir(categoryType, "import");
fileSavePath = importPath.resolve(UUID.randomUUID().toString().concat(extName)); fileSavePath = importPath.resolve(UUID.randomUUID().toString().concat(extName));
} catch (IOException e) { } catch (IOException e) {
String errorMsg = ExceptionUtils.getErrorStackTrace(e); String errorMsg = ExceptionUtils.getErrorStackTrace(e);
if (categoryType.equals(CATEGORY_TYPE)) {
log.error("【旅游资源管理导入】创建临时存储文件失败:{}", errorMsg);
} else {
log.error("【公共服务管理导入】创建临时存储文件失败:{}", errorMsg); log.error("【公共服务管理导入】创建临时存储文件失败:{}", errorMsg);
}
throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "文件上传失败", "文件上传失败"); throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "文件上传失败", "文件上传失败");
} }
@ -217,11 +233,11 @@ public class IcPublicServiceController implements ResultDataResolver {
ImportTaskUtils.createImportTask(originalFilename, ImportTaskConstants.IC_PUBLIC_SERVICE), ImportTaskUtils.createImportTask(originalFilename, ImportTaskConstants.IC_PUBLIC_SERVICE),
ServiceConstant.EPMET_COMMON_SERVICE, ServiceConstant.EPMET_COMMON_SERVICE,
EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),
"excel导入公共服务管理信息错误", categoryType.equals(CATEGORY_TYPE) ? "excel导入旅游资源管理信息错误" : "excel导入公共服务管理信息错误",
"导入公共服务管理信息失败"); categoryType.equals(CATEGORY_TYPE) ? "导入旅游资源管理信息失败" : "导入公共服务管理信息失败");
// 3.执行导入 // 3.执行导入
icPublicServiceService.importExcel(fileSavePath, rstData.getTaskId()); icPublicServiceService.importExcel(fileSavePath, rstData.getTaskId(), categoryType);
return new Result(); return new Result();
} }

38
epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/excel/handler/IcPublicServiceExcelImportListener.java

@ -37,6 +37,7 @@ public class IcPublicServiceExcelImportListener implements ReadListener<IcPublic
private String currentCustomerId; private String currentCustomerId;
private String currentAgencyId; private String currentAgencyId;
private String currentAgencyPids; private String currentAgencyPids;
private String categoryType;
//数据 //数据
private List<IcPublicServiceEntity> datas = new ArrayList<>(); private List<IcPublicServiceEntity> datas = new ArrayList<>();
@ -50,12 +51,13 @@ public class IcPublicServiceExcelImportListener implements ReadListener<IcPublic
//存放当前组织下已存在的场所信息 //存放当前组织下已存在的场所信息
private Map<String, String> existMap = null; private Map<String, String> existMap = null;
public IcPublicServiceExcelImportListener(String currentUserId, String currentCustomerId, String currentAgencyId, String currentAgencyPids, public IcPublicServiceExcelImportListener(String currentUserId, String currentCustomerId, String currentAgencyId, String currentAgencyPids, String categoryType,
IcPublicServiceServiceImpl IcPublicServiceServiceImpl, CoverageService coverageService, IcPublicServiceDao icPublicServiceDao) { IcPublicServiceServiceImpl IcPublicServiceServiceImpl, CoverageService coverageService, IcPublicServiceDao icPublicServiceDao) {
this.currentUserId = currentUserId; this.currentUserId = currentUserId;
this.currentCustomerId = currentCustomerId; this.currentCustomerId = currentCustomerId;
this.currentAgencyId = currentAgencyId; this.currentAgencyId = currentAgencyId;
this.currentAgencyPids = currentAgencyPids; this.currentAgencyPids = currentAgencyPids;
this.categoryType = categoryType;
this.IcPublicServiceServiceImpl = IcPublicServiceServiceImpl; this.IcPublicServiceServiceImpl = IcPublicServiceServiceImpl;
this.coverageService = coverageService; this.coverageService = coverageService;
this.icPublicServiceDao = icPublicServiceDao; this.icPublicServiceDao = icPublicServiceDao;
@ -88,33 +90,37 @@ public class IcPublicServiceExcelImportListener implements ReadListener<IcPublic
data.setAddress(data.getAddress().trim()); data.setAddress(data.getAddress().trim());
} }
IcPublicServiceEntity IcPublicServiceEntity = ConvertUtils.sourceToTarget(data, IcPublicServiceEntity.class); IcPublicServiceEntity icPublicServiceEntity = ConvertUtils.sourceToTarget(data, IcPublicServiceEntity.class);
IcPublicServiceEntity.setCustomerId(currentCustomerId); icPublicServiceEntity.setCustomerId(currentCustomerId);
IcPublicServiceEntity.setAgencyId(currentAgencyId); icPublicServiceEntity.setAgencyId(currentAgencyId);
IcPublicServiceEntity.setAgencyIdPath(currentAgencyPids); icPublicServiceEntity.setAgencyIdPath(currentAgencyPids);
IcPublicServiceEntity.setSourceType("import"); icPublicServiceEntity.setSourceType("import");
IcPublicServiceEntity.setCategory(data.getCategoryName()); icPublicServiceEntity.setCategory(data.getCategoryName());
IcPublicServiceEntity.setAreaCovered(data.getAreaCoveredName()); icPublicServiceEntity.setAreaCovered(data.getAreaCoveredName());
IcPublicServiceEntity.setPrincipal(data.getPrincipal()); icPublicServiceEntity.setPrincipal(data.getPrincipal());
IcPublicServiceEntity.setMobile(data.getMobile()); icPublicServiceEntity.setMobile(data.getMobile());
if (StringUtils.isNotBlank(data.getCapacityName())) { if (StringUtils.isNotBlank(data.getCapacityName())) {
IcPublicServiceEntity.setCapacity(Integer.parseInt(data.getCapacityName())); icPublicServiceEntity.setCapacity(Integer.parseInt(data.getCapacityName()));
} }
IcPublicServiceEntity.setCreatedBy(currentUserId); icPublicServiceEntity.setCreatedBy(currentUserId);
IcPublicServiceEntity.setUpdatedBy(currentUserId); icPublicServiceEntity.setUpdatedBy(currentUserId);
datas.add(IcPublicServiceEntity); datas.add(icPublicServiceEntity);
if (datas.size() == MAX_THRESHOLD) { if (datas.size() == MAX_THRESHOLD) {
execPersist(); execPersist();
} }
} catch (Exception e) { } catch (Exception e) {
String errorMsg = null; String errorMsg;
if (e instanceof ValidateException) { if (e instanceof ValidateException) {
errorMsg = ((ValidateException) e).getMsg(); errorMsg = ((ValidateException) e).getMsg();
} else { } else {
errorMsg = "未知错误"; errorMsg = "未知错误";
if (categoryType.equals("tourism_resource")) {
log.error("【旅游资源管理导入】出错:{}", ExceptionUtils.getErrorStackTrace(e));
} else {
log.error("【公共服务管理导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); log.error("【公共服务管理导入】出错:{}", ExceptionUtils.getErrorStackTrace(e));
} }
}
IcPublicServiceExcel.RowRemarkMessage errorRow = new IcPublicServiceExcel.RowRemarkMessage(); IcPublicServiceExcel.RowRemarkMessage errorRow = new IcPublicServiceExcel.RowRemarkMessage();
errorRow.setName(data.getName()); errorRow.setName(data.getName());
@ -150,7 +156,7 @@ public class IcPublicServiceExcelImportListener implements ReadListener<IcPublic
*/ */
private void execPersist() { private void execPersist() {
try { try {
List<IcCoverageCategoryDictListResultDTO> dictList = coverageService.dictMap(currentCustomerId, "public_service"); List<IcCoverageCategoryDictListResultDTO> dictList = coverageService.dictMap(currentCustomerId, categoryType);
Map<String, String> dictMap = dictList.stream().collect(Collectors.toMap(IcCoverageCategoryDictListResultDTO::getLabel, IcCoverageCategoryDictListResultDTO::getValue)); Map<String, String> dictMap = dictList.stream().collect(Collectors.toMap(IcCoverageCategoryDictListResultDTO::getLabel, IcCoverageCategoryDictListResultDTO::getValue));
datas.forEach(d -> { datas.forEach(d -> {
d.setCategory(dictMap.get(d.getCategory())); d.setCategory(dictMap.get(d.getCategory()));

2
epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/IcPublicServiceService.java

@ -69,5 +69,5 @@ public interface IcPublicServiceService extends BaseService<IcPublicServiceEntit
IcPublicServiceListResultDTO detail(IcPublicServiceListFormDTO formDTO); IcPublicServiceListResultDTO detail(IcPublicServiceListFormDTO formDTO);
void importExcel(Path fileSavePath, String taskId); void importExcel(Path fileSavePath, String taskId, String categoryType);
} }

17
epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/IcPublicServiceServiceImpl.java

@ -213,7 +213,7 @@ public class IcPublicServiceServiceImpl extends BaseServiceImpl<IcPublicServiceD
@Async @Async
@Override @Override
public void importExcel(Path filePath, String importTaskId) { public void importExcel(Path filePath, String importTaskId, String categoryType) {
String userId = null; String userId = null;
try { try {
userId = EpmetRequestHolder.getHeader(AppClientConstant.USER_ID); userId = EpmetRequestHolder.getHeader(AppClientConstant.USER_ID);
@ -227,7 +227,7 @@ public class IcPublicServiceServiceImpl extends BaseServiceImpl<IcPublicServiceD
String agencyId = staffInfo.getAgencyId(); String agencyId = staffInfo.getAgencyId();
String agencyIdPath = StringUtils.isNotBlank(staffInfo.getAgencyPIds()) ? staffInfo.getAgencyPIds() + ":" + staffInfo.getAgencyId() : staffInfo.getAgencyId(); String agencyIdPath = StringUtils.isNotBlank(staffInfo.getAgencyPIds()) ? staffInfo.getAgencyPIds() + ":" + staffInfo.getAgencyId() : staffInfo.getAgencyId();
IcPublicServiceExcelImportListener listener = new IcPublicServiceExcelImportListener(userId, customerId, agencyId, agencyIdPath, this, coverageService, icPublicServiceDao); IcPublicServiceExcelImportListener listener = new IcPublicServiceExcelImportListener(userId, customerId, agencyId, agencyIdPath, categoryType, this, coverageService, icPublicServiceDao);
EasyExcel.read(filePath.toFile(), IcPublicServiceExcel.class, listener).headRowNumber(1).sheet(0).doRead(); EasyExcel.read(filePath.toFile(), IcPublicServiceExcel.class, listener).headRowNumber(1).sheet(0).doRead();
@ -280,12 +280,19 @@ public class IcPublicServiceServiceImpl extends BaseServiceImpl<IcPublicServiceD
""); "");
if (!result.success()) { if (!result.success()) {
if (categoryType.equals("tourism_resource")) {
log.error("【旅游资源管理导入】finishImportTask失败");
} else {
log.error("【公共服务管理导入】finishImportTask失败"); log.error("【公共服务管理导入】finishImportTask失败");
} }
}
} catch (Exception e) { } catch (Exception e) {
String errorMsg = ExceptionUtils.getErrorStackTrace(e); String errorMsg = ExceptionUtils.getErrorStackTrace(e);
if (categoryType.equals("tourism_resource")) {
log.error("【旅游资源管理导入】出错:{}", errorMsg);
} else {
log.error("【公共服务管理导入】出错:{}", errorMsg); log.error("【公共服务管理导入】出错:{}", errorMsg);
}
ImportTaskCommonFormDTO importFinishTaskForm = new ImportTaskCommonFormDTO(); ImportTaskCommonFormDTO importFinishTaskForm = new ImportTaskCommonFormDTO();
importFinishTaskForm.setTaskId(importTaskId); importFinishTaskForm.setTaskId(importTaskId);
importFinishTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL); importFinishTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL);
@ -294,8 +301,12 @@ public class IcPublicServiceServiceImpl extends BaseServiceImpl<IcPublicServiceD
Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm); Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm);
if (!result.success()) { if (!result.success()) {
if (categoryType.equals("tourism_resource")) {
log.error("【旅游资源管理导入】导入记录状态修改为'完成'失败");
} else {
log.error("【公共服务管理导入】导入记录状态修改为'完成'失败"); log.error("【公共服务管理导入】导入记录状态修改为'完成'失败");
} }
}
} finally { } finally {
// 删除临时文件 // 删除临时文件
if (Files.exists(filePath)) { if (Files.exists(filePath)) {

BIN
epmet-module/gov-org/gov-org-server/src/main/resources/excel/tourism_resource_template.xlsx

Binary file not shown.

2
epmet-module/gov-org/gov-org-server/src/main/resources/mapper/IcPublicServiceDao.xml

@ -75,7 +75,7 @@
`name` name, `name` name,
category category, category category,
area_covered areaCovered, area_covered areaCovered,
CONCAT(area_covered,'平方公里') areaCoveredName, CONCAT(area_covered,'平方') areaCoveredName,
capacity capacity, capacity capacity,
CONCAT(capacity,'人') capacityName, CONCAT(capacity,'人') capacityName,
address address, address address,

Loading…
Cancel
Save