TemplateMessageDao.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\wechat;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\wechat\TemplateMessage;
  14. class TemplateMessageDao extends BaseDao
  15. {
  16. protected function getModel(): string
  17. {
  18. return TemplateMessage::class;
  19. }
  20. /**
  21. * 根据条件搜索数据。
  22. *
  23. * 该方法用于根据提供的条件数组搜索数据库中的记录。支持的条件包括状态(status)、类型(type)和关键字(keyword)。
  24. * 搜索逻辑是条件性的,只有当相应的条件存在且不为空时,才会应用到查询中。
  25. * 查询结果按照创建时间降序排序。
  26. *
  27. * @param array $where 包含搜索条件的数组。支持的条件有:status(状态)、type(类型)、keyword(关键字)。
  28. * @return \Illuminate\Database\Query\Builder|static 返回构建器对象或静态调用结果。
  29. */
  30. public function search(array $where)
  31. {
  32. // 获取模型对应的数据库实例。
  33. return ($this->getModel()::getDB())->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  34. // 如果提供了状态条件,则应用到查询中。
  35. $query->where('status', $where['status']);
  36. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  37. // 如果提供了类型条件,则应用到查询中。
  38. $query->where('type', $where['type']);
  39. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  40. // 如果提供了关键字条件,则应用到查询中,支持名称和临时ID的模糊搜索。
  41. $query->where(function($query) use ($where) {
  42. $query->where('name', 'like', '%' . $where['keyword'] . '%');
  43. $query->whereOr('tempid', 'like', '%' . $where['keyword'] . '%');
  44. });
  45. })->order('create_time DESC');
  46. }
  47. /**
  48. * 根据键值和类型获取模板消息的临时ID
  49. *
  50. * 此方法用于从数据库中检索特定类型和键值对应的模板消息临时ID。
  51. * 它通过查询数据库表中的相应记录,返回满足条件的模板消息的临时ID。
  52. *
  53. * @param string $key 模板消息的键值,用于唯一标识模板消息。
  54. * @param string $type 模板消息的类型,用于进一步筛选模板消息。
  55. * @return string 返回查询到的模板消息的临时ID,如果未找到则返回空字符串。
  56. */
  57. public function getTempId($key, $type)
  58. {
  59. // 通过Type和TempKey查询数据库,返回对应的TempID
  60. return TemplateMessage::getDB()->where(['type' => $type, 'tempkey' => $key])->value('tempid');
  61. }
  62. }