|
|
@@ -0,0 +1,241 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services\Manage;
|
|
|
+
|
|
|
+use App\Facade\Site;
|
|
|
+use App\Libs\Utils;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+class ArtStyleService
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 画风管理列表(分页)
|
|
|
+ *
|
|
|
+ * @param array $params
|
|
|
+ * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
|
|
+ */
|
|
|
+ public function getList(array $params)
|
|
|
+ {
|
|
|
+ $this->assertSuperadmin();
|
|
|
+
|
|
|
+ $perPage = (int)getProp($params, 'per_page', 15);
|
|
|
+ if ($perPage < 1) {
|
|
|
+ $perPage = 15;
|
|
|
+ }
|
|
|
+
|
|
|
+ $query = DB::table('mp_art_styles')->where('is_deleted', 0)->select('id', 'art_style', 'prompt', 'character_prefix', 'scene_prefix', 'pic_url', 'is_enabled', 'created_at');
|
|
|
+
|
|
|
+ // 是否启用筛选
|
|
|
+ if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
|
|
|
+ $query->where('is_enabled', (int)$params['is_enabled']);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 画风名称模糊筛选
|
|
|
+ $keyword = trim((string)getProp($params, 'art_style', ''));
|
|
|
+ if ($keyword !== '') {
|
|
|
+ $query->where('art_style', 'like', "%{$keyword}%");
|
|
|
+ }
|
|
|
+
|
|
|
+ return $query->orderByDesc('id')->paginate($perPage);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增画风
|
|
|
+ *
|
|
|
+ * @param array $params
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function addArtStyle(array $params): array
|
|
|
+ {
|
|
|
+ $this->assertSuperadmin();
|
|
|
+
|
|
|
+ $artStyle = trim((string)getProp($params, 'art_style', ''));
|
|
|
+ $picUrl = trim((string)getProp($params, 'pic_url', ''));
|
|
|
+ $isEnabled = (int)getProp($params, 'is_enabled', 1);
|
|
|
+
|
|
|
+ if ($artStyle === '') {
|
|
|
+ Utils::throwError('1002:请传入画风名称');
|
|
|
+ }
|
|
|
+ if ($picUrl === '') {
|
|
|
+ Utils::throwError('1002:请传入画风图片URL');
|
|
|
+ }
|
|
|
+ if (trim((string)getProp($params, 'prompt', '')) === '') {
|
|
|
+ Utils::throwError('1002:请传入画风提示词prompt');
|
|
|
+ }
|
|
|
+ if (DB::table('mp_art_styles')->where('art_style', $artStyle)->where('is_deleted', 0)->exists()) {
|
|
|
+ Utils::throwError('1002:画风名称已存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $id = DB::table('mp_art_styles')->insertGetId([
|
|
|
+ 'art_style' => $artStyle,
|
|
|
+ 'aliases' => $this->normalizeAliases($params, $artStyle),
|
|
|
+ 'prompt' => trim((string)getProp($params, 'prompt', '')),
|
|
|
+ 'character_prefix'=> (string)getProp($params, 'character_prefix', ''),
|
|
|
+ 'scene_prefix' => (string)getProp($params, 'scene_prefix', ''),
|
|
|
+ 'pic_url' => $picUrl,
|
|
|
+ 'is_enabled' => $isEnabled,
|
|
|
+ 'is_deleted' => 0,
|
|
|
+ 'created_at' => date('Y-m-d H:i:s'),
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s'),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $row = DB::table('mp_art_styles')->where('id', $id)->first();
|
|
|
+ $result = $this->buildStyleResult($row);
|
|
|
+ $result['id'] = $id;
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改画风
|
|
|
+ *
|
|
|
+ * @param array $params
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function editArtStyle(array $params): array
|
|
|
+ {
|
|
|
+ $this->assertSuperadmin();
|
|
|
+
|
|
|
+ $id = (int)getProp($params, 'id', 0);
|
|
|
+ if ($id < 1) {
|
|
|
+ Utils::throwError('1002:请传入画风ID');
|
|
|
+ }
|
|
|
+
|
|
|
+ $row = DB::table('mp_art_styles')->where('id', $id)->where('is_deleted', 0)->first();
|
|
|
+ if (!$row) {
|
|
|
+ Utils::throwError('20003:画风不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $update = [];
|
|
|
+
|
|
|
+ $artStyle = trim((string)getProp($params, 'art_style', ''));
|
|
|
+ if ($artStyle !== '') {
|
|
|
+ if (DB::table('mp_art_styles')->where('art_style', $artStyle)->where('id', '<>', $id)->where('is_deleted', 0)->exists()) {
|
|
|
+ Utils::throwError('1002:画风名称已存在');
|
|
|
+ }
|
|
|
+ $update['art_style'] = $artStyle;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 图片URL必填
|
|
|
+ $picUrl = trim((string)getProp($params, 'pic_url', ''));
|
|
|
+ if ($picUrl === '') {
|
|
|
+ Utils::throwError('1002:请传入画风图片URL');
|
|
|
+ }
|
|
|
+ $update['pic_url'] = $picUrl;
|
|
|
+
|
|
|
+ // prompt 必填(非空)
|
|
|
+ $prompt = trim((string)getProp($params, 'prompt', ''));
|
|
|
+ if ($prompt === '') {
|
|
|
+ Utils::throwError('1002:请传入画风提示词prompt');
|
|
|
+ }
|
|
|
+ $update['prompt'] = $prompt;
|
|
|
+
|
|
|
+ // aliases 不传时默认使用画风名(新画风名优先,未改则沿用原画风名)
|
|
|
+ $defaultAliasStyle = $artStyle !== '' ? $artStyle : (string)$row->art_style;
|
|
|
+ $update['aliases'] = $this->normalizeAliases($params, $defaultAliasStyle);
|
|
|
+
|
|
|
+ if (array_key_exists('character_prefix', $params)) {
|
|
|
+ $update['character_prefix'] = (string)$params['character_prefix'];
|
|
|
+ }
|
|
|
+ if (array_key_exists('scene_prefix', $params)) {
|
|
|
+ $update['scene_prefix'] = (string)$params['scene_prefix'];
|
|
|
+ }
|
|
|
+ if (array_key_exists('is_enabled', $params) && $params['is_enabled'] !== '' && $params['is_enabled'] !== null) {
|
|
|
+ $update['is_enabled'] = (int)$params['is_enabled'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $update['updated_at'] = date('Y-m-d H:i:s');
|
|
|
+ DB::table('mp_art_styles')->where('id', $id)->update($update);
|
|
|
+
|
|
|
+ $row = DB::table('mp_art_styles')->where('id', $id)->first();
|
|
|
+
|
|
|
+ return $this->buildStyleResult($row);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除画风(软删除 is_deleted=1)
|
|
|
+ *
|
|
|
+ * @param array $params
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function deleteArtStyle(array $params)
|
|
|
+ {
|
|
|
+ $this->assertSuperadmin();
|
|
|
+
|
|
|
+ $id = (int)getProp($params, 'id', 0);
|
|
|
+ if ($id < 1) {
|
|
|
+ Utils::throwError('1002:请传入画风ID');
|
|
|
+ }
|
|
|
+
|
|
|
+ $row = DB::table('mp_art_styles')->where('id', $id)->where('is_deleted', 0)->first();
|
|
|
+ if (!$row) {
|
|
|
+ Utils::throwError('20003:画风不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ return DB::table('mp_art_styles')->where('id', $id)->update([
|
|
|
+ 'is_deleted' => 1,
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s'),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 规范化别名(支持数组或JSON字符串)
|
|
|
+ *
|
|
|
+ * @param array $params
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ private function normalizeAliases(array $params, string $defaultArtStyle = ''): string
|
|
|
+ {
|
|
|
+ // 未传 aliases(或为空)时,默认使用画风名本身作为别名
|
|
|
+ if (!array_key_exists('aliases', $params) || $params['aliases'] === null || $params['aliases'] === '') {
|
|
|
+ return json_encode([$defaultArtStyle], JSON_UNESCAPED_UNICODE);
|
|
|
+ }
|
|
|
+
|
|
|
+ $aliases = getProp($params, 'aliases', []);
|
|
|
+ if (is_string($aliases)) {
|
|
|
+ $aliases = json_decode($aliases, true) ?: [];
|
|
|
+ }
|
|
|
+ if (!is_array($aliases)) {
|
|
|
+ $aliases = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ $aliases = array_values(array_filter(array_map('trim', $aliases), function ($item) {
|
|
|
+ return $item !== '';
|
|
|
+ }));
|
|
|
+
|
|
|
+ return json_encode($aliases, JSON_UNESCAPED_UNICODE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 仅超管可管理画风
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ private function assertSuperadmin(): void
|
|
|
+ {
|
|
|
+ if (Site::getRole() !== 'superadmin') {
|
|
|
+ Utils::throwError('1005:权限不足');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建画风返回信息(add/edit 成功后返回)
|
|
|
+ *
|
|
|
+ * @param object $row
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function buildStyleResult($row): array
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ 'id' => (int)$row->id,
|
|
|
+ 'art_style' => (string)$row->art_style,
|
|
|
+ 'prompt' => (string)$row->prompt,
|
|
|
+ 'character_prefix' => (string)$row->character_prefix,
|
|
|
+ 'scene_prefix' => (string)$row->scene_prefix,
|
|
|
+ 'pic_url' => (string)$row->pic_url,
|
|
|
+ 'is_enabled' => (int)$row->is_enabled,
|
|
|
+ 'created_at' => transDate($row->created_at),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+}
|