TeamService.php 11 KB

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