| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\api\logic;
- use app\api\controller\Product;
- use app\common\model\ProductArea;
- use app\common\controller\Api;
- use app\common\model\UserWelfare;
- use app\common\model\ProductPopular;
- use app\common\model\ProductOrder;
- use app\common\model\UserModel;
- use fast\Action;
- use fast\Asset;
- use fast\MembershipLevel;
- use think\Db;
- use think\Log;
- use think\Exception;
- use think\console\Output;
- use fast\Http;
- /**
- * 空投福利
- */
- class WelfareLoginc
- {
-
- /** 添加新人福利
- * @param int $uid 用户id
- * @param array $productId 产品ID
- * @param int $num 数量
- * @param int $lan 语言
- */
- public static function setUserWelfareLos($uid, int $productId, $num, $tim, $lan, $typeId)
- {
- $result = ProductPopular::getPopularByTime($productId, $lan, $tim);
- if(!$result || $num > $result->stock) throw new Exception(__('库存不足'));
- // if(empty($result->is_area)){
- // ProductOrder::setPopularNoAreaOrder($num, $result->id, $result->price, $result->product_id, $uid, $typeId);
- // }else{
- // $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::NORMAL)->orderRaw('id desc')->limit($num)->column('id');
- // ProductOrder::setPopularAreaOrder($areaArr, $result->id, $result->price, $result->product_id, $uid, $typeId);
- // }
- self::setUserProductOrder($num, $result->is_area, $result->id, $result->price, $productId, $uid, $typeId);
- return ['start_time'=>$tim, 'name'=>$result->name, 'num'=>$num];
- }
- /*
- 这个条件是
- 向持有选定产品超过指定数量的用户空投指定数量/或比例/的指定产品,
- 比如
- 向持有A茶超过10套的用户,空投2套B茶
- 或
- 向持有A茶超过10套的用户,空投持有 数量X20%套B茶,数量取整
- */
- /** $mod 0指定数量 1比
- * 获取用户Rwa数量
- */
- public static function getUserRwaNum(int $rwa_num, $mod, $num): int
- {
- //读取当日新增数据
- $list = UserModel::where('rwa_num', '>=',$rwa_num)->column('id,rwa_num');
- if(empty($list)) return 0;
- $total = 0; //总数量
- if($mod == 1 && $num > 0){
- $num = bcdiv($num, 100, 2);
- foreach ($list as $item) {
- $total += bcmul($item, $num);
- }
- }else{
- $total = count($list) * $num;
- }
- return $total;
- }
- //空投Rwa发放
- public static function setUserExRwaNum(int $rwa_num, $productId, $isArea, $orderId, $price, $mod, $num): int
- {
- //读取当日新增数据
- //$list = UserModel::where('rwa_num', '>=',$rwa_num)->column('id,rwa_num');
- $total = 0; //总数量
- // 使用bcdiv函数进行高精度除法运算,$num除以100,保留两位小数
- $div = bcdiv($num, 100, 2);
- // 使用chunk方法分批处理满足条件的用户,每次处理100条
- UserModel::where('rwa_num', '>=',$rwa_num)->chunk(100,function($users) use($mod,$div,$num,$isArea,$orderId,$price,$productId,&$total){
- foreach($users as $user){
- // 根据$mod的值决定是否需要对用户的rwa_num进行乘以$div的运算
- $num = ($mod == 1)? bcmul($user->rwa_num, $div) : $num;
- //对$num 取整
- $num = intval($num);
- // 尝试为用户设置产品订单,如果成功,则更新用户的rwa_num
- if(!empty(self::setUserProductOrder($num, $isArea, $orderId, $price, $productId, $user->id, ProductOrder::Airdrop))){
- // $user->rwa_num += $num;
- // $total +=$user->save();
- // 调用静态方法更新用户的rwa_num,增加$num的值
- $user::updateForRwaNum($user->id, $user->parent_id, $num, '+');
- }
- }
- });
- return $total;
- }
- /**
- * 设置用户产品订单
- *
- * 该方法根据是否需要区域信息来设置用户的订单信息如果没有指定区域,则调用setPopularNoAreaOrder方法,
- * 否则,首先查询产品关联的区域ID,然后调用setPopularAreaOrder方法设置订单信息
- *
- * @param int $num 订单数量
- * @param bool $isArea 是否需要区域信息
- * @param mixed $orderId 订单ID
- * @param float $price 产品价格
- * @param int $productId 产品ID
- * @param int $uid 用户ID
- * @param int $typeId 订单类型ID
- *
- * @return mixed 返回订单设置结果
- */
- private static function setUserProductOrder(int $num, $isArea, $orderId, $price, $productId, $uid, $typeId)
- {
- // 判断是否需要区域信息
- if(empty($isArea)){
- // 不需要区域信息,调用相应的方法设置订单
- $result = ProductOrder::setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId);
- }else{
- // 需要区域信息,首先查询符合条件的区域ID
- $areaArr = ProductArea::where('product_id', $productId)->where('status', ProductArea::NORMAL)->orderRaw('id desc')->limit($num)->column('id');
- // 使用查询到的区域ID调用相应的方法设置订单
- $result = ProductOrder::setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId);
- }
- // 返回订单设置结果
- return $result;
- }
-
-
- }
|