UserUserBrokerageDao.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. declare (strict_types=1);
  12. namespace app\dao\user;
  13. use app\dao\BaseDao;
  14. use app\model\user\User;
  15. use app\model\user\UserBrokerage;
  16. /**
  17. * 用户关联佣金
  18. * Class UserUserBrokerageDao
  19. * @package app\dao\user
  20. */
  21. class UserUserBrokerageDao extends BaseDao
  22. {
  23. /**
  24. * 设置模型
  25. * @return string
  26. */
  27. protected function setModel(): string
  28. {
  29. return User::class;
  30. }
  31. public function joinModel(): string
  32. {
  33. return UserBrokerage::class;
  34. }
  35. /**
  36. * 关联模型
  37. * @param string $alias
  38. * @param string $join_alias
  39. * @return \crmeb\basic\BaseModel
  40. */
  41. public function getModel(string $alias = 'u', string $join_alias = 'b', $join = 'left')
  42. {
  43. $this->alias = $alias;
  44. $this->join_alis = $join_alias;
  45. /** @var UserBrokerage $userBrokerage */
  46. $userBrokerage = app()->make($this->joinModel());
  47. $table = $userBrokerage->getName();
  48. return parent::getModel()
  49. ->join($table . ' ' . $join_alias, $alias . '.uid = ' . $join_alias . '.uid', $join)
  50. ->alias($alias);
  51. }
  52. /**
  53. * 组合条件模型查询列表
  54. * @param array $where
  55. * @param string $field
  56. * @param string $order
  57. * @param int $page
  58. * @param int $limit
  59. * @return array
  60. */
  61. public function getList(array $where, string $field = '', string $order = '', int $page = 0, int $limit = 0)
  62. {
  63. $time = $where['time'];
  64. unset($where['time']);
  65. return $this->getModel()->when($time != '', function ($query) use ($time) {
  66. $timeKey = $this->join_alis . '.add_time';
  67. switch ($time) {
  68. case 'today':
  69. case 'week':
  70. case 'month':
  71. case 'year':
  72. case 'yesterday':
  73. case 'last year':
  74. case 'last week':
  75. case 'last month':
  76. $query->whereTime($timeKey, $time);
  77. break;
  78. case 'quarter':
  79. [$startTime, $endTime] = $this->getMonth();
  80. $query->whereBetween($timeKey, [$startTime, $endTime]);
  81. break;
  82. case 'lately7':
  83. $query->whereBetween($timeKey, [strtotime("-7 day"), time()]);
  84. break;
  85. case 'lately30':
  86. $query->whereBetween($timeKey, [strtotime("-30 day"), time()]);
  87. break;
  88. default:
  89. if (strstr($time, '-') !== false) {
  90. [$startTime, $endTime] = explode('-', $time);
  91. $startTime = trim($startTime);
  92. $endTime = trim($endTime);
  93. if ($startTime && $endTime) {
  94. $query->whereBetween($timeKey, [strtotime($startTime), $startTime == $endTime ? strtotime($endTime) + 86400 : strtotime($endTime)]);
  95. } else if (!$startTime && $endTime) {
  96. $query->whereTime($timeKey, '<', strtotime($endTime) + 86400);
  97. } else if ($startTime && !$endTime) {
  98. $query->whereTime($timeKey, '>=', strtotime($startTime));
  99. }
  100. }
  101. break;
  102. }
  103. })->where($where)->field($field)->group('u.uid')->order($order)->order('id desc')
  104. ->when($page && $limit, function ($query) use ($page, $limit) {
  105. $query->page($page, $limit);
  106. })->select()->toArray();
  107. }
  108. /**
  109. * 获取条数
  110. * @param array $where
  111. * @return mixed
  112. */
  113. public function getCount(array $where)
  114. {
  115. $time = $where['time'];
  116. unset($where['time']);
  117. return $this->getModel()->when($time != '', function ($query) use ($time) {
  118. $timeKey = $this->join_alis . '.add_time';
  119. switch ($time) {
  120. case 'today':
  121. case 'week':
  122. case 'month':
  123. case 'year':
  124. case 'yesterday':
  125. case 'last year':
  126. case 'last week':
  127. case 'last month':
  128. $query->whereTime($timeKey, $time);
  129. break;
  130. case 'quarter':
  131. [$startTime, $endTime] = $this->getMonth();
  132. $query->whereBetween($timeKey, [$startTime, $endTime]);
  133. break;
  134. case 'lately7':
  135. $query->whereBetween($timeKey, [strtotime("-7 day"), time()]);
  136. break;
  137. case 'lately30':
  138. $query->whereBetween($timeKey, [strtotime("-30 day"), time()]);
  139. break;
  140. default:
  141. if (strstr($time, '-') !== false) {
  142. [$startTime, $endTime] = explode('-', $time);
  143. $startTime = trim($startTime);
  144. $endTime = trim($endTime);
  145. if ($startTime && $endTime) {
  146. $query->whereBetween($timeKey, [strtotime($startTime), $startTime == $endTime ? strtotime($endTime) + 86400 : strtotime($endTime)]);
  147. } else if (!$startTime && $endTime) {
  148. $query->whereTime($timeKey, '<', strtotime($endTime) + 86400);
  149. } else if ($startTime && !$endTime) {
  150. $query->whereTime($timeKey, '>=', strtotime($startTime));
  151. }
  152. }
  153. break;
  154. }
  155. })->where($where)->group('u.uid')->count();
  156. }
  157. /**
  158. * 获取本季度 time
  159. * @param int $ceil
  160. * @return array
  161. */
  162. public function getMonth(int $ceil = 0)
  163. {
  164. if ($ceil != 0) {
  165. $season = ceil(date('n') / 3) - $ceil;
  166. } else {
  167. $season = ceil(date('n') / 3);
  168. }
  169. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  170. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  171. return [$firstday, $lastday];
  172. }
  173. }