You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.0 KiB
131 lines
3.0 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\AdminVipList;
|
|
use Exception;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AdminVipListService
|
|
{
|
|
|
|
|
|
/**
|
|
* @var OperationLogService
|
|
*/
|
|
private OperationLogService $logService;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param OperationLogService $logService
|
|
*/
|
|
public function __construct(OperationLogService $logService)
|
|
{
|
|
$this->logService = $logService;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return Model|Builder
|
|
* @throws Exception
|
|
*/
|
|
public function createModel(array $data): Model|Builder
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if (AdminVipList::query()->where('license', $data['license'])
|
|
->exists()
|
|
) {
|
|
throw new Exception(
|
|
__('service.admin_vip_list.license_exists')
|
|
);
|
|
}
|
|
|
|
$model = AdminVipList::query()->create([
|
|
'license' => $data['license'],
|
|
'user_id' => Auth::guard('sanctum')->user()['id'],
|
|
'created_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logCreated($model, '创建VIP名单');
|
|
|
|
DB::commit();
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @param array $data
|
|
* @param int $id
|
|
* @return Model|Builder
|
|
* @throws Exception
|
|
*/
|
|
public function updateModel(array $data, int $id): Model|Builder
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// 验证
|
|
$existsWhere = [
|
|
['license', '=', $data['license']],
|
|
['id', '<>', $id]
|
|
];
|
|
if (AdminVipList::query()->where($existsWhere)->exists()) {
|
|
throw new Exception(__('service.admin_vip_list.license_exists'));
|
|
}
|
|
|
|
// 更新
|
|
$model = AdminVipList::query()->findOrFail($id);
|
|
$oldValues = $model->toArray();
|
|
|
|
$model->update([
|
|
'license' => $data['license'],
|
|
'user_id' => Auth::guard('sanctum')->user()['id'],
|
|
'updated_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logUpdated($model, $oldValues, '更新VIP名单');
|
|
|
|
DB::commit();
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return bool
|
|
* @throws Exception
|
|
*/
|
|
public function deleteModel($id): bool
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$model = AdminVipList::query()->findOrFail($id);
|
|
|
|
$this->logService->logDeleted($model, '删除VIP名单');
|
|
|
|
$model->delete();
|
|
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
}
|
|
|