Commit 7c1e949d77315754d0c0377e7bea8df1764a8e1a
1 parent
aa7c2cb2
【解决todo】将 LambdaUpdateWrapper 隔离在 mapper,优化toSet toMap 使用 CollectionUtils
Showing
2 changed files
with
31 additions
and
16 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/image/AiImageMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.ai.dal.mysql.image; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; | |
| 3 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 4 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 6 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 6 | 7 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 7 | 8 | import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO; |
| 9 | +import cn.iocoder.yudao.module.ai.enums.image.AiImageStatusEnum; | |
| 10 | +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | |
| 8 | 11 | import org.apache.ibatis.annotations.Mapper; |
| 9 | 12 | |
| 13 | +import java.util.List; | |
| 14 | + | |
| 10 | 15 | /** |
| 11 | 16 | * AI 绘图 Mapper |
| 12 | 17 | * |
| ... | ... | @@ -16,6 +21,7 @@ import org.apache.ibatis.annotations.Mapper; |
| 16 | 21 | public interface AiImageMapper extends BaseMapperX<AiImageDO> { |
| 17 | 22 | |
| 18 | 23 | // TODO @fan:这个建议,直接使用 update,service 拼接要改的状态哈 |
| 24 | + | |
| 19 | 25 | /** |
| 20 | 26 | * 更新 - 根据 messageId |
| 21 | 27 | * |
| ... | ... | @@ -37,10 +43,29 @@ public interface AiImageMapper extends BaseMapperX<AiImageDO> { |
| 37 | 43 | return this.selectOne(new LambdaQueryWrapperX<AiImageDO>().eq(AiImageDO::getTaskId, id)); |
| 38 | 44 | } |
| 39 | 45 | |
| 46 | + /** | |
| 47 | + * 查询 - page | |
| 48 | + * | |
| 49 | + * @param userId | |
| 50 | + * @param pageReqVO | |
| 51 | + * @return | |
| 52 | + */ | |
| 40 | 53 | default PageResult<AiImageDO> selectPage(Long userId, PageParam pageReqVO) { |
| 41 | 54 | return selectPage(pageReqVO, new LambdaQueryWrapperX<AiImageDO>() |
| 42 | 55 | .eq(AiImageDO::getUserId, userId) |
| 43 | 56 | .orderByDesc(AiImageDO::getId)); |
| 44 | 57 | } |
| 45 | 58 | |
| 59 | + /** | |
| 60 | + * 查询 - 根据 status 和 platform | |
| 61 | + * | |
| 62 | + * @return | |
| 63 | + */ | |
| 64 | + default List<AiImageDO> selectByStatusAndPlatform(AiImageStatusEnum statusEnum, AiPlatformEnum platformEnum) { | |
| 65 | + return this.selectList(new LambdaUpdateWrapper<AiImageDO>() | |
| 66 | + .eq(AiImageDO::getStatus, statusEnum.getStatus()) | |
| 67 | + .eq(AiImageDO::getPlatform, platformEnum.getPlatform()) | |
| 68 | + ); | |
| 69 | + } | |
| 70 | + | |
| 46 | 71 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/job/MidjourneyJob.java
| ... | ... | @@ -3,13 +3,12 @@ package cn.iocoder.yudao.module.ai.job; |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum; |
| 5 | 5 | import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi; |
| 6 | +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | |
| 6 | 7 | import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; |
| 7 | -import cn.iocoder.yudao.module.ai.controller.admin.image.vo.MidjourneyNotifyReqVO; | |
| 8 | 8 | import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO; |
| 9 | 9 | import cn.iocoder.yudao.module.ai.dal.mysql.image.AiImageMapper; |
| 10 | 10 | import cn.iocoder.yudao.module.ai.enums.image.AiImageStatusEnum; |
| 11 | 11 | import cn.iocoder.yudao.module.ai.service.image.AiImageService; |
| 12 | -import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | |
| 13 | 12 | import lombok.extern.slf4j.Slf4j; |
| 14 | 13 | import org.springframework.beans.factory.annotation.Autowired; |
| 15 | 14 | import org.springframework.stereotype.Component; |
| ... | ... | @@ -17,7 +16,6 @@ import org.springframework.stereotype.Component; |
| 17 | 16 | import java.util.ArrayList; |
| 18 | 17 | import java.util.List; |
| 19 | 18 | import java.util.Map; |
| 20 | -import java.util.stream.Collectors; | |
| 21 | 19 | |
| 22 | 20 | /** |
| 23 | 21 | * midjourney job 定时拉去 midjourney 绘制状态 |
| ... | ... | @@ -42,25 +40,17 @@ public class MidjourneyJob implements JobHandler { |
| 42 | 40 | @Override |
| 43 | 41 | public String execute(String param) throws Exception { |
| 44 | 42 | // 1、获取 midjourney 平台,状态在 “进行中” 的 image |
| 45 | - // TODO @fan:43 和 51 其实有点重叠,日志,建议只打 51 | |
| 46 | - log.info("Midjourney 同步 - 开始..."); | |
| 47 | - // TODO @fan:Job、Service 等业务层,不要直接使用 LambdaUpdateWrapper,这样会导致 mapper 穿透到逻辑层。要收敛到 mapper 里。 | |
| 48 | - List<AiImageDO> imageList = imageMapper.selectList( | |
| 49 | - new LambdaUpdateWrapper<AiImageDO>() | |
| 50 | - .eq(AiImageDO::getStatus, AiImageStatusEnum.IN_PROGRESS.getStatus()) | |
| 51 | - .eq(AiImageDO::getPlatform, AiPlatformEnum.MIDJOURNEY.getPlatform()) | |
| 52 | - ); | |
| 43 | + List<AiImageDO> imageList = imageMapper.selectByStatusAndPlatform(AiImageStatusEnum.IN_PROGRESS, AiPlatformEnum.MIDJOURNEY); | |
| 53 | 44 | log.info("Midjourney 同步 - 任务数量 {}!", imageList.size()); |
| 54 | 45 | if (CollUtil.isEmpty(imageList)) { |
| 55 | - // TODO @fan:51 和 54,其实有点重叠。建议 51 挪到 55 之后打。 | |
| 56 | 46 | return "Midjourney 同步 - 数量为空!"; |
| 57 | 47 | } |
| 48 | + log.info("Midjourney 同步 - 开始..."); | |
| 58 | 49 | // 2、批量拉去 task 信息 |
| 59 | - // TODO @fan:imageList.stream().map(AiImageDO::getTaskId).collect(Collectors.toSet())),可以使用 CollectionUtils.convertSet 简化 | |
| 60 | 50 | List<MidjourneyApi.NotifyRequest> taskList = midjourneyApi |
| 61 | - .listByCondition(imageList.stream().map(AiImageDO::getTaskId).collect(Collectors.toSet())); | |
| 62 | - // TODO @fan:taskList.stream().collect(Collectors.toMap(MidjourneyNotifyReqVO::getId, o -> o)),也可以使用 CollectionUtils.convertMap;本质上,重用 set、map 转换,要 convert 简化 | |
| 63 | - Map<String, MidjourneyApi.NotifyRequest> taskIdMap = taskList.stream().collect(Collectors.toMap(MidjourneyApi.NotifyRequest::id, o -> o)); | |
| 51 | + .listByCondition(CollectionUtils.convertSet(imageList, AiImageDO::getTaskId)); | |
| 52 | + Map<String, MidjourneyApi.NotifyRequest> taskIdMap | |
| 53 | + = CollectionUtils.convertMap(taskList, MidjourneyApi.NotifyRequest::id); | |
| 64 | 54 | // 3、更新 image 状态 |
| 65 | 55 | List<AiImageDO> updateImageList = new ArrayList<>(); |
| 66 | 56 | for (AiImageDO aiImageDO : imageList) { | ... | ... |