CommonLogic.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\api\logic;
  3. use Exception;
  4. use think\Env;
  5. use fast\Asset;
  6. use think\Loader;
  7. use app\common\model\UserModel;
  8. use app\common\model\UserPledge;
  9. use app\common\model\UserPathModel;
  10. use app\common\model\LedgerWalletModel;
  11. use app\common\model\LedgerTokenChangeModel;
  12. use app\common\model\LedgerFrozenChangeModel;
  13. //公共
  14. class CommonLogic
  15. {
  16. // 升级
  17. public static function setIsUpLevel(int $user_id, int $team_level_id, int $address_level, int $parent_id): bool
  18. {
  19. $config = config('team_level_where');
  20. if($team_level_id == count($config)) return false;
  21. $result = false;
  22. if($team_level_id == 0){
  23. if($address_level >= $config[$team_level_id]['direct']) {
  24. $result = true;
  25. $team_level_id += 1;
  26. self::setIsUpLevelParent($user_id, $config);
  27. }
  28. }else{
  29. //组合套数
  30. $groupCount = UserPledge::getPledgeCount($user_id) + 1;
  31. //直推
  32. $teamCount = UserModel::where('parent_id', $user_id)->where('team_level_id', $team_level_id)->count();
  33. if($teamCount >= $config[$team_level_id]['direct'] && $groupCount >= $config[$team_level_id]['num']){
  34. $result = true;
  35. $team_level_id += 1; //+1
  36. self::setIsUpLevelParent($user_id, $config);
  37. }
  38. }
  39. return $result? UserModel::where('id', $user_id)->update(['team_level_id'=> $team_level_id]): false;
  40. }
  41. //上级升级
  42. public static function setIsUpLevelParent(int $user_id, array $config)
  43. {
  44. $pathId = UserPathModel::where('user_id', $user_id)->order('distance', 'asc')->column('parent_id');
  45. $userModel = Loader::model('UserModel');
  46. $userPledge = Loader::model('UserPledge');
  47. foreach ($pathId as $item)
  48. {
  49. $parent = $userModel::where('parent_id', $item)->find();
  50. if($parent->team_level_id > 0){
  51. //推广人数
  52. $teamCount = $userModel::where('parent_id', $item)->where('team_level_id', $parent->team_level_id)->count() +1;
  53. //组合套数
  54. $groupCount = $userPledge::getPledgeCount($item);
  55. if($teamCount >= $config[$parent->team_level_id]['direct'] && $groupCount >= $config[$parent->team_level_id]['num']){
  56. $parent->team_level_id +=1;
  57. $parent->save();
  58. }
  59. }
  60. }
  61. }
  62. //降级
  63. public static function setIsOuLevelSave($user_id, $team_level_id, $address_level, $parent_id)
  64. {
  65. $config = config('team_level_where');
  66. $result = false;
  67. //组合套数
  68. $groupCount = UserPledge::getPledgeCount($user_id) -1 ;
  69. if($team_level_id == 1 && ($groupCount == 0 || $address_level < $config[0]['direct'])){
  70. $result = true;
  71. $team_level_id = 0;
  72. self::setIsOuLevelParent($parent_id, $config);
  73. }
  74. if($team_level_id > 1){
  75. //直推
  76. $teamCount = UserModel::where('parent_id', $user_id)->where('team_level_id', $team_level_id)->count();
  77. if($teamCount < $config[$team_level_id]['direct'] && $groupCount < $config[$team_level_id]['num']){
  78. $result = true;
  79. $team_level_id -= 1;
  80. self::setIsOuLevelParent($parent_id, $config);
  81. }
  82. }
  83. return $result? UserModel::where('id', $user_id)->update(['team_level_id'=> $team_level_id]): false;
  84. }
  85. //上级降低级
  86. public static function setIsOuLevelParent(int $user_id, array $config){
  87. $pathId = UserPathModel::where('user_id', $user_id)->order('distance', 'asc')->column('parent_id');
  88. $userModel = Loader::model('UserModel');
  89. $userPledge= Loader::model('UserPledge');
  90. foreach ($pathId as $item)
  91. {
  92. $parent = $userModel::where('parent_id', $item)->find();
  93. if($parent->team_level_id > 0){
  94. //推广人数
  95. $teamCount = $userModel::where('parent_id', $item)->where('team_level_id', $parent->team_level_id)->count()-1;
  96. //组合套数
  97. $groupCount = $userPledge::getPledgeCount($item);
  98. if($teamCount < $config[$parent->team_level_id]['direct'] || $groupCount < $config[$parent->team_level_id]['num']){
  99. $parent->team_level_id -=1;
  100. $parent->save();
  101. }
  102. }
  103. }
  104. }
  105. //发放层级收益
  106. public static function setTeamLevelIncome(int $user_id, float $num, int $team_level_id, string $asset, string $action)
  107. {
  108. $config = config('team_level_where');
  109. $team_level_id = $team_level_id - 1;
  110. $paths = UserPathModel::where('user_id', $user_id)->where('distance', '<=', $config[$team_level_id]['level'])->order('distance', 'asc')->column('parent_id');
  111. $income = bcmul($num, config('team_level_inc'), 2); //收益
  112. if(count($paths) > 0 && $income > 0){
  113. //余额记录
  114. foreach($paths as $pathId){
  115. if(!empty(Loader::model('UserModel')::getTeamLevelId($pathId))) {
  116. Loader::model('LedgerWalletModel')->changeWalletAccount($pathId, $asset, $income, $action, $user_id);
  117. }
  118. }
  119. }
  120. return true;
  121. }
  122. //判断发放茶宝数量
  123. public static function getFrozenIsChabao($token, $frozen){
  124. //标记茶宝
  125. if(bccomp(bcmul($frozen, config('team_level_inc'), 6) , 0.0001, 6) == -1)
  126. {
  127. return [$token, Asset::TOKEN, LedgerTokenChangeModel::TeamLevel];
  128. }else{
  129. //标记茶宝
  130. return [$frozen, Asset::FROZEN, LedgerFrozenChangeModel::TeamLevel];
  131. }
  132. }
  133. }