|
|
@@ -0,0 +1,220 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.entity.TeamStarSnapshot;
|
|
|
+import com.etotem.cfc.mapper.TeamStarSnapshotMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 月度星级简报生成组件(能力键 {@code team.star.digest},单元 team,任务 S-cfc-70 / TDD-GREEN)。
|
|
|
+ *
|
|
|
+ * <p>规格:{@code docs/superpowers/specs/2026-11-07-referral-team-profit-sharing.md}
|
|
|
+ * §4.3.1 ③④ + §7.6 + §12-T8。</p>
|
|
|
+ *
|
|
|
+ * <p><b>③ 月度星级简报</b>(每月 1 日 00:30 重评后主动推送微信订阅消息):
|
|
|
+ * 升星(恭喜 + 新星级 + 解锁分佣份数 + 权益)/ 降星(提示 + 原因(滚动窗口滑出)
|
|
|
+ * + 需补多少分回原级)/ 持平(距下一星差多少 + 本月行动建议,复用②换算);
|
|
|
+ * 数据源 = {@code team_star_snapshot} 本月 vs 上月对比({@code uk_user_period} 唯一键)。</p>
|
|
|
+ *
|
|
|
+ * <p><b>④ 星级权益可见</b>:星级徽章(团队页展示)、培训/专家课优先、带教与领队资格、
|
|
|
+ * 年度表彰;高星权益只多不少。</p>
|
|
|
+ *
|
|
|
+ * <p><b>权限流</b>:简报为<b>服务端主动推送</b>(每月 1 日重评后发微信订阅消息),
|
|
|
+ * 收件人即成员本人,无用户鉴权分支,权限流不适用。</p>
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class StarDigestService {
|
|
|
+
|
|
|
+ /** 最低/最高星级(§4.3 一星~六星)。 */
|
|
|
+ private static final int MIN_STAR = 1;
|
|
|
+ private static final int MAX_STAR = 6;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 星级业绩分门槛(§4.3 档位表,index=星级,与 {@link StarScoreCalculator} 同源)。
|
|
|
+ * 本地副本用于简报稿件的纯函数计算(契约级单测无 Spring 上下文)。
|
|
|
+ */
|
|
|
+ private static final int[] STAR_THRESHOLDS = {0, 0, 8_000, 25_000, 70_000, 140_000, 250_000};
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TeamStarSnapshotMapper teamStarSnapshotMapper;
|
|
|
+
|
|
|
+ // ==================== 三态趋势分类(§4.3.1 ③) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 升/降/持平分类:+1=升星 / 0=持平 / -1=降星(§4.3.1 ③)。
|
|
|
+ * <p>{@code previousStarLevel=0}(首期无上月快照)按持平处理,不虚报升/降。</p>
|
|
|
+ */
|
|
|
+ public int digestTrend(int currentStarLevel, int previousStarLevel) {
|
|
|
+ if (previousStarLevel <= 0) {
|
|
|
+ // 首期无上月快照:按持平处理,不虚报升/降
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (currentStarLevel > previousStarLevel) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ if (currentStarLevel < previousStarLevel) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 月度简报正文入口(§12-T8) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 月度简报正文入口:本月 vs 上月 {@code team_star_snapshot} 对比后按三态出稿(§12-T8)。
|
|
|
+ *
|
|
|
+ * @param userId 成员用户ID
|
|
|
+ * @param period 当期周期(YYYYMM,对应 {@code team_star_snapshot.period})
|
|
|
+ * @return 简报正文(升/降/持平三态稿件)
|
|
|
+ */
|
|
|
+ public String digestMonthly(long userId, String period) {
|
|
|
+ TeamStarSnapshot current = loadSnapshot(userId, period);
|
|
|
+ TeamStarSnapshot previous = loadSnapshot(userId, previousPeriod(period));
|
|
|
+ int currentStar = current != null && current.getStarLevel() != null
|
|
|
+ ? current.getStarLevel() : MIN_STAR;
|
|
|
+ int previousStar = previous != null && previous.getStarLevel() != null
|
|
|
+ ? previous.getStarLevel() : 0;
|
|
|
+ long currentScore = current != null && current.getScore() != null
|
|
|
+ ? current.getScore() : 0L;
|
|
|
+ int trend = digestTrend(currentStar, previousStar);
|
|
|
+ if (trend > 0) {
|
|
|
+ return upgradeMessage(userId, currentStar);
|
|
|
+ }
|
|
|
+ if (trend < 0) {
|
|
|
+ int oldStar = previousStar > 0 ? previousStar : currentStar;
|
|
|
+ return downgradeMessage(userId, oldStar, currentStar, (int) currentScore);
|
|
|
+ }
|
|
|
+ return flatMessage(userId, currentStar, (int) currentScore);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 三态简报稿件(§4.3.1 ③) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 升星稿件:恭喜 + 新星级 + 解锁分佣份数(每层按星级拿份数 = 新星级份,最多 6 份)+ 星级权益(§4.3.1 ③④)。
|
|
|
+ */
|
|
|
+ public String upgradeMessage(long userId, int newStarLevel) {
|
|
|
+ int star = clamp(newStarLevel);
|
|
|
+ int shares = star;
|
|
|
+ String benefits = String.join("、", starBenefits(star));
|
|
|
+ return "恭喜!您已升至 " + star + " 星。本月星级重评后,您的分佣解锁份数提升至 "
|
|
|
+ + shares + " 份(每层按星级拿份数,最多 " + MAX_STAR + " 份)。星级权益:" + benefits + "。"
|
|
|
+ + "继续带教团队、推荐新人,冲刺更高星级!";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 降星稿件:提示 + 原因(滚动窗口滑出)+ 需补多少分回原级(§4.3.1 ③)。
|
|
|
+ */
|
|
|
+ public String downgradeMessage(long userId, int oldStarLevel, int currentStarLevel, int currentScore) {
|
|
|
+ int oldStar = clamp(oldStarLevel);
|
|
|
+ int curStar = clamp(currentStarLevel);
|
|
|
+ int score = Math.max(0, currentScore);
|
|
|
+ int gap = thresholdOfStar(oldStar) - score;
|
|
|
+ String reason = gap > 0
|
|
|
+ ? "缺口 = " + oldStar + " 星门槛 " + thresholdOfStar(oldStar) + " - 当前 " + score + " 分"
|
|
|
+ : "业绩分已达标,还需满足团队直推 ≥1";
|
|
|
+ return "很遗憾,您的星级由 " + oldStar + " 星降至 " + curStar + " 星。"
|
|
|
+ + "原因:近 12 个月滚动窗口内部分业绩(团队直推 / 业绩分)滑出窗口,"
|
|
|
+ + "加权业绩分自然下降,星级自动降级(§4.3 不保留历史级别、不固化)。"
|
|
|
+ + "回到 " + oldStar + " 星需补 " + Math.max(0, gap) + " 分(" + reason + ")。"
|
|
|
+ + "本月继续努力,加油!";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 持平稿件:距下一星差多少 + 本月行动建议(复用②差距→行动换算);六星(顶流)无下一档不再虚构差距。
|
|
|
+ */
|
|
|
+ public String flatMessage(long userId, int currentStarLevel, int currentScore) {
|
|
|
+ int star = clamp(currentStarLevel);
|
|
|
+ int score = Math.max(0, currentScore);
|
|
|
+ if (star >= MAX_STAR) {
|
|
|
+ return "您保持 " + star + " 星(顶流团队长,已顶格)。"
|
|
|
+ + "本月行动建议:保持带教与团队服务,守住六星份额。";
|
|
|
+ }
|
|
|
+ int nextStar = star + 1;
|
|
|
+ int gap = Math.max(0, thresholdOfStar(nextStar) - score);
|
|
|
+ int directTimes = ceilDiv(gap, 250);
|
|
|
+ int teamTimes = ceilDiv(gap, 200);
|
|
|
+ int referralYuan = ceilDiv(gap * 100, 10);
|
|
|
+ return "您保持 " + star + " 星(与上月持平)。距 " + nextStar + " 星还差 " + gap + " 分。"
|
|
|
+ + "本月行动建议(§4.3.1 ② 差距→行动换算):"
|
|
|
+ + "A 直接服务 250 分/次 → 再 " + directTimes + " 次 1对1 咨询;"
|
|
|
+ + "B 团队带教 200 分/次 → 再 " + teamTimes + " 次带教服务;"
|
|
|
+ + "C 推荐消费 10 分/百元 → 再 ¥" + referralYuan + " 推荐消费。";
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 星级权益可见(§4.3.1 ④) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 该星级的可感知权益清单(星级徽章 / 培训与专家课优先 / 带教与领队资格 / 年度表彰,§4.3.1 ④),
|
|
|
+ * 高星权益只多不少。
|
|
|
+ */
|
|
|
+ public List<String> starBenefits(int starLevel) {
|
|
|
+ int star = clamp(starLevel);
|
|
|
+ List<String> benefits = new ArrayList<>();
|
|
|
+ benefits.add("星级徽章");
|
|
|
+ if (star >= 2) {
|
|
|
+ benefits.add("培训与专家课优先");
|
|
|
+ }
|
|
|
+ if (star >= 3) {
|
|
|
+ benefits.add("带教与领队资格");
|
|
|
+ }
|
|
|
+ if (star >= 4) {
|
|
|
+ benefits.add("年度表彰");
|
|
|
+ }
|
|
|
+ if (star >= 5) {
|
|
|
+ benefits.add("团队奖池上浮");
|
|
|
+ }
|
|
|
+ if (star >= 6) {
|
|
|
+ benefits.add("顶流团队长特权");
|
|
|
+ }
|
|
|
+ return Collections.unmodifiableList(benefits);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 私有辅助 ====================
|
|
|
+
|
|
|
+ /** 星级业绩分门槛(§4.3 档位表)。 */
|
|
|
+ private int thresholdOfStar(int star) {
|
|
|
+ return STAR_THRESHOLDS[clamp(star)];
|
|
|
+ }
|
|
|
+
|
|
|
+ private int clamp(int star) {
|
|
|
+ return Math.max(MIN_STAR, Math.min(star, MAX_STAR));
|
|
|
+ }
|
|
|
+
|
|
|
+ private int ceilDiv(int a, int b) {
|
|
|
+ return (a + b - 1) / b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 读取某周期快照(无则返回 null)。 */
|
|
|
+ private TeamStarSnapshot loadSnapshot(long userId, String period) {
|
|
|
+ if (period == null || period.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return teamStarSnapshotMapper.selectOne(new LambdaQueryWrapper<TeamStarSnapshot>()
|
|
|
+ .eq(TeamStarSnapshot::getUserId, userId)
|
|
|
+ .eq(TeamStarSnapshot::getPeriod, period));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 当期周期(YYYYMM)的上月周期(YYYYMM)。 */
|
|
|
+ private String previousPeriod(String period) {
|
|
|
+ if (period == null || period.length() < 6) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ int year = Integer.parseInt(period.substring(0, 4));
|
|
|
+ int month = Integer.parseInt(period.substring(4, 6));
|
|
|
+ if (month <= 1) {
|
|
|
+ return String.format("%04d%02d", year - 1, 12);
|
|
|
+ }
|
|
|
+ return String.format("%04d%02d", year, month - 1);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|