CommonLogic.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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): bool
  18. {
  19. $config = config('team_level_where');
  20. if($team_level_id == count($config)) return false;
  21. $result = false;
  22. //组合套数
  23. $groupCount = UserPledge::getPledgeCount($user_id) ;
  24. if($team_level_id == 0){
  25. if($groupCount >= $config[$team_level_id]['num']) {
  26. $result = true;
  27. $team_level_id += 1;
  28. self::setIsUpLevelParent($user_id, $config);
  29. }
  30. }else{
  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('id', $item)->find();
  50. if($parent->team_level_id > 0 && $parent->team_level_id < 3){
  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)
  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 < $config[0]['num']){
  70. $result = true;
  71. $team_level_id = 0;
  72. self::setIsOuLevelParent($user_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()-1;
  77. if($teamCount < $config[$team_level_id-1]['direct'] && $groupCount < $config[$team_level_id-1]['num']){
  78. $result = true;
  79. $team_level_id -= 1;
  80. self::setIsOuLevelParent($user_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('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-1]['direct'] || $groupCount < $config[$parent->team_level_id-1]['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,string $asset, string $action)
  107. {
  108. $income = bcmul($num, config('team_level_inc'), 6); //收益
  109. $parents_info = UserPathModel::where('p.user_id', $user_id)
  110. ->alias('p')
  111. ->join('user u', 'u.id = p.parent_id', 'left')
  112. ->field('u.id, u.address_level, u.team_level_id,u.parent_id, p.distance')
  113. ->where('u.team_level_id', '>', 0)
  114. ->order('p.distance asc')
  115. ->select();
  116. if(empty($parents_info) || empty($income)) return true;
  117. //分享 1 个拿 1 层,分享 2个拿 2 层
  118. foreach ($parents_info as $parent){
  119. $send_user_id = 0; //需要发放的会员ID列表
  120. if($parent['team_level_id'] >= 1 && $parent['distance'] <= 1){
  121. $send_user_id = $parent['id'];
  122. }else if($parent['team_level_id'] >= 2 && $parent['distance'] <= 2){
  123. $send_user_id = $parent['id'];
  124. }else if($parent['team_level_id'] >= 3 && $parent['distance'] <= 3){
  125. $send_user_id = $parent['id'];
  126. }
  127. if($send_user_id > 0) Loader::model('LedgerWalletModel')->changeWalletAccount($send_user_id, $asset, $income, $action, $user_id);
  128. }
  129. return true;
  130. }
  131. //判断发放茶宝数量
  132. public static function getFrozenIsChabao($token, $frozen){
  133. //标记茶宝
  134. if(bccomp(bcmul($frozen, config('team_level_inc'), 6) , 0.0001, 6) == -1)
  135. {
  136. return [$token, Asset::TOKEN, LedgerTokenChangeModel::TeamLevel];
  137. }else{
  138. //标记茶宝
  139. return [$frozen, Asset::FROZEN, LedgerFrozenChangeModel::TeamLevel];
  140. }
  141. }
  142. }