| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- namespace App\Services\PromptTemplate;
- use App\Consts\ErrorConst;
- use App\Exceptions\ApiException;
- use App\Facade\Site;
- use App\Libs\Utils;
- use Illuminate\Support\Facades\DB;
- class PromptTemplateService
- {
- /**
- * 获取提示词模板列表
- *
- * @param array $data
- * @return array
- */
- public function getTemplates($data = [])
- {
- $cpid = Site::getCpid();
- $uid = Site::getUid();
- $templateId = getProp($data, 'template_id');
- $templateName = getProp($data, 'template_name');
- $remark = getProp($data, 'remark');
- $publicType = getProp($data, 'public_type', 2); // 0=个人库, 1=公共库, 2=所有库(默认)
- $query = DB::table('mp_prompt_templates')
- ->where('cpid', $cpid)
- ->where('is_deleted', 0)
- ->select(
- 'id as template_id',
- 'template_name',
- 'template_prompt',
- 'remark',
- 'user_id',
- DB::raw('IF(user_id = 0, 1, 0) as is_public')
- );
- // 根据 public_type 筛选
- if ($publicType == 0) {
- // 个人库:user_id 等于当前用户
- $query->where('user_id', $uid);
- } elseif ($publicType == 1) {
- // 公共库:user_id 为 0
- $query->where('user_id', 0);
- }
- // public_type == 2 时不添加 user_id 条件,返回所有库
- // 模板ID精确查询
- if (!empty($templateId)) {
- $query->where('id', $templateId);
- }
- // 模板名称模糊查询
- if (!empty($templateName)) {
- $query->where('template_name', 'like', "%{$templateName}%");
- }
- // 备注标签模糊查询
- if (!empty($remark)) {
- $query->where('remark', 'like', "%{$remark}%");
- }
- // 查询所有数据,排序:个人库在前(user_id > 0),公共库在后(user_id = 0)
- $list = $query->orderByRaw('IF(user_id = 0, 1, 0) ASC')->orderBy('id', 'desc')->get();
- return $list;
- }
- /**
- * 创建提示词模板
- *
- * @param array $data
- * @return array
- * @throws ApiException
- */
- public function createTemplate($data)
- {
- $cpid = Site::getCpid();
- $uid = Site::getUid();
- $role = Site::getRole();
- $templateName = trim($data['template_name']);
- $templatePrompt = trim($data['template_prompt']);
- $remark = $data['remark'] ?? '';
- $isPublic = getProp($data, 'is_public', 0); // 是否创建为公共库,默认为 0(个人库)
- // 如果要创建公共库数据,需要验证是否为 admin 角色
- if ($isPublic == 1) {
- if ($role !== 'admin') {
- Utils::throwError('20003:权限不足,只有管理员可以操作公共库');
- }
- }
- // 确定 user_id:公共库为 0,个人库为当前用户 ID
- $userId = $isPublic == 1 ? 0 : $uid;
- // 检查模板名称是否已存在(同cpid、同user_id下)
- $exists = DB::table('mp_prompt_templates')
- ->where('cpid', $cpid)
- ->where('user_id', $userId)
- ->where('template_name', $templateName)
- ->where('is_deleted', 0)
- ->exists();
- if ($exists) {
- Utils::throwError('20003:模版名已存在,请调整');
- }
- $insertData = [
- 'cpid' => $cpid,
- 'user_id' => $userId,
- 'template_name' => $templateName,
- 'template_prompt' => $templatePrompt,
- 'remark' => $remark,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s'),
- ];
- $templateId = DB::table('mp_prompt_templates')->insertGetId($insertData);
- return [
- 'template_id' => $templateId,
- 'template_name' => $templateName,
- 'template_prompt' => $templatePrompt,
- 'remark' => $remark,
- 'is_public' => $isPublic,
- ];
- }
- /**
- * 编辑提示词模板
- *
- * @param array $data
- * @return array
- * @throws ApiException
- */
- public function editTemplate($data)
- {
- $cpid = Site::getCpid();
- $uid = Site::getUid();
- $role = Site::getRole();
- $templateId = $data['template_id'];
- // 检查模板是否存在
- $template = DB::table('mp_prompt_templates')
- ->where('id', $templateId)
- ->where('is_deleted', 0)
- ->first();
- if (!$template) {
- Utils::throwError('20003:模板不存在');
- }
- // 验证cpid权限
- if ($template->cpid != $cpid) {
- Utils::throwError('20003:没有权限操作此模板');
- }
- // 判断是否为公共库数据
- $isPublic = $template->user_id == 0;
- // 如果是公共库数据,需要验证是否为 admin 角色
- if ($isPublic && $role !== 'admin') {
- Utils::throwError('20003:权限不足,只有管理员可以操作公共库');
- }
- // 如果是个人库数据,需要验证是否为所有者
- if (!$isPublic && $template->user_id != $uid) {
- Utils::throwError('20003:没有权限操作此模板');
- }
- $updateData = ['updated_at' => date('Y-m-d H:i:s')];
- // 如果要更新模板名称,检查是否重复(同cpid、同user_id下)
- if (isset($data['template_name'])) {
- $templateName = trim($data['template_name']);
-
- $exists = DB::table('mp_prompt_templates')
- ->where('cpid', $cpid)
- ->where('user_id', $template->user_id)
- ->where('template_name', $templateName)
- ->where('id', '<>', $templateId)
- ->where('is_deleted', 0)
- ->exists();
- if ($exists) {
- Utils::throwError('20003:模版名已存在,请调整');
- }
- $updateData['template_name'] = $templateName;
- }
- if (isset($data['template_prompt'])) {
- $updateData['template_prompt'] = trim($data['template_prompt']);
- }
- if (isset($data['remark'])) {
- $updateData['remark'] = $data['remark'];
- }
- DB::table('mp_prompt_templates')
- ->where('id', $templateId)
- ->update($updateData);
- // 返回更新后的数据
- $updatedTemplate = DB::table('mp_prompt_templates')
- ->where('id', $templateId)
- ->first();
- return [
- 'template_id' => $updatedTemplate->id,
- 'template_name' => $updatedTemplate->template_name,
- 'template_prompt' => $updatedTemplate->template_prompt,
- 'remark' => $updatedTemplate->remark ?? '',
- 'is_public' => $updatedTemplate->user_id == 0 ? 1 : 0,
- 'updated_at' => $updatedTemplate->updated_at,
- ];
- }
- /**
- * 删除提示词模板
- *
- * @param int $templateId
- * @return array
- * @throws ApiException
- */
- public function deleteTemplate($templateId)
- {
- $cpid = Site::getCpid();
- $uid = Site::getUid();
- $role = Site::getRole();
- // 检查模板是否存在
- $template = DB::table('mp_prompt_templates')
- ->where('id', $templateId)
- ->where('is_deleted', 0)
- ->first();
- if (!$template) {
- Utils::throwError('20003:模板不存在');
- }
- // 验证cpid权限
- if ($template->cpid != $cpid) {
- Utils::throwError('20003:没有权限操作此模板');
- }
- // 判断是否为公共库数据
- $isPublic = $template->user_id == 0;
- // 如果是公共库数据,需要验证是否为 admin 角色
- if ($isPublic && $role !== 'admin') {
- Utils::throwError('20003:权限不足,只有管理员可以操作公共库');
- }
- // 如果是个人库数据,需要验证是否为所有者
- if (!$isPublic && $template->user_id != $uid) {
- Utils::throwError('20003:没有权限操作此模板');
- }
- return DB::table('mp_prompt_templates')
- ->where('id', $templateId)
- ->update([
- 'is_deleted' => 1,
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- }
|