PromptTemplateService.php 9.1 KB

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