UserSpreadDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\UserSpread;
  15. /**
  16. * Class UserSpreadDao
  17. * @package app\dao\user
  18. */
  19. class UserSpreadDao extends BaseDao
  20. {
  21. /**
  22. * 设置模型
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return UserSpread::class;
  28. }
  29. /**
  30. * 获取推广列表
  31. * @param array $where
  32. * @param string $field
  33. * @param int $page
  34. * @param int $limit
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getList(array $where, string $field = '*', array $with = [], int $page = 0, int $limit = 0)
  41. {
  42. return $this->search($where)->field($field)
  43. ->when($with, function ($query) use ($with) {
  44. $query->with($with);
  45. })->when($page && $limit, function ($query) use ($page, $limit) {
  46. $query->page($page, $limit);
  47. })->order('spread_time desc,id desc')->select()->toArray();
  48. }
  49. /**
  50. * 获取推广uids
  51. * @param array $where
  52. * @param string $field
  53. * @param int $page
  54. * @param int $limit
  55. * @return array
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function getSpreadUids(array $where)
  61. {
  62. return $this->search($where)->order('spread_time desc,id desc')->column('uid');
  63. }
  64. /**
  65. * 获取一段时间内推广人数
  66. * @param $datebefor
  67. * @param $dateafter
  68. * @return mixed
  69. */
  70. public function spreadTimeList(array $where, array $time, string $timeType = "week", string $countField = '*', string $sumField = 'pay_price', string $groupField = 'add_time')
  71. {
  72. return $this->getModel()->where($where)
  73. ->where($groupField, 'between time', $time)
  74. ->when($timeType, function ($query) use ($timeType, $countField, $sumField, $groupField) {
  75. switch ($timeType) {
  76. case "hour":
  77. $timeUnix = "%H";
  78. break;
  79. case "day" :
  80. $timeUnix = "%m-%d";
  81. break;
  82. case "week" :
  83. $timeUnix = "%w";
  84. break;
  85. case "month" :
  86. $timeUnix = "%d";
  87. break;
  88. case "year" :
  89. $timeUnix = "%m";
  90. break;
  91. default:
  92. $timeUnix = "%m-%d";
  93. break;
  94. }
  95. $query->field("FROM_UNIXTIME(`" . $groupField . "`,'$timeUnix') as day,count(`" . $countField . "`) as count,sum(`" . $sumField . "`) as price");
  96. $query->group("FROM_UNIXTIME($groupField, '$timeUnix')");
  97. })->order('add_time asc')->select()->toArray();
  98. }
  99. /**
  100. * 获取推广人排行
  101. * @param array $time
  102. * @param string $field
  103. * @param int $page
  104. * @param int $limit
  105. */
  106. public function getAgentRankList(array $time, string $field = '*', int $page = 0, int $limit = 0)
  107. {
  108. return $this->getModel()->with(['spreadUser'])
  109. ->field($field)
  110. ->order('count desc')
  111. ->order('uid desc')
  112. ->where('spread_time', 'BETWEEN', $time)
  113. ->page($page, $limit)
  114. ->group('spread_uid')
  115. ->select()->toArray();
  116. }
  117. }