UserBrokerageDao.php 5.1 KB

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