| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace App\Module\UrsPromotion\AdminControllers\Actions;
- use App\Module\UrsPromotion\Models\UrsProfit;
- use App\Module\UrsPromotion\Services\UrsProfitService;
- use Illuminate\Http\Request;
- use UCore\DcatAdmin\RowActionHandler;
- /**
- * 重新计算收益操作
- *
- * 重新计算指定收益记录的分成金额
- */
- class RecalculateProfitAction extends RowActionHandler
- {
- /**
- * 操作按钮标题
- *
- * @var string
- */
- public $title = '重新计算';
- /**
- * 检查是否允许显示此操作
- *
- * @return bool
- */
- public function allowed()
- {
- $row = $this->getRow();
- // 只有正常状态的收益记录才允许重新计算
- return $row->status === UrsProfit::STATUS_NORMAL;
- }
- /**
- * 处理请求
- *
- * @param Request $request
- * @return mixed
- */
- public function handle(Request $request)
- {
- $id = $this->getKey();
- $profit = UrsProfit::find($id);
-
- if (!$profit) {
- return $this->response()->error('收益记录不存在');
- }
- if ($profit->status !== UrsProfit::STATUS_NORMAL) {
- return $this->response()->error('只能重新计算正常状态的收益记录');
- }
- try {
- // 保存原始数据
- $originalAmount = $profit->profit_amount;
- $originalRate = $profit->profit_rate;
-
- // 重新计算收益
- $result = UrsProfitService::recalculateProfit($profit);
-
- if ($result) {
- // 重新获取更新后的记录
- $profit->refresh();
-
- $message = "收益重新计算成功!";
- if ($originalAmount != $profit->profit_amount) {
- $message .= " 分成金额从 {$originalAmount} 调整为 {$profit->profit_amount}";
- if ($originalRate != $profit->profit_rate) {
- $message .= ",分成比例从 " . ($originalRate * 100) . "% 调整为 " . ($profit->profit_rate * 100) . "%";
- }
- } else {
- $message .= " 金额无变化";
- }
-
- return $this->response()
- ->success($message)
- ->refresh();
- } else {
- return $this->response()->error('收益重新计算失败');
- }
- } catch (\Exception $e) {
- return $this->response()->error('重新计算失败:' . $e->getMessage());
- }
- }
- /**
- * 确认对话框
- *
- * @return string
- */
- public function confirm()
- {
- return '确定要重新计算此收益记录吗?这将根据当前的达人等级和分成配置重新计算分成金额。';
- }
- }
|