package com.webmanage.controller; import com.webmanage.common.PageResult; import com.webmanage.common.Result; import com.webmanage.dto.*; import com.webmanage.entity.Points; import com.webmanage.entity.PointsRule; import com.webmanage.entity.UserPoints; import com.webmanage.service.PointsFlowService; import com.webmanage.service.PointsRuleService; import com.webmanage.service.PointsService; import com.webmanage.vo.PointsRuleResultVO; import com.webmanage.vo.PointsRuleVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; import javax.validation.constraints.NotNull; import java.util.List; import com.webmanage.entity.PointsFlow; /** * 积分管理Controller */ @Slf4j @RestController @RequestMapping("/api/points") @Api(tags = "积分管理") @Validated public class PointsController { @Resource private PointsRuleService pointsRuleService; @Resource private PointsFlowService pointsFlowService; @Resource private PointsService pointsService; // ==================== 积分主表管理 ==================== @PostMapping("/main/page") @ApiOperation("分页查询积分主表") public Result> getPointsMainPage(@Valid @RequestBody PointsMainQueryDTO queryDTO) { try { return Result.success(pointsService.getPointsMainPage(queryDTO)); } catch (Exception e) { log.error("查询积分主表失败", e); return Result.error("查询积分主表失败:" + e.getMessage()); } } // ==================== 积分规则管理 ==================== @PostMapping("/rule/list") @ApiOperation("根据规则主表ID查询积分规则") public Result getPointsRuleListById( @RequestParam(required = true) @NotNull Long ruleId) { try { return Result.success(pointsRuleService.getPointsRuleList(ruleId)); } catch (Exception e) { log.error("查询积分规则失败", e); return Result.error("查询积分规则失败:" + e.getMessage()); } } @PostMapping("/rule/add") @ApiOperation("新增积分规则") public Result addPointsRule(@Valid @RequestBody PointsRuleDTO pointsRuleDTO) { try { boolean result = pointsRuleService.addPointsRule(pointsRuleDTO); if (result) { return Result.success("新增积分规则成功"); } else { return Result.error("新增积分规则失败"); } } catch (Exception e) { log.error("新增积分规则失败", e); return Result.error("新增积分规则失败:" + e.getMessage()); } } @PostMapping("/rule/update") @ApiOperation("修改积分规则") public Result updatePointsRule(@Valid @RequestBody List pointsRuleDTO) { try { boolean result = pointsRuleService.updatePointsRule(pointsRuleDTO); if (result) { return Result.success("修改积分规则成功"); } else { return Result.error("修改积分规则失败"); } } catch (Exception e) { log.error("修改积分规则失败", e); return Result.error("修改积分规则失败:" + e.getMessage()); } } @DeleteMapping("/rule/{id}") @ApiOperation("删除积分规则") public Result deletePointsRule(@ApiParam("规则ID") @PathVariable Long id) { try { boolean result = pointsRuleService.deletePointsRule(id); if (result) { return Result.success("删除积分规则成功"); } else { return Result.error("删除积分规则失败"); } } catch (Exception e) { log.error("删除积分规则失败", e); return Result.error("删除积分规则失败:" + e.getMessage()); } } @PostMapping("/rule/toggle/{id}") @ApiOperation("启用/禁用积分规则") public Result togglePointsRuleStatus(@ApiParam("规则ID") @PathVariable Long id, @ApiParam("是否启用") @RequestParam @NotNull Boolean isEnabled) { try { boolean result = pointsRuleService.togglePointsRuleStatus(id, isEnabled); if (result) { return Result.success("操作成功"); } else { return Result.error("操作失败"); } } catch (Exception e) { log.error("操作积分规则状态失败", e); return Result.error("操作积分规则状态失败:" + e.getMessage()); } } @GetMapping("/rule/type/{ruleType}") @ApiOperation("根据规则类型查询启用的规则") public Result getEnabledRulesByType(@ApiParam("规则类型") @PathVariable String ruleType) { try { List rules = pointsRuleService.getEnabledRulesByType(ruleType); return Result.success(rules); } catch (Exception e) { log.error("查询积分规则失败", e); return Result.error("查询积分规则失败:" + e.getMessage()); } } // ==================== 积分流水查询 ==================== @PostMapping("/flow/personal/page") @ApiOperation("分页查询个人积分流水") public Result getPersonalPointsFlowPage(@Valid @RequestBody PointsFlowQueryDTO queryDTO) { try { return Result.success(pointsFlowService.getPersonalPointsFlowPage(queryDTO)); } catch (Exception e) { log.error("查询个人积分流水失败", e); return Result.error("查询个人积分流水失败:" + e.getMessage()); } } @PostMapping("/flow/unit/page") @ApiOperation("分页查询单位积分流水") public Result getUnitPointsFlowPage(@Valid @RequestBody PointsFlowQueryDTO queryDTO) { try { return Result.success(pointsFlowService.getUnitPointsFlowPage(queryDTO)); } catch (Exception e) { log.error("查询单位积分流水失败", e); return Result.error("查询单位积分流水失败:" + e.getMessage()); } } @GetMapping("/flow/user/{userId}") @ApiOperation("根据用户ID查询积分流水") public Result getPointsFlowByUserId(@ApiParam("用户ID") @PathVariable Long userId) { try { List flows = pointsFlowService.getPointsFlowByUserId(userId); return Result.success(flows); } catch (Exception e) { log.error("查询用户积分流水失败", e); return Result.error("查询用户积分流水失败:" + e.getMessage()); } } @GetMapping("/flow/unit/{unitId}") @ApiOperation("根据单位ID查询积分流水") public Result getPointsFlowByUnitId(@ApiParam("单位ID") @PathVariable Long unitId) { try { List flows = pointsFlowService.getPointsFlowByUnitId(unitId); return Result.success(flows); } catch (Exception e) { log.error("查询单位积分流水失败", e); return Result.error("查询单位积分流水失败:" + e.getMessage()); } } // ==================== 新增积分流水 ==================== @PostMapping("/flow/add") @ApiOperation("新增积分流水(根据规则自动计算)") public Result addPointsFlow(@Valid @RequestBody AddPointsFlowDTO addPointsFlowDTO) { try { boolean result = pointsFlowService.addPointsFlowByRule(addPointsFlowDTO); if (result) { return Result.success("新增积分流水成功"); } else { return Result.error("新增积分流水失败"); } } catch (Exception e) { log.error("新增积分流水失败", e); return Result.error("新增积分流水失败:" + e.getMessage()); } } @PostMapping("/flow/add/personal") @ApiOperation("新增个人积分流水") public Result addPersonalPointsFlow(@Valid @RequestBody AddPointsFlowDTO addPointsFlowDTO) { try { boolean result = pointsFlowService.addPointsFlowByRule(addPointsFlowDTO); if (result) { return Result.success("新增个人积分流水成功"); } else { return Result.error("新增个人积分流水失败"); } } catch (Exception e) { log.error("新增个人积分流水失败", e); return Result.error("新增个人积分流水失败:" + e.getMessage()); } } @PostMapping("/flow/add/unit") @ApiOperation("新增单位积分流水") public Result addUnitPointsFlow(@Valid @RequestBody AddPointsFlowDTO addPointsFlowDTO) { try { boolean result = pointsFlowService.addPointsFlowByRule(addPointsFlowDTO); if (result) { return Result.success("新增单位积分流水成功"); } else { return Result.error("新增单位积分流水失败"); } } catch (Exception e) { log.error("新增单位积分流水失败", e); return Result.error("新增单位积分流水失败:" + e.getMessage()); } } // ==================== 积分统计 ==================== @GetMapping("/total/user/{userId}") @ApiOperation("获取用户积分统计") public Result getUserPointsTotal(@ApiParam("用户ID") @PathVariable String userId) { try { UserPoints userPoints = pointsFlowService.getUserPointsTotal(userId); return Result.success(userPoints); } catch (Exception e) { log.error("获取用户积分统计失败", e); return Result.error("获取用户积分统计失败:" + e.getMessage()); } } @GetMapping("/total/unit/{unitId}") @ApiOperation("获取单位积分统计") public Result getUnitPointsTotal(@ApiParam("单位ID") @PathVariable Long unitId) { try { UserPoints total = pointsFlowService.getUnitPointsTotal(unitId); return Result.success(total); } catch (Exception e) { log.error("获取单位积分统计失败", e); return Result.error("获取单位积分统计失败:" + e.getMessage()); } } // ==================== 积分扣减 ==================== @PostMapping("/user/deduct") @ApiOperation("扣减用户积分") public Result deductUserPoints(@Valid @RequestBody DeductUserPointsDTO deductDTO) { try { boolean result = pointsFlowService.deductUserPoints(deductDTO); if (result) { return Result.success("积分扣减成功"); } else { return Result.error("积分扣减失败"); } } catch (Exception e) { log.error("积分扣减失败", e); return Result.error("积分扣减失败:" + e.getMessage()); } } // ==================== 积分流水数据类目 ==================== @GetMapping("/flow/categories") @ApiOperation("获取积分流水数据类目列表") public Result getPointsFlowCategories() { try { List categories = pointsFlowService.getPointsFlowCategories(); return Result.success(categories); } catch (Exception e) { log.error("获取积分流水数据类目失败", e); return Result.error("获取积分流水数据类目失败:" + e.getMessage()); } } }