CommonLogic.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 setIsLevelSave(int $user_id, int $team_level_id, int $address_level, int $num): 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. }
  27. }else{
  28. //直推
  29. $teamCount = UserModel::where('parent_id', $user_id)->where('team_level_id', $team_level_id)->count();
  30. //组合套数
  31. $groupCount = UserPledge::getPledgeCount($user_id) + $num;
  32. if($team_level_id == 1 && ($groupCount == 0 || $address_level < $config[0]['direct'])){
  33. $result = true;
  34. $team_level_id = 0;
  35. }
  36. //大于1
  37. if($team_level_id > 1){
  38. if($teamCount >= $config[$team_level_id]['direct'] && $groupCount >= $config[$team_level_id]['num']){
  39. $result = true;
  40. $team_level_id += 1; //+1
  41. }else{
  42. $result = true;
  43. $team_level_id -=1; //-1
  44. }
  45. }
  46. }
  47. return $result? UserModel::where('id', $user_id)->update(['team_level_id'=> $team_level_id]): false;
  48. }
  49. //发放层级收益
  50. public static function setTeamLevelIncome(int $user_id, float $num, int $team_level_id, string $asset, string $action)
  51. {
  52. $config = config('team_level_where');
  53. $team_level_id = $team_level_id - 1;
  54. $paths = UserPathModel::where('user_id', $user_id)->where('distance', '<=', $config[$team_level_id]['level'])->order('distance', 'asc')->column('parent_id');
  55. if(count($paths) > 0){
  56. //收益
  57. $income = bcmul($num, config('team_level_inc'), 2);
  58. //余额记录
  59. foreach($paths as $pathId){
  60. Loader::model('LedgerWalletModel')->changeWalletAccount($pathId, $asset, $income, $action, $user_id);
  61. }
  62. }
  63. return true;
  64. }
  65. //判断发放茶宝数量
  66. public static function getFrozenIsChabao($token, $frozen)
  67. {
  68. //标记茶宝
  69. if(bccomp(bcmul($frozen, config('team_level_inc'), 6) , 0.0001, 6) == -1)
  70. {
  71. return [$token, Asset::TOKEN, LedgerTokenChangeModel::TeamLevel];
  72. }else{
  73. //标记茶宝
  74. return [$frozen, Asset::FROZEN, LedgerFrozenChangeModel::TeamLevel];
  75. }
  76. }
  77. }