WelfareLoginc.php 7.6 KB

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