4.3 私人空间图片批量编辑

This commit is contained in:
zhangsan 2025-04-03 18:14:20 +08:00
parent 886d2a89d6
commit a44466e846
4 changed files with 124 additions and 0 deletions

View File

@ -294,4 +294,15 @@ public class PictureController {
List<PictureVO> pictureVOList = pictureService.searchPictureByColor(spaceId, picColor, loginUser);
return ResultUtils.success(pictureVOList);
}
/**
* 批量编辑图片
*/
@PostMapping("/edit/batch")
public BaseResponse<Boolean> editPictureByBatch(@RequestBody PictureEditByBatchRequest pictureEditByBatchRequest, HttpServletRequest request) {
ThrowUtils.throwIf(pictureEditByBatchRequest == null, ErrorCode.PARAMS_ERROR);
User loginUser = userService.getLoginUser(request);
pictureService.editPictureByBatch(pictureEditByBatchRequest, loginUser);
return ResultUtils.success(true);
}
}

View File

@ -0,0 +1,40 @@
package edu.whut.smilepicturebackend.model.dto.picture;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 图片批量编辑请求
*/
@Data
public class PictureEditByBatchRequest implements Serializable {
/**
* 图片 id 列表
*/
private List<Long> pictureIdList;
/**
* 空间 id
*/
private Long spaceId;
/**
* 分类
*/
private String category;
/**
* 标签
*/
private List<String> tags;
/**
* 命名规则
*/
private String nameRule;
private static final long serialVersionUID = 1L;
}

View File

@ -135,4 +135,13 @@ public interface PictureService extends IService<Picture> {
* @return
*/
List<PictureVO> searchPictureByColor(Long spaceId, String picColor, User loginUser);
/**
* 批量编辑图片
*
* @param pictureEditByBatchRequest
* @param loginUser
*/
void editPictureByBatch(PictureEditByBatchRequest pictureEditByBatchRequest, User loginUser);
}

View File

@ -637,6 +637,70 @@ public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture>
.map(PictureVO::objToVo)
.collect(Collectors.toList());
}
@Override
public void editPictureByBatch(PictureEditByBatchRequest pictureEditByBatchRequest, User loginUser) {
// 1. 获取和校验参数
List<Long> pictureIdList = pictureEditByBatchRequest.getPictureIdList();
Long spaceId = pictureEditByBatchRequest.getSpaceId();
String category = pictureEditByBatchRequest.getCategory();
List<String> tags = pictureEditByBatchRequest.getTags();
ThrowUtils.throwIf(CollUtil.isEmpty(pictureIdList), ErrorCode.PARAMS_ERROR);
ThrowUtils.throwIf(spaceId == null, ErrorCode.PARAMS_ERROR);
ThrowUtils.throwIf(loginUser == null, ErrorCode.NO_AUTH_ERROR);
// 2. 校验空间权限
Space space = spaceService.getById(spaceId);
ThrowUtils.throwIf(space == null, ErrorCode.NOT_FOUND_ERROR, "空间不存在");
if (!space.getUserId().equals(loginUser.getId())) {
throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "没有空间访问权限");
}
// 3. 查询指定图片仅选择需要的字段
List<Picture> pictureList = this.lambdaQuery()
.select(Picture::getId, Picture::getSpaceId)
.eq(Picture::getSpaceId, spaceId)
.in(Picture::getId, pictureIdList)
.list();
if (pictureList.isEmpty()) {
return;
}
// 4. 更新分类和标签
pictureList.forEach(picture -> {
if (StrUtil.isNotBlank(category)) {
picture.setCategory(category);
}
if (CollUtil.isNotEmpty(tags)) {
picture.setTags(JSONUtil.toJsonStr(tags));
}
});
// 批量重命名
String nameRule = pictureEditByBatchRequest.getNameRule();
fillPictureWithNameRule(pictureList, nameRule);
// 5. 操作数据库进行批量更新
boolean result = this.updateBatchById(pictureList);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "批量编辑失败");
}
/**
* nameRule 格式图片-{序号} =>图片-1 图片-2 ...
*
* @param pictureList
* @param nameRule
*/
private void fillPictureWithNameRule(List<Picture> pictureList, String nameRule) {
if (StrUtil.isBlank(nameRule) || CollUtil.isEmpty(pictureList)) {
return;
}
long count = 1;
try {
for (Picture picture : pictureList) {
String pictureName = nameRule.replaceAll("\\{序号}", String.valueOf(count++));
picture.setName(pictureName);
}
} catch (Exception e) {
log.error("名称解析错误", e);
throw new BusinessException(ErrorCode.OPERATION_ERROR, "名称解析错误");
}
}
}