故事项新建调整,上传图像增加缩略图
This commit is contained in:
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.timeline.common.response.ResponseEntity;
|
||||
import com.timeline.story.entity.StoryItem;
|
||||
import com.timeline.story.service.StoryItemService;
|
||||
import com.timeline.story.vo.StoryItemAddVo;
|
||||
import com.timeline.story.vo.StoryItemVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -21,10 +22,10 @@ public class StoryItemController {
|
||||
private StoryItemService storyItemService;
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<String> createItem(@RequestParam("storyItem") String storyItemVoString, @RequestParam("cover") MultipartFile cover, @RequestParam("images") List<MultipartFile> images) {
|
||||
public ResponseEntity<String> createItem(@RequestParam("storyItem") String storyItemVoString, @RequestParam(value = "images", required = false) List<MultipartFile> images) {
|
||||
|
||||
log.info("创建 StoryItem,{}", storyItemVoString);
|
||||
storyItemService.createItemWithCover(JSONObject.parseObject(storyItemVoString, StoryItemVo.class), cover, images);
|
||||
storyItemService.createStoryItem(JSONObject.parseObject(storyItemVoString, StoryItemAddVo.class), images);
|
||||
return ResponseEntity.success("StoryItem 创建成功");
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.List;
|
||||
public interface FileServiceClient {
|
||||
|
||||
@PostMapping(value = "/upload-image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
ResponseEntity<String> uploadCover(@RequestPart("image") MultipartFile image);
|
||||
ResponseEntity<String> uploadImage(@RequestPart("image") MultipartFile image);
|
||||
|
||||
@GetMapping("/download/cover/{coverKey}")
|
||||
InputStreamResource downloadCover(@PathVariable String coverKey);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.timeline.story.service;
|
||||
|
||||
import com.timeline.story.entity.StoryItem;
|
||||
import com.timeline.story.vo.StoryItemAddVo;
|
||||
import com.timeline.story.vo.StoryItemVo;
|
||||
import com.timeline.story.vo.StoryItemWithCoverVo;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -8,8 +9,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import java.util.List;
|
||||
|
||||
public interface StoryItemService {
|
||||
void createItem(StoryItemVo storyItemVo);
|
||||
void createItemWithCover(StoryItemVo storyItemVo, MultipartFile cover, List<MultipartFile> images);
|
||||
void createStoryItem(StoryItemAddVo storyItemVo, List<MultipartFile> images);
|
||||
StoryItemWithCoverVo getStoryItemWithCover(String itemId);
|
||||
void updateItem(String itemId, String description, String location);
|
||||
void deleteItem(String itemId);
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.timeline.story.dao.StoryItemMapper;
|
||||
import com.timeline.story.entity.StoryItem;
|
||||
import com.timeline.story.feign.FileServiceClient;
|
||||
import com.timeline.story.service.StoryItemService;
|
||||
import com.timeline.story.vo.StoryItemAddVo;
|
||||
import com.timeline.story.vo.StoryItemVo;
|
||||
import com.timeline.story.vo.StoryItemWithCoverVo;
|
||||
import com.timeline.common.utils.IdUtils;
|
||||
@@ -33,35 +34,10 @@ public class StoryItemServiceImpl implements StoryItemService {
|
||||
@Autowired
|
||||
private CommonRelationMapper commonRelationMapper;
|
||||
|
||||
@Override
|
||||
public void createItem(StoryItemVo storyItemVo) {
|
||||
try {
|
||||
StoryItem item = new StoryItem();
|
||||
item.setInstanceId(IdUtils.randomUuidUpper());
|
||||
item.setMasterItemId(storyItemVo.getMasterItemId());
|
||||
item.setDescription(storyItemVo.getDescription());
|
||||
item.setTitle(storyItemVo.getTitle());
|
||||
item.setLocation(storyItemVo.getLocation());
|
||||
item.setCreateId("createId");
|
||||
item.setIsDelete(0);
|
||||
item.setCreateTime(LocalDateTime.now());
|
||||
item.setUpdateTime(LocalDateTime.now());
|
||||
item.setStoryItemTime(storyItemVo.getStoryItemTime());
|
||||
storyItemMapper.insert(item);
|
||||
} catch (Exception e) {
|
||||
log.error("创建 StoryItem 失败", e);
|
||||
throw new RuntimeException("创建 StoryItem 失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createItemWithCover(StoryItemVo storyItemVo, MultipartFile cover, List<MultipartFile> images) {
|
||||
public void createStoryItem(StoryItemAddVo storyItemVo, List<MultipartFile> images) {
|
||||
try {
|
||||
// 1. 上传封面到 file 服务
|
||||
ResponseEntity<String> coverResponse = fileServiceClient.uploadCover(cover);
|
||||
String coverKey = coverResponse.getData();
|
||||
log.info("上传成功,文件instanceId:{}", coverKey);
|
||||
|
||||
// 2. 创建 StoryItem 实体
|
||||
StoryItem item = new StoryItem();
|
||||
item.setInstanceId(IdUtils.randomUuidUpper());
|
||||
@@ -72,21 +48,25 @@ public class StoryItemServiceImpl implements StoryItemService {
|
||||
item.setLocation(storyItemVo.getLocation());
|
||||
item.setCreateId("createId");
|
||||
item.setStoryItemTime(storyItemVo.getStoryItemTime());
|
||||
item.setIsDelete(0);
|
||||
item.setCoverInstanceId(coverKey);
|
||||
|
||||
// 3. 上传所有图片并建立关联
|
||||
for (MultipartFile image : images) {
|
||||
ResponseEntity<String> response = fileServiceClient.uploadCover(image);
|
||||
String key = response.getData();
|
||||
log.info("上传成功,文件instanceId:{}", key);
|
||||
// 4. 保存封面与 StoryItem 的关联关系
|
||||
buildStoryItemImageRelation(item.getInstanceId(), key);
|
||||
}
|
||||
// 4. 记录封面与 StoryItem 的关联关系
|
||||
buildStoryItemImageRelation(item.getInstanceId(), coverKey);
|
||||
// 3. 插入到 story_item 表
|
||||
item.setIsDelete(CommonConstants.NOT_DELETED);
|
||||
storyItemMapper.insert(item);
|
||||
if (storyItemVo.getRelatedImageInstanceIds() != null && !storyItemVo.getRelatedImageInstanceIds().isEmpty()) {
|
||||
for (String imageInstanceId : storyItemVo.getRelatedImageInstanceIds()) {
|
||||
log.info("关联现有图像 {} - {}", imageInstanceId, item.getInstanceId());
|
||||
// 3. 建立 StoryItem 与图像关系
|
||||
buildStoryItemImageRelation(item.getInstanceId(), imageInstanceId);
|
||||
}
|
||||
}
|
||||
if (images != null) {
|
||||
log.info("上传 StoryItem 关联图像");
|
||||
for (MultipartFile image : images) {
|
||||
ResponseEntity<String> response = fileServiceClient.uploadImage(image);
|
||||
String key = response.getData();
|
||||
log.info("上传成功,文件instanceId:{}", key);
|
||||
// 4. 建立图像与StoryItem 关系
|
||||
buildStoryItemImageRelation(item.getInstanceId(), key);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("创建 StoryItem 并上传封面失败", e);
|
||||
throw new CustomException(ResponseEnum.INTERNAL_SERVER_ERROR, "上传封面失败");
|
||||
@@ -160,7 +140,7 @@ public class StoryItemServiceImpl implements StoryItemService {
|
||||
return storyItemMapper.countByStoryId(instanceId);
|
||||
}
|
||||
|
||||
private void buildStoryItemImageRelation(String storyItemId, String imageIds) {
|
||||
private void buildStoryItemImageRelation(String storyItemId, String imageIds) {
|
||||
CommonRelationDTO relationDTO = new CommonRelationDTO();
|
||||
relationDTO.setRelaId(storyItemId);
|
||||
relationDTO.setSubRelaId(imageIds);
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.timeline.story.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class StoryItemAddVo extends StoryItemVo{
|
||||
private MultipartFile cover;
|
||||
private List<String> relatedImageInstanceIds;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
<mapper namespace="com.timeline.story.dao.StoryItemMapper">
|
||||
|
||||
<insert id="insert">
|
||||
INSERT INTO story_item (instance_id, master_item_id, description, location, title, create_id, story_instance_id, is_delete, story_item_time, cover_instance_id)
|
||||
VALUES (#{instanceId}, #{masterItemId}, #{description}, #{location}, #{title},#{createId}, #{storyInstanceId}, #{isDelete}, #{storyItemTime}, #{coverInstanceId})
|
||||
INSERT INTO story_item (instance_id, master_item_id, description, location, title, create_id, story_instance_id, is_delete, story_item_time)
|
||||
VALUES (#{instanceId}, #{masterItemId}, #{description}, #{location}, #{title},#{createId}, #{storyInstanceId}, #{isDelete}, #{storyItemTime})
|
||||
</insert>
|
||||
|
||||
<update id="update">
|
||||
|
||||
Reference in New Issue
Block a user