Просмотр исходного кода

feat(energy): 新增grantEnergy和deductForWish接口

- EnergyController: 新增/api/energy/grant和/api/energy/deduct-for-wish端点
- EnergyService: 新增grantEnergy()(Long,Long,Long,String)和deductForWish(Long,Long,Long)方法
- 添加WishMapper依赖用于心愿兑换能量扣除

Ultraworked with Sisyphus

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Xiaogang Liao 2 месяцев назад
Родитель
Сommit
3e44f18569

+ 45 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/EnergyController.java

@@ -87,6 +87,51 @@ public class EnergyController {
         return Result.success(data);
         return Result.success(data);
     }
     }
 
 
+    /**
+     * 发放能量(任务完成)
+     */
+    @Operation(summary = "发放能量")
+    @PostMapping("/grant")
+    public Result<EnergyLog> grantEnergy(
+            @RequestBody Map<String, Object> params) {
+        Long childId = params.get("childId") != null
+                ? Long.valueOf(params.get("childId").toString()) : null;
+        Long taskId = params.get("taskId") != null
+                ? Long.valueOf(params.get("taskId").toString()) : null;
+        Long amount = params.get("amount") != null
+                ? Long.valueOf(params.get("amount").toString()) : null;
+        String dimensionCode = (String) params.get("dimensionCode");
+
+        if (childId == null || taskId == null || amount == null) {
+            return Result.error("childId、taskId和amount不能为空");
+        }
+
+        EnergyLog energyLog = energyService.grantEnergy(childId, taskId, amount, dimensionCode);
+        return Result.success(energyLog);
+    }
+
+    /**
+     * 心愿兑换扣除能量
+     */
+    @Operation(summary = "心愿兑换扣除能量")
+    @PostMapping("/deduct-for-wish")
+    public Result<Boolean> deductForWish(
+            @RequestBody Map<String, Object> params) {
+        Long childId = params.get("childId") != null
+                ? Long.valueOf(params.get("childId").toString()) : null;
+        Long wishId = params.get("wishId") != null
+                ? Long.valueOf(params.get("wishId").toString()) : null;
+        Long amount = params.get("amount") != null
+                ? Long.valueOf(params.get("amount").toString()) : null;
+
+        if (childId == null || wishId == null || amount == null) {
+            return Result.error("childId、wishId和amount不能为空");
+        }
+
+        Boolean result = energyService.deductForWish(childId, wishId, amount);
+        return Result.success(result);
+    }
+
     /**
     /**
      * 配置能量来源比例(需登录)
      * 配置能量来源比例(需登录)
      */
      */

+ 71 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/EnergyService.java

@@ -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;
+    }
+
     // ==================== 辅助方法 ====================
     // ==================== 辅助方法 ====================
 
 
     /**
     /**