RecalculateProfitAction.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Module\UrsPromotion\AdminControllers\Actions;
  3. use App\Module\UrsPromotion\Models\UrsProfit;
  4. use App\Module\UrsPromotion\Services\UrsProfitService;
  5. use Illuminate\Http\Request;
  6. use UCore\DcatAdmin\RowActionHandler;
  7. /**
  8. * 重新计算收益操作
  9. *
  10. * 重新计算指定收益记录的分成金额
  11. */
  12. class RecalculateProfitAction extends RowActionHandler
  13. {
  14. /**
  15. * 操作按钮标题
  16. *
  17. * @var string
  18. */
  19. public $title = '重新计算';
  20. /**
  21. * 检查是否允许显示此操作
  22. *
  23. * @return bool
  24. */
  25. public function allowed()
  26. {
  27. $row = $this->getRow();
  28. // 只有正常状态的收益记录才允许重新计算
  29. return $row->status === UrsProfit::STATUS_NORMAL;
  30. }
  31. /**
  32. * 处理请求
  33. *
  34. * @param Request $request
  35. * @return mixed
  36. */
  37. public function handle(Request $request)
  38. {
  39. $id = $this->getKey();
  40. $profit = UrsProfit::find($id);
  41. if (!$profit) {
  42. return $this->response()->error('收益记录不存在');
  43. }
  44. if ($profit->status !== UrsProfit::STATUS_NORMAL) {
  45. return $this->response()->error('只能重新计算正常状态的收益记录');
  46. }
  47. try {
  48. // 保存原始数据
  49. $originalAmount = $profit->profit_amount;
  50. $originalRate = $profit->profit_rate;
  51. // 重新计算收益
  52. $result = UrsProfitService::recalculateProfit($profit);
  53. if ($result) {
  54. // 重新获取更新后的记录
  55. $profit->refresh();
  56. $message = "收益重新计算成功!";
  57. if ($originalAmount != $profit->profit_amount) {
  58. $message .= " 分成金额从 {$originalAmount} 调整为 {$profit->profit_amount}";
  59. if ($originalRate != $profit->profit_rate) {
  60. $message .= ",分成比例从 " . ($originalRate * 100) . "% 调整为 " . ($profit->profit_rate * 100) . "%";
  61. }
  62. } else {
  63. $message .= " 金额无变化";
  64. }
  65. return $this->response()
  66. ->success($message)
  67. ->refresh();
  68. } else {
  69. return $this->response()->error('收益重新计算失败');
  70. }
  71. } catch (\Exception $e) {
  72. return $this->response()->error('重新计算失败:' . $e->getMessage());
  73. }
  74. }
  75. /**
  76. * 确认对话框
  77. *
  78. * @return string
  79. */
  80. public function confirm()
  81. {
  82. return '确定要重新计算此收益记录吗?这将根据当前的达人等级和分成配置重新计算分成金额。';
  83. }
  84. }