| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace app\api\logic;
- use Exception;
- use think\Env;
- use fast\Asset;
- use think\Loader;
- use app\common\model\UserModel;
- use app\common\model\UserPledge;
- use app\common\model\UserPathModel;
- use app\common\model\LedgerWalletModel;
- use app\common\model\LedgerTokenChangeModel;
- use app\common\model\LedgerFrozenChangeModel;
- //公共
- class CommonLogic
- {
- // 升级
- public static function setIsUpLevel(int $user_id, int $team_level_id): bool
- {
- $config = config('team_level_where');
- if($team_level_id == count($config)) return false;
- $result = false;
- //组合套数
- $groupCount = UserPledge::getPledgeCount($user_id) ;
- if($team_level_id == 0){
- if($groupCount >= $config[$team_level_id]['num']) {
- $result = true;
- $team_level_id += 1;
- self::setIsUpLevelParent($user_id, $config);
- }
- }else{
- //直推
- $teamCount = UserModel::where('parent_id', $user_id)->where('team_level_id','>=', $team_level_id)->count();
- if($teamCount >= $config[$team_level_id]['direct'] && $groupCount >= $config[$team_level_id]['num']){
- $result = true;
- $team_level_id += 1; //+1
- self::setIsUpLevelParent($user_id, $config);
- }
- }
- return $result? UserModel::where('id', $user_id)->update(['team_level_id'=> $team_level_id]): false;
- }
- //上级升级
- public static function setIsUpLevelParent(int $user_id, array $config)
- {
- $pathId = UserPathModel::where('user_id', $user_id)->order('distance', 'asc')->column('parent_id');
- $userModel = Loader::model('UserModel');
- $userPledge = Loader::model('UserPledge');
- foreach ($pathId as $item)
- {
- $parent = $userModel::where('id', $item)->find();
- if($parent->team_level_id > 0 && $parent->team_level_id < 3){
- //推广人数
- $teamCount = $userModel::where('parent_id', $item)->where('team_level_id', '>=',$parent->team_level_id)->count() +1;
- //组合套数
- $groupCount = $userPledge::getPledgeCount($item);
- if($teamCount >= $config[$parent->team_level_id]['direct'] && $groupCount >= $config[$parent->team_level_id]['num']){
- $parent->team_level_id +=1;
- $parent->save();
- }
- }
- }
- }
- //降级
- public static function setIsOuLevelSave($user_id, $team_level_id)
- {
- $config = config('team_level_where');
- $result = false;
- //组合套数
- $groupCount = UserPledge::getPledgeCount($user_id)-1 ;
- if($team_level_id == 1 && $groupCount < $config[0]['num']){
- $result = true;
- $team_level_id = 0;
- self::setIsOuLevelParent($user_id, $config);
- }
- if($team_level_id > 1){
- //直推
- $teamCount = UserModel::where('parent_id', $user_id)->where('team_level_id','>=', $team_level_id)->count()-1;
- if($teamCount < $config[$team_level_id-1]['direct'] && $groupCount < $config[$team_level_id-1]['num']){
- $result = true;
- $team_level_id -= 1;
- self::setIsOuLevelParent($user_id, $config);
- }
- }
- return $result? UserModel::where('id', $user_id)->update(['team_level_id'=> $team_level_id]): false;
- }
- //上级降低级
- public static function setIsOuLevelParent(int $user_id, array $config){
- $pathId = UserPathModel::where('user_id', $user_id)->order('distance', 'asc')->column('parent_id');
- $userModel = Loader::model('UserModel');
- $userPledge= Loader::model('UserPledge');
- foreach ($pathId as $item)
- {
- $parent = $userModel::where('id', $item)->find();
- if($parent->team_level_id > 0){
- //推广人数
- $teamCount = $userModel::where('parent_id', $item)->where('team_level_id', '>=',$parent->team_level_id)->count()-1;
- //组合套数
- $groupCount = $userPledge::getPledgeCount($item);
- if($teamCount < $config[$parent->team_level_id-1]['direct'] || $groupCount < $config[$parent->team_level_id-1]['num']){
- $parent->team_level_id -=1;
- $parent->save();
- }
- }
- }
- }
- //发放层级收益
- public static function setTeamLevelIncome(int $user_id, float $num,string $asset, string $action)
- {
- $income = bcmul($num, config('team_level_inc'), 6); //收益
- $parents_info = UserPathModel::where('p.user_id', $user_id)
- ->alias('p')
- ->join('user u', 'u.id = p.parent_id', 'left')
- ->field('u.id, u.address_level, u.team_level_id,u.parent_id, p.distance')
- ->where('u.team_level_id', '>', 0)
- ->order('p.distance asc')
- ->select();
- if(empty($parents_info) || empty($income)) return true;
-
- //分享 1 个拿 1 层,分享 2个拿 2 层
- foreach ($parents_info as $parent){
- $send_user_id = 0; //需要发放的会员ID列表
- if($parent['team_level_id'] >= 1 && $parent['distance'] <= 1){
- $send_user_id = $parent['id'];
- }else if($parent['team_level_id'] >= 2 && $parent['distance'] <= 2){
- $send_user_id = $parent['id'];
- }else if($parent['team_level_id'] >= 3 && $parent['distance'] <= 3){
- $send_user_id = $parent['id'];
- }
- if($send_user_id > 0) Loader::model('LedgerWalletModel')->changeWalletAccount($send_user_id, $asset, $income, $action, $user_id);
- }
- return true;
- }
- //判断发放茶宝数量
- public static function getFrozenIsChabao($token, $frozen){
- //标记茶宝
- if(bccomp(bcmul($frozen, config('team_level_inc'), 6) , 0.0001, 6) == -1)
- {
- return [$token, Asset::TOKEN, LedgerTokenChangeModel::TeamLevel];
- }else{
- //标记茶宝
- return [$frozen, Asset::FROZEN, LedgerFrozenChangeModel::TeamLevel];
- }
- }
-
- }
|