CommonLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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($parent_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($parent_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 $parent_id, array $config){
  43. $parent = UserModel::where('id', $parent_id)->find();
  44. if($parent->team_level_id > 0){
  45. //推广人数
  46. $teamCount = UserModel::where('parent_id', $parent_id)->where('team_level_id', $parent->team_level_id)->count() +1;
  47. //组合套数
  48. $groupCount = UserPledge::getPledgeCount($parent_id);
  49. if($teamCount >= $config[$parent->team_level_id]['direct'] && $groupCount >= $config[$parent->team_level_id]['num']){
  50. $parent->team_level_id +=1;
  51. $parent->save();
  52. }
  53. }
  54. }
  55. //降级
  56. public static function setIsOuLevelSave($user_id, $team_level_id, $address_level, $parent_id)
  57. {
  58. $config = config('team_level_where');
  59. $result = false;
  60. //组合套数
  61. $groupCount = UserPledge::getPledgeCount($user_id) -1 ;
  62. if($team_level_id == 1 && ($groupCount == 0 || $address_level < $config[0]['direct'])){
  63. $result = true;
  64. $team_level_id = 0;
  65. self::setIsOuLevelParent($parent_id, $config);
  66. }
  67. if($team_level_id > 1){
  68. //直推
  69. $teamCount = UserModel::where('parent_id', $user_id)->where('team_level_id', $team_level_id)->count();
  70. if($teamCount < $config[$team_level_id]['direct'] && $groupCount < $config[$team_level_id]['num']){
  71. $result = true;
  72. $team_level_id -= 1;
  73. self::setIsOuLevelParent($parent_id, $config);
  74. }
  75. }
  76. return $result? UserModel::where('id', $user_id)->update(['team_level_id'=> $team_level_id]): false;
  77. }
  78. //上级降低级
  79. public static function setIsOuLevelParent(int $parent_id, array $config){
  80. $parent = UserModel::where('id', $parent_id)->find();
  81. if($parent->team_level_id > 0){
  82. //推广人数
  83. $teamCount = UserModel::where('parent_id', $parent_id)->where('team_level_id', $parent->team_level_id)->count() +1;
  84. //组合套数
  85. $groupCount = UserPledge::getPledgeCount($parent_id);
  86. if($teamCount < $config[$parent->team_level_id]['direct'] && $groupCount >= $config[$parent->team_level_id]['num']){
  87. $parent->team_level_id -=1;
  88. $parent->save();
  89. }
  90. }
  91. }
  92. //发放层级收益
  93. public static function setTeamLevelIncome(int $user_id, float $num, int $team_level_id, string $asset, string $action)
  94. {
  95. $config = config('team_level_where');
  96. $team_level_id = $team_level_id - 1;
  97. $paths = UserPathModel::where('user_id', $user_id)->where('distance', '<=', $config[$team_level_id]['level'])->order('distance', 'asc')->column('parent_id');
  98. $income = bcmul($num, config('team_level_inc'), 2); //收益
  99. if(count($paths) > 0 && $income > 0){
  100. //余额记录
  101. foreach($paths as $pathId){
  102. if(!empty(Loader::model('UserModel')::getTeamLevelId($pathId))) {
  103. Loader::model('LedgerWalletModel')->changeWalletAccount($pathId, $asset, $income, $action, $user_id);
  104. }
  105. }
  106. }
  107. return true;
  108. }
  109. //判断发放茶宝数量
  110. public static function getFrozenIsChabao($token, $frozen){
  111. //标记茶宝
  112. if(bccomp(bcmul($frozen, config('team_level_inc'), 6) , 0.0001, 6) == -1)
  113. {
  114. return [$token, Asset::TOKEN, LedgerTokenChangeModel::TeamLevel];
  115. }else{
  116. //标记茶宝
  117. return [$frozen, Asset::FROZEN, LedgerFrozenChangeModel::TeamLevel];
  118. }
  119. }
  120. }