Team.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\LedgerPowerChangeModel;
  5. use app\common\model\LedgerSmhChangeModel;
  6. use app\common\model\LedgerTokenChangeModel;
  7. use app\common\model\LedgerUsdtChangeModel;
  8. use app\common\model\OfflineRechargeRecordModel;
  9. use app\common\model\UserModel;
  10. use app\common\model\UserPathModel;
  11. use fast\Action;
  12. use fast\Date;
  13. use think\Db;
  14. use think\Model;
  15. class Team extends Api
  16. {
  17. /**
  18. * 团队算力收益的Action
  19. * @var array
  20. */
  21. private array $actionPower = [Action::PowerTeamAward, Action::PowerEqualAward];
  22. /**
  23. * 团队USDT收益的Action
  24. * @var array
  25. */
  26. private array $actionUsdt = [Action::UsdtWeightDividend];
  27. /**
  28. * 团队虚拟币收益的Action
  29. * @var array
  30. */
  31. private array $actionSmh = [Action::TokenAllocateEtc];
  32. /**
  33. * 团队页的几项数据
  34. * @return void
  35. */
  36. public function all()
  37. {
  38. $user = $this->auth->getUser();
  39. $resp = [
  40. 'team_power' => $user['team_power'],
  41. 'team_num' => $user['team_num'],
  42. 'direct_num' => $user['direct_num'],
  43. 'yesterday_profit' => $this->getYesterdayProfit($user['id']),
  44. ];
  45. $resp['list'] = (new UserModel())
  46. ->field('id,address,team_power,team_num,create_time')
  47. ->where('parent_id', $user['id'])
  48. ->order('id desc')
  49. ->select();
  50. foreach ($resp['list'] as $key => $item){
  51. //统计团队人数
  52. $resp['list'][$key]['user_power'] = (new OfflineRechargeRecordModel())
  53. ->where('user_id', $item['id'])
  54. ->where('status', OfflineRechargeRecordModel::StatusSuccess)
  55. ->sum('power');
  56. $resp['list'][$key]['address'] = substr($item['address'], 0, 4) . '****' . substr($item['address'], -4);
  57. unset($resp['list'][$key]['id']);
  58. }
  59. $this->success('', $resp);
  60. }
  61. /**
  62. * 获取用户昨日团队收益
  63. * @param int $uid 用户ID
  64. * @return array
  65. */
  66. private function getYesterdayProfit(int $uid): array
  67. {
  68. $yesterdayStart = Date::unixtime('day', -1); // 昨日0点0分0秒
  69. $yesterdayEnd = Date::unixtime() - 1; // 昨日23点59分59秒
  70. // 算力
  71. $sumPoser = (new LedgerPowerChangeModel())
  72. ->where('user_id', $uid)
  73. //->where('action', 'IN', $this->actionPower)
  74. ->where('create_time', ['>=', $yesterdayStart], ['<=', $yesterdayEnd], 'AND')
  75. ->where('change_amount', '>', 0)
  76. ->sum('change_amount');
  77. // USDT
  78. // $sumUsdt = (new LedgerUsdtChangeModel())
  79. // ->where('user_id', $uid)
  80. // ->where('action', 'IN', $this->actionUsdt)
  81. // ->where('create_time', ['>=', $yesterdayStart], ['<=', $yesterdayEnd], 'AND')
  82. // ->where('change_amount', '>', 0)
  83. // ->sum('change_amount');
  84. // SMH
  85. $sumSmh = (new LedgerSmhChangeModel())
  86. ->where('user_id', $uid)
  87. ->where('create_time', ['>=', $yesterdayStart], ['<=', $yesterdayEnd], 'AND')
  88. ->where('change_amount', '>', 0)
  89. ->sum('change_amount');
  90. //昨日团队业绩
  91. $yesterdayTeamPower = DB::table('team_rewards')
  92. ->where('user_id', $uid)
  93. ->where('date_time', date('Y-m-d', strtotime('-1 day')))
  94. ->value('team_power');
  95. // 转成字符串型
  96. return [
  97. 'power' => strval($sumPoser),
  98. //'usdt' => strval($sumUsdt),
  99. 'smh' => strval($sumSmh),
  100. 'team_power' => strval($yesterdayTeamPower) ? '' : 0,
  101. ];
  102. }
  103. /**
  104. * 团队收益列表
  105. * @return void
  106. */
  107. public function profitList()
  108. {
  109. $type = $this->request->post('query.type'); // 资产类型
  110. $model = new LedgerPowerChangeModel();
  111. $action = [];
  112. switch ($type) {
  113. case 1:
  114. $model = new LedgerPowerChangeModel();
  115. $action = $this->actionPower;
  116. break;
  117. case 2:
  118. $model = new LedgerUsdtChangeModel();
  119. $action = $this->actionUsdt;
  120. break;
  121. case 3:
  122. $model = new LedgerTokenChangeModel();
  123. $action = $this->actionToken;
  124. break;
  125. default:
  126. $this->error('资产类型错误');
  127. break;
  128. }
  129. $where = [
  130. 'user_id' => $this->auth->getTokenUserID(),
  131. 'action' => ['IN', $action],
  132. ];
  133. $paginator = $model->where($where)->order('id DESC')->paginate($this->pageSize);
  134. $this->success('', $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items()));
  135. }
  136. }