| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\MemberModel;
- use App\Services\BaseService;
- use App\Services\CregisPayService;
- use App\Services\RedisService;
- /**
- * 团队-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api\
- */
- class TeamService extends BaseService
- {
- protected static $instance;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new MemberModel();
- }
- /**
- * 静态入口
- */
- public static function make(){
- if(!self::$instance){
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 团队列表(直推)
- * @param $userId
- * @param int $page
- * @param int $pageSize
- * @return array
- */
- public function getDataList($userId, $page=1, $pageSize=10)
- {
- $cacheKey = "caches:team:list_{$userId}_{$page}_{$pageSize}";
- $datas = RedisService::get($cacheKey);
- if ($datas) {
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($datas['total']) ? $datas['total'] : 0,
- 'list' => isset($datas['data']) ? $datas['data'] : []
- ];
- }
- $where = ['status' => 1, 'mark' => 1];
- $field = ['id', 'nickname', 'usdt','sbt', 'parent_id','profit','performance','bonus_rate','bonus_performance','team_bonus_performance','wallet_url', 'status'];
- $datas = $this->model->where($where)
- ->where(function ($query) use ($userId) {
- $query->where('parent_id', $userId);
- })
- ->select($field)
- ->orderBy('create_time', 'desc')
- ->orderBy('id', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $datas = $datas ? $datas->toArray() : [];
- if ($datas) {
- foreach ($datas['data'] as &$item) {
- $item['wallet_url'] = $item['wallet_url'] ? format_str($item['wallet_url']) : '';
- $item['is_online'] = RedisService::get("auths:info:".$item['id'])? 1: 0;
- $item['old_bonus_performance'] = $item['bonus_performance'];
- $item['bonus_performance'] = $item['team_bonus_performance'];
- }
- unset($item);
- RedisService::set($cacheKey, $datas, rand(5, 10));
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($datas['total']) ? $datas['total'] : 0,
- 'list' => isset($datas['data']) ? $datas['data'] : []
- ];
- }
- /**
- * 用户团队信息
- * @param int $userId 用户ID
- * @return array|mixed
- */
- public function getInfo(int $userId, $type=1)
- {
- $cacheKey = "caches:team:info_{$userId}_{$type}";
- $info = RedisService::get($cacheKey);
- if($info){
- return $info;
- }
- $info = $this->model->with(['parent'])->where(['id'=> $userId,'mark'=>1])
- ->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'])
- ->first();
- $info = $info? $info->toArray() : [];
- if(empty($info)){
- $this->error = 2016;
- return false;
- }
- $info = [
- 'id'=> $userId,
- 'wallet_url'=> $info['wallet_url'],
- 'member_level'=> intval($info['member_level']),
- 'usdt'=> moneyFormat($info['usdt'],2),
- 'sbt'=> moneyFormat($info['sbt'],2),
- 'performance'=> moneyFormat($info['performance'],2),
- 'team_performance'=> 0,
- 'profit'=> moneyFormat($info['profit'],2),
- 'profit_total'=> moneyFormat($info['profit_total'],2),
- 'pledge_total'=> moneyFormat($info['pledge_profit'],2),
- 'share_profit'=> moneyFormat($info['share_profit'],2),
- 'manage_profit'=> moneyFormat($info['manage_profit'],2),
- 'global_profit'=> moneyFormat($info['global_profit'],2),
- 'pj_profit'=> moneyFormat($info['pj_profit'],2),
- 'dapp_url'=> get_web_url('/#/?scode='.$info['code']),
- 'code'=> $info['code'],
- 'nickname'=> $info['nickname'],
- 'parent_id'=> $info['parent_id'],
- 'bonus_rate'=> $info['bonus_rate'],
- 'share_count'=> 0,
- 'today_share_count'=> 0,
- 'next_level_performance'=> 0,
- ];
- if($type == 1){
- $info['share_count'] = $this->getTeamUserCountByType($userId, 2); //
- $info['today_share_count'] = $this->getTeamUserCountByType($userId, 3);
- // 下一等级业绩,团队业绩
- $nextLevelData = MemberService::make()->getLevelData($info['member_level'], 2);
- $upgradeUsdt = isset($nextLevelData['upgrade_usdt']) && $nextLevelData['upgrade_usdt']>0? floatval($nextLevelData['upgrade_usdt']) : 0;
- $teamPerformance = $this->getTeamPerformanceByType($userId, 2);
- $info['team_performance'] = moneyFormat($teamPerformance + $info['performance'],1);
- $info['next_level'] = isset($nextLevelData['id'])? $nextLevelData['id'] : 0;
- $info['next_level_performance'] = max(0, $upgradeUsdt - floatval($info['team_performance']));
- }else{
- if($info['recharge_url'] && $info['recharge_token']){
- $info['error'] = ($info['recharge_token'] != make_wallet_token($info['recharge_url'], $userId))? '钱包地址已被修改': '';
- }else if(empty($info['recharge_url'])){
- $info['recharge_url'] = CregisPayService::make()->createAddress(1000);
- if($info['recharge_url']){
- $rechargeToken = make_wallet_token($info['recharge_url'], $userId);
- $this->model->where(['id'=>$userId])->update(['recharge_url'=> $info['recharge_url'],'recharge_token'=>$rechargeToken,'update_time'=>time()]);
- }
- }
- }
- unset($info['recharge_token']);
- RedisService::set($cacheKey, $info, rand(5,10));
- return $info;
- }
- /**
- * 获取下级团队/直推用户数量(未包括自身,团队业绩需额外加上自身业绩)
- * @param $userId 当前用户ID
- * @param int $type 类型:1-直推,2-团队,3-今日直推推荐人数
- * @return array|mixed
- */
- public function getTeamUserCountByType($userId, $type = 1, $status=1)
- {
- $cacheKey = "caches:team:count_{$userId}_{$type}_{$status}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- $where = ['status'=>1,'mark' => 1];
- if($status){
- $where['status'] = $status;
- }else {
- unset($where['status']);
- }
- $data = $this->model->where($where)
- ->where(function ($query) use ($type, $userId) {
- if ($type == 1) { // 直推人数
- $query->where('parent_id', $userId);
- } else if($type == 2){ // 团队人数
- $query->whereRaw("FIND_IN_SET({$userId},parent_ids)");
- } else if($type == 3){ // 今日推荐人数
- $query->where('parent_id', $userId)->where('create_time','>=', strtotime(date('Y-m-d')));
- }
- })->count();
- if ($data) {
- RedisService::set($cacheKey, $data, rand(5, 10));
- }
- return $data;
- }
- /**
- * 获取团队/直推用户总业绩
- * @param $userId 当前用户ID
- * @param int $type 类型:1-直推,2-团队
- * @return array|mixed
- */
- public function getTeamPerformanceByType($userId, $type = 1, $refresh=false)
- {
- $cacheKey = "caches:team:performance_{$userId}_{$type}";
- $data = RedisService::get($cacheKey);
- if ($data && !$refresh) {
- return $data;
- }
- $where = ['status' => 1, 'mark' => 1];
- $data = $this->model->where($where)
- ->where(function ($query) use ($type, $userId) {
- if ($type == 1) { // 直推的
- $query->where('parent_id', $userId);
- } else if($type == 2){ // 团队
- $query->whereRaw("FIND_IN_SET({$userId},parent_ids)");
- }
- })->sum('performance');
- if ($data) {
- RedisService::set($cacheKey, $data, rand(3, 5));
- }
- return $data? floatval($data) : 0;
- }
- /**
- * 获取全网所有该等级的团队业绩
- * @param $userId 当前用户ID
- * @param int $refresh 是否刷新:true-是
- * @return array|mixed
- */
- public function getTeamPerformanceByLevel($level = 0, $refresh=false)
- {
- $cacheKey = "caches:team:global_{$level}";
- $data = RedisService::get($cacheKey);
- if ($data && !$refresh) {
- return $data;
- }
- $users = $this->model->where(['member_level'=>$level,'mark'=>1])->select(['id','performance'])->get();
- $users = $users? $users->toArray() : [];
- $globalPerformance = 0;
- if($users){
- foreach ($users as $item){
- $uid = isset($item['id'])? $item['id'] : 0;
- $performance = isset($item['performance'])? $item['performance'] : 0;
- $teamPerformance = $this->getTeamPerformanceByType($uid, 2, true);
- $teamPerformance = round($teamPerformance + $performance, 2); // 团队业绩
- $globalPerformance += $teamPerformance;
- }
- }
- if ($globalPerformance) {
- RedisService::set($cacheKey, $globalPerformance, rand(10, 20));
- }
- return $globalPerformance? round($globalPerformance, 2) : 0;
- }
- /**
- * 获取最近到平级推荐人
- * @param int $level 当前用户等级
- * @param array $parentIds 推荐人ID数组(由下到上)
- * @return array
- */
- public function getUpperLevelUser(int $level, array $parentIds)
- {
- $info = $this->model->whereIn('id', $parentIds)
- ->where(['member_level'=>$level,'mark'=>1])
- ->select(['id','usdt','member_level','profit','profit_total','sbt','status'])
- ->orderByRaw('FIELD(id, "' . implode(',', $parentIds) . '")')
- ->first();
- return $info? $info->toArray() : [];
- }
- }
|