|
@@ -1,5 +1,9 @@
|
|
|
package com.backendsys.modules.crt.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
+import com.backendsys.exception.CustException;
|
|
|
+import com.backendsys.modules.common.config.security.utils.SecurityUtil;
|
|
|
import com.backendsys.modules.crt.dao.CrtLoraStyleCategoryDao;
|
|
|
import com.backendsys.modules.crt.dao.CrtLoraStyleCollectDao;
|
|
|
import com.backendsys.modules.crt.dao.CrtLoraStyleDao;
|
|
@@ -15,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -35,19 +40,45 @@ public class CrtLoraStyleServiceImpl implements CrtLoraStyleService {
|
|
|
@Override
|
|
|
public PageEntity selectCrtLoraStyleList(CrtLoraStyle crtLoraStyle) {
|
|
|
PageUtils.startPage(); // 分页
|
|
|
+
|
|
|
List<Map<String, Object>> list = crtLoraStyleDao.selectCrtLoraStyleList(crtLoraStyle);
|
|
|
+ if (!list.isEmpty()) {
|
|
|
+
|
|
|
+ // 查询风格LoRA-当前用户的收藏状态,将收藏状态添加到 list 中
|
|
|
+ LambdaQueryWrapper<CrtLoraStyleCollect> wrapperCollect = new LambdaQueryWrapper<>();
|
|
|
+ wrapperCollect.eq(CrtLoraStyleCollect::getUser_id, SecurityUtil.getUserId());
|
|
|
+ List<CrtLoraStyleCollect> collectList = crtLoraStyleCollectDao.selectList(wrapperCollect);
|
|
|
+ List<Long> collect_lora_style_ids = collectList.stream().map(item -> item.getLora_style_id()).collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 将收藏状态添加到 list 中
|
|
|
+ list = list.stream().map(item -> {
|
|
|
+ item.put("is_collect", (collect_lora_style_ids.contains(Convert.toLong(item.get("id")))) ? 1 : -1);
|
|
|
+ return item;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ }
|
|
|
return new PageInfoResult(list).toEntity();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取短剧创作-风格分类列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<CrtLoraStyleCategory> selectCrtLoraStyleCategoryList() {
|
|
|
+ PageUtils.startPage(); // 分页
|
|
|
+ return crtLoraStyleCategoryDao.selectList(null);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取短剧创作-风格LoRA列表-用户收藏的
|
|
|
*/
|
|
|
@Override
|
|
|
public PageEntity selectCrtLoraStyleListByCollect(CrtLoraStyle crtLoraStyle) {
|
|
|
|
|
|
- // 查询当前用户,已经收藏的风格ID集合
|
|
|
+ // 查询当前用户,已经收藏的风格ID集合 (按创建时间倒序)
|
|
|
LambdaQueryWrapper<CrtLoraStyleCollect> wrapperCollect = new LambdaQueryWrapper<>();
|
|
|
wrapperCollect.eq(CrtLoraStyleCollect::getUser_id, crtLoraStyle.getUser_id());
|
|
|
+ wrapperCollect.orderByDesc(CrtLoraStyleCollect::getCreate_time);
|
|
|
List<CrtLoraStyleCollect> crtLoraStyleCollectList = crtLoraStyleCollectDao.selectList(wrapperCollect);
|
|
|
|
|
|
PageUtils.startPage(); // 分页
|
|
@@ -56,30 +87,65 @@ public class CrtLoraStyleServiceImpl implements CrtLoraStyleService {
|
|
|
List<CrtLoraStyle> list = new ArrayList<>();
|
|
|
if (!crtLoraStyleCollectList.isEmpty()) {
|
|
|
|
|
|
- LambdaQueryWrapper<CrtLoraStyle> wrapper = new LambdaQueryWrapper<>();
|
|
|
- List<Long> lora_style_ids = crtLoraStyleCollectList.stream()
|
|
|
+ // 获得已经收藏的风格LoRA ID集合
|
|
|
+ List<Long> collect_lora_style_ids = crtLoraStyleCollectList.stream()
|
|
|
.map(CrtLoraStyleCollect::getLora_style_id)
|
|
|
.collect(Collectors.toList());
|
|
|
- wrapper.in(CrtLoraStyle::getId, lora_style_ids);
|
|
|
+
|
|
|
+ LambdaQueryWrapper<CrtLoraStyle> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.in(CrtLoraStyle::getId, collect_lora_style_ids);
|
|
|
if (!crtLoraStyle.getName().isEmpty()) wrapper.like(CrtLoraStyle::getName, crtLoraStyle.getName());
|
|
|
if (crtLoraStyle.getCategory_id() != null) wrapper.eq(CrtLoraStyle::getCategory_id, crtLoraStyle.getCategory_id());
|
|
|
|
|
|
+ // [DB] 查询风格LoRA列表 (已经收藏的
|
|
|
list = crtLoraStyleDao.selectList(wrapper);
|
|
|
- list = list.stream().map(item -> {
|
|
|
- item.setUser_id(crtLoraStyle.getUser_id());
|
|
|
- return item;
|
|
|
- }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 按照 lora_style_ids 的顺序 (按创建时间倒序) 对 list 进行重新排序
|
|
|
+ CollUtil.sort(list, Comparator.comparingInt(item -> collect_lora_style_ids.indexOf(item.getId())));
|
|
|
+
|
|
|
+ list = list.stream()
|
|
|
+ .map(item -> {
|
|
|
+ item.setUser_id(crtLoraStyle.getUser_id());
|
|
|
+ item.setIs_collect(1);
|
|
|
+ return item;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
}
|
|
|
return new PageInfoResult(list).toEntity();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取短剧创作-风格分类列表
|
|
|
+ * 获取短剧创作-风格LoRA列表-加入/取消收藏
|
|
|
*/
|
|
|
@Override
|
|
|
- public List<CrtLoraStyleCategory> selectCrtLoraStyleCategoryList() {
|
|
|
- PageUtils.startPage(); // 分页
|
|
|
- return crtLoraStyleCategoryDao.selectList(null);
|
|
|
+ public Map<String, Object> setLoraStyleCollect(CrtLoraStyleCollect crtLoraStyleCollect) {
|
|
|
+
|
|
|
+ // 是否收藏 (-1取消收藏, 1加入收藏)
|
|
|
+ Integer is_collect = crtLoraStyleCollect.getIs_collect();
|
|
|
+ Long lora_style_id = crtLoraStyleCollect.getLora_style_id();
|
|
|
+
|
|
|
+ // 查询当前用户,是否已经收藏过该风格
|
|
|
+ LambdaQueryWrapper<CrtLoraStyleCollect> wrapperCollect = new LambdaQueryWrapper<>();
|
|
|
+ wrapperCollect.eq(CrtLoraStyleCollect::getUser_id, crtLoraStyleCollect.getUser_id());
|
|
|
+ wrapperCollect.eq(CrtLoraStyleCollect::getLora_style_id, lora_style_id);
|
|
|
+
|
|
|
+ if (is_collect == 1) {
|
|
|
+ // 如果已经收藏过,则不变
|
|
|
+ CrtLoraStyleCollect detail = crtLoraStyleCollectDao.selectOne(wrapperCollect);
|
|
|
+ if (detail == null) {
|
|
|
+
|
|
|
+ // [DB] 判断风格LoRA是否存在
|
|
|
+ Boolean is_exist = crtLoraStyleDao.exists(new LambdaQueryWrapper<CrtLoraStyle>().eq(CrtLoraStyle::getId, lora_style_id));
|
|
|
+ if (!is_exist) throw new CustException("风格LoRA不存在");
|
|
|
+
|
|
|
+ // [DB] 加入收藏
|
|
|
+ crtLoraStyleCollectDao.insert(crtLoraStyleCollect);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // [DB] 取消收藏
|
|
|
+ crtLoraStyleCollectDao.delete(wrapperCollect);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Map.of("lora_style_id", crtLoraStyleCollect.getLora_style_id());
|
|
|
}
|
|
|
|
|
|
}
|