UserBrokerageDao.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. return $this->search($where)->field('uid,SUM(number) as brokerage_price')->with(['user' => function ($query) {
  71. $query->field('uid,avatar,nickname');
  72. }])->order('brokerage_price desc')->group('uid')->when($page && $limit, function ($query) use ($page, $limit) {
  73. $query->page($page, $limit);
  74. })->select()->toArray();
  75. }
  76. /**
  77. * 获取某些条件的佣金总数
  78. * @param array $where
  79. * @return mixed
  80. * @throws \ReflectionException
  81. */
  82. public function getBrokerageSumColumn(array $where)
  83. {
  84. if (isset($where['uid']) && is_array($where['uid'])) {
  85. return $this->search($where)->group('uid')->column('sum(number) as num', 'uid');
  86. } else
  87. return $this->search($where)->sum('number');
  88. }
  89. /**
  90. * 获取某个账户下的冻结佣金
  91. * @param int $uid
  92. * @return float
  93. * @throws \ReflectionException
  94. */
  95. public function getUserFrozenPrice(int $uid)
  96. {
  97. return $this->search(['uid' => $uid, 'status' => 1, 'pm' => 1])->where('frozen_time', '>', time())->sum('number');
  98. }
  99. }