更新数据库结构

This commit is contained in:
技术老胡
2026-03-10 13:47:28 +08:00
parent 54ad21ac8f
commit 9de902231f
54 changed files with 5070 additions and 501 deletions

View File

@@ -0,0 +1,31 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\Admin;
/**
* 管理员仓储
*/
class AdminRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new Admin());
}
/**
* 根据用户名查询
*/
public function findByUserName(string $userName): ?Admin
{
/** @var Admin|null $admin */
$admin = $this->model
->newQuery()
->where('user_name', $userName)
->first();
return $admin;
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\MerchantApp;
/**
* 商户应用仓储
*/
class MerchantAppRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new MerchantApp());
}
/**
* 根据AppId查询
*/
public function findByAppId(string $appId): ?MerchantApp
{
return $this->model->newQuery()
->where('app_id', $appId)
->where('status', 1)
->first();
}
/**
* 根据商户ID和应用ID查询
*/
public function findByMerchantAndApp(int $merchantId, int $appId): ?MerchantApp
{
return $this->model->newQuery()
->where('merchant_id', $merchantId)
->where('id', $appId)
->where('status', 1)
->first();
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\Merchant;
/**
* 商户仓储
*/
class MerchantRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new Merchant());
}
/**
* 根据商户号查询
*/
public function findByMerchantNo(string $merchantNo): ?Merchant
{
return $this->model->newQuery()
->where('merchant_no', $merchantNo)
->first();
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\PaymentCallbackLog;
/**
* 支付回调日志仓储
*/
class PaymentCallbackLogRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new PaymentCallbackLog());
}
public function createLog(array $data): PaymentCallbackLog
{
return $this->model->newQuery()->create($data);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\PaymentChannel;
/**
* 支付通道仓储
*/
class PaymentChannelRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new PaymentChannel());
}
/**
* 根据商户、应用、支付方式查找可用通道
*/
public function findAvailableChannel(int $merchantId, int $merchantAppId, int $methodId): ?PaymentChannel
{
return $this->model->newQuery()
->where('merchant_id', $merchantId)
->where('merchant_app_id', $merchantAppId)
->where('method_id', $methodId)
->where('status', 1)
->orderBy('sort', 'asc')
->first();
}
public function findByChanCode(string $chanCode): ?PaymentChannel
{
return $this->model->newQuery()
->where('chan_code', $chanCode)
->first();
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\PaymentMethod;
/**
* 支付方式仓储
*/
class PaymentMethodRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new PaymentMethod());
}
public function getAllEnabled(): array
{
return $this->model->newQuery()
->where('status', 1)
->orderBy('sort', 'asc')
->get()
->toArray();
}
public function findByCode(string $methodCode): ?PaymentMethod
{
return $this->model->newQuery()
->where('method_code', $methodCode)
->where('status', 1)
->first();
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\PaymentNotifyTask;
/**
* 商户通知任务仓储
*/
class PaymentNotifyTaskRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new PaymentNotifyTask());
}
public function findByOrderId(string $orderId): ?PaymentNotifyTask
{
return $this->model->newQuery()
->where('order_id', $orderId)
->first();
}
public function getPendingRetryTasks(int $limit = 100): array
{
return $this->model->newQuery()
->where('status', PaymentNotifyTask::STATUS_PENDING)
->where('next_retry_at', '<=', date('Y-m-d H:i:s'))
->limit($limit)
->get()
->toArray();
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\PaymentOrder;
/**
* 支付订单仓储
*/
class PaymentOrderRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new PaymentOrder());
}
public function findByOrderId(string $orderId): ?PaymentOrder
{
return $this->model->newQuery()
->where('order_id', $orderId)
->first();
}
/**
* 根据商户订单号查询(幂等校验)
*/
public function findByMchNo(int $merchantId, int $merchantAppId, string $mchOrderNo): ?PaymentOrder
{
return $this->model->newQuery()
->where('merchant_id', $merchantId)
->where('merchant_app_id', $merchantAppId)
->where('mch_order_no', $mchOrderNo)
->first();
}
public function updateStatus(string $orderId, int $status, array $extra = []): bool
{
$data = array_merge(['status' => $status], $extra);
$order = $this->findByOrderId($orderId);
return $order ? $this->updateById($order->id, $data) : false;
}
public function updateChannelInfo(string $orderId, string $chanOrderNo, string $chanTradeNo = ''): bool
{
$order = $this->findByOrderId($orderId);
if (!$order) {
return false;
}
$data = ['chan_order_no' => $chanOrderNo];
if ($chanTradeNo !== '') {
$data['chan_trade_no'] = $chanTradeNo;
}
return $this->updateById($order->id, $data);
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\PaymentPlugin;
/**
* 支付插件仓储
*/
class PaymentPluginRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new PaymentPlugin());
}
public function getActivePlugins()
{
return $this->model->newQuery()
->where('status', 1)
->get(['plugin_code', 'class_name']);
}
public function findActiveByCode(string $pluginCode): ?PaymentPlugin
{
return $this->model->newQuery()
->where('plugin_code', $pluginCode)
->where('status', 1)
->first();
}
}

View File

@@ -1,47 +0,0 @@
<?php
namespace app\repositories;
use app\common\base\BaseRepository;
use app\models\User;
/**
* 用户仓储
*/
class UserRepository extends BaseRepository
{
public function __construct()
{
parent::__construct(new User());
}
/**
* 根据用户名查询用户
*/
public function findByUserName(string $userName): ?User
{
/** @var User|null $user */
$user = $this->model
->newQuery()
->where('user_name', $userName)
->first();
return $user;
}
/**
* 根据主键查询并预加载角色
*/
public function findWithRoles(int $id): ?User
{
/** @var User|null $user */
$user = $this->model
->newQuery()
->with('roles')
->find($id);
return $user;
}
}