|
|
@@ -0,0 +1,253 @@
|
|
|
+package com.doc.scanner.controller;
|
|
|
+
|
|
|
+import com.doc.biz.service.IDocInfoService;
|
|
|
+import com.doc.biz.service.IMongoService;
|
|
|
+import com.doc.biz.vo.DocumentVO;
|
|
|
+import com.doc.common.annotation.Log;
|
|
|
+import com.doc.common.config.RuoYiConfig;
|
|
|
+import com.doc.common.core.controller.BaseController;
|
|
|
+import com.doc.common.core.domain.AjaxResult;
|
|
|
+import com.doc.common.core.page.TableDataInfo;
|
|
|
+import com.doc.common.enums.BusinessType;
|
|
|
+import com.doc.common.enums.EventLevel;
|
|
|
+import com.doc.common.utils.SecurityUtils;
|
|
|
+import com.doc.common.utils.encrypt.Sm2Util;
|
|
|
+import com.doc.common.utils.file.FileUtils;
|
|
|
+import com.doc.common.utils.poi.ExcelUtil;
|
|
|
+import com.doc.scanner.domain.ScannerFtp;
|
|
|
+import com.doc.scanner.domain.ScannerInfo;
|
|
|
+import com.doc.scanner.service.IScannerFtpService;
|
|
|
+import com.doc.scanner.service.IScannerInfoService;
|
|
|
+import com.doc.vo.ScannerVO;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+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.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 扫描仪管理Controller
|
|
|
+ *
|
|
|
+ * @author wukai
|
|
|
+ * @date 2023-11-28
|
|
|
+ */
|
|
|
+@Api(tags = "扫描仪管理")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/scanner/info")
|
|
|
+public class ScannerInfoController extends BaseController {
|
|
|
+ @Resource
|
|
|
+ private IScannerInfoService scannerInfoService;
|
|
|
+ @Resource
|
|
|
+ private IScannerFtpService scannerFtpService;
|
|
|
+ @Resource
|
|
|
+ private IDocInfoService docInfoService;
|
|
|
+ @Resource
|
|
|
+ private IMongoService mongoService;
|
|
|
+ @Value("${ftp.port}")
|
|
|
+ private int port;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询扫描仪管理列表
|
|
|
+ */
|
|
|
+ @ApiOperation("查询扫描仪管理列表")
|
|
|
+ @PreAuthorize("@ss.hasPermi('scanner:info:list')")
|
|
|
+ @GetMapping("/list")
|
|
|
+ public TableDataInfo list(ScannerInfo scannerInfo) {
|
|
|
+ startPage();
|
|
|
+ List<ScannerInfo> list = scannerInfoService.selectScannerInfoList(scannerInfo);
|
|
|
+ list.forEach(info -> {
|
|
|
+ info.setPort(port);
|
|
|
+ });
|
|
|
+ return getDataTable(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询扫描仪管理列表
|
|
|
+ */
|
|
|
+ @ApiOperation("查询可用扫描仪")
|
|
|
+ @GetMapping("/select")
|
|
|
+ public TableDataInfo select(ScannerInfo scannerInfo) {
|
|
|
+ startPage();
|
|
|
+ Map<String, Object> params = new HashMap<>(3);
|
|
|
+ params.put("select", "1");
|
|
|
+ params.put("deptId", SecurityUtils.getDeptId());
|
|
|
+ scannerInfo.setParams(params);
|
|
|
+ List<ScannerInfo> list = scannerInfoService.selectScannerInfoList(scannerInfo);
|
|
|
+ return getDataTable(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取目录下的所有文件
|
|
|
+ *
|
|
|
+ * @param dir 目录
|
|
|
+ * @param files 文件列表
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private void findFileList(File dir, List<File> files) {
|
|
|
+ if (!dir.exists() || !dir.isDirectory()) {
|
|
|
+ // 判断是否存在目录
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File[] fileList = dir.listFiles();
|
|
|
+ for (File file : fileList) {
|
|
|
+ if (file.isFile()) {
|
|
|
+ // 如果文件
|
|
|
+ // 添加文件全路径名
|
|
|
+ files.add(file);
|
|
|
+ } else {
|
|
|
+ // 如果是目录
|
|
|
+ // 回调自身继续查询
|
|
|
+ findFileList(file, files);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("根据扫描仪ID获取文件")
|
|
|
+ @GetMapping(value = "/files/{scannerId}")
|
|
|
+ public AjaxResult files(@PathVariable("scannerId") Long scannerId) {
|
|
|
+ ScannerFtp ftp = scannerFtpService.selectScannerFtpByUserid("user" + scannerId);
|
|
|
+ List<File> files = new ArrayList<>();
|
|
|
+ findFileList(new File(ftp.getHomedirectory()), files);
|
|
|
+ List<Map<String, String>> list = new ArrayList<>();
|
|
|
+ for (File f : files) {
|
|
|
+ File base = new File(RuoYiConfig.getProfile());
|
|
|
+ String path = f.getPath().replace(base.getPath(), "/profile");
|
|
|
+ Map<String, String> map = new HashMap<>(16);
|
|
|
+ map.put("name", f.getName());
|
|
|
+ map.put("path", path);
|
|
|
+ map.put("q", Sm2Util.encrypt(f.getPath()));
|
|
|
+ list.add(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ return success(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("认领扫描文件")
|
|
|
+ @PostMapping(value = "/claim")
|
|
|
+ @Log(title = "扫描仪管理", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
|
|
|
+ public AjaxResult claim(@RequestBody ScannerVO obj) {
|
|
|
+ AtomicInteger index = new AtomicInteger(1);
|
|
|
+ obj.getQ().forEach(q -> {
|
|
|
+ String path = Sm2Util.decrypt(q);
|
|
|
+ File f = new File(path);
|
|
|
+ try (FileInputStream input = new FileInputStream(f);) {
|
|
|
+ String name = index.get() == 1 ? obj.getName() : obj.getName() + index;
|
|
|
+ index.getAndIncrement();
|
|
|
+ name += path.substring(path.lastIndexOf("."));
|
|
|
+ MultipartFile file = FileUtils.getMultipartFile(input, name);
|
|
|
+
|
|
|
+ DocumentVO vo = mongoService.uploadFile(file);
|
|
|
+ docInfoService.upload(vo, obj.getSpaceId(), obj.getDirId());
|
|
|
+
|
|
|
+ FileUtils.deleteFile(path);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("认领出错啦:{}", e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ return success(1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出扫描仪管理列表
|
|
|
+ */
|
|
|
+ @ApiOperation("导出扫描仪管理列表")
|
|
|
+ @PreAuthorize("@ss.hasPermi('scanner:info:export')")
|
|
|
+ @Log(title = "扫描仪管理", businessType = BusinessType.EXPORT)
|
|
|
+ @PostMapping("/export")
|
|
|
+ public void export(HttpServletResponse response, ScannerInfo scannerInfo) {
|
|
|
+ List<ScannerInfo> list = scannerInfoService.selectScannerInfoList(scannerInfo);
|
|
|
+ ExcelUtil<ScannerInfo> util = new ExcelUtil<ScannerInfo>(ScannerInfo.class);
|
|
|
+ util.exportExcel(response, list, "扫描仪管理数据");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取扫描仪管理详细信息
|
|
|
+ */
|
|
|
+ @ApiOperation("获取扫描仪管理详细信息")
|
|
|
+ @PreAuthorize("@ss.hasPermi('scanner:info:query')")
|
|
|
+ @GetMapping(value = "/{scannerId}")
|
|
|
+ public AjaxResult getInfo(@PathVariable("scannerId") Long scannerId) {
|
|
|
+ return success(scannerInfoService.selectScannerInfoByScannerId(scannerId));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增扫描仪管理
|
|
|
+ */
|
|
|
+ @ApiOperation("新增扫描仪管理")
|
|
|
+ @PreAuthorize("@ss.hasPermi('scanner:info:add')")
|
|
|
+ @Log(title = "扫描仪管理", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
|
|
|
+ @PostMapping
|
|
|
+ public AjaxResult add(@RequestBody ScannerInfo scannerInfo) throws IOException {
|
|
|
+ String y = "Y";
|
|
|
+ if (!y.equals(scannerInfo.getIsPublic())) {
|
|
|
+ scannerInfo.setDeptId(SecurityUtils.getDeptId());
|
|
|
+ }
|
|
|
+ scannerInfo.setCreateBy(SecurityUtils.getUsername());
|
|
|
+ scannerInfoService.insertScannerInfo(scannerInfo);
|
|
|
+
|
|
|
+ //4位随机密码
|
|
|
+ String name = "user" + scannerInfo.getScannerId();
|
|
|
+ String pass = new Random().nextInt(9000) + 1000 + "";
|
|
|
+ ScannerFtp ftp = new ScannerFtp();
|
|
|
+ ftp.setUserid(name);
|
|
|
+ ftp.setUserpassword(Sm2Util.encrypt(pass));
|
|
|
+ ftp.setEnableflag("true");
|
|
|
+ ftp.setWritepermission("true");
|
|
|
+ String dir = RuoYiConfig.getProfile() + File.separator + "scanner" + File.separator + name;
|
|
|
+ ftp.setHomedirectory(dir);
|
|
|
+ scannerFtpService.insertScannerFtp(ftp);
|
|
|
+
|
|
|
+ try {
|
|
|
+ //创建目录
|
|
|
+ Files.createDirectories(Paths.get(dir));
|
|
|
+ } catch (IOException ignored) {
|
|
|
+ //不用管,表示已经有该目录
|
|
|
+ }
|
|
|
+ scannerInfo.setUser(name);
|
|
|
+ scannerInfo.setPass(ftp.getUserpassword());
|
|
|
+ scannerInfo.setPort(port);
|
|
|
+ return AjaxResult.success(scannerInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改扫描仪管理
|
|
|
+ */
|
|
|
+ @ApiOperation("修改扫描仪管理")
|
|
|
+ @PreAuthorize("@ss.hasPermi('scanner:info:edit')")
|
|
|
+ @Log(title = "扫描仪管理", businessType = BusinessType.UPDATE, eventLevel = EventLevel.MIDDLE)
|
|
|
+ @PutMapping
|
|
|
+ public AjaxResult edit(@RequestBody ScannerInfo scannerInfo) {
|
|
|
+ return toAjax(scannerInfoService.updateScannerInfo(scannerInfo));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除扫描仪管理
|
|
|
+ */
|
|
|
+ @ApiOperation("删除扫描仪管理")
|
|
|
+ @PreAuthorize("@ss.hasPermi('scanner:info:remove')")
|
|
|
+ @Log(title = "扫描仪管理", businessType = BusinessType.DELETE, eventLevel = EventLevel.MIDDLE)
|
|
|
+ @DeleteMapping("/{scannerIds}")
|
|
|
+ public AjaxResult remove(@PathVariable Long[] scannerIds) {
|
|
|
+ for (Long id : scannerIds) {
|
|
|
+ try {
|
|
|
+ scannerFtpService.deleteScannerFtpByUserid("user" + id);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return toAjax(scannerInfoService.deleteScannerInfoByScannerIds(scannerIds));
|
|
|
+ }
|
|
|
+}
|