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.
141 lines
3.6 KiB
141 lines
3.6 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\AdminChannelRole;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AdminChannelRoleService extends BaseService
|
|
{
|
|
|
|
protected string $menuTitle = 'channel_permissions';
|
|
|
|
private static array $statusArr = ['disabled', 'enable'];
|
|
|
|
/**
|
|
* @return array|string[]
|
|
*/
|
|
public static function getStatus(): array
|
|
{
|
|
$statusArr = self::$statusArr;
|
|
foreach ($statusArr as $key => $value) {
|
|
$statusArr[$key] = __('admin.' . $value);
|
|
}
|
|
return $statusArr;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @throws Exception
|
|
*/
|
|
public function createModel(array $data)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$existsWhere = [
|
|
['member_type', '=', $data['member_type']]
|
|
];
|
|
if (AdminChannelRole::query()->where($existsWhere)->exists()
|
|
) {
|
|
throw new Exception(
|
|
__('service.' . $this->menuTitle . '.type_exists')
|
|
);
|
|
}
|
|
|
|
$channel_ids = implode(',', $data['channel_ids']);
|
|
$model = AdminChannelRole::query()->create([
|
|
'member_type' => $data['member_type'],
|
|
'status' => $data['status'] ?? 1,
|
|
'describe' => $data['describe'] ?? '',
|
|
'channel_ids' => $channel_ids ?? '',
|
|
'created_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logCreated(
|
|
$model,
|
|
$this->menuTitle . '.create'
|
|
);
|
|
|
|
DB::commit();
|
|
return $model;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @param int $id
|
|
* @throws Exception
|
|
*/
|
|
public function updateModel(array $data, int $id)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// 验证
|
|
$existsWhere = [
|
|
['member_type', '=', $data['member_type']],
|
|
['id', '<>', $id]
|
|
];
|
|
if (AdminChannelRole::query()->where($existsWhere)->exists()
|
|
) {
|
|
throw new Exception(
|
|
__('service.' . $this->menuTitle . '.reason_exists')
|
|
);
|
|
}
|
|
|
|
// 更新
|
|
$model = AdminChannelRole::query()->findOrFail($id);
|
|
$oldValues = $model->toArray();
|
|
|
|
$channel_ids = implode(',', $data['channel_ids']);
|
|
$model->update([
|
|
'member_type' => $data['member_type'],
|
|
'status' => $data['status'] ?? 1,
|
|
'describe' => $data['describe'] ?? '',
|
|
'channel_ids' => $channel_ids ?? '',
|
|
'updated_at' => get_datetime()
|
|
]);
|
|
|
|
$this->logService->logUpdated(
|
|
$model,
|
|
$oldValues,
|
|
$this->menuTitle . '.update'
|
|
);
|
|
|
|
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 = AdminChannelRole::query()->findOrFail($id);
|
|
|
|
$this->logService->logDeleted($model, $this->menuTitle . '.delete');
|
|
|
|
$model->delete();
|
|
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|
|
|