275 lines
10 KiB
Java

package com.xkrs.microservice.controller;
import com.xkrs.microservice.common.encapsulation.PromptMessageEnum;
import com.xkrs.microservice.model.entity.ecology.DocumentEntity;
import com.xkrs.microservice.model.vo.FileVo;
import com.xkrs.microservice.model.vo.PageVo;
import com.xkrs.microservice.service.FileService;
import com.xkrs.microservice.service.ecology.DocumentService;
import com.xkrs.microservice.service.ecology.RemoteSenseingService;
import com.xkrs.microservice.service.ecology.RemoteService;
import com.xkrs.microservice.util.filestorage.FileFastDfs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static com.xkrs.microservice.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
import static com.xkrs.microservice.common.tool.TokenUtil.getTokenUserName;
import static com.xkrs.microservice.util.NumberUtil.isStrNumeric;
import static com.xkrs.microservice.util.filestorage.FileUtil.getUploadInfo;
/**
* @author dong
* @date 2020/12/30
*/
@RestController
@RequestMapping("/file")
public class FileController {
private static final Logger log = LoggerFactory.getLogger(FileController.class);
@Resource
private FileService fileService;
@Resource
private DocumentService documentService;
@Resource
private FileFastDfs fileFastDfs;
@Resource
private RemoteSenseingService remoteSenseingService;
@Resource
private RemoteService remoteService;
/**
* 接收上传文件信息
* <p>
* 1:技术规范、
* 2:评价报告、
* 3:野外核查报告、
* 4:其他
*
* @param files
* @return
*/
@PreAuthorize("hasAnyRole('ROLE_PRO_USER', 'ROLE_ADMINISTOR', 'ROLE_SYSTEM_MANAGER')")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addFile2FileService(MultipartFile[] files, @RequestParam("fileType") Integer fileType, @RequestHeader(value = "Authorization") String token) {
Locale locale = LocaleContextHolder.getLocale();
if (null == files || files.length == 0) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_NULL, "result or fileType is null", locale);
}
String userName = getTokenUserName(token);
List<Map<String, String>> list = new ArrayList<>(files.length);
try {
for (MultipartFile file :
files) {
File file1 = fileFastDfs.multipartFileToFile(file);
String dir = "document";
String info = fileFastDfs.uploadFile(file1, dir);
Map<String, String> map = getUploadInfo(info);
list.add(map);
file1.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
if (fileService.addDocument2(list, userName, fileType)) {
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "success", locale);
}
return outputEncapsulationObject(PromptMessageEnum.PARAM_NULL, "result or fileType is null", locale);
}
/**
* 删除文件
*
* @param fileIdList 文件id
* @return
*/
@PreAuthorize("hasAnyRole('ROLE_ADMINISTOR', 'ROLE_SYSTEM_MANAGER','ROLE_PRO_USER')")
@ResponseBody
@RequestMapping(value = "/delete/id", method = RequestMethod.POST)
public String deleteById(@RequestBody List<Integer> fileIdList) {
//获取区域信息
Locale locale = LocaleContextHolder.getLocale();
if (null == fileIdList || fileIdList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "fileIdList is error", locale);
}
if (!documentService.deleDocument(fileIdList)) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "delet failed", locale);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "ok", locale);
}
@RequestMapping(value = "/get/condition", method = RequestMethod.GET)
public String getDocumentByCondition(@RequestParam(value = "startTime", required = false) String startTime,
@RequestParam(value = "endTime", required = false) String endTime,
@RequestParam(value = "username", required = false) String username,
@RequestParam(value = "fileName", required = false) String fileName,
@RequestParam(value = "fileType", required = false) String fileType,
@RequestParam(value = "page") Integer page) {
Locale locale = LocaleContextHolder.getLocale();
if (page < 1) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "页数大于0", locale);
}
PageVo documentByCondition = documentService.getDocumentByCondition(startTime, endTime, username, fileName, fileType, page);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, documentByCondition, locale);
}
/**
* 获取所有文件
*
* @return
*/
@RequestMapping(value = "/get/all", method = RequestMethod.POST)
public String getAll() {
//获取区域信息
Locale locale = LocaleContextHolder.getLocale();
List<DocumentEntity> all = documentService.getAll();
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, all, locale);
}
/**
* 根据时间获取所有文件
*
* @param startDate
* @param endDate
* @param page
* @return
*/
@RequestMapping(value = "/get/date", method = RequestMethod.POST)
public String getAllByDate(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate,
@RequestParam("page") Integer page) {
// 区域信息
Locale locale = LocaleContextHolder.getLocale();
if (null == page || page < 1) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "page should not be null or page less than 1", locale);
}
PageVo document = documentService.getDocument(startDate, endDate, page);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, document, locale);
}
/**
* 删除文件 传MD5更省事
*
* @param md5List 文件md5
* @return
*/
@Deprecated
@RequestMapping(value = "/delete/md", method = RequestMethod.POST)
public String deleteByMd(@RequestBody List<Map<String, String>> md5List) {
//获取区域信息
Locale locale = LocaleContextHolder.getLocale();
if (null == md5List || md5List.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "md5 is null", locale);
}
for (Map<String, String> md5Map : md5List) {
String md5 = md5Map.get("md5");
if (!fileService.deleteByMd(md5)) {
FileVo fileVo = fileService.findByMd5(md5);
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, fileVo.getFileName() + " deletion failed", locale);
}
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "ok", locale);
}
/**
* 根据id获取文件
*
* @param fileId 文件id
* @return
*/
@Deprecated
@RequestMapping(value = "/get/id", method = RequestMethod.GET)
public String getById(@RequestParam("fileId") String fileId) {
//获取区域信息
Locale locale = LocaleContextHolder.getLocale();
if (!isStrNumeric(fileId)) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "fileId error", locale);
}
FileVo file = fileService.findById(Integer.valueOf(fileId));
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, file, locale);
}
/**
* 根据文件名获取文件
*
* @param fileName 文件名
* @return
*/
@Deprecated
@RequestMapping(value = "/get/name", method = RequestMethod.POST)
public String getByName(@RequestParam("fileName") String fileName) {
//获取区域信息
Locale locale = LocaleContextHolder.getLocale();
if (fileName.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "fileId error", locale);
}
FileVo file = fileService.findByFileName(fileName);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, file, locale);
}
/**
* 批量下载
*
* @param fileIdList 文件ID
* @return
*/
@Deprecated
@RequestMapping(value = "/download/many", method = RequestMethod.POST)
public String downloadFile(@RequestBody List<Integer> fileIdList, HttpServletResponse response, HttpServletRequest request) {
//获取区域信息
Locale locale = LocaleContextHolder.getLocale();
if (null == fileIdList) {
return outputEncapsulationObject(PromptMessageEnum.PARAM_NULL, "fileId should not be null", locale);
}
documentService.downloadDocument(fileIdList, request, response);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "ok", locale);
}
/**
* 启动入库
*
* @return
*/
@PreAuthorize("hasAnyRole('ROLE_ADMINISTOR', 'ROLE_SYSTEM_MANAGER','ROLE_PRO_USER')")
@RequestMapping(value = "/scan", method = RequestMethod.GET)
public String sacnFile() {
Locale locale = LocaleContextHolder.getLocale();
boolean b = remoteSenseingService.getFile();
if (b) {
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "开始入库", locale);
}
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "无入库数据", locale);
}
@RequestMapping(value = "/tif/base", method = RequestMethod.POST)
public String tif2Base(@RequestBody List<String> tifPaths) {
Locale locale = LocaleContextHolder.getLocale();
remoteService.tif2Database(tifPaths);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "", locale);
}
}