WelfareLoginc.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 think\Loader;
  11. use think\Db;
  12. use think\Log;
  13. use Exception;
  14. /**
  15. * 空投福利
  16. */
  17. class WelfareLoginc
  18. {
  19. /** 添加新人福利
  20. * @param int $uid 用户id
  21. * @param array $productId 产品ID
  22. * @param int $num 数量
  23. * @param int $lan 语言
  24. */
  25. public static function setUserWelfareLos($uid, int $productId, $num, $tim, $lan, $typeId)
  26. {
  27. $result = ProductPopular::getPopularByTime($productId, $lan, $tim);
  28. if(!$result || $num > ($result->stock-$result->init_num - $result->num)) throw new Exception(__('库存不足'));
  29. //添加产品
  30. self::setUserProductOrder($num, $result->is_area, $result->id, $result->price, $productId, $uid, $typeId);
  31. return ['start_time'=>$tim, 'name'=>$result->name, 'num'=>$num, 'price'=>$result->price];
  32. }
  33. /*
  34. 这个条件是
  35. 向持有选定产品超过指定数量的用户空投指定数量/或比例/的指定产品,
  36. 比如
  37. 向持有A茶超过10套的用户,空投2套B茶
  38. 向持有A茶超过10套的用户,空投持有 数量X20%套B茶,数量取整
  39. */
  40. /** $mod 0指定数量 1比
  41. * 获取用户Rwa数量
  42. */
  43. public static function getUserRwaProductNum(int $productId, int $rwa_num, $mod, $num): int
  44. {
  45. //读取当前新增数据
  46. $list = ProductOrder::getUserOrderByProductId($productId, $rwa_num);
  47. if(empty($list)) return 0;
  48. $total = 0; //总数量
  49. if($mod == 1 && $num > 0){
  50. $num = bcdiv($num, 100, 2);
  51. foreach ($list as $item) {
  52. $total += bcmul($item->total_num, $num);
  53. }
  54. }else{
  55. $total = count($list) * $num;
  56. }
  57. return $total;
  58. }
  59. /**
  60. * 向达到一定持有量的用户空投产品
  61. * @param int $rwa_num //数量
  62. * @param $productId //产品ID
  63. * @param $rwaProductId //Rwa产品ID
  64. * @param $isArea
  65. * @param $orderId
  66. * @param $price
  67. * @param $mod
  68. * @param $num
  69. * @return int
  70. */
  71. public static function setUserExRwaNum(int $rwa_num, $productId, $rwaProductId, $isArea, $orderId, $price, $mod, $num): int
  72. {
  73. $total = 0; //总数量
  74. // 使用bcdiv函数进行高精度除法运算,$num除以100,保留两位小数
  75. $div = bcdiv($num, 100, 2);
  76. $list = ProductOrder::getUserOrderByProductId($rwaProductId, $rwa_num);
  77. $user = new UserModel();
  78. foreach($list as $order){
  79. //根据$mod的值决定是否需要对用户的rwa_num进行乘以$div的运算
  80. $num = ($mod == 1)? bcmul($order->total_num, $div): $num;
  81. // 尝试为用户设置产品订单,如果成功,则更新用户的rwa_num
  82. if(!empty(self::setUserProductOrder($num, $isArea, $orderId, $price, $productId, $order->user_id, ProductOrder::Airdrop))){
  83. // 调用静态方法更新用户的rwa_num,增加$num的值
  84. $user::updateForRwaNum($order->user_id, $user::getByParentId($order->user_id), $num, '+');
  85. }
  86. }
  87. return $total;
  88. }
  89. //合成订单
  90. public static function setSynthesisOrder($synthesis, $productId, $num, $uid):array
  91. {
  92. $result = array();
  93. $model = Loader::model('ProductOrder');
  94. foreach ($productId as $key => $item) {
  95. //获取需求数量
  96. if(!isset($synthesis[$key.'_num'])) throw new Exception(__("参数有误,无可用产品"));
  97. //判断持有数量足够
  98. $total_num = $synthesis[$key.'_num'] * $num;
  99. $order_arr = $model::getUserOrderByProductNum($uid, $item, $total_num);
  100. if($total_num != count($order_arr)) throw new Exception(__("材料不足"));
  101. //关闭持有订单
  102. $model::whereIn('id', $order_arr)->setField('status', $model::Closure);
  103. $result[$key][] = ['combine'=>$item, 'new_order'=>$order_arr];
  104. }
  105. return $result;
  106. }
  107. /**
  108. * 设置用户产品订单
  109. * 该方法根据是否需要区域信息来设置用户的订单信息如果没有指定区域,则调用setPopularNoAreaOrder方法,
  110. * 否则,首先查询产品关联的区域ID,然后调用setPopularAreaOrder方法设置订单信息
  111. * @param int $num 订单数量
  112. * @param bool $isArea 是否需要区域信息
  113. * @param mixed $orderId 订单ID
  114. * @param float $price 产品价格
  115. * @param int $productId 产品ID
  116. * @param int $uid 用户ID
  117. * @param int $typeId 订单类型ID
  118. * @return mixed 返回订单设置结果
  119. */
  120. public static function setUserProductOrder(int $num, $isArea, $orderId, $price, $productId, $uid, $typeId)
  121. {
  122. // 判断是否需要区域信息
  123. if(empty($isArea)){
  124. // 不需要区域信息,调用相应的方法设置订单
  125. $result = ProductOrder::setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId);
  126. }else{
  127. // 需要区域信息,首先查询符合条件的区域ID
  128. $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::Normal)->orderRaw('id desc')->limit($num)->column('id');
  129. // 使用查询到的区域ID调用相应的方法设置订单
  130. $result = ProductOrder::setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId);
  131. }
  132. // 返回订单设置结果
  133. return $result;
  134. }
  135. }