CoachUserService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Services;
  3. use App\Models\CoachUser;
  4. use App\Enums\TransactionType;
  5. use App\Enums\TechnicianStatus;
  6. use App\Enums\TransactionStatus;
  7. use App\Models\WalletTransRecord;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Auth;
  11. use Slowlyo\OwlAdmin\Services\AdminService;
  12. /**
  13. * 技师
  14. *
  15. * @method CoachUser getModel()
  16. * @method CoachUser|\Illuminate\Database\Query\Builder query()
  17. */
  18. class CoachUserService extends AdminService
  19. {
  20. protected string $modelName = CoachUser::class;
  21. /**
  22. * 拉黑技师
  23. *
  24. * @param array $data 包含 coach_id 和 reason
  25. */
  26. public function blockCoach(array $data): array
  27. {
  28. try {
  29. DB::beginTransaction();
  30. // 获取技师信息
  31. $coach = CoachUser::findOrFail($data['coach_id']);
  32. // 验证技师当前状态
  33. abort_if($coach->state === TechnicianStatus::BLACKLIST->value, 422, '技师已经被拉黑');
  34. // 更新技师状态为拉黑
  35. $coach->state = TechnicianStatus::BLACKLIST->value;
  36. $coach->block_reason = $data['reason'];
  37. $coach->block_time = now();
  38. $coach->block_operator_id = Auth::user()->id;
  39. $coach->save();
  40. DB::commit();
  41. return [
  42. 'code' => 200,
  43. 'message' => '拉黑成功',
  44. 'data' => null,
  45. ];
  46. } catch (\Exception $e) {
  47. DB::rollBack();
  48. Log::error('拉黑技师失败', [
  49. 'coach_id' => $data['coach_id'],
  50. 'error' => $e->getMessage(),
  51. 'trace' => $e->getTraceAsString(),
  52. ]);
  53. throw $e;
  54. }
  55. }
  56. /**
  57. * 冻结技师余额
  58. *
  59. * @param array $data 包含 coach_id, amount 和 reason
  60. */
  61. public function freezeCoachBalance(array $data): array
  62. {
  63. try {
  64. DB::beginTransaction();
  65. // 获取技师及其钱包信息
  66. $coach = CoachUser::findOrFail($data['coach_id']);
  67. $wallet = $coach->wallet()->lockForUpdate()->firstOrFail();
  68. // 验证可用余额是否足够
  69. abort_if($wallet->available_amount < $data['amount'], 422, '技师可用余额不足');
  70. // 更新钱包余额
  71. $wallet->available_amount -= $data['amount'];
  72. $wallet->frozen_amount += $data['amount'];
  73. $wallet->save();
  74. // 记录冻结流水
  75. WalletTransRecord::create([
  76. 'user_id' => $data['coach_id'],
  77. 'type' => TransactionType::FREEZE->value,
  78. 'amount' => $data['amount'],
  79. 'before_amount' => $wallet->available_amount + $data['amount'],
  80. 'after_amount' => $wallet->available_amount,
  81. 'operator_id' => Auth::user()->id,
  82. 'operator_type' => 'admin',
  83. 'remark' => $data['reason'],
  84. 'state' => TransactionStatus::SUCCESS->value,
  85. ]);
  86. DB::commit();
  87. return [
  88. 'code' => 200,
  89. 'message' => '冻结成功',
  90. 'data' => null,
  91. ];
  92. } catch (\Exception $e) {
  93. DB::rollBack();
  94. Log::error('冻结技师余额失败', [
  95. 'coach_id' => $data['coach_id'],
  96. 'amount' => $data['amount'],
  97. 'error' => $e->getMessage(),
  98. 'trace' => $e->getTraceAsString(),
  99. ]);
  100. throw $e;
  101. }
  102. }
  103. }