SystemNoticeLogDao.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\system\notice;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\notice\SystemNoticeLog;
  15. /**
  16. * Class SystemNoticeLogDao
  17. * @package app\common\dao\system\notice
  18. * @author xaboy
  19. * @day 2020/11/6
  20. */
  21. class SystemNoticeLogDao extends BaseDao
  22. {
  23. /**
  24. * @return string
  25. * @author xaboy
  26. * @day 2020/11/6
  27. */
  28. protected function getModel(): string
  29. {
  30. return SystemNoticeLog::class;
  31. }
  32. /**
  33. * 标记通知为已读
  34. *
  35. * 本函数用于更新指定通知的阅读状态,将通知的阅读状态设置为已读,并记录阅读时间。
  36. * 主要针对商家后台的操作通知,通过通知ID和商家ID定位到特定的通知记录,进行状态更新。
  37. *
  38. * @param int $id 通知记录ID,用于唯一标识一条通知。
  39. * @param int $merId 商家ID,标识通知所属的商家。
  40. * @return bool 更新操作的结果,成功返回true,失败返回false。
  41. */
  42. public function read($id, $merId)
  43. {
  44. // 使用where子句指定通知记录ID和商家ID,更新通知的阅读状态和阅读时间
  45. return SystemNoticeLog::getDB()->where('notice_log_id', $id)->where('mer_id', $merId)->update(['is_read' => 1, 'read_time' => date('Y-m-d H:i:s')]);
  46. }
  47. /**
  48. * 获取未读通知数量
  49. *
  50. * 本函数用于查询指定商户ID对应的未读系统通知的数量。
  51. * 通过筛选is_read字段为0的记录,确保只统计未读的通知。
  52. *
  53. * @param int $merId 商户ID,用于指定查询哪个商户的通知数量。
  54. * @return int 返回未读通知的数量。
  55. */
  56. public function unreadCount($merId)
  57. {
  58. // 使用SystemNoticeLog类的getDB方法获取数据库实例,并链式调用where方法设置查询条件,最后调用count方法统计符合条件的记录数。
  59. return SystemNoticeLog::getDB()->where('mer_id', $merId)->where('is_read', 0)->count();
  60. }
  61. /**
  62. * 删除指定通知日志
  63. *
  64. * 本函数用于根据通知日志ID和商户ID删除相应的通知日志记录。
  65. * 参数$id代表通知日志的唯一标识,$merId代表商户的标识。
  66. * 函数返回删除操作的结果,通常是一个布尔值,表示删除是否成功。
  67. *
  68. * @param int $id 通知日志ID
  69. * @param int $merId 商户ID
  70. * @return bool 删除操作的结果,成功返回true,失败返回false
  71. */
  72. public function del($id, $merId)
  73. {
  74. // 使用SystemNoticeLog类的getDB方法获取数据库操作对象,并构造删除条件
  75. // 其中where('notice_log_id', $id)指定了通知日志ID,where('mer_id', $merId)指定了商户ID
  76. // 最后执行删除操作并返回结果
  77. return SystemNoticeLog::getDB()->where('notice_log_id', $id)->where('mer_id', $merId)->delete();
  78. }
  79. /**
  80. * 根据条件搜索系统通知日志
  81. *
  82. * 本函数用于查询系统通知日志表中的数据,根据传入的条件进行过滤。条件包括商家ID、阅读状态、通知创建时间范围和关键词。
  83. * 查询结果将返回符合所有条件的通知日志记录。
  84. *
  85. * @param array $where 查询条件数组,包含各种过滤条件如商家ID、阅读状态、日期和关键词。
  86. * @return \think\Paginator 返回查询结果的分页对象,包含系统通知日志的数据。
  87. */
  88. public function search(array $where)
  89. {
  90. // 初始化系统通知日志的数据库查询
  91. return SystemNoticeLog::getDB()->alias('A')
  92. // 加入系统通知表,以便查询通知的详细信息
  93. ->join('SystemNotice B', 'A.notice_id = B.notice_id')
  94. // 过滤条件:商家ID
  95. ->where('mer_id', $where['mer_id'])
  96. // 当传入阅读状态条件时,添加对应过滤条件
  97. ->when(isset($where['is_read']) && $where['is_read'] !== '', function ($query) use ($where) {
  98. $query->where('A.is_read', intval($where['is_read']));
  99. })
  100. // 当传入日期条件时,添加对应过滤条件,用于查询指定日期范围的通知
  101. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  102. getModelTime($query, $where['date'], 'B.create_time');
  103. })
  104. // 当传入关键词条件时,添加对应过滤条件,用于查询包含指定关键词的通知
  105. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  106. $query->whereLike('B.notice_title|B.notice_content', "%{$where['keyword']}%");
  107. })
  108. // 过滤条件:删除状态,只返回未删除的通知日志
  109. ->where('A.is_del', 0);
  110. }
  111. }