SolutionController.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.zksy.web.controller.base;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.zksy.base.domain.FileManage;
  6. import com.zksy.base.domain.Solution;
  7. import com.zksy.base.domain.dto.SolutionDto;
  8. import com.zksy.base.service.FileManageService;
  9. import com.zksy.base.service.SolutionService;
  10. import com.zksy.common.core.domain.AjaxResult;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import io.swagger.annotations.ApiParam;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import org.springframework.web.bind.annotation.*;
  17. import java.time.LocalDateTime;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import java.util.stream.Collectors;
  21. /**
  22. * @author Administrator
  23. * @version 1.0
  24. * @project zksy-website-service
  25. * @description 解决方案
  26. * @date 2025/9/8 09:24:07
  27. */
  28. @RestController
  29. @RequestMapping("/solution")
  30. @Api(tags = "解决方案",description = "解决方案desc")
  31. public class SolutionController {
  32. @Autowired
  33. private SolutionService service;
  34. @Autowired
  35. private FileManageService fileManageService;
  36. @GetMapping("/findByPage")
  37. @ApiOperation(value = "解决方案查询分页")
  38. public AjaxResult findByPage(@ApiParam(value = "页码", required = true)long pageNum,
  39. @ApiParam(value = "页数", required = true)long pageSize){
  40. Page<Solution> page = new Page<>(pageNum, pageSize);
  41. return AjaxResult.success(service.page(page));
  42. }
  43. @GetMapping("/getSolutionList")
  44. @ApiOperation(value = "解决方案查询")
  45. public AjaxResult getSolutionList(){
  46. return AjaxResult.success(service.list());
  47. }
  48. @GetMapping("/getById/{id}")
  49. @ApiOperation(value = "根据Id查询解决方案")
  50. public AjaxResult getById(@PathVariable String id){
  51. return AjaxResult.success(service.getById(id));
  52. }
  53. @PostMapping("/save")
  54. @ApiOperation(value = "解决方案保存")
  55. @Transactional
  56. public AjaxResult save(@ModelAttribute SolutionDto entity) {
  57. FileManage cover = null;
  58. if (entity.getSolutionMultipartFile() != null) {
  59. AjaxResult result = fileManageService.uploadFile(entity.getSolutionMultipartFile(), "0", "solution_cover");
  60. cover = (FileManage) result.get("data");
  61. entity.setProductUrl(cover.getFileUrl());
  62. }
  63. service.save(entity);
  64. if (cover != null) {
  65. LambdaUpdateWrapper<FileManage> updateWrapper = new LambdaUpdateWrapper<>();
  66. updateWrapper.eq(FileManage::getId, cover.getId());
  67. updateWrapper.set(FileManage::getFid, entity.getId());
  68. fileManageService.update(updateWrapper);
  69. }
  70. // 删除图片
  71. if (entity.getDeletedImages() != null) {
  72. JSON.parseArray(entity.getDeletedImages()).forEach(q -> {
  73. int idIndex = q.toString().indexOf("id=");
  74. if (idIndex != -1) {
  75. String id = q.toString().substring(idIndex + 3);
  76. fileManageService.deleteFile(id);
  77. }
  78. });
  79. }
  80. // 插入图片
  81. if (entity.getInsertedImages() != null) {
  82. JSON.parseArray(entity.getInsertedImages()).forEach(q -> {
  83. int idIndex = q.toString().indexOf("id=");
  84. if (idIndex != -1) {
  85. String id = q.toString().substring(idIndex + 3);
  86. LambdaUpdateWrapper<FileManage> updateWrapper = new LambdaUpdateWrapper<>();
  87. updateWrapper.eq(FileManage::getId, id);
  88. updateWrapper.set(FileManage::getFid, entity.getId());
  89. fileManageService.update(updateWrapper);
  90. }
  91. });
  92. }
  93. return AjaxResult.success(entity);
  94. }
  95. @PostMapping("/update")
  96. @ApiOperation(value = "解决方案修改")
  97. @Transactional
  98. public AjaxResult update(@ModelAttribute SolutionDto entity) {
  99. if(entity.getSolutionMultipartFile() != null){
  100. // 先删除旧的封面
  101. List<FileManage> oldCovers = fileManageService.listFileByFid(entity.getId());
  102. fileManageService.deleteFiles(oldCovers.stream().filter(q->q.getModuleName().equals("solution_cover")).map(FileManage::getId).collect(Collectors.toList()));
  103. // 上传新的封面
  104. AjaxResult result = fileManageService.uploadFile(entity.getSolutionMultipartFile(), entity.getId(), "solution_cover");
  105. FileManage data = (FileManage) result.get("data");
  106. entity.setProductUrl(data.getFileUrl());
  107. }
  108. if(entity.getDeletedImages() != null){
  109. JSON.parseArray(entity.getDeletedImages()).forEach(q->{
  110. int idIndex = q.toString().indexOf("id=");
  111. if(idIndex != -1){
  112. String id = q.toString().substring(idIndex + 3);
  113. fileManageService.deleteFile(id);
  114. }
  115. });
  116. }
  117. if(entity.getInsertedImages() != null){
  118. JSON.parseArray(entity.getInsertedImages()).forEach(q->{
  119. int idIndex = q.toString().indexOf("id=");
  120. if(idIndex != -1){
  121. String id = q.toString().substring(idIndex + 3);
  122. LambdaUpdateWrapper<FileManage> updateWrapper = new LambdaUpdateWrapper<>();
  123. updateWrapper.eq(FileManage::getId, id);
  124. updateWrapper.set(FileManage::getFid, entity.getId());
  125. fileManageService.update(updateWrapper);
  126. }
  127. });
  128. }
  129. entity.setUpdateTime(LocalDateTime.now());
  130. return service.updateById(entity) ? AjaxResult.success(entity): AjaxResult.error("修改失败");
  131. }
  132. @PostMapping("/deleteBatch")
  133. @ApiOperation(value = "解决方案删除")
  134. @Transactional
  135. public AjaxResult delete(@RequestBody String[] ids) {
  136. fileManageService.deleteFileByFids(Arrays.asList(ids));
  137. return service.removeByIds(Arrays.asList(ids)) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
  138. }
  139. }