StoreServiceLogDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\service;
  12. use app\dao\BaseDao;
  13. use app\model\service\StoreServiceLog;
  14. use think\facade\Db;
  15. use think\Model;
  16. /**
  17. * 客服聊天记录dao
  18. * Class StoreServiceLogDao
  19. * @package app\dao\service
  20. */
  21. class StoreServiceLogDao extends BaseDao
  22. {
  23. /**
  24. * StoreServiceLogDao constructor.
  25. */
  26. public function __construct()
  27. {
  28. //清楚去年的聊天记录
  29. $this->removeChat();
  30. $this->removeYesterDayChat();
  31. }
  32. /**
  33. * 设置模型
  34. * @return string
  35. */
  36. protected function setModel(): string
  37. {
  38. return StoreServiceLog::class;
  39. }
  40. /**
  41. * 获取聊天记录下的uid和to_uid
  42. * @param int $uid
  43. * @return mixed
  44. */
  45. public function getServiceUserUids(int $uid)
  46. {
  47. return $this->search(['uid' => $uid])->group('uid,to_uid')->field(['uid', 'to_uid'])->select()->toArray();
  48. }
  49. /**
  50. * 获取聊天记录并分页
  51. * @param array $where
  52. * @param int $page
  53. * @param int $limit
  54. * @return array
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function getServiceList(array $where, int $page, int $limit, array $field = ['*'])
  60. {
  61. return $this->search($where)->with('user')->field($field)->order('add_time DESC')->page($page, $limit)->select()->toArray();
  62. }
  63. /**
  64. * 获取聊天记录上翻页
  65. * @param array $where
  66. * @param int $page
  67. * @param int $limit
  68. * @param bool $isUp
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function getChatList(array $where, int $limit = 20, int $upperId = 0)
  75. {
  76. return $this->search($where)->when($upperId, function ($query) use ($upperId, $limit) {
  77. $query->where('id', '<', $upperId)->limit($limit)->order('id DESC');
  78. })->when(!$upperId, function ($query) use ($limit) {
  79. $query->limit($limit)->order('id DESC');
  80. })->with(['user', 'service'])->select()->toArray();
  81. }
  82. /**
  83. * 清楚去年的聊天记录
  84. * @return bool
  85. */
  86. public function removeChat()
  87. {
  88. return $this->search(['time' => 'last year'])->delete();
  89. }
  90. /**
  91. * 清楚上周的游客用户聊天记录
  92. * @return bool
  93. */
  94. public function removeYesterDayChat()
  95. {
  96. return $this->search(['time' => 'last week', 'is_tourist' => 1])->delete();
  97. }
  98. /**
  99. * 根据条件获取条数
  100. * @param array $where
  101. * @return int
  102. */
  103. public function whereByCount(array $where)
  104. {
  105. return $this->search(['uid' => $where['uid']])->order('id DESC')->where('add_time', '<', time() - 300)->count();
  106. }
  107. /**
  108. * 获取未读消息条数
  109. * @param array $where
  110. * @return int
  111. */
  112. public function getMessageNum(array $where)
  113. {
  114. return $this->getModel()->where($where)->count();
  115. }
  116. /**
  117. * 搜索聊天记录
  118. * @param array $where
  119. * @return array
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function getMessageList(array $where)
  125. {
  126. return $this->search(['chat' => $where['chat']])->when(isset($where['add_time']) && $where['add_time'], function ($query) use ($where) {
  127. $query->where('add_time', '>', $where['add_time']);
  128. })->select()->toArray();
  129. }
  130. }