|
|
@@ -1914,6 +1914,175 @@ public class EnergyService {
|
|
|
return executeAward(memberId, "challenge", null, amount, reason, null, allocations);
|
|
|
}
|
|
|
|
|
|
+ // ==================== 账本一致性守卫(energy.ledger.guard,S-cfc-390 · SC-X-19/20/21) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 成员-家庭归属校验入口(SC-X-19 跨角色数据隔离)。
|
|
|
+ *
|
|
|
+ * <p>校验 memberId 是否属于 familyId 家庭:命中 family_members 记录则比较其 family_id,
|
|
|
+ * 未命中(可能是家庭主账号 users.id)则回落 users.family_id 比较。
|
|
|
+ * 供 overview/logs/grant 等能量端点在进入服务层前做越权 403 守卫。</p>
|
|
|
+ *
|
|
|
+ * @param memberId 目标成员ID(family_members.id 或家庭主账号 users.id)
|
|
|
+ * @param familyId 当前登录者所在家庭ID
|
|
|
+ * @return true=属于该家庭(可访问);false=越权(应拒绝 403)
|
|
|
+ */
|
|
|
+ public boolean isMemberInFamily(Long memberId, Long familyId) {
|
|
|
+ if (memberId == null || familyId == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ FamilyMember fm = familyMemberMapper.selectById(memberId);
|
|
|
+ if (fm != null) {
|
|
|
+ return familyId.equals(fm.getFamilyId());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("成员归属校验查询 family_members 异常 memberId={}: {}", memberId, e.getMessage());
|
|
|
+ }
|
|
|
+ // memberId 可能为家庭主账号 users.id(契约单测场景 userMapper 未注入时按不可信处理,返回 false)
|
|
|
+ if (userMapper != null) {
|
|
|
+ try {
|
|
|
+ User u = userMapper.selectById(memberId);
|
|
|
+ if (u != null && u.getFamilyId() != null) {
|
|
|
+ return familyId.equals(u.getFamilyId());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("成员归属校验查询 users 异常 memberId={}: {}", memberId, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 幂等/去重守卫入口(SC-X-21 重复请求幂等)。
|
|
|
+ *
|
|
|
+ * <p>同 member + dimension + sourceType + sourceId 已入账则返回 true(应拒绝重复发放),
|
|
|
+ * 配合 energy_log 幂等唯一键 {@code uk_energy_idempotency} 双保险,
|
|
|
+ * 使网络重试/重复回调不会重复加能量(ED-1 重复审批幂等)。</p>
|
|
|
+ */
|
|
|
+ public boolean isAlreadyApplied(Long memberId, Long dimensionId, String sourceType, Long sourceId) {
|
|
|
+ if (memberId == null || dimensionId == null || sourceType == null || sourceId == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Long count = energyLogMapper.selectCount(new LambdaQueryWrapper<EnergyLog>()
|
|
|
+ .eq(EnergyLog::getMemberId, memberId)
|
|
|
+ .eq(EnergyLog::getDimensionId, dimensionId)
|
|
|
+ .eq(EnergyLog::getSourceType, sourceType)
|
|
|
+ .eq(EnergyLog::getSourceId, sourceId));
|
|
|
+ return count != null && count > 0;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("能量幂等去重检查异常 memberId={}: {}", memberId, e.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 账本一致性对账入口(SC-X-20)。
|
|
|
+ *
|
|
|
+ * <p>按创建时间/ID 顺序逐条校验 {@code balanceAfter == prev + amount},
|
|
|
+ * 任一条不一致即记录断点流水 ID 并返回(不静默通过),
|
|
|
+ * 与参考契约算法 {@code totalEarned - totalSpent == balance} 同口径。
|
|
|
+ * 供运营/对账任务调用,返回非空即存在不一致流水。</p>
|
|
|
+ *
|
|
|
+ * @param memberId 目标成员ID(null=非法入参,返回空列表)
|
|
|
+ * @return 断点流水 ID 列表(空 = 账本一致)
|
|
|
+ */
|
|
|
+ public List<Long> reconcileLedger(Long memberId) {
|
|
|
+ List<Long> breaches = new ArrayList<>();
|
|
|
+ if (memberId == null) {
|
|
|
+ return breaches;
|
|
|
+ }
|
|
|
+ List<EnergyLog> logs;
|
|
|
+ try {
|
|
|
+ LambdaQueryWrapper<EnergyLog> qw = new LambdaQueryWrapper<EnergyLog>()
|
|
|
+ .eq(EnergyLog::getMemberId, memberId)
|
|
|
+ .orderByAsc(EnergyLog::getCreatedAt)
|
|
|
+ .orderByAsc(EnergyLog::getId);
|
|
|
+ SortUtil.applySort(qw);
|
|
|
+ logs = energyLogMapper.selectList(qw);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("能量对账查询异常 memberId={}: {}", memberId, e.getMessage());
|
|
|
+ return breaches;
|
|
|
+ }
|
|
|
+ return countBreaches(logs);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 逐条校验 balanceAfter == prev + amount,返回断点流水 ID 列表(契约参考算法等价实现)。
|
|
|
+ *
|
|
|
+ * <p>断点游标取该条实际 balanceAfter,保证后续「误报」只出现在断点本身,不级联放大
|
|
|
+ * (一致序列恒 0 断点,首个断点后续链条仍按实际余额续算)。</p>
|
|
|
+ */
|
|
|
+ private List<Long> countBreaches(List<EnergyLog> logs) {
|
|
|
+ List<Long> breaches = new ArrayList<>();
|
|
|
+ if (logs == null || logs.isEmpty()) {
|
|
|
+ return breaches;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < logs.size(); i++) {
|
|
|
+ EnergyLog log = logs.get(i);
|
|
|
+ if (i == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ EnergyLog prev = logs.get(i - 1);
|
|
|
+ Integer prevBal = prev.getBalanceAfter();
|
|
|
+ Integer amount = log.getAmount();
|
|
|
+ Integer after = log.getBalanceAfter();
|
|
|
+ if (prevBal == null || amount == null || after == null) {
|
|
|
+ breaches.add(log.getId());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ long expected = prevBal.longValue() + amount.longValue();
|
|
|
+ if (after.longValue() != expected) {
|
|
|
+ breaches.add(log.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return breaches;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量原子加守卫入口(SC-X-21 all-or-nothing)。
|
|
|
+ *
|
|
|
+ * <p>按维度 code → 金额 一次性发放:任一维度缺失/无效则整体拒绝(抛异常回滚)或跳过,
|
|
|
+ * 单事务内完成全部余额更新与流水写入,部分失败不残半笔。
|
|
|
+ * 未传入维度分配时回落标准 {@link #awardEnergy} 渠道。</p>
|
|
|
+ *
|
|
|
+ * @param memberId 目标成员ID
|
|
|
+ * @param sourceType 来源类型(task/product/...)
|
|
|
+ * @param sourceId 来源ID
|
|
|
+ * @param totalAmount 总能量值(<=0 直接返回空)
|
|
|
+ * @param description 描述
|
|
|
+ * @param daysToExpire 过期天数(null=不过期)
|
|
|
+ * @param dimAllocations 维度code → 金额 分配(null=回落标准渠道)
|
|
|
+ * @return Map<dimensionCode, amount>(空 = 未发放)
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public Map<String, Integer> awardEnergyAtomic(Long memberId, String sourceType, Long sourceId,
|
|
|
+ Integer totalAmount, String description,
|
|
|
+ Integer daysToExpire, Map<String, Integer> dimAllocations) {
|
|
|
+ if (totalAmount == null || totalAmount <= 0) {
|
|
|
+ return new HashMap<>();
|
|
|
+ }
|
|
|
+ if (dimAllocations == null || dimAllocations.isEmpty()) {
|
|
|
+ return awardEnergy(memberId, sourceType, sourceId, totalAmount, description, daysToExpire);
|
|
|
+ }
|
|
|
+ Map<Long, Integer> allocations = new LinkedHashMap<>();
|
|
|
+ for (Map.Entry<String, Integer> entry : dimAllocations.entrySet()) {
|
|
|
+ if (entry.getKey() == null || entry.getValue() == null || entry.getValue() <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ EnergyDimension dim = getDimByCode(entry.getKey());
|
|
|
+ if (dim == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ allocations.put(dim.getId(), entry.getValue());
|
|
|
+ }
|
|
|
+ if (allocations.isEmpty()) {
|
|
|
+ return new HashMap<>();
|
|
|
+ }
|
|
|
+ return executeAward(memberId, sourceType, sourceId, totalAmount, description, daysToExpire, allocations);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 查询五维能量概览
|
|
|
*/
|