WelfareLoginc.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\api\logic;
  3. use app\api\controller\Product;
  4. use app\common\model\ProductArea;
  5. use app\common\controller\Api;
  6. use app\common\model\UserWelfare;
  7. use app\common\model\ProductPopular;
  8. use app\common\model\ProductOrder;
  9. use app\common\model\UserModel;
  10. use fast\Action;
  11. use fast\Asset;
  12. use fast\MembershipLevel;
  13. use think\Db;
  14. use think\Log;
  15. use think\Exception;
  16. use think\console\Output;
  17. use fast\Http;
  18. /**
  19. * 空投福利
  20. */
  21. class WelfareLoginc
  22. {
  23. /** 添加新人福利
  24. * @param int $uid 用户id
  25. * @param array $productId 产品ID
  26. * @param int $num 数量
  27. * @param int $lan 语言
  28. */
  29. public static function setUserWelfareLos($uid, int $productId, $num, $tim, $lan, $typeId)
  30. {
  31. $result = ProductPopular::getPopularByTime($productId, $lan, $tim);
  32. if(!$result || $num > $result->stock) throw new Exception(__('库存不足'));
  33. // if(empty($result->is_area)){
  34. // ProductOrder::setPopularNoAreaOrder($num, $result->id, $result->price, $result->product_id, $uid, $typeId);
  35. // }else{
  36. // $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::NORMAL)->orderRaw('id desc')->limit($num)->column('id');
  37. // ProductOrder::setPopularAreaOrder($areaArr, $result->id, $result->price, $result->product_id, $uid, $typeId);
  38. // }
  39. self::setUserProductOrder($num, $result->is_area, $result->id, $result->price, $productId, $uid, $typeId);
  40. return ['start_time'=>$tim, 'name'=>$result->name, 'num'=>$num];
  41. }
  42. /*
  43. 这个条件是
  44. 向持有选定产品超过指定数量的用户空投指定数量/或比例/的指定产品,
  45. 比如
  46. 向持有A茶超过10套的用户,空投2套B茶
  47. 向持有A茶超过10套的用户,空投持有 数量X20%套B茶,数量取整
  48. */
  49. /** $mod 0指定数量 1比
  50. * 获取用户Rwa数量
  51. */
  52. public static function getUserRwaNum(int $rwa_num, $mod, $num): int
  53. {
  54. //读取当日新增数据
  55. $list = UserModel::where('rwa_num', '>=',$rwa_num)->column('id,rwa_num');
  56. if(empty($list)) return 0;
  57. $total = 0; //总数量
  58. if($mod == 1 && $num > 0){
  59. $num = bcdiv($num, 100, 2);
  60. foreach ($list as $item) {
  61. $total += bcmul($item, $num);
  62. }
  63. }else{
  64. $total = count($list) * $num;
  65. }
  66. return $total;
  67. }
  68. //空投Rwa发放
  69. public static function setUserExRwaNum(int $rwa_num, $productId, $isArea, $orderId, $price, $mod, $num): int
  70. {
  71. //读取当日新增数据
  72. //$list = UserModel::where('rwa_num', '>=',$rwa_num)->column('id,rwa_num');
  73. $total = 0; //总数量
  74. // 使用bcdiv函数进行高精度除法运算,$num除以100,保留两位小数
  75. $div = bcdiv($num, 100, 2);
  76. // 使用chunk方法分批处理满足条件的用户,每次处理100条
  77. UserModel::where('rwa_num', '>=',$rwa_num)->chunk(100,function($users) use($mod,$div,$num,$isArea,$orderId,$price,$productId,&$total){
  78. foreach($users as $user){
  79. // 根据$mod的值决定是否需要对用户的rwa_num进行乘以$div的运算
  80. $num = ($mod == 1)? bcmul($user->rwa_num, $div) : $num;
  81. //对$num 取整
  82. $num = intval($num);
  83. // 尝试为用户设置产品订单,如果成功,则更新用户的rwa_num
  84. if(!empty(self::setUserProductOrder($num, $isArea, $orderId, $price, $productId, $user->id, ProductOrder::Airdrop))){
  85. // $user->rwa_num += $num;
  86. // $total +=$user->save();
  87. // 调用静态方法更新用户的rwa_num,增加$num的值
  88. $user::updateForRwaNum($user->id, $user->parent_id, $num, '+');
  89. }
  90. }
  91. });
  92. return $total;
  93. }
  94. /**
  95. * 设置用户产品订单
  96. *
  97. * 该方法根据是否需要区域信息来设置用户的订单信息如果没有指定区域,则调用setPopularNoAreaOrder方法,
  98. * 否则,首先查询产品关联的区域ID,然后调用setPopularAreaOrder方法设置订单信息
  99. *
  100. * @param int $num 订单数量
  101. * @param bool $isArea 是否需要区域信息
  102. * @param mixed $orderId 订单ID
  103. * @param float $price 产品价格
  104. * @param int $productId 产品ID
  105. * @param int $uid 用户ID
  106. * @param int $typeId 订单类型ID
  107. *
  108. * @return mixed 返回订单设置结果
  109. */
  110. private static function setUserProductOrder(int $num, $isArea, $orderId, $price, $productId, $uid, $typeId)
  111. {
  112. // 判断是否需要区域信息
  113. if(empty($isArea)){
  114. // 不需要区域信息,调用相应的方法设置订单
  115. $result = ProductOrder::setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId);
  116. }else{
  117. // 需要区域信息,首先查询符合条件的区域ID
  118. $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::NORMAL)->orderRaw('id desc')->limit($num)->column('id');
  119. // 使用查询到的区域ID调用相应的方法设置订单
  120. $result = ProductOrder::setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId);
  121. }
  122. // 返回订单设置结果
  123. return $result;
  124. }
  125. }