UserBrokerageDao.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. namespace app\dao\user;
  12. use app\dao\BaseDao;
  13. use app\model\user\UserBrokerage;
  14. class UserBrokerageDao extends BaseDao
  15. {
  16. /**
  17. * 设置模型
  18. * @return string
  19. */
  20. protected function setModel(): string
  21. {
  22. return UserBrokerage::class;
  23. }
  24. /**
  25. * 获取列表
  26. * @param array $where
  27. * @param string $field
  28. * @param int $page
  29. * @param int $limit
  30. * @param array $typeWhere
  31. * @return array
  32. * @throws \ReflectionException
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0, array $typeWhere = [])
  38. {
  39. return $this->search($where)->when(count($typeWhere) > 0, function ($query) use ($typeWhere) {
  40. $query->where($typeWhere);
  41. })->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
  42. $query->page($page, $limit);
  43. })->order('id desc')->select()->toArray();
  44. }
  45. /**
  46. * 获取列表
  47. * @param array $where
  48. * @param string $field
  49. * @param int $page
  50. * @param int $limit
  51. * @return array
  52. */
  53. public function getBrokerageList(array $where, string $field = '*', int $page = 0, int $limit = 0)
  54. {
  55. return $this->search($where)->field($field)->with([
  56. 'user' => function ($query) {
  57. $query->field('uid,nickname');
  58. }])->when($page && $limit, function ($query) use ($page, $limit) {
  59. $query->page($page, $limit);
  60. })->order('id desc')->select()->toArray();
  61. }
  62. /**
  63. * 查询列表
  64. * @param array $where
  65. * @return array
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function getUserBrokerageList(array $where)
  71. {
  72. return $this->search($where)->select()->toArray();
  73. }
  74. /**
  75. * 获取佣金排行
  76. * @param array $where
  77. * @param int $page
  78. * @param int $limit
  79. * @return array
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function brokerageRankList(array $where, int $page = 0, int $limit = 0)
  85. {
  86. //SUM(IF(pm=1,`number`,-`number`))
  87. if ($where['pm'] == 1) $where['not_type'] = ['extract_fail'];
  88. return $this->search($where)->field('uid,SUM(number) as brokerage_price')->with(['user' => function ($query) {
  89. $query->field('uid,avatar,nickname');
  90. }])->order('brokerage_price desc')->group('uid')->when($page && $limit, function ($query) use ($page, $limit) {
  91. $query->page($page, $limit);
  92. })->select()->toArray();
  93. }
  94. /**
  95. * 获取某些条件的佣金总数
  96. * @param array $where
  97. * @return mixed
  98. * @throws \ReflectionException
  99. */
  100. public function getBrokerageSumColumn(array $where)
  101. {
  102. if ($where['pm'] == 1) $where['not_type'] = ['extract_fail'];
  103. if (isset($where['uid']) && is_array($where['uid'])) {
  104. return $this->search($where)->group('uid')->column('sum(number) as num', 'uid');
  105. } else
  106. return $this->search($where)->sum('number');
  107. }
  108. /**
  109. * 获取某些条件的bill总数
  110. * @param array $where
  111. * @return mixed
  112. */
  113. public function getBrokerageSumColumnNew(array $where)
  114. {
  115. if (isset($where['uid']) && is_array($where['uid'])) {
  116. return $this->search($where)->group('uid')->column('sum(number) as num', 'uid');
  117. } else
  118. return $this->search($where)->sum('number');
  119. }
  120. /**
  121. * 获取某个账户下的冻结佣金
  122. * @param int $uid
  123. * @return float
  124. * @throws \ReflectionException
  125. */
  126. public function getUserFrozenPrice(int $uid)
  127. {
  128. return $this->search(['uid' => $uid, 'status' => 1, 'pm' => 1])->where('frozen_time', '>', time())->sum('number');
  129. }
  130. /**
  131. * 按时间获取佣金列表(完整版,包含user信息)
  132. * @param array $where
  133. * @param string $field
  134. * @param int $page
  135. * @param int $limit
  136. * @return array
  137. * @throws \think\db\exception\DataNotFoundException
  138. * @throws \think\db\exception\DbException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. */
  141. public function getBrokerageListByTime(array $where, string $field = '*', int $page = 0, int $limit = 0)
  142. {
  143. return $this->search($where)->field($field)->with([
  144. 'user' => function ($query) {
  145. $query->field('uid,nickname,phone,avatar');
  146. }])->when($page && $limit, function ($query) use ($page, $limit) {
  147. $query->page($page, $limit);
  148. })->order('add_time desc')->select()->toArray();
  149. }
  150. }