WelfareLoginc.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\model\ProductArea;
  4. use fast\Asset;
  5. use app\common\model\LedgerFrozenChangeModel;
  6. use app\common\model\ProductLists;
  7. use app\common\model\ProductOrder;
  8. use app\common\model\UserModel;
  9. use think\Loader;
  10. use app\common\model\UserAirdrop;
  11. use think\Log;
  12. use Exception;
  13. /**
  14. * 空投福利
  15. */
  16. class WelfareLoginc
  17. {
  18. /** 添加新人福利
  19. * @param int $uid 用户id
  20. * @param array $productId 产品ID
  21. * @param int $num 数量
  22. * @param int $lan 语言
  23. */
  24. public static function setUserWelfareLos($uid, int $productId, $num, $tim, $lan, $typeId)
  25. {
  26. $result = ProductLists::where('id', $productId)->field('id,'.$lan.'_name as name')->find();
  27. //添加产品
  28. self::setUserProductOrder($num, 0, 0, 0, $productId, $uid, $typeId);
  29. return ['start_time'=>$tim, 'name'=>$result->name, 'num'=>$num, 'price'=>0];
  30. }
  31. /*
  32. 这个条件是
  33. 向持有选定产品超过指定数量的用户空投指定数量/或比例/的指定产品,
  34. 比如
  35. 向持有A茶超过10套的用户,空投2套B茶
  36. 向持有A茶超过10套的用户,空投持有 数量X20%套B茶,数量取整
  37. */
  38. /** $mod 0指定数量 1比
  39. * 获取用户Rwa数量
  40. */
  41. public static function getUserRwaProductNum(int $productId, int $rwa_num, $mod, $num): int
  42. {
  43. //读取当前持有RWa数据
  44. $list = ProductOrder::getUserOrderByProductId($productId, $rwa_num);
  45. if(empty($list)) return 0;
  46. $total = 0; //总数量
  47. if($mod == 1 && $num > 0){
  48. $num = bcdiv($num, 100, 2);
  49. foreach ($list as $item) {
  50. $total += bcmul($item->total_num, $num);
  51. }
  52. }else{
  53. $total = count($list) * $num;
  54. }
  55. return $total;
  56. }
  57. /**
  58. * 向达到一定持有量的用户空投产品
  59. * @param int $rwa_num //数量
  60. * @param $productId //产品ID
  61. * @param $rwaProductId //Rwa产品ID
  62. * @param $isArea
  63. * @param $orderId
  64. * @param $price
  65. * @param $mod
  66. * @param $num
  67. * @return int
  68. */
  69. public static function setUserExRwaNum(int $rwa_num, $productId, $rwaProductId, $isArea, $orderId, $price, $mod, $num): int
  70. {
  71. $total = 0; //总数量
  72. // 使用bcdiv函数进行高精度除法运算,$num除以100,保留两位小数
  73. $div = bcdiv($num, 100, 2);
  74. $list = ProductOrder::getUserOrderByProductId($rwaProductId, $rwa_num);
  75. $user = new UserModel();
  76. foreach($list as $order){
  77. //根据$mod的值决定是否需要对用户的rwa_num进行乘以$div的运算
  78. $num = ($mod == 1)? bcmul($order->total_num, $div): $num;
  79. // 尝试为用户设置产品订单,如果成功,则更新用户的rwa_num
  80. if(!empty(self::setUserProductOrder($num, $isArea, $orderId, $price, $productId, $order->user_id, ProductOrder::Airdrop))){
  81. // 调用静态方法更新用户的rwa_num,增加$num的值
  82. $user::updateForRwaNum($order->user_id, $user::getByParentId($order->user_id), $num, '+');
  83. }
  84. }
  85. return $total;
  86. }
  87. //合成订单
  88. public static function setSynthesisOrder($synthesis, $productId, $num, $uid):array
  89. {
  90. $result = array();
  91. $model = Loader::model('ProductOrder');
  92. foreach ($productId as $key => $item) {
  93. //获取需求数量
  94. if(!isset($synthesis[$key.'_num'])) throw new Exception(__("参数有误,无可用产品"));
  95. //判断持有数量足够
  96. $total_num = $synthesis[$key.'_num'] * $num;
  97. $order_arr = $model::getUserOrderByProductNum($uid, $item, $total_num);
  98. if($total_num != count($order_arr)) throw new Exception(__("材料不足"));
  99. //关闭持有订单
  100. $model::whereIn('id', $order_arr)->setField('status', $model::Closure);
  101. $result[$key][] = ['combine'=>$item, 'new_order'=>$order_arr];
  102. }
  103. return $result;
  104. }
  105. /**
  106. * 设置用户产品订单
  107. * 该方法根据是否需要区域信息来设置用户的订单信息如果没有指定区域,则调用setPopularNoAreaOrder方法,
  108. * 否则,首先查询产品关联的区域ID,然后调用setPopularAreaOrder方法设置订单信息
  109. * @param int $num 订单数量
  110. * @param bool $isArea 是否需要区域信息
  111. * @param mixed $orderId 订单ID
  112. * @param float $price 产品价格
  113. * @param int $productId 产品ID
  114. * @param int $uid 用户ID
  115. * @param int $typeId 订单类型ID
  116. * @return mixed 返回订单设置结果
  117. */
  118. public static function setUserProductOrder(int $num, $isArea, $orderId, $price, $productId, $uid, $typeId)
  119. {
  120. // 判断是否需要区域信息
  121. if(empty($isArea)){
  122. // 不需要区域信息,调用相应的方法设置订单
  123. $result = ProductOrder::setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId);
  124. }else{
  125. // 需要区域信息,首先查询符合条件的区域ID
  126. $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::Normal)->orderRaw('id desc')->limit($num)->column('id');
  127. // 使用查询到的区域ID调用相应的方法设置订单
  128. $result = ProductOrder::setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId);
  129. }
  130. // 返回订单设置结果
  131. return $result;
  132. }
  133. //推广空投产品
  134. public static function setUserEcologyAirdrop(int $uid, int $productId): bool
  135. {
  136. $ecology = config('ecology');
  137. if(!in_array($productId, $ecology['product_id'])){
  138. return true;
  139. }
  140. $user = Loader::model('UserModel')->getById($uid);
  141. if($user->is_ecology) return true;
  142. //空投产品
  143. if($ecology['airdrop']){
  144. $userAirdrop = Loader::model('UserAirdrop');
  145. foreach ($ecology['airdrop'] as $item) {
  146. $userAirdrop::create([
  147. 'user_id' => $uid,
  148. 'type_id' => UserAirdrop::TypeUser,
  149. 'product_id' => $item['product_id'],
  150. 'num' => $item['num'],
  151. 'total_num' => $item['num'],
  152. 'remark' => '生态节点空投',
  153. 'status' =>UserAirdrop::Normal,//为0 待发放层级奖励
  154. ]);
  155. }
  156. }
  157. //发放标记茶宝
  158. Loader::model('LedgerWalletModel')->changeWalletAccount($uid, Asset::FROZEN, $ecology['frozen'], LedgerFrozenChangeModel::Ecology, 0);
  159. //修改生态节点标识
  160. $user->is_ecology = 1;
  161. return $user->save();
  162. }
  163. //添加Rwa兑换福利产品
  164. public static function setUserWelfareProduct(int $uid, string $productId, string $orderNo, int $typeId)
  165. {
  166. $productOrder = Loader::model('ProductOrder');
  167. $productList = explode(',', $productId);
  168. $result = [];
  169. foreach ($productList as $item) {
  170. $result[] = $productOrder::setCreateOrder(0, ['product_id'=> $item, 'area_id'=>0, 'price'=>0], $typeId, $uid, 0, $orderNo, 0, 0);
  171. }
  172. return $result;
  173. }
  174. }