| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package com.zksy.web.controller.base;
- import com.alibaba.fastjson2.JSON;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.zksy.base.domain.FileManage;
- import com.zksy.base.domain.Solution;
- import com.zksy.base.domain.dto.SolutionDto;
- import com.zksy.base.service.FileManageService;
- import com.zksy.base.service.SolutionService;
- import com.zksy.common.core.domain.AjaxResult;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.bind.annotation.*;
- import java.time.LocalDateTime;
- import java.util.Arrays;
- import java.util.List;
- import java.util.stream.Collectors;
- /**
- * @author Administrator
- * @version 1.0
- * @project zksy-website-service
- * @description 解决方案
- * @date 2025/9/8 09:24:07
- */
- @RestController
- @RequestMapping("/solution")
- @Api(tags = "解决方案",description = "解决方案desc")
- public class SolutionController {
- @Autowired
- private SolutionService service;
- @Autowired
- private FileManageService fileManageService;
- @GetMapping("/findByPage")
- @ApiOperation(value = "解决方案查询分页")
- public AjaxResult findByPage(@ApiParam(value = "页码", required = true)long pageNum,
- @ApiParam(value = "页数", required = true)long pageSize){
- Page<Solution> page = new Page<>(pageNum, pageSize);
- return AjaxResult.success(service.page(page));
- }
- @GetMapping("/getSolutionList")
- @ApiOperation(value = "解决方案查询")
- public AjaxResult getSolutionList(){
- return AjaxResult.success(service.list());
- }
- @GetMapping("/getById/{id}")
- @ApiOperation(value = "根据Id查询解决方案")
- public AjaxResult getById(@PathVariable String id){
- return AjaxResult.success(service.getById(id));
- }
- @PostMapping("/save")
- @ApiOperation(value = "解决方案保存")
- @Transactional
- public AjaxResult save(@ModelAttribute SolutionDto entity) {
- FileManage cover = null;
- if (entity.getSolutionMultipartFile() != null) {
- AjaxResult result = fileManageService.uploadFile(entity.getSolutionMultipartFile(), "0", "solution_cover");
- cover = (FileManage) result.get("data");
- entity.setProductUrl(cover.getFileUrl());
- }
- service.save(entity);
- if (cover != null) {
- LambdaUpdateWrapper<FileManage> updateWrapper = new LambdaUpdateWrapper<>();
- updateWrapper.eq(FileManage::getId, cover.getId());
- updateWrapper.set(FileManage::getFid, entity.getId());
- fileManageService.update(updateWrapper);
- }
- // 删除图片
- if (entity.getDeletedImages() != null) {
- JSON.parseArray(entity.getDeletedImages()).forEach(q -> {
- int idIndex = q.toString().indexOf("id=");
- if (idIndex != -1) {
- String id = q.toString().substring(idIndex + 3);
- fileManageService.deleteFile(id);
- }
- });
- }
- // 插入图片
- if (entity.getInsertedImages() != null) {
- JSON.parseArray(entity.getInsertedImages()).forEach(q -> {
- int idIndex = q.toString().indexOf("id=");
- if (idIndex != -1) {
- String id = q.toString().substring(idIndex + 3);
- LambdaUpdateWrapper<FileManage> updateWrapper = new LambdaUpdateWrapper<>();
- updateWrapper.eq(FileManage::getId, id);
- updateWrapper.set(FileManage::getFid, entity.getId());
- fileManageService.update(updateWrapper);
- }
- });
- }
- return AjaxResult.success(entity);
- }
- @PostMapping("/update")
- @ApiOperation(value = "解决方案修改")
- @Transactional
- public AjaxResult update(@ModelAttribute SolutionDto entity) {
- if(entity.getSolutionMultipartFile() != null){
- // 先删除旧的封面
- List<FileManage> oldCovers = fileManageService.listFileByFid(entity.getId());
- fileManageService.deleteFiles(oldCovers.stream().filter(q->q.getModuleName().equals("solution_cover")).map(FileManage::getId).collect(Collectors.toList()));
- // 上传新的封面
- AjaxResult result = fileManageService.uploadFile(entity.getSolutionMultipartFile(), entity.getId(), "solution_cover");
- FileManage data = (FileManage) result.get("data");
- entity.setProductUrl(data.getFileUrl());
- }
- if(entity.getDeletedImages() != null){
- JSON.parseArray(entity.getDeletedImages()).forEach(q->{
- int idIndex = q.toString().indexOf("id=");
- if(idIndex != -1){
- String id = q.toString().substring(idIndex + 3);
- fileManageService.deleteFile(id);
- }
- });
- }
- if(entity.getInsertedImages() != null){
- JSON.parseArray(entity.getInsertedImages()).forEach(q->{
- int idIndex = q.toString().indexOf("id=");
- if(idIndex != -1){
- String id = q.toString().substring(idIndex + 3);
- LambdaUpdateWrapper<FileManage> updateWrapper = new LambdaUpdateWrapper<>();
- updateWrapper.eq(FileManage::getId, id);
- updateWrapper.set(FileManage::getFid, entity.getId());
- fileManageService.update(updateWrapper);
- }
- });
- }
- entity.setUpdateTime(LocalDateTime.now());
- return service.updateById(entity) ? AjaxResult.success(entity): AjaxResult.error("修改失败");
- }
- @PostMapping("/deleteBatch")
- @ApiOperation(value = "解决方案删除")
- @Transactional
- public AjaxResult delete(@RequestBody String[] ids) {
- fileManageService.deleteFileByFids(Arrays.asList(ids));
- return service.removeByIds(Arrays.asList(ids)) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
- }
- }
|