PromptTemplateService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. $templateId = getProp($data, 'template_id');
  20. $templateName = getProp($data, 'template_name');
  21. $remark = getProp($data, 'remark');
  22. $query = DB::table('mp_prompt_templates')->where('cpid', $cpid)->where('is_deleted', 0)->select('id as template_id', 'template_name', 'template_prompt', 'remark');
  23. // 模板ID精确查询
  24. if (!empty($templateId)) {
  25. $query->where('id', $templateId);
  26. }
  27. // 模板名称模糊查询
  28. if (!empty($templateName)) {
  29. $query->where('template_name', 'like', "%{$templateName}%");
  30. }
  31. // 备注标签模糊查询
  32. if (!empty($remark)) {
  33. $query->where('remark', 'like', "%{$remark}%");
  34. }
  35. // 查询所有数据
  36. $list = $query->orderBy('id', 'desc')->get();
  37. return $list;
  38. }
  39. /**
  40. * 创建提示词模板
  41. *
  42. * @param array $data
  43. * @return array
  44. * @throws ApiException
  45. */
  46. public function createTemplate($data)
  47. {
  48. $cpid = Site::getCpid();
  49. $templateName = trim($data['template_name']);
  50. $templatePrompt = trim($data['template_prompt']);
  51. $remark = $data['remark'] ?? '';
  52. // 检查模板名称是否已存在(同cpid下)
  53. $exists = DB::table('mp_prompt_templates')
  54. ->where('cpid', $cpid)
  55. ->where('template_name', $templateName)
  56. ->where('is_deleted', 0)
  57. ->exists();
  58. if ($exists) {
  59. Utils::throwError('20003:模版名已存在,请调整');
  60. }
  61. $insertData = [
  62. 'cpid' => $cpid,
  63. 'template_name' => $templateName,
  64. 'template_prompt' => $templatePrompt,
  65. 'remark' => $remark,
  66. 'created_at' => date('Y-m-d H:i:s'),
  67. 'updated_at' => date('Y-m-d H:i:s'),
  68. ];
  69. $templateId = DB::table('mp_prompt_templates')->insertGetId($insertData);
  70. return [
  71. 'template_id' => $templateId,
  72. 'template_name' => $templateName,
  73. 'template_prompt' => $templatePrompt,
  74. 'remark' => $remark,
  75. ];
  76. }
  77. /**
  78. * 编辑提示词模板
  79. *
  80. * @param array $data
  81. * @return array
  82. * @throws ApiException
  83. */
  84. public function editTemplate($data)
  85. {
  86. $cpid = Site::getCpid();
  87. $templateId = $data['template_id'];
  88. // 检查模板是否存在
  89. $template = DB::table('mp_prompt_templates')
  90. ->where('id', $templateId)
  91. ->where('is_deleted', 0)
  92. ->first();
  93. if (!$template) {
  94. Utils::throwError('20003:模板不存在');
  95. }
  96. // 验证cpid权限
  97. if ($template->cpid != $cpid) {
  98. Utils::throwError('20003:没有权限操作此模板');
  99. }
  100. $updateData = ['updated_at' => date('Y-m-d H:i:s')];
  101. // 如果要更新模板名称,检查是否重复(同cpid下)
  102. if (isset($data['template_name'])) {
  103. $templateName = trim($data['template_name']);
  104. $exists = DB::table('mp_prompt_templates')
  105. ->where('cpid', $cpid)
  106. ->where('template_name', $templateName)
  107. ->where('id', '<>', $templateId)
  108. ->where('is_deleted', 0)
  109. ->exists();
  110. if ($exists) {
  111. Utils::throwError('20003:模版名已存在,请调整');
  112. }
  113. $updateData['template_name'] = $templateName;
  114. }
  115. if (isset($data['template_prompt'])) {
  116. $updateData['template_prompt'] = trim($data['template_prompt']);
  117. }
  118. if (isset($data['remark'])) {
  119. $updateData['remark'] = $data['remark'];
  120. }
  121. DB::table('mp_prompt_templates')
  122. ->where('id', $templateId)
  123. ->update($updateData);
  124. // 返回更新后的数据
  125. $updatedTemplate = DB::table('mp_prompt_templates')
  126. ->where('id', $templateId)
  127. ->first();
  128. return [
  129. 'template_id' => $updatedTemplate->id,
  130. 'template_name' => $updatedTemplate->template_name,
  131. 'template_prompt' => $updatedTemplate->template_prompt,
  132. 'remark' => $updatedTemplate->remark ?? '',
  133. 'updated_at' => $updatedTemplate->updated_at,
  134. ];
  135. }
  136. /**
  137. * 删除提示词模板
  138. *
  139. * @param int $templateId
  140. * @return array
  141. * @throws ApiException
  142. */
  143. public function deleteTemplate($templateId)
  144. {
  145. $cpid = Site::getCpid();
  146. // 检查模板是否存在
  147. $template = DB::table('mp_prompt_templates')
  148. ->where('id', $templateId)
  149. ->where('is_deleted', 0)
  150. ->first();
  151. if (!$template) {
  152. Utils::throwError('20003:模板不存在');
  153. }
  154. // 验证cpid权限
  155. if ($template->cpid != $cpid) {
  156. Utils::throwError('20003:没有权限操作此模板');
  157. }
  158. return DB::table('mp_prompt_templates')
  159. ->where('id', $templateId)
  160. ->update([
  161. 'is_deleted' => 1,
  162. 'updated_at' => date('Y-m-d H:i:s')
  163. ]);
  164. }
  165. }