findOrFail($data['shop_id']); // 验证店铺当前状态 abort_if($shop->state !== ShopStatus::PENDING->value, 422, '店铺状态不允许审核'); // 验证认证记录是否存在 abort_if(! $shop->authRecord, 422, '店铺认证信息不存在'); // 更新店铺状态 $shop->state = $data['action'] === 'approve' ? ShopStatus::ACTIVE->value : ShopStatus::REJECTED->value; $shop->save(); // 更新认证记录状态 $shop->authRecord->state = $data['action'] === 'approve' ? ShopStatus::ACTIVE->value : ShopStatus::REJECTED->value; $shop->authRecord->audit_remark = $data['reason']; $shop->authRecord->audit_time = now(); $shop->authRecord->auditor = Auth::user()->id; $shop->authRecord->save(); DB::commit(); return [ 'code' => 200, 'message' => '审核成功', 'data' => null, ]; } catch (\Exception $e) { DB::rollBack(); Log::error('审核店铺失败', [ 'shop_id' => $data['shop_id'], 'action' => $data['action'], 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); throw $e; } } /** * 拉黑店铺 * * @param array $data 包含 shop_id 和 reason */ public function blockShop(array $data): array { try { DB::beginTransaction(); // 获取店铺信息 $shop = ShopInfo::findOrFail($data['shop_id']); // 验证店铺当前状态 abort_if($shop->state === ShopStatus::CLOSED->value, 422, '店铺已经被拉黑'); // 更新店铺状态为拉黑 $shop->state = ShopStatus::CLOSED->value; $shop->save(); DB::commit(); return [ 'code' => 200, 'message' => '拉黑成功', 'data' => null, ]; } catch (\Exception $e) { DB::rollBack(); Log::error('拉黑店铺失败', [ 'shop_id' => $data['shop_id'], 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); throw $e; } } /** * 获取店铺审核记录 * * @param array $data 包含 shop_id, page 和 perPage */ public function getShopReviewRecords(array $data): array { try { // 获取店铺认证记录 $query = ShopAuthRecord::where('shop_id', $data['shop_id']) ->with('auditorUser:id,name') ->orderBy('created_at', 'desc'); // 分页 $perPage = $data['perPage'] ?? 20; $records = $query->paginate($perPage, ['*'], 'page', $data['page'] ?? 1); // 转换数据 $records->through(function ($record) { // 添加审核人名称 $record->auditor_name = $record->auditorUser?->name ?? ''; unset($record->auditorUser); // 翻译状态 $record->state_text = ShopStatus::fromValue($record->state)?->label() ?? '未知状态'; return $record; }); return [ 'code' => 200, 'message' => 'success', 'data' => $records, ]; } catch (\Exception $e) { Log::error('获取店铺审核记录失败', [ 'shop_id' => $data['shop_id'], 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); throw $e; } } /** * 冻结店铺余额 * * @param array $data 包含 shop_id, amount 和 reason */ public function freezeShopBalance(array $data): array { try { DB::beginTransaction(); // 获取店铺及其钱包信息 $shop = ShopInfo::findOrFail($data['shop_id']); $wallet = $shop->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([ 'wallet_id' => $wallet->id, 'owner_id' => $data['shop_id'], 'owner_type' => ShopInfo::class, 'trans_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('冻结店铺余额失败', [ 'shop_id' => $data['shop_id'], 'amount' => $data['amount'], 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); throw $e; } } }