WorkGroupChatMemberDao.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\work;
  12. use app\dao\BaseDao;
  13. use app\model\work\WorkGroupChatMember;
  14. use crmeb\traits\SearchDaoTrait;
  15. /**
  16. * 企业微信群成员
  17. * Class WorkGroupChatMemberDao
  18. * @package app\dao\work
  19. */
  20. class WorkGroupChatMemberDao extends BaseDao
  21. {
  22. use SearchDaoTrait;
  23. /**
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return WorkGroupChatMember::class;
  29. }
  30. /**
  31. * 获取今日新增成员数
  32. * @param int $groupId
  33. * @return int
  34. */
  35. public function getToDaySum(int $groupId)
  36. {
  37. return $this->getModel()->where('status', 1)->where('group_id', $groupId)->whereDay('join_time')->count();
  38. }
  39. /**
  40. * 获取今日退群成员数
  41. * @param int $groupId
  42. * @return int
  43. */
  44. public function getToDayReturn(int $groupId)
  45. {
  46. return $this->getModel()->where('status', 0)->where('group_id', $groupId)->whereDay('join_time')->count();
  47. }
  48. /**
  49. * 获取人数统计
  50. * @param int $groupId
  51. * @param string $fromUnixtime
  52. * @param array $field
  53. * @param int $status
  54. * @param string $time
  55. * @param int $page
  56. * @param int $limit
  57. * @return mixed
  58. */
  59. public function getChatMemberStatistics(int $groupId, string $fromUnixtime = 'create_time', array $field = [], int $status = 1, string $time = '', int $page = 0, int $limit = 0)
  60. {
  61. $date = '%Y-%m-%d';
  62. switch ($time) {
  63. case 'today':
  64. case 'yesterday':
  65. $date = '%Y-%m-%d %H';
  66. break;
  67. case 'week':
  68. case 'last week':
  69. case 'last month':
  70. case 'month':
  71. $date = '%Y-%m-%d';
  72. break;
  73. case 'year':
  74. case 'last year':
  75. $date = '%Y-%m';
  76. break;
  77. }
  78. return $this->search(['time' => $time, 'timeKey' => $fromUnixtime])
  79. ->where('group_id', $groupId)
  80. ->where('status', $status)
  81. ->when($page && $limit, function ($query) use ($page, $limit) {
  82. $query->page($page, $limit);
  83. })
  84. ->field(array_merge($field, ['from_unixtime(' . $fromUnixtime . ',"' . $date . '") as time']))
  85. ->group('time')
  86. ->select()->toArray();
  87. }
  88. /**
  89. * 获取人数统计条数
  90. * @param int $groupId
  91. * @param int $status
  92. * @param string $time
  93. * @return mixed
  94. */
  95. public function getChatMemberStatisticsCount(int $groupId, int $status = 1, string $time = '')
  96. {
  97. $date = '%Y-%m-%d';
  98. switch ($time) {
  99. case 'today':
  100. case 'yesterday':
  101. $date = '%Y-%m-%d %H';
  102. break;
  103. case 'week':
  104. case 'last week':
  105. case 'last month':
  106. case 'month':
  107. $date = '%Y-%m-%d';
  108. break;
  109. case 'year':
  110. case 'last year':
  111. $date = '%Y-%m';
  112. break;
  113. }
  114. return $this->search(['time' => $time, 'timeKey' => 'update_time'])
  115. ->where('group_id', $groupId)
  116. ->where('status', $status)
  117. ->field(['from_unixtime(update_time,"' . $date . '") as time'])
  118. ->group('time')
  119. ->count();
  120. }
  121. /**
  122. * 当前用户所在群个数
  123. * @param string $userid
  124. * @return int
  125. */
  126. public function getChatSum(string $userid)
  127. {
  128. return $this->getModel()->where('userid', $userid)->group('group_id')->count();
  129. }
  130. }