StoreServiceLogDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\service;
  12. use app\dao\BaseDao;
  13. use app\model\service\StoreServiceLog;
  14. /**
  15. * 客服聊天记录dao
  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 $page
  65. * @param int $limit
  66. * @param bool $isUp
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getChatList(array $where, int $limit = 20, int $upperId = 0)
  73. {
  74. return $this->search($where)->when($upperId, function ($query) use ($upperId, $limit) {
  75. $query->where('id', '<', $upperId)->limit($limit)->order('id DESC');
  76. })->when(!$upperId, function ($query) use ($limit) {
  77. $query->limit($limit)->order('id DESC');
  78. })->with(['user'])->select()->toArray();
  79. }
  80. /**
  81. * 清楚去年的聊天记录
  82. * @return bool
  83. */
  84. public function removeChat()
  85. {
  86. return $this->search(['time' => 'last year'])->delete();
  87. }
  88. /**
  89. * 清楚上周的游客用户聊天记录
  90. * @return bool
  91. */
  92. public function removeYesterDayChat()
  93. {
  94. return $this->search(['time' => 'last week', 'is_tourist' => 1])->delete();
  95. }
  96. /**
  97. * 根据条件获取条数
  98. * @param array $where
  99. * @return int
  100. */
  101. public function whereByCount(array $where)
  102. {
  103. return $this->search(['uid' => $where['uid']])->order('id DESC')->where('add_time', '<', time() - 300)->count();
  104. }
  105. /**
  106. * 获取未读消息条数
  107. * @param array $where
  108. * @return int
  109. */
  110. public function getMessageNum(array $where)
  111. {
  112. return $this->getModel()->where($where)->count();
  113. }
  114. /**
  115. * 搜索聊天记录
  116. * @param array $where
  117. * @return array
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function getMessageList(array $where)
  123. {
  124. return $this->search(['chat' => $where['chat']])->when(isset($where['add_time']) && $where['add_time'], function ($query) use ($where) {
  125. $query->where('add_time', '>', $where['add_time']);
  126. })->select()->toArray();
  127. }
  128. }