mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	【解决todo】将 LambdaUpdateWrapper 隔离在 mapper,优化toSet toMap 使用 CollectionUtils
This commit is contained in:
		| @@ -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:这个建议,直接使用 update,service 拼接要改的状态哈 |     // TODO @fan:这个建议,直接使用 update,service 拼接要改的状态哈 | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 更新 - 根据 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()) | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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 @fan:43 和 51 其实有点重叠,日志,建议只打 51 |         List<AiImageDO> imageList = imageMapper.selectByStatusAndPlatform(AiImageStatusEnum.IN_PROGRESS, AiPlatformEnum.MIDJOURNEY); | ||||||
|         log.info("Midjourney 同步 - 开始..."); |  | ||||||
|         // TODO @fan:Job、Service 等业务层,不要直接使用 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 @fan:51 和 54,其实有点重叠。建议 51 挪到 55 之后打。 |  | ||||||
|             return "Midjourney 同步 - 数量为空!"; |             return "Midjourney 同步 - 数量为空!"; | ||||||
|         } |         } | ||||||
|  |         log.info("Midjourney 同步 - 开始..."); | ||||||
|         // 2、批量拉去 task 信息 |         // 2、批量拉去 task 信息 | ||||||
|         // TODO @fan:imageList.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 @fan:taskList.stream().collect(Collectors.toMap(MidjourneyNotifyReqVO::getId, o -> o)),也可以使用 CollectionUtils.convertMap;本质上,重用 set、map 转换,要 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) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 cherishsince
					cherishsince