|
|
@@ -1,7 +1,13 @@
|
|
|
package com.zksy.web.controller.base;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+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.NewsUpdates;
|
|
|
+import com.zksy.base.domain.dto.NewsUpdatesDto;
|
|
|
import com.zksy.base.service.FileManageService;
|
|
|
import com.zksy.base.service.NewsUpdatesService;
|
|
|
import com.zksy.common.core.domain.AjaxResult;
|
|
|
@@ -14,6 +20,8 @@ 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
|
|
|
@@ -38,6 +46,7 @@ public class NewsUpdatesController {
|
|
|
Page<NewsUpdates> page = new Page<>(pageNum, pageSize);
|
|
|
return AjaxResult.success(service.page(page));
|
|
|
}
|
|
|
+
|
|
|
@GetMapping("/getNewsUpdatesList")
|
|
|
@ApiOperation(value = "新闻动态查询")
|
|
|
public AjaxResult getNewsUpdatesList(){
|
|
|
@@ -51,20 +60,80 @@ public class NewsUpdatesController {
|
|
|
@PostMapping("/save")
|
|
|
@ApiOperation(value = "新闻动态保存")
|
|
|
@Transactional
|
|
|
- public AjaxResult save(@RequestBody NewsUpdates entity) {
|
|
|
- if(entity.getNewsMultipartFile() != null){
|
|
|
- String filePath = fileManageService.uploadFilePath(entity.getNewsMultipartFile());
|
|
|
- entity.setNewsUrl(filePath);
|
|
|
+ public AjaxResult save(@ModelAttribute NewsUpdatesDto entity) {
|
|
|
+ FileManage cover = null;
|
|
|
+ if (entity.getFile() != null) {
|
|
|
+ AjaxResult result = fileManageService.uploadFile(entity.getFile(), "0", "news_cover");
|
|
|
+ cover = (FileManage) result.get("data");
|
|
|
+ entity.setNewsUrl(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 service.save(entity) ? AjaxResult.success(entity): AjaxResult.error("保存失败");
|
|
|
+
|
|
|
+ return AjaxResult.success(entity);
|
|
|
}
|
|
|
@PostMapping("/update")
|
|
|
@ApiOperation(value = "新闻动态修改")
|
|
|
@Transactional
|
|
|
- public AjaxResult update(@RequestBody NewsUpdates entity) {
|
|
|
- if(entity.getNewsMultipartFile() != null){
|
|
|
- String filePath = fileManageService.uploadFilePath(entity.getNewsMultipartFile());
|
|
|
- entity.setNewsUrl(filePath);
|
|
|
+ public AjaxResult update(@ModelAttribute NewsUpdatesDto entity) {
|
|
|
+ if(entity.getFile() != null){
|
|
|
+ // 先删除旧的封面
|
|
|
+ List<FileManage> oldCovers = fileManageService.listFileByFid(entity.getId());
|
|
|
+ fileManageService.deleteFiles(oldCovers.stream().filter(q->q.getModuleName().equals("news_cover")).map(FileManage::getId).collect(Collectors.toList()));
|
|
|
+ // 上传新的封面
|
|
|
+ AjaxResult result = fileManageService.uploadFile(entity.getFile(), entity.getId(), "news_cover");
|
|
|
+ FileManage data = (FileManage) result.get("data");
|
|
|
+ entity.setNewsUrl(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("修改失败");
|