UpdateTalentAction.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Module\UrsPromotion\AdminControllers\Actions;
  3. use App\Module\UrsPromotion\Models\UrsUserTalent;
  4. use App\Module\UrsPromotion\Services\UrsTalentService;
  5. use App\Module\UrsPromotion\Enums\UrsTalentLevel;
  6. use Illuminate\Http\Request;
  7. use UCore\DcatAdmin\RowActionHandler;
  8. /**
  9. * 更新用户达人等级操作
  10. *
  11. * 提供单个用户达人等级的快速更新功能
  12. */
  13. class UpdateTalentAction extends RowActionHandler
  14. {
  15. /**
  16. * 操作按钮标题
  17. *
  18. * @var string
  19. */
  20. public $title = '更新等级';
  21. /**
  22. * 检查是否允许显示此操作
  23. *
  24. * @return bool
  25. */
  26. public function allowed()
  27. {
  28. // 所有记录都允许更新等级
  29. return true;
  30. }
  31. /**
  32. * 处理请求
  33. *
  34. * @param Request $request
  35. * @return mixed
  36. */
  37. public function handle(Request $request)
  38. {
  39. $id = $this->getKey();
  40. $talent = UrsUserTalent::find($id);
  41. if (!$talent) {
  42. return $this->response()->error('用户达人记录不存在');
  43. }
  44. try {
  45. // 获取更新前的等级
  46. $oldLevel = $talent->talent_level;
  47. // 更新达人等级
  48. $result = UrsTalentService::updateTalentLevel($talent->user_id);
  49. // 获取更新后的等级
  50. $newLevel = $result->talentLevel;
  51. $message = "达人等级更新成功!";
  52. if ($oldLevel !== $newLevel) {
  53. $oldLevelName = UrsTalentLevel::getLevelName($oldLevel);
  54. $newLevelName = UrsTalentLevel::getLevelName($newLevel);
  55. $message .= " 从 {$oldLevelName} 升级到 {$newLevelName}";
  56. } else {
  57. $newLevelName = UrsTalentLevel::getLevelName($newLevel);
  58. $message .= " 等级保持为 {$newLevelName}";
  59. }
  60. return $this->response()
  61. ->success($message)
  62. ->refresh();
  63. } catch (\Exception $e) {
  64. return $this->response()->error('更新失败:' . $e->getMessage());
  65. }
  66. }
  67. }