PromptTemplateService.php 9.0 KB

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