|
|
@@ -19,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.stream.Collectors;
|
|
|
import com.etotem.cfc.util.SortUtil;
|
|
|
import com.etotem.cfc.service.NotificationDispatchService;
|
|
|
@@ -32,6 +33,13 @@ import com.etotem.cfc.service.NotificationDispatchService;
|
|
|
@Service
|
|
|
public class EnergyService {
|
|
|
|
|
|
+ /**
|
|
|
+ * SC-P-89 / energy.admin.adjust:工单号维度的手动发放幂等台账。
|
|
|
+ * 同一工单号重复提交时直接返回首次发放结果,不再写流水(进程内去重;DB 侧仍由
|
|
|
+ * uk_energy_idempotency 兜底普通来源的当日幂等)。
|
|
|
+ */
|
|
|
+ private final Map<String, EnergyLog> processedWorkOrders = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
@Resource
|
|
|
private UserMapper userMapper;
|
|
|
|
|
|
@@ -1966,6 +1974,26 @@ public class EnergyService {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * SC-P-89 / energy.admin.adjust:发放能量的跨家庭拦截判定。
|
|
|
+ *
|
|
|
+ * <p>仅当目标成员明确带有 familyId 且与当前登录者家庭不一致时才判为越权(403)。
|
|
|
+ * 成员归属未知(familyId 为空 / 查不到成员)时不拦截,保持既有观察口径,
|
|
|
+ * 避免历史数据缺 familyId 时误伤正常发放。</p>
|
|
|
+ */
|
|
|
+ public boolean isMemberInOtherFamily(Long memberId, Long familyId) {
|
|
|
+ if (memberId == null || familyId == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ FamilyMember fm = familyMemberMapper.selectById(memberId);
|
|
|
+ return fm != null && fm.getFamilyId() != null && !familyId.equals(fm.getFamilyId());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("发放能量归属校验查询 family_members 异常 memberId={}: {}", memberId, e.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* SC-P-19 energy.dimension.view:只读查询五维配置(身/心/智/行/富定义与权重)。
|
|
|
*
|
|
|
@@ -2302,6 +2330,23 @@ public class EnergyService {
|
|
|
@Transactional
|
|
|
public void configureSourceRatios(String sourceType, Long sourceId, String sourceName,
|
|
|
List<Map<String, Object>> ratios) {
|
|
|
+ // SC-P-92 / energy.admin.adjust:比例之和必须为 100%,校验必须在任何写操作之前,
|
|
|
+ // 否则会先 delete 旧配置再写入非法配置(TC-P-92-E)。
|
|
|
+ int total = 0;
|
|
|
+ if (ratios == null || ratios.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("来源比例不能为空");
|
|
|
+ }
|
|
|
+ for (Map<String, Object> r : ratios) {
|
|
|
+ Object raw = r == null ? null : r.get("ratio");
|
|
|
+ if (raw == null) {
|
|
|
+ throw new IllegalArgumentException("来源比例不能为空");
|
|
|
+ }
|
|
|
+ total += Integer.parseInt(raw.toString());
|
|
|
+ }
|
|
|
+ if (total != 100) {
|
|
|
+ throw new IllegalArgumentException("来源比例之和必须为100%,当前为" + total + "%");
|
|
|
+ }
|
|
|
+
|
|
|
energySourceConfigMapper.delete(
|
|
|
new LambdaQueryWrapper<EnergySourceConfig>()
|
|
|
.eq(EnergySourceConfig::getSourceType, sourceType)
|
|
|
@@ -2335,6 +2380,19 @@ public class EnergyService {
|
|
|
*/
|
|
|
@Transactional
|
|
|
public EnergyLog grantEnergy(Long memberId, Long taskId, Long amount, String dimensionCode) {
|
|
|
+ return grantEnergy(memberId, taskId, amount, dimensionCode, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * SC-P-89 / energy.admin.adjust:发放能量(任务奖励 或 家长/运营手动调整)。
|
|
|
+ *
|
|
|
+ * <p>无 taskId 且带备注/工单号时按 {@code sourceType=manual} 留痕,备注写入流水描述;
|
|
|
+ * 带 taskId 时保持既有 {@code sourceType=task} 语义不变(EnergyGrantContractTest 不受影响)。
|
|
|
+ * 带工单号时按工单号幂等:重复提交直接返回首次结果,不重复写流水。</p>
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public EnergyLog grantEnergy(Long memberId, Long taskId, Long amount, String dimensionCode,
|
|
|
+ String remark, String workOrderNo) {
|
|
|
// CFC-EN-001 修复:grantEnergy 的维度由调用方显式指定,能量应直接发放到该维度。
|
|
|
// 旧实现委托 awardEnergy("task"),命中 task_complete 行为配置(100% 分配到 action),
|
|
|
// 随后按请求的 dimensionCode 从分配结果取数,请求 body 维度时 balanceMap.get("body")=null
|
|
|
@@ -2347,6 +2405,24 @@ public class EnergyService {
|
|
|
throw new IllegalArgumentException("维度代码无效:" + dimensionCode);
|
|
|
}
|
|
|
|
|
|
+ boolean manual = taskId == null;
|
|
|
+ String sourceType = manual ? "manual" : "task";
|
|
|
+ Long sourceId = manual ? null : taskId;
|
|
|
+ String description = manual
|
|
|
+ ? (remark != null && !remark.trim().isEmpty() ? remark : "手动调整")
|
|
|
+ : "任务奖励";
|
|
|
+
|
|
|
+ // 工单号幂等:同一工单号重复发放直接返回首次结果,不重复写流水(TC-P-89-E)
|
|
|
+ String workOrderKey = (workOrderNo != null && !workOrderNo.trim().isEmpty())
|
|
|
+ ? (memberId == null ? "" : memberId) + ":" + workOrderNo.trim() : null;
|
|
|
+ if (workOrderKey != null) {
|
|
|
+ EnergyLog cached = processedWorkOrders.get(workOrderKey);
|
|
|
+ if (cached != null) {
|
|
|
+ log.info("工单号重复发放,幂等返回首次结果: workOrderNo={}, memberId={}", workOrderNo, memberId);
|
|
|
+ return cached;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
Map<Long, Integer> allocations = new LinkedHashMap<>();
|
|
|
allocations.put(dim.getId(), amount.intValue());
|
|
|
|
|
|
@@ -2358,8 +2434,8 @@ public class EnergyService {
|
|
|
}
|
|
|
|
|
|
// 执行发放(更新余额 + 写流水 + 站内通知)
|
|
|
- Map<String, Integer> balanceMap = executeAward(memberId, "task", taskId,
|
|
|
- amount.intValue(), "任务奖励", null, allocations);
|
|
|
+ Map<String, Integer> balanceMap = executeAward(memberId, sourceType, sourceId,
|
|
|
+ amount.intValue(), description, null, allocations);
|
|
|
|
|
|
Integer energyAmount = balanceMap.get(dimensionCode);
|
|
|
if (energyAmount == null || energyAmount <= 0) {
|
|
|
@@ -2371,11 +2447,14 @@ public class EnergyService {
|
|
|
energyLog.setMemberId(memberId);
|
|
|
energyLog.setDimensionId(dim.getId());
|
|
|
energyLog.setAmount(energyAmount);
|
|
|
- energyLog.setSourceType("task");
|
|
|
- energyLog.setSourceId(taskId);
|
|
|
- energyLog.setDescription("任务奖励");
|
|
|
+ energyLog.setSourceType(sourceType);
|
|
|
+ energyLog.setSourceId(sourceId);
|
|
|
+ energyLog.setDescription(description);
|
|
|
energyLog.setCreatedAt(new Date());
|
|
|
|
|
|
+ if (workOrderKey != null) {
|
|
|
+ processedWorkOrders.putIfAbsent(workOrderKey, energyLog);
|
|
|
+ }
|
|
|
return energyLog;
|
|
|
}
|
|
|
|