| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- declare(strict_types=1);
- namespace SixDec\Message\Model;
- use SixDec\Message\Entity\MessageTemplateEntity;
- /**
- * 消息模板模型
- */
- class MessageTemplateModel
- {
- /**
- * 获取模板列表
- *
- * @param array $params 查询参数
- * @param int $page 页码
- * @param int $limit 每页数量
- * @return array
- */
- public function getList(array $params = [], int $page = 1, int $limit = 10)
- {
- $query = MessageTemplateEntity::where('1=1');
-
- // 模板代码搜索
- if (!empty($params['code'])) {
- $query->where('code', 'like', '%' . $params['code'] . '%');
- }
-
- // 模板名称搜索
- if (!empty($params['name'])) {
- $query->where('name', 'like', '%' . $params['name'] . '%');
- }
-
- // 类型筛选
- if (isset($params['type']) && $params['type'] !== '') {
- $query->where('type', $params['type']);
- }
-
- // 状态筛选
- if (isset($params['status']) && $params['status'] !== '') {
- $query->where('status', $params['status']);
- }
-
- // 获取总数
- $total = $query->count();
-
- // 获取分页数据
- $list = $query->order('id', 'desc')
- ->page($page, $limit)
- ->select()
- ->toArray();
-
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit,
- ];
- }
-
- /**
- * 获取单个模板详情
- *
- * @param int $id 模板ID
- * @return array|null
- */
- public function getDetail(int $id)
- {
- return MessageTemplateEntity::find($id);
- }
-
- /**
- * 根据模板代码获取模板
- *
- * @param string $code 模板代码
- * @return array|null
- */
- public function getByCode(string $code)
- {
- return MessageTemplateEntity::where('code', $code)
- ->where('status', 1)
- ->find();
- }
-
- /**
- * 添加模板
- *
- * @param array $data 模板数据
- * @return int|bool
- */
- public function add(array $data)
- {
- // 检查模板代码是否已存在
- $exists = MessageTemplateEntity::where('code', $data['code'])->find();
- if ($exists) {
- return false;
- }
-
- $entity = new MessageTemplateEntity();
- $result = $entity->save($data);
-
- // 确保保存成功并返回ID
- if ($result) {
- return $entity->id ?: true; // 如果没有id属性,至少返回true表示成功
- }
-
- return false;
- }
-
- /**
- * 更新模板
- *
- * @param int $id 模板ID
- * @param array $data 更新数据
- * @return bool
- */
- public function update(int $id, array $data)
- {
- $entity = MessageTemplateEntity::find($id);
- if (!$entity) {
- return false;
- }
-
- // 检查模板代码是否已存在
- if (isset($data['code']) && $data['code'] != $entity->code) {
- $exists = MessageTemplateEntity::where('code', $data['code'])->find();
- if ($exists) {
- return false;
- }
- }
-
- return $entity->save($data);
- }
-
- /**
- * 删除模板
- *
- * @param int $id 模板ID
- * @return bool
- */
- public function delete(int $id)
- {
- $entity = MessageTemplateEntity::find($id);
- if (!$entity) {
- return false;
- }
-
- return $entity->delete();
- }
-
- /**
- * 批量删除模板
- *
- * @param array $ids 模板ID数组
- * @return bool
- */
- public function batchDelete(array $ids)
- {
- if (empty($ids)) {
- return false;
- }
-
- return MessageTemplateEntity::destroy($ids);
- }
-
- /**
- * 解析模板内容
- *
- * @param string $code 模板代码
- * @param array $data 替换数据
- * @return array|null 解析后的标题和内容
- */
- public function parseTemplate(string $code, array $data)
- {
- $template = $this->getByCode($code);
- if (!$template) {
- return null;
- }
-
- return $template->parse($data);
- }
- }
|