|
@@ -0,0 +1,212 @@
|
|
|
+package com.taais.demo.controller;
|
|
|
+
|
|
|
+import cn.dev33.satoken.annotation.SaCheckPermission;
|
|
|
+import com.taais.common.core.config.TaaisConfig;
|
|
|
+import com.taais.common.core.core.domain.CommonResult;
|
|
|
+import com.taais.common.core.core.page.PageResult;
|
|
|
+import com.taais.common.core.utils.MapstructUtils;
|
|
|
+import com.taais.common.core.utils.StringUtils;
|
|
|
+import com.taais.common.core.utils.file.FileUploadUtils;
|
|
|
+import com.taais.common.core.utils.file.FileUtils;
|
|
|
+import com.taais.common.core.utils.file.UnPackedUtil;
|
|
|
+import com.taais.common.excel.utils.ExcelUtil;
|
|
|
+import com.taais.common.log.annotation.Log;
|
|
|
+import com.taais.common.log.enums.BusinessType;
|
|
|
+import com.taais.common.web.annotation.RepeatSubmit;
|
|
|
+import com.taais.common.web.core.BaseController;
|
|
|
+import com.taais.demo.domain.Data;
|
|
|
+import com.taais.demo.domain.bo.DataBo;
|
|
|
+import com.taais.demo.domain.vo.DataVo;
|
|
|
+import com.taais.demo.service.IDataService;
|
|
|
+import com.taais.system.config.ServerConfig;
|
|
|
+import com.taais.system.domain.vo.SysOssUploadVo;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import net.lingala.zip4j.model.FileHeader;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据管理Controller
|
|
|
+ *
|
|
|
+ * @author km
|
|
|
+ * 2024-04-22
|
|
|
+ */
|
|
|
+@Validated
|
|
|
+@RequiredArgsConstructor
|
|
|
+@RestController
|
|
|
+@RequestMapping("/demo/data")
|
|
|
+public class DataController extends BaseController {
|
|
|
+ @Resource
|
|
|
+ private IDataService dataService;
|
|
|
+ @Autowired
|
|
|
+ private ServerConfig serverConfig;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询数据管理列表
|
|
|
+ */
|
|
|
+ @SaCheckPermission("demo:data:list")
|
|
|
+ @GetMapping("/list")
|
|
|
+ public CommonResult<PageResult<DataVo>> list(DataBo dataBo) {
|
|
|
+
|
|
|
+ return CommonResult.success(dataService.selectPage(dataBo));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出数据管理列表
|
|
|
+ */
|
|
|
+ @SaCheckPermission("demo:data:export")
|
|
|
+ @Log(title = "数据管理", businessType = BusinessType.EXPORT)
|
|
|
+ @PostMapping("/export")
|
|
|
+ public void export(HttpServletResponse response, DataBo dataBo) {
|
|
|
+ List<DataVo> list = dataService.selectList(dataBo);
|
|
|
+ ExcelUtil.exportExcel(list, "数据管理", DataVo.class, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取数据管理详细信息
|
|
|
+ */
|
|
|
+ @SaCheckPermission("demo:data:query")
|
|
|
+ @GetMapping(value = "/{id}")
|
|
|
+ public CommonResult<DataVo> getInfo(@PathVariable Long id) {
|
|
|
+
|
|
|
+ return CommonResult.success(dataService.selectById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增数据管理
|
|
|
+ */
|
|
|
+ @SaCheckPermission("demo:data:add")
|
|
|
+ @Log(title = "数据管理", businessType = BusinessType.INSERT)
|
|
|
+ @RepeatSubmit()
|
|
|
+ @PostMapping
|
|
|
+ public CommonResult<Void> add(@Validated @RequestBody DataBo dataBo) {
|
|
|
+ boolean inserted = dataService.insert(dataBo);
|
|
|
+ if (!inserted) {
|
|
|
+ return CommonResult.fail("新增数据管理记录失败!");
|
|
|
+ }
|
|
|
+ return CommonResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改数据管理
|
|
|
+ */
|
|
|
+ @SaCheckPermission("demo:data:edit")
|
|
|
+ @Log(title = "数据管理", businessType = BusinessType.UPDATE)
|
|
|
+ @RepeatSubmit()
|
|
|
+ @PutMapping
|
|
|
+ public CommonResult<Void> edit(@Validated @RequestBody DataBo dataBo) {
|
|
|
+ Boolean updated = dataService.update(dataBo);
|
|
|
+ if (!updated) {
|
|
|
+ return CommonResult.fail("修改数据管理记录失败!");
|
|
|
+ }
|
|
|
+ return CommonResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除数据管理
|
|
|
+ */
|
|
|
+ @SaCheckPermission("demo:data:remove")
|
|
|
+ @Log(title = "数据管理", businessType = BusinessType.DELETE)
|
|
|
+ @DeleteMapping("/{ids}")
|
|
|
+ public CommonResult<Void> remove(@PathVariable Long[] ids) {
|
|
|
+ boolean deleted = dataService.deleteByIds(ids);
|
|
|
+ if (!deleted) {
|
|
|
+ return CommonResult.fail("删除数据管理记录失败!");
|
|
|
+ }
|
|
|
+ return CommonResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/importTemplate")
|
|
|
+ public void importTemplate(HttpServletResponse response) {
|
|
|
+ ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", DataVo.class, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/zip/upload")
|
|
|
+ public CommonResult<SysOssUploadVo> uploadZipFile(@RequestParam("file") MultipartFile file, @RequestParam("excelFile") MultipartFile excelFile) throws Exception {
|
|
|
+
|
|
|
+ List<DataVo> userList = ExcelUtil.importExcel(excelFile.getInputStream(),DataVo.class);
|
|
|
+ Map<String, Data> picName2Obj = new HashMap<>();
|
|
|
+ //为插入数据库做准备
|
|
|
+ for(DataVo dataVo:userList){
|
|
|
+ picName2Obj.put(dataVo.getName(), MapstructUtils.convert(dataVo, Data.class));
|
|
|
+ }
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ assert originalFilename != null;
|
|
|
+ String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
|
|
|
+
|
|
|
+
|
|
|
+ // 解压
|
|
|
+ String basedir = TaaisConfig.getUploadPath();
|
|
|
+ File zipOrRarTemp = FileUploadUtils.getAbsoluteFile(basedir,FileUploadUtils.extractFilename(file));// 解压目录
|
|
|
+ String destZip = zipOrRarTemp.getAbsolutePath();
|
|
|
+
|
|
|
+ file.transferTo(Paths.get(destZip));
|
|
|
+ String dest = zipOrRarTemp.getParent();
|
|
|
+
|
|
|
+ List<File> extractedFileList = new ArrayList<File>();
|
|
|
+ if(suffix.equals(".zip")){
|
|
|
+ List<FileHeader> fileheaders = UnPackedUtil.unPackZip(zipOrRarTemp,dest);
|
|
|
+ //添加解压目录
|
|
|
+ for(FileHeader fileHeader : fileheaders) {
|
|
|
+
|
|
|
+ if (!fileHeader.isDirectory()) {
|
|
|
+ String fileHeaderName = fileHeader.getFileName();
|
|
|
+ String fileHeader_suffix = StringUtils.substring(fileHeaderName, fileHeaderName.lastIndexOf("."), fileHeaderName.length());
|
|
|
+ if(!fileHeader_suffix.equals(".jpg")&&!fileHeader_suffix.equals(".jpeg")
|
|
|
+ &&!fileHeader_suffix.equals(".png")){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ extractedFileList.add(new File(dest,fileHeader.getFileName()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }else if(suffix.equals(".rar")){
|
|
|
+ List<com.github.junrar.rarfile.FileHeader> fileheaders = UnPackedUtil.unPackRar(zipOrRarTemp,dest);
|
|
|
+
|
|
|
+
|
|
|
+ //添加解压目录
|
|
|
+ for(com.github.junrar.rarfile.FileHeader fileHeader : fileheaders) {
|
|
|
+
|
|
|
+ if (!fileHeader.isDirectory()) {
|
|
|
+ String fileHeaderName = fileHeader.getFileName();
|
|
|
+ String fileHeader_suffix = StringUtils.substring(fileHeaderName, fileHeaderName.lastIndexOf("."), fileHeaderName.length());
|
|
|
+ if(!fileHeader_suffix.equals(".jpg")&&!fileHeader_suffix.equals(".jpeg")
|
|
|
+ &&!fileHeader_suffix.equals(".png")){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ extractedFileList.add(new File(dest,fileHeader.getFileName()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for(File efile:extractedFileList){
|
|
|
+ String fileName = FileUploadUtils.getPathFileName(dest,efile.getName());
|
|
|
+ String url = serverConfig.getUrl() + fileName;
|
|
|
+ url = url.replaceAll("\\\\", "/");
|
|
|
+ Data mydata = picName2Obj.get(efile.getName());
|
|
|
+
|
|
|
+ // TODO: 将导入的excel表和url一起插入数据库,图片名称对应
|
|
|
+ mydata.setUrl(url);
|
|
|
+ dataService.save(mydata);
|
|
|
+ }
|
|
|
+ //删除压缩文件
|
|
|
+ FileUtils.deleteFile(destZip);
|
|
|
+
|
|
|
+
|
|
|
+ return CommonResult.success("数据集上传成功!");
|
|
|
+ }
|
|
|
+}
|