forked from rongchao/epmet-cloud-rizhao
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
9.2 KiB
222 lines
9.2 KiB
package com.epmet.controller;
|
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
import com.alibaba.excel.ExcelWriter;
|
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
|
import com.epmet.commons.tools.annotation.LoginUser;
|
|
import com.epmet.commons.tools.aop.NoRepeatSubmit;
|
|
import com.epmet.commons.tools.constant.AppClientConstant;
|
|
import com.epmet.commons.tools.constant.NumConstant;
|
|
import com.epmet.commons.tools.constant.ServiceConstant;
|
|
import com.epmet.commons.tools.dto.form.PageFormDTO;
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode;
|
|
import com.epmet.commons.tools.exception.EpmetException;
|
|
import com.epmet.commons.tools.exception.ExceptionUtils;
|
|
import com.epmet.commons.tools.page.PageData;
|
|
import com.epmet.commons.tools.security.dto.TokenDto;
|
|
import com.epmet.commons.tools.utils.*;
|
|
import com.epmet.commons.tools.utils.poi.excel.handler.FreezeAndFilter;
|
|
import com.epmet.commons.tools.feign.*;
|
|
import com.epmet.commons.tools.validator.ValidatorUtils;
|
|
import com.epmet.commons.tools.validator.group.AddGroup;
|
|
import com.epmet.commons.tools.validator.group.UpdateGroup;
|
|
import com.epmet.commons.tools.validator.group.DefaultGroup;
|
|
import com.epmet.constants.ImportTaskConstants;
|
|
import com.epmet.dto.IcDangerAreaDTO;
|
|
import com.epmet.dto.form.DangerAreaListFormDTO;
|
|
import com.epmet.dto.form.ImportTaskCommonFormDTO;
|
|
import com.epmet.dto.result.ImportTaskCommonResultDTO;
|
|
import com.epmet.dto.result.NatListCommonExcelResultDTO;
|
|
import com.epmet.dto.result.NatListResultDTO;
|
|
import com.epmet.enums.DangerLevelEnum;
|
|
import com.epmet.excel.IcDangerAreaResultExcel;
|
|
import com.epmet.feign.EpmetCommonServiceOpenFeignClient;
|
|
import com.epmet.service.IcDangerAreaService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.net.URLEncoder;
|
|
import java.nio.file.Path;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
|
|
/**
|
|
* 疫情风险地区
|
|
*
|
|
* @author generator generator@elink-cn.com
|
|
* @since v1.0.0 2022-10-31
|
|
*/
|
|
@RestController
|
|
@Slf4j
|
|
@RequestMapping("icDangerArea")
|
|
public class IcDangerAreaController implements ResultDataResolver{
|
|
|
|
@Autowired
|
|
private IcDangerAreaService icDangerAreaService;
|
|
@Autowired
|
|
private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient;
|
|
|
|
@RequestMapping("page")
|
|
public Result<PageData<IcDangerAreaDTO>> page(@RequestParam Map<String, Object> params){
|
|
PageData<IcDangerAreaDTO> page = icDangerAreaService.page(params);
|
|
return new Result<PageData<IcDangerAreaDTO>>().ok(page);
|
|
}
|
|
|
|
@RequestMapping(value = "{id}",method = {RequestMethod.POST,RequestMethod.GET})
|
|
public Result<IcDangerAreaDTO> get(@PathVariable("id") String id){
|
|
IcDangerAreaDTO data = icDangerAreaService.get(id);
|
|
return new Result<IcDangerAreaDTO>().ok(data);
|
|
}
|
|
|
|
/**
|
|
* @Description 新增
|
|
* @param dto
|
|
* @param tokenDto
|
|
* @Author zxc
|
|
* @Date 2022/11/1 09:09
|
|
*/
|
|
@NoRepeatSubmit
|
|
@PostMapping("save")
|
|
public Result save(@RequestBody IcDangerAreaDTO dto, @LoginUser TokenDto tokenDto){
|
|
ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
|
|
dto.setCustomerId(tokenDto.getCustomerId());
|
|
icDangerAreaService.save(dto);
|
|
return new Result();
|
|
}
|
|
|
|
@NoRepeatSubmit
|
|
@PostMapping("update")
|
|
public Result update(@RequestBody IcDangerAreaDTO dto){
|
|
//效验数据
|
|
ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
|
|
icDangerAreaService.update(dto);
|
|
return new Result();
|
|
}
|
|
|
|
@PostMapping("delete")
|
|
public Result delete(@RequestBody List<String> ids,@LoginUser TokenDto tokenDto){
|
|
icDangerAreaService.delete(ids,tokenDto.getUserId());
|
|
return new Result();
|
|
}
|
|
|
|
@RequestMapping("list")
|
|
public Result<PageData<IcDangerAreaDTO>> list(@RequestBody DangerAreaListFormDTO formDTO,@LoginUser TokenDto tokenDto){
|
|
ValidatorUtils.validateEntity(formDTO, PageFormDTO.AddUserInternalGroup.class);
|
|
formDTO.setCustomerId(tokenDto.getCustomerId());
|
|
return new Result<PageData<IcDangerAreaDTO>>().ok(icDangerAreaService.list(formDTO));
|
|
}
|
|
|
|
@RequestMapping("export")
|
|
public void export(@LoginUser TokenDto tokenDto,@RequestBody DangerAreaListFormDTO formDTO,HttpServletResponse response){
|
|
formDTO.setCustomerId(tokenDto.getCustomerId());
|
|
ExcelWriter excelWriter = null;
|
|
int pageNo = NumConstant.ONE;
|
|
formDTO.setPageSize(NumConstant.ONE_THOUSAND);
|
|
try {
|
|
excelWriter = EasyExcel.write(ExcelUtils.getOutputStreamForExcel("风险地区", response), IcDangerAreaResultExcel.class).build();
|
|
WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").registerWriteHandler(new FreezeAndFilter()).build();
|
|
Integer size;
|
|
do {
|
|
PageData<IcDangerAreaDTO> list = icDangerAreaService.list(formDTO);
|
|
List<IcDangerAreaDTO> data = list.getList();
|
|
if (CollectionUtils.isNotEmpty(data)){
|
|
data.forEach(d -> {
|
|
d.setDangerLevel(DangerLevelEnum.getNameByCode(d.getDangerLevel()));
|
|
});
|
|
}
|
|
excelWriter.write(ConvertUtils.sourceToTarget(data,IcDangerAreaResultExcel.class), writeSheet);
|
|
formDTO.setPageNo(pageNo++);
|
|
size = data.size();
|
|
} while (size == NumConstant.ONE_THOUSAND);
|
|
}catch (Exception e){
|
|
log.error("export exception", e);
|
|
}finally {
|
|
if (excelWriter != null) {
|
|
excelWriter.finish();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @Description 模板下载
|
|
* @param response
|
|
* @Author zxc
|
|
* @Date 2022/11/1 09:52
|
|
*/
|
|
@PostMapping("downloadTemplate")
|
|
public void downloadTemplate(HttpServletResponse response) 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/ic_danger_area_template.xlsx");
|
|
try {
|
|
ServletOutputStream os = response.getOutputStream();
|
|
IOUtils.copy(is, os);
|
|
} finally {
|
|
if (is != null) {
|
|
is.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
@PostMapping("import")
|
|
public Result importExcel(@LoginUser TokenDto tokenDto, @RequestPart("file") MultipartFile file) {
|
|
String userId = EpmetRequestHolder.getHeader(AppClientConstant.USER_ID);
|
|
// 1.暂存文件
|
|
String originalFilename = file.getOriginalFilename();
|
|
String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
Path fileSavePath;
|
|
try {
|
|
Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_danger_area", "import");
|
|
fileSavePath = importPath.resolve(UUID.randomUUID().toString().concat(extName));
|
|
} catch (IOException e) {
|
|
String errorMsg = ExceptionUtils.getErrorStackTrace(e);
|
|
log.error("【风险地区】创建临时存储文件失败:{}", errorMsg);
|
|
throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "文件上传失败", "文件上传失败");
|
|
}
|
|
|
|
InputStream is = null;
|
|
FileOutputStream os = null;
|
|
|
|
try {
|
|
is = file.getInputStream();
|
|
os = new FileOutputStream(fileSavePath.toString());
|
|
IOUtils.copy(is, os);
|
|
} catch (Exception e) {
|
|
log.error("method exception", e);
|
|
} finally {
|
|
org.apache.poi.util.IOUtils.closeQuietly(is);
|
|
org.apache.poi.util.IOUtils.closeQuietly(os);
|
|
}
|
|
|
|
// 2.生成导入任务记录
|
|
ImportTaskCommonFormDTO importTaskForm = new ImportTaskCommonFormDTO();
|
|
importTaskForm.setOperatorId(userId);
|
|
importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_IC_DANGER_AREA);
|
|
importTaskForm.setOriginFileName(originalFilename);
|
|
|
|
ImportTaskCommonResultDTO rstData = getResultDataOrThrowsException(commonServiceOpenFeignClient.createImportTask(importTaskForm),
|
|
ServiceConstant.EPMET_COMMON_SERVICE,
|
|
EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),
|
|
"excel导入风险地区错误",
|
|
"风险地区导入失败");
|
|
|
|
// 3.执行导入
|
|
icDangerAreaService.execAsyncExcelImport(fileSavePath, rstData.getTaskId(),tokenDto.getCustomerId(),tokenDto.getUserId());
|
|
return new Result();
|
|
}
|
|
|
|
}
|
|
|