MessageTemplateModel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixDec\Message\Model;
  4. use SixDec\Message\Entity\MessageTemplateEntity;
  5. /**
  6. * 消息模板模型
  7. */
  8. class MessageTemplateModel
  9. {
  10. /**
  11. * 获取模板列表
  12. *
  13. * @param array $params 查询参数
  14. * @param int $page 页码
  15. * @param int $limit 每页数量
  16. * @return array
  17. */
  18. public function getList(array $params = [], int $page = 1, int $limit = 10)
  19. {
  20. $query = MessageTemplateEntity::where('1=1');
  21. // 模板代码搜索
  22. if (!empty($params['code'])) {
  23. $query->where('code', 'like', '%' . $params['code'] . '%');
  24. }
  25. // 模板名称搜索
  26. if (!empty($params['name'])) {
  27. $query->where('name', 'like', '%' . $params['name'] . '%');
  28. }
  29. // 类型筛选
  30. if (isset($params['type']) && $params['type'] !== '') {
  31. $query->where('type', $params['type']);
  32. }
  33. // 状态筛选
  34. if (isset($params['status']) && $params['status'] !== '') {
  35. $query->where('status', $params['status']);
  36. }
  37. // 获取总数
  38. $total = $query->count();
  39. // 获取分页数据
  40. $list = $query->order('id', 'desc')
  41. ->page($page, $limit)
  42. ->select()
  43. ->toArray();
  44. return [
  45. 'list' => $list,
  46. 'total' => $total,
  47. 'page' => $page,
  48. 'limit' => $limit,
  49. ];
  50. }
  51. /**
  52. * 获取单个模板详情
  53. *
  54. * @param int $id 模板ID
  55. * @return array|null
  56. */
  57. public function getDetail(int $id)
  58. {
  59. return MessageTemplateEntity::find($id);
  60. }
  61. /**
  62. * 根据模板代码获取模板
  63. *
  64. * @param string $code 模板代码
  65. * @return array|null
  66. */
  67. public function getByCode(string $code)
  68. {
  69. return MessageTemplateEntity::where('code', $code)
  70. ->where('status', 1)
  71. ->find();
  72. }
  73. /**
  74. * 添加模板
  75. *
  76. * @param array $data 模板数据
  77. * @return int|bool
  78. */
  79. public function add(array $data)
  80. {
  81. // 检查模板代码是否已存在
  82. $exists = MessageTemplateEntity::where('code', $data['code'])->find();
  83. if ($exists) {
  84. return false;
  85. }
  86. $entity = new MessageTemplateEntity();
  87. $result = $entity->save($data);
  88. // 确保保存成功并返回ID
  89. if ($result) {
  90. return $entity->id ?: true; // 如果没有id属性,至少返回true表示成功
  91. }
  92. return false;
  93. }
  94. /**
  95. * 更新模板
  96. *
  97. * @param int $id 模板ID
  98. * @param array $data 更新数据
  99. * @return bool
  100. */
  101. public function update(int $id, array $data)
  102. {
  103. $entity = MessageTemplateEntity::find($id);
  104. if (!$entity) {
  105. return false;
  106. }
  107. // 检查模板代码是否已存在
  108. if (isset($data['code']) && $data['code'] != $entity->code) {
  109. $exists = MessageTemplateEntity::where('code', $data['code'])->find();
  110. if ($exists) {
  111. return false;
  112. }
  113. }
  114. return $entity->save($data);
  115. }
  116. /**
  117. * 删除模板
  118. *
  119. * @param int $id 模板ID
  120. * @return bool
  121. */
  122. public function delete(int $id)
  123. {
  124. $entity = MessageTemplateEntity::find($id);
  125. if (!$entity) {
  126. return false;
  127. }
  128. return $entity->delete();
  129. }
  130. /**
  131. * 批量删除模板
  132. *
  133. * @param array $ids 模板ID数组
  134. * @return bool
  135. */
  136. public function batchDelete(array $ids)
  137. {
  138. if (empty($ids)) {
  139. return false;
  140. }
  141. return MessageTemplateEntity::destroy($ids);
  142. }
  143. /**
  144. * 解析模板内容
  145. *
  146. * @param string $code 模板代码
  147. * @param array $data 替换数据
  148. * @return array|null 解析后的标题和内容
  149. */
  150. public function parseTemplate(string $code, array $data)
  151. {
  152. $template = $this->getByCode($code);
  153. if (!$template) {
  154. return null;
  155. }
  156. return $template->parse($data);
  157. }
  158. }