UserBrokerageDao.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getUserBrokerageList(array $where)
  54. {
  55. return $this->search($where)->select()->toArray();
  56. }
  57. /**
  58. * 获取佣金排行
  59. * @param array $where
  60. * @param int $page
  61. * @param int $limit
  62. * @return array
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function brokerageRankList(array $where, int $page = 0, int $limit = 0)
  68. {
  69. //SUM(IF(pm=1,`number`,-`number`))
  70. if ($where['pm'] == 1) $where['not_type'] = ['extract_fail'];
  71. return $this->search($where)->field('uid,SUM(number) as brokerage_price')->with(['user' => function ($query) {
  72. $query->field('uid,avatar,nickname');
  73. }])->order('brokerage_price desc')->group('uid')->when($page && $limit, function ($query) use ($page, $limit) {
  74. $query->page($page, $limit);
  75. })->select()->toArray();
  76. }
  77. /**
  78. * 获取某些条件的佣金总数
  79. * @param array $where
  80. * @return mixed
  81. * @throws \ReflectionException
  82. */
  83. public function getBrokerageSumColumn(array $where)
  84. {
  85. if ($where['pm'] == 1) $where['not_type'] = ['extract_fail'];
  86. if (isset($where['uid']) && is_array($where['uid'])) {
  87. return $this->search($where)->group('uid')->column('sum(number) as num', 'uid');
  88. } else
  89. return $this->search($where)->sum('number');
  90. }
  91. /**
  92. * 获取某个账户下的冻结佣金
  93. * @param int $uid
  94. * @return float
  95. * @throws \ReflectionException
  96. */
  97. public function getUserFrozenPrice(int $uid)
  98. {
  99. return $this->search(['uid' => $uid, 'status' => 1, 'pm' => 1])->where('frozen_time', '>', time())->sum('number');
  100. }
  101. }