CommonLogic.php 2.9 KB

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