feat(ai-cam): 实现AI相机免费照片赠送逻辑

- 新增统计免费关联记录数的Mapper方法
- 在AppAiCamServiceImpl中注入ScenicRepository依赖
- 删除旧关系前查询已有免费记录避免重复赠送
- 根据景区配置获取免费照片数量
- 随机选择免费照片并标记为免费状态
- 保留已存在的免费照片记录
- 更新日志记录以区分普通和免费照片数量
This commit is contained in:
2025-12-17 20:47:21 +08:00
parent 144c338972
commit 8e0990832b
3 changed files with 54 additions and 2 deletions

View File

@@ -165,4 +165,12 @@ public interface SourceMapper {
* @return 删除的记录数 * @return 删除的记录数
*/ */
int deleteRelationsByFaceIdAndType(Long faceId, Integer type); int deleteRelationsByFaceIdAndType(Long faceId, Integer type);
/**
* 统计指定faceId和type的免费关联记录数
* @param faceId 人脸ID
* @param type 素材类型
* @return 免费记录数
*/
int countFreeRelationsByFaceIdAndType(Long faceId, Integer type);
} }

View File

@@ -56,6 +56,7 @@ public class AppAiCamServiceImpl implements AppAiCamService {
private final FaceService faceService; private final FaceService faceService;
private final FaceDetectLogAiCamService faceDetectLogAiCamService; private final FaceDetectLogAiCamService faceDetectLogAiCamService;
private final ScenicService scenicService; private final ScenicService scenicService;
private final ScenicRepository scenicRepository;
@Override @Override
public List<GoodsDetailVO> getAiCamGoodsByFaceId(Long faceId) { public List<GoodsDetailVO> getAiCamGoodsByFaceId(Long faceId) {
@@ -268,10 +269,48 @@ public class AppAiCamServiceImpl implements AppAiCamService {
throw new IllegalArgumentException("Face未关联会员: faceId=" + faceId); throw new IllegalArgumentException("Face未关联会员: faceId=" + faceId);
} }
// 在删除之前,先查询已有的免费记录
List<MemberSourceEntity> existingFreeRelations = sourceMapper.listByFaceRelation(faceId, AI_CAM_SOURCE_TYPE);
Set<Long> existingFreeSourceIds = existingFreeRelations.stream()
.filter(r -> r.getIsFree() != null && r.getIsFree() == 1)
.map(MemberSourceEntity::getSourceId)
.collect(Collectors.toSet());
boolean hasGivenFree = !existingFreeSourceIds.isEmpty();
// 删除该faceId对应的旧的type=3关系 // 删除该faceId对应的旧的type=3关系
int deleted = sourceMapper.deleteRelationsByFaceIdAndType(faceId, AI_CAM_SOURCE_TYPE); int deleted = sourceMapper.deleteRelationsByFaceIdAndType(faceId, AI_CAM_SOURCE_TYPE);
log.info("删除faceId={}的旧AI相机关联记录: {}条", faceId, deleted); 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<Long> freeSourceIds = new HashSet<>(existingFreeSourceIds);
if (!hasGivenFree && freePhotoCount > 0) {
List<Long> 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列表 // 构建MemberSourceEntity列表
List<MemberSourceEntity> relations = sourceIds.stream().map(sourceId -> { List<MemberSourceEntity> relations = sourceIds.stream().map(sourceId -> {
MemberSourceEntity entity = new MemberSourceEntity(); MemberSourceEntity entity = new MemberSourceEntity();
@@ -281,13 +320,13 @@ public class AppAiCamServiceImpl implements AppAiCamService {
entity.setSourceId(sourceId); entity.setSourceId(sourceId);
entity.setType(AI_CAM_SOURCE_TYPE); entity.setType(AI_CAM_SOURCE_TYPE);
entity.setIsBuy(0); entity.setIsBuy(0);
entity.setIsFree(0); entity.setIsFree(freeSourceIds.contains(sourceId) ? 1 : 0);
return entity; return entity;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
// 批量插入 // 批量插入
int inserted = sourceMapper.addRelations(relations); int inserted = sourceMapper.addRelations(relations);
log.info("为faceId={}添加新AI相机关联记录: {}条", faceId, inserted); log.info("为faceId={}添加新AI相机关联记录: {}条 (其中免费: {}张)", faceId, inserted, freeSourceIds.size());
return inserted; return inserted;
} }

View File

@@ -507,4 +507,9 @@
DELETE FROM member_source DELETE FROM member_source
WHERE face_id = #{faceId} AND `type` = #{type} WHERE face_id = #{faceId} AND `type` = #{type}
</delete> </delete>
<select id="countFreeRelationsByFaceIdAndType" resultType="int">
SELECT COUNT(*) FROM member_source
WHERE face_id = #{faceId} AND `type` = #{type} AND is_free = 1
</select>
</mapper> </mapper>