PromptTemplateService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App\Services\PromptTemplate;
  3. use App\Consts\ErrorConst;
  4. use App\Exceptions\ApiException;
  5. use App\Facade\Site;
  6. use App\Libs\Utils;
  7. use Illuminate\Support\Facades\DB;
  8. class PromptTemplateService
  9. {
  10. /**
  11. * 获取提示词模板列表
  12. *
  13. * @param array $data
  14. * @return array
  15. */
  16. public function getTemplates($data = [])
  17. {
  18. $cpid = Site::getCpid();
  19. $uid = Site::getUid();
  20. $templateId = getProp($data, 'template_id');
  21. $templateName = getProp($data, 'template_name');
  22. $remark = getProp($data, 'remark');
  23. $publicType = getProp($data, 'public_type', 2); // 0=个人库, 1=公共库, 2=所有库(默认)
  24. $query = DB::table('mp_prompt_templates')
  25. ->where('cpid', $cpid)
  26. ->where('is_deleted', 0)
  27. ->select(
  28. 'id as template_id',
  29. 'template_name',
  30. 'template_prompt',
  31. 'remark',
  32. 'user_id',
  33. DB::raw('IF(user_id = 0, 1, 0) as is_public')
  34. );
  35. // 根据 public_type 筛选
  36. if ($publicType == 0) {
  37. // 个人库:user_id 等于当前用户
  38. $query->where('user_id', $uid);
  39. } elseif ($publicType == 1) {
  40. // 公共库:user_id 为 0
  41. $query->where('user_id', 0);
  42. }
  43. // public_type == 2 时不添加 user_id 条件,返回所有库
  44. // 模板ID精确查询
  45. if (!empty($templateId)) {
  46. $query->where('id', $templateId);
  47. }
  48. // 模板名称模糊查询
  49. if (!empty($templateName)) {
  50. $query->where('template_name', 'like', "%{$templateName}%");
  51. }
  52. // 备注标签模糊查询
  53. if (!empty($remark)) {
  54. $query->where('remark', 'like', "%{$remark}%");
  55. }
  56. // 查询所有数据,排序:个人库在前(user_id > 0),公共库在后(user_id = 0)
  57. $list = $query->orderByRaw('IF(user_id = 0, 1, 0) ASC')->orderBy('id', 'desc')->get();
  58. return $list;
  59. }
  60. /**
  61. * 创建提示词模板
  62. *
  63. * @param array $data
  64. * @return array
  65. * @throws ApiException
  66. */
  67. public function createTemplate($data)
  68. {
  69. $cpid = Site::getCpid();
  70. $uid = Site::getUid();
  71. $role = Site::getRole();
  72. $templateName = trim($data['template_name']);
  73. $templatePrompt = trim($data['template_prompt']);
  74. $remark = $data['remark'] ?? '';
  75. $isPublic = getProp($data, 'is_public', 0); // 是否创建为公共库,默认为 0(个人库)
  76. // 如果要创建公共库数据,需要验证是否为 admin 角色
  77. if ($isPublic == 1) {
  78. if ($role !== 'admin') {
  79. Utils::throwError('20003:权限不足,只有管理员可以操作公共库');
  80. }
  81. }
  82. // 确定 user_id:公共库为 0,个人库为当前用户 ID
  83. $userId = $isPublic == 1 ? 0 : $uid;
  84. // 检查模板名称是否已存在(同cpid、同user_id下)
  85. $exists = DB::table('mp_prompt_templates')
  86. ->where('cpid', $cpid)
  87. ->where('user_id', $userId)
  88. ->where('template_name', $templateName)
  89. ->where('is_deleted', 0)
  90. ->exists();
  91. if ($exists) {
  92. Utils::throwError('20003:模版名已存在,请调整');
  93. }
  94. $insertData = [
  95. 'cpid' => $cpid,
  96. 'user_id' => $userId,
  97. 'template_name' => $templateName,
  98. 'template_prompt' => $templatePrompt,
  99. 'remark' => $remark,
  100. 'created_at' => date('Y-m-d H:i:s'),
  101. 'updated_at' => date('Y-m-d H:i:s'),
  102. ];
  103. $templateId = DB::table('mp_prompt_templates')->insertGetId($insertData);
  104. return [
  105. 'template_id' => $templateId,
  106. 'template_name' => $templateName,
  107. 'template_prompt' => $templatePrompt,
  108. 'remark' => $remark,
  109. 'is_public' => $isPublic,
  110. ];
  111. }
  112. /**
  113. * 编辑提示词模板
  114. *
  115. * @param array $data
  116. * @return array
  117. * @throws ApiException
  118. */
  119. public function editTemplate($data)
  120. {
  121. $cpid = Site::getCpid();
  122. $uid = Site::getUid();
  123. $role = Site::getRole();
  124. $templateId = $data['template_id'];
  125. // 检查模板是否存在
  126. $template = DB::table('mp_prompt_templates')
  127. ->where('id', $templateId)
  128. ->where('is_deleted', 0)
  129. ->first();
  130. if (!$template) {
  131. Utils::throwError('20003:模板不存在');
  132. }
  133. // 验证cpid权限
  134. if ($template->cpid != $cpid) {
  135. Utils::throwError('20003:没有权限操作此模板');
  136. }
  137. // 判断是否为公共库数据
  138. $isPublic = $template->user_id == 0;
  139. // 如果是公共库数据,需要验证是否为 admin 角色
  140. if ($isPublic && $role !== 'admin') {
  141. Utils::throwError('20003:权限不足,只有管理员可以操作公共库');
  142. }
  143. // 如果是个人库数据,需要验证是否为所有者
  144. if (!$isPublic && $template->user_id != $uid) {
  145. Utils::throwError('20003:没有权限操作此模板');
  146. }
  147. $updateData = ['updated_at' => date('Y-m-d H:i:s')];
  148. // 如果要更新模板名称,检查是否重复(同cpid、同user_id下)
  149. if (isset($data['template_name'])) {
  150. $templateName = trim($data['template_name']);
  151. $exists = DB::table('mp_prompt_templates')
  152. ->where('cpid', $cpid)
  153. ->where('user_id', $template->user_id)
  154. ->where('template_name', $templateName)
  155. ->where('id', '<>', $templateId)
  156. ->where('is_deleted', 0)
  157. ->exists();
  158. if ($exists) {
  159. Utils::throwError('20003:模版名已存在,请调整');
  160. }
  161. $updateData['template_name'] = $templateName;
  162. }
  163. if (isset($data['template_prompt'])) {
  164. $updateData['template_prompt'] = trim($data['template_prompt']);
  165. }
  166. if (isset($data['remark'])) {
  167. $updateData['remark'] = $data['remark'];
  168. }
  169. DB::table('mp_prompt_templates')
  170. ->where('id', $templateId)
  171. ->update($updateData);
  172. // 返回更新后的数据
  173. $updatedTemplate = DB::table('mp_prompt_templates')
  174. ->where('id', $templateId)
  175. ->first();
  176. return [
  177. 'template_id' => $updatedTemplate->id,
  178. 'template_name' => $updatedTemplate->template_name,
  179. 'template_prompt' => $updatedTemplate->template_prompt,
  180. 'remark' => $updatedTemplate->remark ?? '',
  181. 'is_public' => $updatedTemplate->user_id == 0 ? 1 : 0,
  182. 'updated_at' => $updatedTemplate->updated_at,
  183. ];
  184. }
  185. /**
  186. * 删除提示词模板
  187. *
  188. * @param int $templateId
  189. * @return array
  190. * @throws ApiException
  191. */
  192. public function deleteTemplate($templateId)
  193. {
  194. $cpid = Site::getCpid();
  195. $uid = Site::getUid();
  196. $role = Site::getRole();
  197. // 检查模板是否存在
  198. $template = DB::table('mp_prompt_templates')
  199. ->where('id', $templateId)
  200. ->where('is_deleted', 0)
  201. ->first();
  202. if (!$template) {
  203. Utils::throwError('20003:模板不存在');
  204. }
  205. // 验证cpid权限
  206. if ($template->cpid != $cpid) {
  207. Utils::throwError('20003:没有权限操作此模板');
  208. }
  209. // 判断是否为公共库数据
  210. $isPublic = $template->user_id == 0;
  211. // 如果是公共库数据,需要验证是否为 admin 角色
  212. if ($isPublic && $role !== 'admin') {
  213. Utils::throwError('20003:权限不足,只有管理员可以操作公共库');
  214. }
  215. // 如果是个人库数据,需要验证是否为所有者
  216. if (!$isPublic && $template->user_id != $uid) {
  217. Utils::throwError('20003:没有权限操作此模板');
  218. }
  219. return DB::table('mp_prompt_templates')
  220. ->where('id', $templateId)
  221. ->update([
  222. 'is_deleted' => 1,
  223. 'updated_at' => date('Y-m-d H:i:s')
  224. ]);
  225. }
  226. }