|
|
@@ -0,0 +1,290 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.Activity;
|
|
|
+import com.etotem.cfc.entity.ButlerResourceFollowUp;
|
|
|
+import com.etotem.cfc.entity.ButlerResourceMatchLog;
|
|
|
+import com.etotem.cfc.entity.ButlerResourceMatchResult;
|
|
|
+import com.etotem.cfc.mapper.ActivityMapper;
|
|
|
+import com.etotem.cfc.mapper.ButlerResourceFollowUpMapper;
|
|
|
+import com.etotem.cfc.mapper.ButlerResourceMatchLogMapper;
|
|
|
+import com.etotem.cfc.mapper.ButlerResourceMatchResultMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 线下资源/活动与家庭需求匹配(能力键 {@code butler.resource.match},SC-H-05)。
|
|
|
+ *
|
|
|
+ * <p>正常流:按家长需求与孩子画像匹配资源 → 推送匹配结果 → 协助预约/报名 → 参与后回访并沉淀偏好。
|
|
|
+ * 候选仅来自<b>已审核</b>资源(AC1,{@code auditStatus=approved} 且 {@code status=published});
|
|
|
+ * 推送留痕表按同类资源频次受控(TC-H-05-E-4);预约/报名联动 {@link ActivityRegistrationService#register}
|
|
|
+ * (内部 {@code occupyQuota} 原子占用,满员提示「名额已满」,AC3/TC-H-05-E-2/3)。</p>
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ButlerResourceMatchService {
|
|
|
+
|
|
|
+ /** 同类资源高频推送阈值:超过后限制推送(TC-H-05-E-4) */
|
|
|
+ private static final int PUSH_FREQUENCY_LIMIT = 5;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ActivityMapper activityMapper;
|
|
|
+ @Resource
|
|
|
+ private ButlerResourceMatchResultMapper matchResultMapper;
|
|
|
+ @Resource
|
|
|
+ private ButlerResourceMatchLogMapper matchLogMapper;
|
|
|
+ @Resource
|
|
|
+ private ButlerResourceFollowUpMapper followUpMapper;
|
|
|
+ @Resource
|
|
|
+ private ActivityRegistrationService activityRegistrationService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 匹配入口:按家长需求与孩子画像匹配线下资源/活动(TC-H-05 正常流步骤1)。
|
|
|
+ *
|
|
|
+ * <p>候选仅已审核资源({@code auditStatus=approved} 且 {@code status=published},AC1/TC-H-05-E-1);
|
|
|
+ * 每个候选携带距离/时间/适龄信息(AC1);匹配结果落库供家长端查看选择(AC2);
|
|
|
+ * 推送留痕频次受控(TC-H-05-E-4)。</p>
|
|
|
+ *
|
|
|
+ * @param butlerUserId 推荐管家用户 ID
|
|
|
+ * @param familyId 家庭 ID
|
|
|
+ * @param memberId 目标成员 ID(孩子画像)
|
|
|
+ * @param requirements 家长需求:dimensionCode(五维)/keyword(关键词)/childAge(年龄,适龄判定)/location(位置,距离估算)
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public Result<List<ButlerResourceMatchResult>> match(Long butlerUserId, Long familyId, Long memberId,
|
|
|
+ Map<String, Object> requirements) {
|
|
|
+ // 仅已审核 + 已发布资源入候选(未审核/已下架资源不出现,TC-H-05-E-1)
|
|
|
+ LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<Activity>()
|
|
|
+ .eq(Activity::getStatus, "published")
|
|
|
+ .eq(Activity::getAuditStatus, "approved");
|
|
|
+
|
|
|
+ String dimensionCode = requirements != null ? asString(requirements.get("dimensionCode")) : null;
|
|
|
+ if (dimensionCode != null && !dimensionCode.trim().isEmpty()) {
|
|
|
+ wrapper.eq(Activity::getDimensionCode, dimensionCode.trim());
|
|
|
+ }
|
|
|
+ String keyword = requirements != null ? asString(requirements.get("keyword")) : null;
|
|
|
+ if (keyword != null && !keyword.trim().isEmpty()) {
|
|
|
+ wrapper.and(w -> w.like(Activity::getTitle, keyword.trim())
|
|
|
+ .or().like(Activity::getIntro, keyword.trim())
|
|
|
+ .or().like(Activity::getLocation, keyword.trim()));
|
|
|
+ }
|
|
|
+ wrapper.orderByDesc(Activity::getStartTime);
|
|
|
+
|
|
|
+ List<Activity> activities = activityMapper.selectList(wrapper);
|
|
|
+ if (activities.isEmpty()) {
|
|
|
+ return Result.success(Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ List<ButlerResourceMatchResult> results = new ArrayList<>();
|
|
|
+ for (Activity activity : activities) {
|
|
|
+ String category = activity.getDimensionCode() != null ? activity.getDimensionCode() : "general";
|
|
|
+ // 频次受控:同类资源已达推送阈值则跳过(TC-H-05-E-4)
|
|
|
+ if (isCategoryPushLimited(butlerUserId, memberId, category)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ButlerResourceMatchResult result = buildMatchResult(butlerUserId, familyId, memberId, activity, requirements);
|
|
|
+ matchResultMapper.insert(result);
|
|
|
+ recordPush(butlerUserId, memberId, activity, category);
|
|
|
+ results.add(result);
|
|
|
+ }
|
|
|
+ return Result.success(results);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 协助预约/报名入口(AC3):联动名额实时校验(occupyQuota 原子占用,满员提示「名额已满」)。
|
|
|
+ *
|
|
|
+ * @param userId 操作家长用户 ID
|
|
|
+ * @param memberId 报名成员 ID
|
|
|
+ * @param matchResultId 匹配结果 ID
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public Result<Map<String, Object>> book(Long userId, Long memberId, Long matchResultId) {
|
|
|
+ ButlerResourceMatchResult matchResult = matchResultMapper.selectById(matchResultId);
|
|
|
+ if (matchResult == null) {
|
|
|
+ return Result.notFound("匹配结果不存在");
|
|
|
+ }
|
|
|
+ // 归属校验(TC-H-05-P-②/③):仅本家庭成员可操作该匹配结果
|
|
|
+ if (matchResult.getMemberId() != null && !matchResult.getMemberId().equals(memberId)) {
|
|
|
+ return Result.forbidden("无权操作该匹配结果");
|
|
|
+ }
|
|
|
+ if (!"activity".equals(matchResult.getResourceType())) {
|
|
|
+ return Result.error("该资源暂不支持在线预约,请联系统筹管家");
|
|
|
+ }
|
|
|
+ Result<Map<String, Object>> regResult = activityRegistrationService.register(
|
|
|
+ matchResult.getResourceId(), userId, memberId);
|
|
|
+ if (regResult.getCode() == 200) {
|
|
|
+ matchResult.setStatus("booked");
|
|
|
+ matchResult.setUpdatedAt(new Date());
|
|
|
+ matchResultMapper.updateById(matchResult);
|
|
|
+ }
|
|
|
+ return regResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回访沉淀偏好(AC4):参与后回访记录写入家长档案,后续推荐参考偏好。
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public Result<String> recordFollowUp(Long butlerUserId, Long familyId, Long memberId,
|
|
|
+ Map<String, Object> params) {
|
|
|
+ if (params == null) {
|
|
|
+ return Result.badRequest("回访参数不能为空");
|
|
|
+ }
|
|
|
+ ButlerResourceFollowUp followUp = new ButlerResourceFollowUp();
|
|
|
+ followUp.setFamilyId(familyId);
|
|
|
+ followUp.setMemberId(memberId);
|
|
|
+ followUp.setButlerUserId(butlerUserId);
|
|
|
+ followUp.setResourceId(params.get("resourceId") != null ? Long.valueOf(params.get("resourceId").toString()) : null);
|
|
|
+ followUp.setResourceType(asString(params.getOrDefault("resourceType", "activity")));
|
|
|
+ followUp.setFollowUpContent(asString(params.get("followUpContent")));
|
|
|
+ followUp.setPreferenceTags(params.get("preferenceTags") != null
|
|
|
+ ? params.get("preferenceTags").toString() : null);
|
|
|
+ followUp.setFollowUpAt(new Date());
|
|
|
+ followUp.setCreatedAt(new Date());
|
|
|
+ followUp.setUpdatedAt(new Date());
|
|
|
+ followUpMapper.insert(followUp);
|
|
|
+ return Result.success("回访记录已沉淀");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+ // 内部辅助
|
|
|
+ // ------------------------------------------------------------------
|
|
|
+
|
|
|
+ private ButlerResourceMatchResult buildMatchResult(Long butlerUserId, Long familyId, Long memberId,
|
|
|
+ Activity activity, Map<String, Object> requirements) {
|
|
|
+ ButlerResourceMatchResult result = new ButlerResourceMatchResult();
|
|
|
+ result.setButlerUserId(butlerUserId);
|
|
|
+ result.setFamilyId(familyId);
|
|
|
+ result.setMemberId(memberId);
|
|
|
+ result.setResourceType("activity");
|
|
|
+ result.setResourceId(activity.getId());
|
|
|
+ result.setResourceName(activity.getTitle());
|
|
|
+ result.setDistanceKm(estimateDistanceKm(activity, requirements));
|
|
|
+ result.setStartTime(activity.getStartTime());
|
|
|
+ result.setAgeSuitability(resolveAgeSuitability(activity, requirements));
|
|
|
+ result.setReviewStatus(activity.getAuditStatus()); // approved 门禁(候选仅已审核)
|
|
|
+ result.setMatchReason(buildMatchReason(activity, requirements));
|
|
|
+ result.setMatchScore(buildMatchScore(activity, requirements));
|
|
|
+ result.setStatus("matched");
|
|
|
+ Date now = new Date();
|
|
|
+ result.setCreatedAt(now);
|
|
|
+ result.setUpdatedAt(now);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** AC1:距离估算——位置关键词命中则按区域相近给低值,否则给默认值;无位置信息返回 null。 */
|
|
|
+ private Double estimateDistanceKm(Activity activity, Map<String, Object> requirements) {
|
|
|
+ String location = activity.getLocation();
|
|
|
+ String preferred = requirements != null ? asString(requirements.get("location")) : null;
|
|
|
+ if (location == null || location.trim().isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (preferred != null && !preferred.trim().isEmpty()) {
|
|
|
+ // 同区域(省/市/区关键词)视为近距离
|
|
|
+ return location.contains(preferred.trim()) ? 2.0 : 8.0;
|
|
|
+ }
|
|
|
+ return 5.0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** AC1:适龄判定——按参与人群构成与孩子年龄给出可读标签。 */
|
|
|
+ private String resolveAgeSuitability(Activity activity, Map<String, Object> requirements) {
|
|
|
+ String audienceType = activity.getAudienceType();
|
|
|
+ String childAge = requirements != null ? asString(requirements.get("childAge")) : null;
|
|
|
+ if ("adult".equals(audienceType)) {
|
|
|
+ return "仅成人参与";
|
|
|
+ }
|
|
|
+ if (childAge != null && !childAge.trim().isEmpty()) {
|
|
|
+ return childAge.trim() + "岁可参与";
|
|
|
+ }
|
|
|
+ if ("include_child".equals(audienceType) || "include_child_elder".equals(audienceType)) {
|
|
|
+ return "含孩子参与";
|
|
|
+ }
|
|
|
+ return "通用";
|
|
|
+ }
|
|
|
+
|
|
|
+ private String buildMatchReason(Activity activity, Map<String, Object> requirements) {
|
|
|
+ String dimensionCode = requirements != null ? asString(requirements.get("dimensionCode")) : null;
|
|
|
+ StringBuilder reason = new StringBuilder("匹配维度匹配");
|
|
|
+ if (dimensionCode != null && dimensionCode.equals(activity.getDimensionCode())) {
|
|
|
+ reason.append(",契合需求五维「").append(dimensionCode).append("」");
|
|
|
+ }
|
|
|
+ if (activity.getMaxParticipants() != null && activity.getMaxParticipants() > 0) {
|
|
|
+ int remain = activity.getMaxParticipants()
|
|
|
+ - (activity.getApprovedCount() != null ? activity.getApprovedCount() : 0);
|
|
|
+ reason.append(",余位 ").append(Math.max(remain, 0)).append(" 人");
|
|
|
+ }
|
|
|
+ return reason.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer buildMatchScore(Activity activity, Map<String, Object> requirements) {
|
|
|
+ int score = 60;
|
|
|
+ String dimensionCode = requirements != null ? asString(requirements.get("dimensionCode")) : null;
|
|
|
+ if (dimensionCode != null && dimensionCode.equals(activity.getDimensionCode())) {
|
|
|
+ score += 20;
|
|
|
+ }
|
|
|
+ if ("include_child".equals(activity.getAudienceType())
|
|
|
+ || "include_child_elder".equals(activity.getAudienceType())) {
|
|
|
+ score += 10;
|
|
|
+ }
|
|
|
+ if (activity.getApprovedCount() != null && activity.getMaxParticipants() != null
|
|
|
+ && activity.getMaxParticipants() > 0
|
|
|
+ && activity.getApprovedCount() < activity.getMaxParticipants()) {
|
|
|
+ score += 10;
|
|
|
+ }
|
|
|
+ return Math.min(score, 100);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** TC-H-05-E-4:同类资源累计推送次数是否已达阈值(超过则限制推送)。 */
|
|
|
+ private boolean isCategoryPushLimited(Long butlerUserId, Long memberId, String category) {
|
|
|
+ ButlerResourceMatchLog latest = matchLogMapper.selectOne(new LambdaQueryWrapper<ButlerResourceMatchLog>()
|
|
|
+ .eq(ButlerResourceMatchLog::getButlerUserId, butlerUserId)
|
|
|
+ .eq(ButlerResourceMatchLog::getMemberId, memberId)
|
|
|
+ .eq(ButlerResourceMatchLog::getResourceCategory, category)
|
|
|
+ .orderByDesc(ButlerResourceMatchLog::getPushedAt)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ return latest != null && latest.getPushCount() != null
|
|
|
+ && latest.getPushCount() >= PUSH_FREQUENCY_LIMIT;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 推送留痕:同类资源累计次数 +1(幂等按 管家+成员+分类 聚合)。 */
|
|
|
+ private void recordPush(Long butlerUserId, Long memberId, Activity activity, String category) {
|
|
|
+ ButlerResourceMatchLog latest = matchLogMapper.selectOne(new LambdaQueryWrapper<ButlerResourceMatchLog>()
|
|
|
+ .eq(ButlerResourceMatchLog::getButlerUserId, butlerUserId)
|
|
|
+ .eq(ButlerResourceMatchLog::getMemberId, memberId)
|
|
|
+ .eq(ButlerResourceMatchLog::getResourceCategory, category)
|
|
|
+ .orderByDesc(ButlerResourceMatchLog::getPushedAt)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ Date now = new Date();
|
|
|
+ if (latest == null) {
|
|
|
+ ButlerResourceMatchLog logEntry = new ButlerResourceMatchLog();
|
|
|
+ logEntry.setButlerUserId(butlerUserId);
|
|
|
+ logEntry.setMemberId(memberId);
|
|
|
+ logEntry.setResourceId(activity.getId());
|
|
|
+ logEntry.setResourceType("activity");
|
|
|
+ logEntry.setResourceCategory(category);
|
|
|
+ logEntry.setPushedAt(now);
|
|
|
+ logEntry.setPushCount(1);
|
|
|
+ logEntry.setCreatedAt(now);
|
|
|
+ logEntry.setUpdatedAt(now);
|
|
|
+ matchLogMapper.insert(logEntry);
|
|
|
+ } else {
|
|
|
+ latest.setResourceId(activity.getId());
|
|
|
+ latest.setPushedAt(now);
|
|
|
+ latest.setPushCount((latest.getPushCount() == null ? 0 : latest.getPushCount()) + 1);
|
|
|
+ latest.setUpdatedAt(now);
|
|
|
+ matchLogMapper.updateById(latest);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String asString(Object value) {
|
|
|
+ return value != null ? value.toString() : null;
|
|
|
+ }
|
|
|
+}
|