TeamService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\MemberModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 团队-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * @package App\Services\Api\
  20. */
  21. class TeamService extends BaseService
  22. {
  23. protected static $instance;
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new MemberModel();
  32. }
  33. /**
  34. * 静态入口
  35. */
  36. public static function make(){
  37. if(!self::$instance){
  38. self::$instance = new static();
  39. }
  40. return self::$instance;
  41. }
  42. /**
  43. * 团队列表(直推)
  44. * @param $userId
  45. * @param int $page
  46. * @param int $pageSize
  47. * @return array
  48. */
  49. public function getDataList($userId, $page=1, $pageSize=10)
  50. {
  51. $cacheKey = "caches:team:list_{$userId}_{$page}_{$pageSize}";
  52. $datas = RedisService::get($cacheKey);
  53. if ($datas) {
  54. return [
  55. 'pageSize' => $pageSize,
  56. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  57. 'list' => isset($datas['data']) ? $datas['data'] : []
  58. ];
  59. }
  60. $where = ['status' => 1, 'mark' => 1];
  61. $field = ['id', 'nickname', 'usdt','sbt', 'parent_id','profit','performance','bonus_rate','bonus_performance','team_bonus_performance','wallet_url', 'status'];
  62. $datas = $this->model->where($where)
  63. ->where(function ($query) use ($userId) {
  64. $query->where('parent_id', $userId);
  65. })
  66. ->select($field)
  67. ->orderBy('create_time', 'desc')
  68. ->orderBy('id', 'desc')
  69. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  70. $datas = $datas ? $datas->toArray() : [];
  71. if ($datas) {
  72. foreach ($datas['data'] as &$item) {
  73. $item['wallet_url'] = $item['wallet_url'] ? format_str($item['wallet_url']) : '';
  74. $item['is_online'] = RedisService::get("auths:info:".$item['id'])? 1: 0;
  75. $item['old_bonus_performance'] = $item['bonus_performance'];
  76. $item['bonus_performance'] = $item['team_bonus_performance'];
  77. }
  78. unset($item);
  79. RedisService::set($cacheKey, $datas, rand(5, 10));
  80. }
  81. return [
  82. 'pageSize' => $pageSize,
  83. 'total' => isset($datas['total']) ? $datas['total'] : 0,
  84. 'list' => isset($datas['data']) ? $datas['data'] : []
  85. ];
  86. }
  87. /**
  88. * 用户团队信息
  89. * @param int $userId 用户ID
  90. * @return array|mixed
  91. */
  92. public function getInfo(int $userId, $type=1)
  93. {
  94. $cacheKey = "caches:team:info_{$userId}_{$type}";
  95. $info = RedisService::get($cacheKey);
  96. if($info){
  97. return $info;
  98. }
  99. $info = $this->model->with(['parent'])->where(['id'=> $userId,'mark'=>1])
  100. ->select(['id','nickname','member_level','code','usdt','sbt','performance','profit','profit_total','pledge_profit','manage_profit','share_profit','pj_profit','global_profit','parent_id','wallet_url','recharge_url','bonus_rate','trade_status','status'])
  101. ->first();
  102. $info = $info? $info->toArray() : [];
  103. if(empty($info)){
  104. $this->error = 2016;
  105. return false;
  106. }
  107. $info = [
  108. 'id'=> $userId,
  109. 'wallet_url'=> $info['wallet_url'],
  110. 'member_level'=> intval($info['member_level']),
  111. 'usdt'=> moneyFormat($info['usdt'],2),
  112. 'sbt'=> moneyFormat($info['sbt'],2),
  113. 'performance'=> moneyFormat($info['performance'],2),
  114. 'team_performance'=> 0,
  115. 'profit'=> moneyFormat($info['profit'],2),
  116. 'profit_total'=> moneyFormat($info['profit_total'],2),
  117. 'pledge_total'=> moneyFormat($info['pledge_profit'],2),
  118. 'share_profit'=> moneyFormat($info['share_profit'],2),
  119. 'manage_profit'=> moneyFormat($info['manage_profit'],2),
  120. 'global_profit'=> moneyFormat($info['global_profit'],2),
  121. 'pj_profit'=> moneyFormat($info['pj_profit'],2),
  122. 'dapp_url'=> get_web_url('/#/?scode='.$info['code']),
  123. 'code'=> $info['code'],
  124. 'nickname'=> $info['nickname'],
  125. 'parent_id'=> $info['parent_id'],
  126. 'bonus_rate'=> $info['bonus_rate'],
  127. 'share_count'=> 0,
  128. 'today_share_count'=> 0,
  129. 'next_level_performance'=> 0,
  130. ];
  131. if($type == 1){
  132. $info['share_count'] = $this->getTeamUserCountByType($userId, 1); //
  133. $info['today_share_count'] = $this->getTeamUserCountByType($userId, 3);
  134. // 下一等级业绩,团队业绩
  135. $nextLevelData = MemberService::make()->getLevelData($info['member_level'], 2);
  136. $upgradeUsdt = isset($nextLevelData['upgrade_usdt']) && $nextLevelData['upgrade_usdt']>0? floatval($nextLevelData['upgrade_usdt']) : 0;
  137. $teamPerformance = $this->getTeamPerformanceByType($userId, 2);
  138. $info['team_performance'] = moneyFormat($teamPerformance + $info['performance'],1);
  139. $info['next_level'] = isset($nextLevelData['id'])? $nextLevelData['id'] : 0;
  140. $info['next_level_performance'] = max(0, $upgradeUsdt - floatval($info['team_performance']));
  141. }
  142. RedisService::set($cacheKey, $info, rand(5,10));
  143. return $info;
  144. }
  145. /**
  146. * 获取下级团队/直推用户数量(未包括自身,团队业绩需额外加上自身业绩)
  147. * @param $userId 当前用户ID
  148. * @param int $type 类型:1-直推,2-团队,3-今日直推推荐人数
  149. * @return array|mixed
  150. */
  151. public function getTeamUserCountByType($userId, $type = 1, $status=1)
  152. {
  153. $cacheKey = "caches:team:count_{$userId}_{$type}_{$status}";
  154. $data = RedisService::get($cacheKey);
  155. if ($data) {
  156. return $data;
  157. }
  158. $where = ['status'=>1,'mark' => 1];
  159. if($status){
  160. $where['status'] = $status;
  161. }else {
  162. unset($where['status']);
  163. }
  164. $data = $this->model->where($where)
  165. ->where(function ($query) use ($type, $userId) {
  166. if ($type == 1) { // 直推人数
  167. $query->where('parent_id', $userId);
  168. } else if($type == 2){ // 团队人数
  169. $query->whereRaw("FIND_IN_SET({$userId},parent_ids)");
  170. } else if($type == 3){ // 今日推荐人数
  171. $query->where('parent_id', $userId)->where('create_time','>=', strtotime(date('Y-m-d')));
  172. }
  173. })->count();
  174. if ($data) {
  175. RedisService::set($cacheKey, $data, rand(5, 10));
  176. }
  177. return $data;
  178. }
  179. /**
  180. * 获取团队/直推用户总业绩
  181. * @param $userId 当前用户ID
  182. * @param int $type 类型:1-直推,2-团队
  183. * @return array|mixed
  184. */
  185. public function getTeamPerformanceByType($userId, $type = 1, $refresh=false)
  186. {
  187. $cacheKey = "caches:team:performance_{$userId}_{$type}";
  188. $data = RedisService::get($cacheKey);
  189. if ($data && !$refresh) {
  190. return $data;
  191. }
  192. $where = ['status' => 1, 'mark' => 1];
  193. $data = $this->model->where($where)
  194. ->where(function ($query) use ($type, $userId) {
  195. if ($type == 1) { // 直推的
  196. $query->where('parent_id', $userId);
  197. } else if($type == 2){ // 团队
  198. $query->whereRaw("FIND_IN_SET({$userId},parent_ids)");
  199. }
  200. })->sum('performance');
  201. if ($data) {
  202. RedisService::set($cacheKey, $data, rand(3, 5));
  203. }
  204. return $data? floatval($data) : 0;
  205. }
  206. /**
  207. * 获取全网所有该等级的团队业绩
  208. * @param $userId 当前用户ID
  209. * @param int $refresh 是否刷新:true-是
  210. * @return array|mixed
  211. */
  212. public function getTeamPerformanceByLevel($level = 0, $refresh=false)
  213. {
  214. $cacheKey = "caches:team:global_{$level}";
  215. $data = RedisService::get($cacheKey);
  216. if ($data && !$refresh) {
  217. return $data;
  218. }
  219. $users = $this->model->where(['member_level'=>$level,'mark'=>1])->select(['id','performance'])->get();
  220. $users = $users? $users->toArray() : [];
  221. $globalPerformance = 0;
  222. if($users){
  223. foreach ($users as $item){
  224. $uid = isset($item['id'])? $item['id'] : 0;
  225. $performance = isset($item['performance'])? $item['performance'] : 0;
  226. $teamPerformance = $this->getTeamPerformanceByType($uid, 2, true);
  227. $teamPerformance = round($teamPerformance + $performance, 2); // 团队业绩
  228. $globalPerformance += $teamPerformance;
  229. }
  230. }
  231. if ($globalPerformance) {
  232. RedisService::set($cacheKey, $globalPerformance, rand(10, 20));
  233. }
  234. return $globalPerformance? round($globalPerformance, 2) : 0;
  235. }
  236. /**
  237. * 获取最近到平级推荐人
  238. * @param int $level 当前用户等级
  239. * @param array $parentIds 推荐人ID数组(由下到上)
  240. * @return array
  241. */
  242. public function getUpperLevelUser(int $level, array $parentIds)
  243. {
  244. $info = $this->model->whereIn('id', $parentIds)
  245. ->where(['member_level'=>$level,'mark'=>1])
  246. ->select(['id','usdt','member_level','profit','profit_total','sbt','status'])
  247. ->orderByRaw('FIELD(id, "' . implode(',', $parentIds) . '")')
  248. ->first();
  249. return $info? $info->toArray() : [];
  250. }
  251. }