UserVisitDao.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. declare (strict_types=1);
  12. namespace app\dao\user;
  13. use app\dao\BaseDao;
  14. use app\model\user\UserVisit;
  15. /**
  16. *
  17. * Class UserVisitDao
  18. * @package app\dao\user
  19. */
  20. class UserVisitDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return UserVisit::class;
  29. }
  30. /**
  31. * 用户趋势数据
  32. * @param $time
  33. * @param $type
  34. * @param $timeType
  35. * @param $str
  36. * @return mixed
  37. */
  38. public function getTrendData($time, $type, $timeType, $str)
  39. {
  40. return $this->getModel()->when($type != '', function ($query) use ($type) {
  41. $query->where('channel_type', $type);
  42. })->where(function ($query) use ($time) {
  43. if ($time[0] == $time[1]) {
  44. $query->whereDay('add_time', $time[0]);
  45. } else {
  46. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  47. $query->whereTime('add_time', 'between', $time);
  48. }
  49. })->field("FROM_UNIXTIME(add_time,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  50. }
  51. /**
  52. * 用户地域数据
  53. * @param $time
  54. * @param $userType
  55. * @return mixed
  56. */
  57. public function getRegion($time, $userType)
  58. {
  59. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  60. $query->where('channel_type', $userType);
  61. })->where(function ($query) use ($time) {
  62. if ($time[0] == $time[1]) {
  63. $query->whereDay('add_time', $time[0]);
  64. } else {
  65. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  66. $query->whereTime('add_time', 'between', $time);
  67. }
  68. })->field('COUNT(distinct(uid)) as visitNum,province')
  69. ->group('province')->select()->toArray();
  70. }
  71. /**
  72. * 根据分组获取记录条数
  73. * @param array $where
  74. * @param string $group
  75. * @return mixed
  76. */
  77. public function groupCount(array $where, string $group = 'uid')
  78. {
  79. return $this->search($where)->group($group)->count();
  80. }
  81. }