From 8e0990832b02950c3b7b103188c9f997109a12b5 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Wed, 17 Dec 2025 20:47:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai-cam):=20=E5=AE=9E=E7=8E=B0AI=E7=9B=B8?= =?UTF-8?q?=E6=9C=BA=E5=85=8D=E8=B4=B9=E7=85=A7=E7=89=87=E8=B5=A0=E9=80=81?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增统计免费关联记录数的Mapper方法 - 在AppAiCamServiceImpl中注入ScenicRepository依赖 - 删除旧关系前查询已有免费记录避免重复赠送 - 根据景区配置获取免费照片数量 - 随机选择免费照片并标记为免费状态 - 保留已存在的免费照片记录 - 更新日志记录以区分普通和免费照片数量 --- .../com/ycwl/basic/mapper/SourceMapper.java | 8 ++++ .../mobile/impl/AppAiCamServiceImpl.java | 43 ++++++++++++++++++- src/main/resources/mapper/SourceMapper.xml | 5 +++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ycwl/basic/mapper/SourceMapper.java b/src/main/java/com/ycwl/basic/mapper/SourceMapper.java index 40c34282..60ad3008 100644 --- a/src/main/java/com/ycwl/basic/mapper/SourceMapper.java +++ b/src/main/java/com/ycwl/basic/mapper/SourceMapper.java @@ -165,4 +165,12 @@ public interface SourceMapper { * @return 删除的记录数 */ int deleteRelationsByFaceIdAndType(Long faceId, Integer type); + + /** + * 统计指定faceId和type的免费关联记录数 + * @param faceId 人脸ID + * @param type 素材类型 + * @return 免费记录数 + */ + int countFreeRelationsByFaceIdAndType(Long faceId, Integer type); } diff --git a/src/main/java/com/ycwl/basic/service/mobile/impl/AppAiCamServiceImpl.java b/src/main/java/com/ycwl/basic/service/mobile/impl/AppAiCamServiceImpl.java index fcfe8f4c..7bf8da9a 100644 --- a/src/main/java/com/ycwl/basic/service/mobile/impl/AppAiCamServiceImpl.java +++ b/src/main/java/com/ycwl/basic/service/mobile/impl/AppAiCamServiceImpl.java @@ -56,6 +56,7 @@ public class AppAiCamServiceImpl implements AppAiCamService { private final FaceService faceService; private final FaceDetectLogAiCamService faceDetectLogAiCamService; private final ScenicService scenicService; + private final ScenicRepository scenicRepository; @Override public List getAiCamGoodsByFaceId(Long faceId) { @@ -268,10 +269,48 @@ public class AppAiCamServiceImpl implements AppAiCamService { throw new IllegalArgumentException("Face未关联会员: faceId=" + faceId); } + // 在删除之前,先查询已有的免费记录 + List existingFreeRelations = sourceMapper.listByFaceRelation(faceId, AI_CAM_SOURCE_TYPE); + Set existingFreeSourceIds = existingFreeRelations.stream() + .filter(r -> r.getIsFree() != null && r.getIsFree() == 1) + .map(MemberSourceEntity::getSourceId) + .collect(Collectors.toSet()); + boolean hasGivenFree = !existingFreeSourceIds.isEmpty(); + // 删除该faceId对应的旧的type=3关系 int deleted = sourceMapper.deleteRelationsByFaceIdAndType(faceId, AI_CAM_SOURCE_TYPE); log.info("删除faceId={}的旧AI相机关联记录: {}条", faceId, deleted); + // 获取景区配置中的免费照片数量 + int freePhotoCount = 0; + if (!hasGivenFree) { + try { + com.ycwl.basic.integration.common.manager.ScenicConfigManager configManager = + scenicRepository.getScenicConfigManager(face.getScenicId()); + if (configManager != null) { + Integer aiCamFreeNum = configManager.getInteger("ai_cam_free_num"); + if (aiCamFreeNum != null && aiCamFreeNum > 0) { + freePhotoCount = aiCamFreeNum; + log.info("景区{}配置免费赠送{}张AI相机照片", face.getScenicId(), freePhotoCount); + } + } + } catch (Exception e) { + log.error("获取景区配置失败", e); + } + } + + // 如果需要赠送免费照片,随机选择 + Set freeSourceIds = new HashSet<>(existingFreeSourceIds); + if (!hasGivenFree && freePhotoCount > 0) { + List shuffledIds = new ArrayList<>(sourceIds); + Collections.shuffle(shuffledIds); + int actualFreeCount = Math.min(freePhotoCount, shuffledIds.size()); + freeSourceIds.addAll(shuffledIds.subList(0, actualFreeCount)); + log.info("为faceId={}随机赠送{}张免费照片", faceId, actualFreeCount); + } else if (hasGivenFree) { + log.info("faceId={}已赠送过免费照片,保留{}张原有免费照片", faceId, existingFreeSourceIds.size()); + } + // 构建MemberSourceEntity列表 List relations = sourceIds.stream().map(sourceId -> { MemberSourceEntity entity = new MemberSourceEntity(); @@ -281,13 +320,13 @@ public class AppAiCamServiceImpl implements AppAiCamService { entity.setSourceId(sourceId); entity.setType(AI_CAM_SOURCE_TYPE); entity.setIsBuy(0); - entity.setIsFree(0); + entity.setIsFree(freeSourceIds.contains(sourceId) ? 1 : 0); return entity; }).collect(Collectors.toList()); // 批量插入 int inserted = sourceMapper.addRelations(relations); - log.info("为faceId={}添加新AI相机关联记录: {}条", faceId, inserted); + log.info("为faceId={}添加新AI相机关联记录: {}条 (其中免费: {}张)", faceId, inserted, freeSourceIds.size()); return inserted; } diff --git a/src/main/resources/mapper/SourceMapper.xml b/src/main/resources/mapper/SourceMapper.xml index 65bf2d21..da5b1c5e 100644 --- a/src/main/resources/mapper/SourceMapper.xml +++ b/src/main/resources/mapper/SourceMapper.xml @@ -507,4 +507,9 @@ DELETE FROM member_source WHERE face_id = #{faceId} AND `type` = #{type} + +