123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace App\Services;
- use App\Models\CoachUser;
- use App\Enums\TransactionType;
- use App\Enums\TechnicianStatus;
- use App\Enums\TransactionStatus;
- use App\Models\WalletTransRecord;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Auth;
- use Slowlyo\OwlAdmin\Services\AdminService;
- /**
- * 技师
- *
- * @method CoachUser getModel()
- * @method CoachUser|\Illuminate\Database\Query\Builder query()
- */
- class CoachUserService extends AdminService
- {
- protected string $modelName = CoachUser::class;
- /**
- * 拉黑技师
- *
- * @param array $data 包含 coach_id 和 reason
- */
- public function blockCoach(array $data): array
- {
- try {
- DB::beginTransaction();
- // 获取技师信息
- $coach = CoachUser::findOrFail($data['coach_id']);
- // 验证技师当前状态
- abort_if($coach->state === TechnicianStatus::BLACKLIST->value, 422, '技师已经被拉黑');
- // 更新技师状态为拉黑
- $coach->state = TechnicianStatus::BLACKLIST->value;
- $coach->block_reason = $data['reason'];
- $coach->block_time = now();
- $coach->block_operator_id = Auth::user()->id;
- $coach->save();
- DB::commit();
- return [
- 'code' => 200,
- 'message' => '拉黑成功',
- 'data' => null,
- ];
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('拉黑技师失败', [
- 'coach_id' => $data['coach_id'],
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
- throw $e;
- }
- }
- /**
- * 冻结技师余额
- *
- * @param array $data 包含 coach_id, amount 和 reason
- */
- public function freezeCoachBalance(array $data): array
- {
- try {
- DB::beginTransaction();
- // 获取技师及其钱包信息
- $coach = CoachUser::findOrFail($data['coach_id']);
- $wallet = $coach->wallet()->lockForUpdate()->firstOrFail();
- // 验证可用余额是否足够
- abort_if($wallet->available_amount < $data['amount'], 422, '技师可用余额不足');
- // 更新钱包余额
- $wallet->available_amount -= $data['amount'];
- $wallet->frozen_amount += $data['amount'];
- $wallet->save();
- // 记录冻结流水
- WalletTransRecord::create([
- 'user_id' => $data['coach_id'],
- 'type' => TransactionType::FREEZE->value,
- 'amount' => $data['amount'],
- 'before_amount' => $wallet->available_amount + $data['amount'],
- 'after_amount' => $wallet->available_amount,
- 'operator_id' => Auth::user()->id,
- 'operator_type' => 'admin',
- 'remark' => $data['reason'],
- 'state' => TransactionStatus::SUCCESS->value,
- ]);
- DB::commit();
- return [
- 'code' => 200,
- 'message' => '冻结成功',
- 'data' => null,
- ];
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('冻结技师余额失败', [
- 'coach_id' => $data['coach_id'],
- 'amount' => $data['amount'],
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString(),
- ]);
- throw $e;
- }
- }
- }
|