|
@@ -577,6 +577,9 @@ public class EnergyService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private EnergyBalanceMapper energyBalanceMapper;
|
|
private EnergyBalanceMapper energyBalanceMapper;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private WishMapper wishMapper;
|
|
|
|
|
+
|
|
|
// 维度缓存(懒加载)
|
|
// 维度缓存(懒加载)
|
|
|
private List<EnergyDimension> dimCache;
|
|
private List<EnergyDimension> dimCache;
|
|
|
private Map<String, EnergyDimension> dimCodeMap;
|
|
private Map<String, EnergyDimension> dimCodeMap;
|
|
@@ -749,6 +752,10 @@ public class EnergyService {
|
|
|
return energyLogMapper.selectPage(pageParam, wrapper);
|
|
return energyLogMapper.selectPage(pageParam, wrapper);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 配置来源比例
|
|
* 配置来源比例
|
|
|
*/
|
|
*/
|
|
@@ -777,6 +784,70 @@ public class EnergyService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发放能量 - 任务奖励
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param childId 孩子ID
|
|
|
|
|
+ * @param taskId 任务ID
|
|
|
|
|
+ * @param amount 能量数量
|
|
|
|
|
+ * @param dimensionCode 维度代码
|
|
|
|
|
+ * @return EnergyLog 能量日志
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public EnergyLog grantEnergy(Long childId, Long taskId, Long amount, String dimensionCode) {
|
|
|
|
|
+ // 调用 awardEnergy 发放能量
|
|
|
|
|
+ Map<String, Integer> balanceMap = awardEnergy(childId, "task", taskId, amount.intValue(), "任务奖励", null);
|
|
|
|
|
+
|
|
|
|
|
+ if (balanceMap == null || balanceMap.isEmpty()) {
|
|
|
|
|
+ throw new RuntimeException("能量发放失败:没有分配到任何维度能量");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取维度ID
|
|
|
|
|
+ EnergyDimension dim = getDimByCode(dimensionCode);
|
|
|
|
|
+ if (dim == null) {
|
|
|
|
|
+ throw new RuntimeException("维度代码无效:" + dimensionCode);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取分配到的能量数量
|
|
|
|
|
+ Integer energyAmount = balanceMap.get(dimensionCode);
|
|
|
|
|
+ if (energyAmount == null || energyAmount <= 0) {
|
|
|
|
|
+ throw new RuntimeException("能量发放失败:维度 " + dimensionCode + " 没有分配能量");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 构建 EnergyLog 对象
|
|
|
|
|
+ EnergyLog energyLog = new EnergyLog();
|
|
|
|
|
+ energyLog.setChildId(childId);
|
|
|
|
|
+ energyLog.setDimensionId(dim.getId());
|
|
|
|
|
+ energyLog.setAmount(energyAmount);
|
|
|
|
|
+ energyLog.setBalanceAfter(energyAmount); // 初始余额
|
|
|
|
|
+ energyLog.setSourceType("task");
|
|
|
|
|
+ energyLog.setSourceId(taskId);
|
|
|
|
|
+ energyLog.setDescription("任务奖励");
|
|
|
|
|
+ energyLog.setCreatedAt(new Date());
|
|
|
|
|
+
|
|
|
|
|
+ // 保存到数据库
|
|
|
|
|
+ energyLogMapper.insert(energyLog);
|
|
|
|
|
+
|
|
|
|
|
+ return energyLog;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 扣除能量 - 用于心愿兑换
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param childId 孩子ID
|
|
|
|
|
+ * @param wishId 心愿ID
|
|
|
|
|
+ * @param amount 扣除能量数量
|
|
|
|
|
+ * @return Boolean 是否成功
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public Boolean deductForWish(Long childId, Long wishId, Long amount) {
|
|
|
|
|
+ // 使用 deductEnergyByCode 扣除能量,维度为 "action"
|
|
|
|
|
+ int result = deductEnergyByCode(childId, "action", amount.intValue(), "心愿兑换");
|
|
|
|
|
+
|
|
|
|
|
+ // 返回 true 表示成功,false 表示余额不足
|
|
|
|
|
+ return result != -1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// ==================== 辅助方法 ====================
|
|
// ==================== 辅助方法 ====================
|
|
|
|
|
|
|
|
/**
|
|
/**
|