CoachUserService.php 3.5 KB

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