StoreServiceLogDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\message\service;
  12. use app\dao\BaseDao;
  13. use app\model\message\service\StoreServiceLog;
  14. /**
  15. *
  16. * Class StoreServiceLogDao
  17. * @package app\dao\service
  18. */
  19. class StoreServiceLogDao extends BaseDao
  20. {
  21. /**
  22. * StoreServiceLogDao constructor.
  23. */
  24. public function __construct()
  25. {
  26. //清楚去年的聊天记录
  27. // $this->removeChat();
  28. $this->removeYesterDayChat();
  29. }
  30. /**
  31. * 设置模型
  32. * @return string
  33. */
  34. protected function setModel(): string
  35. {
  36. return StoreServiceLog::class;
  37. }
  38. /**
  39. * 获取聊天记录下的uid和to_uid
  40. * @param int $uid
  41. * @return mixed
  42. */
  43. public function getServiceUserUids(int $uid)
  44. {
  45. return $this->search(['uid' => $uid])->group('uid,to_uid')->field(['uid', 'to_uid'])->select()->toArray();
  46. }
  47. /**
  48. * 获取聊天记录并分页
  49. * @param array $where
  50. * @param int $page
  51. * @param int $limit
  52. * @return array
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function getServiceList(array $where, int $page, int $limit, array $field = ['*'])
  58. {
  59. return $this->search($where)->with('user')->field($field)->order('add_time DESC')->page($page, $limit)->select()->toArray();
  60. }
  61. /**
  62. * 获取聊天记录上翻页
  63. * @param array $where
  64. * @param int $limit
  65. * @param int $upperId
  66. * @return array
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public function getChatList(array $where, int $limit = 20, int $upperId = 0)
  72. {
  73. return $this->search($where)->when($upperId, function ($query) use ($upperId, $limit) {
  74. $query->where('id', '<', $upperId)->limit($limit)->order('id DESC');
  75. })->when(!$upperId, function ($query) use ($limit) {
  76. $query->limit($limit)->order('id DESC');
  77. })->with(['user', 'service'])->select()->toArray();
  78. }
  79. /**
  80. * 清楚去年的聊天记录
  81. * @return bool
  82. */
  83. public function removeChat()
  84. {
  85. return $this->search(['time' => 'last year'])->delete();
  86. }
  87. /**
  88. * 清楚上周的游客用户聊天记录
  89. * @return bool
  90. */
  91. public function removeYesterDayChat()
  92. {
  93. return $this->search(['time' => 'last week', 'is_tourist' => 1])->delete();
  94. }
  95. /**
  96. * 根据条件获取条数
  97. * @param array $where
  98. * @return int
  99. */
  100. public function whereByCount(array $where)
  101. {
  102. return $this->search(['uid' => $where['uid']])->order('id DESC')->where('add_time', '<', time() - 300)->count();
  103. }
  104. /**
  105. * 获取未读消息条数
  106. * @param array $where
  107. * @return int
  108. */
  109. public function getMessageNum(array $where)
  110. {
  111. return $this->getModel()->where($where)->count();
  112. }
  113. /**
  114. * 搜索聊天记录
  115. * @param array $where
  116. * @return array
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getMessageList(array $where)
  122. {
  123. return $this->search(['chat' => $where['chat']])->when(isset($where['add_time']) && $where['add_time'], function ($query) use ($where) {
  124. $query->where('add_time', '>', $where['add_time']);
  125. })->select()->toArray();
  126. }
  127. }