123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace App\Services;
- use App\Enums\ShopStatus;
- use App\Enums\TransactionStatus;
- use App\Enums\TransactionType;
- use App\Models\ShopAuthRecord;
- use App\Models\ShopInfo;
- use App\Models\WalletTransRecord;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Slowlyo\OwlAdmin\Services\AdminService;
- /**
- * 店铺
- *
- * @method ShopInfo getModel()
- * @method ShopInfo|\Illuminate\Database\Query\Builder query()
- */
- class ShopInfoService extends AdminService
- {
- protected string $modelName = ShopInfo::class;
- /**
- * 审核店铺
- *
- * @param array $data 包含 shop_id, action 和 reason
- */
- public function reviewShop(array $data): array
- {
- try {
- DB::beginTransaction();
- // 获取店铺信息
- $shop = ShopInfo::with('authRecord')->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;
- }
- }
- }
|