Browse Source

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

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

44
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 org.apache.commons.collections4.CollectionUtils;
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.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
@ -45,6 +46,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -67,6 +69,8 @@ import java.util.UUID;
@Slf4j
public class IcPublicServiceController implements ResultDataResolver {
private static final String CATEGORY_TYPE = "tourism_resource";
@Autowired
private IcPublicServiceService icPublicServiceService;
@ -77,8 +81,8 @@ public class IcPublicServiceController implements ResultDataResolver {
return new Result<PageData<IcPublicServiceListResultDTO>>().ok(icPublicServiceService.list(formDTO));
}
@RequestMapping(value = "{id}",method = {RequestMethod.POST,RequestMethod.GET})
public Result<IcPublicServiceDTO> get(@PathVariable("id") String id){
@RequestMapping(value = "{id}", method = {RequestMethod.POST, RequestMethod.GET})
public Result<IcPublicServiceDTO> get(@PathVariable("id") String id) {
IcPublicServiceDTO data = icPublicServiceService.get(id);
return new Result<IcPublicServiceDTO>().ok(data);
}
@ -118,13 +122,20 @@ public class IcPublicServiceController implements ResultDataResolver {
}
@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.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition");
response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("公共服务导入模板", "UTF-8") + ".xlsx");
InputStream is = this.getClass().getClassLoader().getResourceAsStream("excel/public_service_template.xlsx");
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_DISPOSITION, "attachment;filename=" + URLEncoder.encode("公共服务导入模板", "UTF-8") + ".xlsx");
fileName = "excel/public_service_template.xlsx";
}
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName);
try {
ServletOutputStream os = response.getOutputStream();
IOUtils.copy(is, os);
@ -144,7 +155,8 @@ public class IcPublicServiceController implements ResultDataResolver {
formDTO.setPageNo(NumConstant.ONE);
formDTO.setPageSize(NumConstant.TEN_THOUSAND);
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();
// 背景设置为红色
@ -184,18 +196,22 @@ public class IcPublicServiceController implements ResultDataResolver {
}
@PostMapping("import")
public Result importExcel(MultipartFile file) {
public Result importExcel(MultipartFile file, String categoryType) {
// 1.暂存文件
String originalFilename = file.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
Path fileSavePath;
try {
Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_public_service", "import");
Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir(categoryType, "import");
fileSavePath = importPath.resolve(UUID.randomUUID().toString().concat(extName));
} catch (IOException e) {
String errorMsg = ExceptionUtils.getErrorStackTrace(e);
log.error("【公共服务管理导入】创建临时存储文件失败:{}", errorMsg);
if (categoryType.equals(CATEGORY_TYPE)) {
log.error("【旅游资源管理导入】创建临时存储文件失败:{}", errorMsg);
} else {
log.error("【公共服务管理导入】创建临时存储文件失败:{}", errorMsg);
}
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),
ServiceConstant.EPMET_COMMON_SERVICE,
EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),
"excel导入公共服务管理信息错误",
"导入公共服务管理信息失败");
categoryType.equals(CATEGORY_TYPE) ? "excel导入旅游资源管理信息错误" : "excel导入公共服务管理信息错误",
categoryType.equals(CATEGORY_TYPE) ? "导入旅游资源管理信息失败" : "导入公共服务管理信息失败");
// 3.执行导入
icPublicServiceService.importExcel(fileSavePath, rstData.getTaskId());
icPublicServiceService.importExcel(fileSavePath, rstData.getTaskId(), categoryType);
return new Result();
}

2
epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/excel/IcPublicServiceExcel.java

@ -66,4 +66,4 @@ public class IcPublicServiceExcel {
private String errorInfo;
}
}
}

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

4
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);
void importExcel(Path fileSavePath, String taskId);
}
void importExcel(Path fileSavePath, String taskId, String categoryType);
}

23
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
@Override
public void importExcel(Path filePath, String importTaskId) {
public void importExcel(Path filePath, String importTaskId, String categoryType) {
String userId = null;
try {
userId = EpmetRequestHolder.getHeader(AppClientConstant.USER_ID);
@ -227,7 +227,7 @@ public class IcPublicServiceServiceImpl extends BaseServiceImpl<IcPublicServiceD
String agencyId = 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();
@ -280,12 +280,19 @@ public class IcPublicServiceServiceImpl extends BaseServiceImpl<IcPublicServiceD
"");
if (!result.success()) {
log.error("【公共服务管理导入】finishImportTask失败");
if (categoryType.equals("tourism_resource")) {
log.error("【旅游资源管理导入】finishImportTask失败");
} else {
log.error("【公共服务管理导入】finishImportTask失败");
}
}
} catch (Exception e) {
String errorMsg = ExceptionUtils.getErrorStackTrace(e);
log.error("【公共服务管理导入】出错:{}", errorMsg);
if (categoryType.equals("tourism_resource")) {
log.error("【旅游资源管理导入】出错:{}", errorMsg);
} else {
log.error("【公共服务管理导入】出错:{}", errorMsg);
}
ImportTaskCommonFormDTO importFinishTaskForm = new ImportTaskCommonFormDTO();
importFinishTaskForm.setTaskId(importTaskId);
importFinishTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL);
@ -294,7 +301,11 @@ public class IcPublicServiceServiceImpl extends BaseServiceImpl<IcPublicServiceD
Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm);
if (!result.success()) {
log.error("【公共服务管理导入】导入记录状态修改为'完成'失败");
if (categoryType.equals("tourism_resource")) {
log.error("【旅游资源管理导入】导入记录状态修改为'完成'失败");
} else {
log.error("【公共服务管理导入】导入记录状态修改为'完成'失败");
}
}
} finally {
// 删除临时文件

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,
category category,
area_covered areaCovered,
CONCAT(area_covered,'平方公里') areaCoveredName,
CONCAT(area_covered,'平方') areaCoveredName,
capacity capacity,
CONCAT(capacity,'人') capacityName,
address address,

Loading…
Cancel
Save