【解决todo】将 LambdaUpdateWrapper 隔离在 mapper,优化toSet toMap 使用 CollectionUtils

This commit is contained in:
cherishsince 2024-06-13 16:03:56 +08:00
parent aa7c2cb251
commit 7c1e949d77
2 changed files with 31 additions and 16 deletions

View File

@ -1,12 +1,17 @@
package cn.iocoder.yudao.module.ai.dal.mysql.image; package cn.iocoder.yudao.module.ai.dal.mysql.image;
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO; import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO;
import cn.iocoder.yudao.module.ai.enums.image.AiImageStatusEnum;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/** /**
* AI 绘图 Mapper * AI 绘图 Mapper
* *
@ -16,6 +21,7 @@ import org.apache.ibatis.annotations.Mapper;
public interface AiImageMapper extends BaseMapperX<AiImageDO> { public interface AiImageMapper extends BaseMapperX<AiImageDO> {
// TODO @fan这个建议直接使用 updateservice 拼接要改的状态哈 // TODO @fan这个建议直接使用 updateservice 拼接要改的状态哈
/** /**
* 更新 - 根据 messageId * 更新 - 根据 messageId
* *
@ -37,10 +43,29 @@ public interface AiImageMapper extends BaseMapperX<AiImageDO> {
return this.selectOne(new LambdaQueryWrapperX<AiImageDO>().eq(AiImageDO::getTaskId, id)); return this.selectOne(new LambdaQueryWrapperX<AiImageDO>().eq(AiImageDO::getTaskId, id));
} }
/**
* 查询 - page
*
* @param userId
* @param pageReqVO
* @return
*/
default PageResult<AiImageDO> selectPage(Long userId, PageParam pageReqVO) { default PageResult<AiImageDO> selectPage(Long userId, PageParam pageReqVO) {
return selectPage(pageReqVO, new LambdaQueryWrapperX<AiImageDO>() return selectPage(pageReqVO, new LambdaQueryWrapperX<AiImageDO>()
.eq(AiImageDO::getUserId, userId) .eq(AiImageDO::getUserId, userId)
.orderByDesc(AiImageDO::getId)); .orderByDesc(AiImageDO::getId));
} }
/**
* 查询 - 根据 status platform
*
* @return
*/
default List<AiImageDO> selectByStatusAndPlatform(AiImageStatusEnum statusEnum, AiPlatformEnum platformEnum) {
return this.selectList(new LambdaUpdateWrapper<AiImageDO>()
.eq(AiImageDO::getStatus, statusEnum.getStatus())
.eq(AiImageDO::getPlatform, platformEnum.getPlatform())
);
}
} }

View File

@ -3,13 +3,12 @@ package cn.iocoder.yudao.module.ai.job;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi; import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.ai.controller.admin.image.vo.MidjourneyNotifyReqVO;
import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO; import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO;
import cn.iocoder.yudao.module.ai.dal.mysql.image.AiImageMapper; import cn.iocoder.yudao.module.ai.dal.mysql.image.AiImageMapper;
import cn.iocoder.yudao.module.ai.enums.image.AiImageStatusEnum; import cn.iocoder.yudao.module.ai.enums.image.AiImageStatusEnum;
import cn.iocoder.yudao.module.ai.service.image.AiImageService; import cn.iocoder.yudao.module.ai.service.image.AiImageService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -17,7 +16,6 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* midjourney job 定时拉去 midjourney 绘制状态 * midjourney job 定时拉去 midjourney 绘制状态
@ -42,25 +40,17 @@ public class MidjourneyJob implements JobHandler {
@Override @Override
public String execute(String param) throws Exception { public String execute(String param) throws Exception {
// 1获取 midjourney 平台状态在 进行中 image // 1获取 midjourney 平台状态在 进行中 image
// TODO @fan43 51 其实有点重叠日志建议只打 51 List<AiImageDO> imageList = imageMapper.selectByStatusAndPlatform(AiImageStatusEnum.IN_PROGRESS, AiPlatformEnum.MIDJOURNEY);
log.info("Midjourney 同步 - 开始...");
// TODO @fanJobService 等业务层不要直接使用 LambdaUpdateWrapper这样会导致 mapper 穿透到逻辑层要收敛到 mapper
List<AiImageDO> imageList = imageMapper.selectList(
new LambdaUpdateWrapper<AiImageDO>()
.eq(AiImageDO::getStatus, AiImageStatusEnum.IN_PROGRESS.getStatus())
.eq(AiImageDO::getPlatform, AiPlatformEnum.MIDJOURNEY.getPlatform())
);
log.info("Midjourney 同步 - 任务数量 {}!", imageList.size()); log.info("Midjourney 同步 - 任务数量 {}!", imageList.size());
if (CollUtil.isEmpty(imageList)) { if (CollUtil.isEmpty(imageList)) {
// TODO @fan51 54其实有点重叠建议 51 挪到 55 之后打
return "Midjourney 同步 - 数量为空!"; return "Midjourney 同步 - 数量为空!";
} }
log.info("Midjourney 同步 - 开始...");
// 2批量拉去 task 信息 // 2批量拉去 task 信息
// TODO @fanimageList.stream().map(AiImageDO::getTaskId).collect(Collectors.toSet()))可以使用 CollectionUtils.convertSet 简化
List<MidjourneyApi.NotifyRequest> taskList = midjourneyApi List<MidjourneyApi.NotifyRequest> taskList = midjourneyApi
.listByCondition(imageList.stream().map(AiImageDO::getTaskId).collect(Collectors.toSet())); .listByCondition(CollectionUtils.convertSet(imageList, AiImageDO::getTaskId));
// TODO @fantaskList.stream().collect(Collectors.toMap(MidjourneyNotifyReqVO::getId, o -> o))也可以使用 CollectionUtils.convertMap本质上重用 setmap 转换 convert 简化 Map<String, MidjourneyApi.NotifyRequest> taskIdMap
Map<String, MidjourneyApi.NotifyRequest> taskIdMap = taskList.stream().collect(Collectors.toMap(MidjourneyApi.NotifyRequest::id, o -> o)); = CollectionUtils.convertMap(taskList, MidjourneyApi.NotifyRequest::id);
// 3更新 image 状态 // 3更新 image 状态
List<AiImageDO> updateImageList = new ArrayList<>(); List<AiImageDO> updateImageList = new ArrayList<>();
for (AiImageDO aiImageDO : imageList) { for (AiImageDO aiImageDO : imageList) {