Эх сурвалжийг харах

增加超级福利层级奖励

jason 8 сар өмнө
parent
commit
27315380a3

+ 18 - 3
application/api/controller/Airdrop.php

@@ -2,6 +2,7 @@
 
 namespace app\api\controller;
 
+use app\common\model\UserAirdrop;
 use think\Exception;
 use app\api\logic\WelfareLoginc;
 use app\common\model\UserModel;
@@ -65,21 +66,35 @@ class Airdrop extends Api
         if(empty($row->is_super)) $this->error(__('暂未开启'));
         if(!empty($this->auth->is_super)) $this->error(__('您已领取'));
 
+        //判断距离领取新人福利是否超过一天
         $info = $productOrder::getUserWelfare($this->auth->id, $productOrder::Newbie);
         if((time()-$info->create_time) >= 86400) $this->error(__('您已过期'));
         Db::startTrans();
         try {
+            //把9.9茶宝转入冻结账号
+            if($row->frozen > 0) (new LedgerWalletModel)->sendUserSubFrozen($this->auth->id, $row->frozen, LedgerWalletModel::Super, '-');
+
             //添加Rwa茶记录
             $result = WelfareLoginc::setUserWelfareLos($this->auth->id, $row->super_product_id, $row->super_num, time(), $this->lan, $productOrder::Super);
 
             //添加超级福利标识/上级
             $userModel::updateUserSuper($this->auth->id, $this->auth->parent_id);
 
-            //标识为社区长
-            if($row->frozen > 0) (new LedgerWalletModel)->sendUserSubFrozen($this->auth->id, $row->frozen, LedgerWalletModel::Super, '-'); 
-            
             //添加茶数量
             $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, $row->super_num, '+');
+
+            //插入待空投记录
+            $rs = UserAirdrop::insert([
+                'user_id'       =>  $this->auth->id,
+                'type_id'       =>  UserAirdrop::typeSuperWeal,
+                'product_id'    =>  $row->super_product_id,
+                'num'           =>  $row->super_num,
+                'address'       =>  $this->auth->address,
+                'remark'        =>  '超级福利空投',
+                'create_time'   =>time(),
+                'status'        =>UserAirdrop::NORMAL,//为0 待发放层级奖励
+            ]);
+
             Db::commit();
         } catch (\Exception $e) {
             Db::rollback();

+ 32 - 2
application/api/logic/WelfareLoginc.php

@@ -81,14 +81,23 @@ class WelfareLoginc
         //读取当日新增数据
         //$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){
-                // 处理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, '+');
                 }
             }
@@ -97,14 +106,35 @@ class WelfareLoginc
     }
 
 
+    /**
+     * 设置用户产品订单
+     *
+     * 该方法根据是否需要区域信息来设置用户的订单信息如果没有指定区域,则调用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;
     }
 

+ 88 - 1
application/common/logic/AirdropLogic.php

@@ -10,6 +10,8 @@ use app\common\model\ProductPopular;
 use app\common\model\LedgerTokenChangeModel;
 use app\common\model\LedgerWalletModel;
 use app\common\model\TimedTaskLogModel;
+use app\common\model\UserModel;
+use app\common\model\UserPathModel;
 use fast\Action;
 use fast\Asset;
 use fast\MembershipLevel;
@@ -47,24 +49,109 @@ class AirdropLogic
             (new Output())->writeln("本次没有空投发放数据:");
             return false;
         }
+        // 获取指定产品的热门信息
         $result = ProductPopular::getPopularByTime($info_list['product_id'], 'zh', $info_list->start_time);
+
+        // 检查库存是否足够
         if(!$result || $info_list->total_num > $result->stock) {
+            // 如果库存不足,输出信息并返回false
             (new Output())->writeln("本次执行库存不足:");
             return false;
         }
+
+        // 更新用户福利信息
         WelfareLoginc::setUserExRwaNum(
             $info_list['rwa_num'],
             $info_list['product_id'],
             $result->is_area,  $result->id, $result->price,
             $info_list['rwa_mod'],
-            $info_list['num']);
+            $info_list['num']
+        );
+
+        // 更新信息列表的状态和备注
         $info_list->status = UserAirdrop::STOP;
         $info_list->remark = '总发放'.$info_list->total_num.'套';
+
+        // 保存更新后的信息列表
         return $info_list->save();
         
         
     }
 
+    public function setParentRewards()
+    {
+        //查找会员资产列表
+        $info_list = UserAirdrop::where('type_id', UserAirdrop::typeSuperWeal)
+            ->where('user_id', '>', 0)
+            ->where('status', UserAirdrop::NORMAL)
+            ->select();
+        if(empty($info_list)){
+            (new Output())->writeln("本次没有层级奖励数据:");
+            return false;
+        }
+        foreach ($info_list as $info){
+            $user_info = UserModel::where('id', $info['user_id'])->find();
+            if($user_info->is_super == 0){
+                (new UserAirdrop)->updateDate($info['id'], UserAirdrop::STOP, '会员未领超级福利');
+            }
+            if($user_info->parent_id == 0){
+                (new UserAirdrop)->updateDate($info['id'], UserAirdrop::STOP, '会员无上级');
+            }
+            $parents_info = UserPathModel::where('p.user_id', $info['user_id'])
+                ->alias('p')
+                ->join('user u', 'u.id = p.parent_id', 'left')
+                ->field('u.id, u.is_super, u.direct_super, u.parent_id, p.distance')
+                ->where('u.is_super', 1)
+                ->order('p.distance asc')
+                ->select();
+            if(empty($parents_info)){
+                (new UserAirdrop)->updateDate($info['id'], UserAirdrop::STOP, '所有上级无超级福利');
+            }
+            $level = 0;
+            $send_user_ids = [];//需要发放的会员ID列表
+            //分享 3 个拿 10 层,分享 10 个拿 31 层
+            foreach ($parents_info as $parent){
+                if($parent['direct_super'] >= 3 && $parent['distance'] <= 10){
+                    $send_user_ids[] = $parent['parent_id'];
+                }else if($parent['direct_super'] >= 10 && $parent['distance'] <= 31){
+                        $send_user_ids[] = $parent['parent_id'];
+                }
+            }
+            if(empty($send_user_ids)){
+                (new UserAirdrop)->updateDate($info['id'], UserAirdrop::STOP, '所有上级都未满足发放条件');
+            }
+
+            
+        }
+
+
+        // 获取指定产品的热门信息
+        $result = ProductPopular::getPopularByTime($info_list['product_id'], 'zh', $info_list->start_time);
+
+        // 检查库存是否足够
+        if(!$result || $info_list->total_num > $result->stock) {
+            // 如果库存不足,输出信息并返回false
+            (new Output())->writeln("本次执行库存不足:");
+            return false;
+        }
+
+        // 更新用户福利信息
+        WelfareLoginc::setUserExRwaNum(
+            $info_list['rwa_num'],
+            $info_list['product_id'],
+            $result->is_area,  $result->id, $result->price,
+            $info_list['rwa_mod'],
+            $info_list['num']
+        );
+
+        // 更新信息列表的状态和备注
+        $info_list->status = UserAirdrop::STOP;
+        $info_list->remark = '总发放'.$info_list->total_num.'套';
+
+        // 保存更新后的信息列表
+        return $info_list->save();
+    }
+
     /**
      * 拨币
      * 向会员发放qubic

+ 19 - 7
application/common/model/LedgerWalletModel.php

@@ -95,10 +95,24 @@ class LedgerWalletModel extends Model
        
     public static function getStatusList()
     {
-        return [self::Popular => __('热销支付'), self::Payment  => __('转让支付'), self::Receive => __('转让收款'), self::Recharge => __('充值'), 
-        self::Withdraw  => __('提现'), self::Share => __('分享'), self::Return => __('退回'), self::Giveaway => __('赠送'), self::Direct => __('布道津贴'),
-        self::System => __('系统调整'),self::GiftPay => __('转账支出'),self::GiftReceipt => __('转账收款'), self::Community => __('社区津贴'), 
-        self::Service => __('服务津贴'), self::Together => __('共创津贴'), self::Freight => __('物流运费'), self::Super => __('茶宝标记激活')];
+        return [
+            self::Popular => __('热销支付'),
+            self::Payment  => __('转让支付'),
+            self::Receive => __('转让收款'),
+            self::Recharge => __('充值'),
+            self::Withdraw  => __('提现'),
+            self::Share => __('分享'),
+            self::Return => __('退回'),
+            self::Giveaway => __('赠送'),
+            self::Direct => __('布道津贴'),
+            self::System => __('系统调整'),
+            self::GiftPay => __('转账支出'),
+            self::GiftReceipt => __('转账收款'),
+            self::Community => __('社区津贴'),
+            self::Service => __('服务津贴'),
+            self::Together => __('共创津贴'),
+            self::Freight => __('物流运费'),
+            self::Super => __('茶宝标记激活')];
     }
 
     /**
@@ -159,7 +173,7 @@ class LedgerWalletModel extends Model
     }
  
     /**
-     * 发放代数收益
+     * 冻结茶宝
      * @param int $uid 服务器算力的用户ID
      * @param string $usdt 报单金额
      * @return void
@@ -189,8 +203,6 @@ class LedgerWalletModel extends Model
         if (empty($insertRs)) {
             throw new Exception('创建账变记录失败');
         }
-       
-
     }
 
     /**

+ 6 - 4
application/common/model/ProductPopular.php

@@ -90,10 +90,12 @@ class ProductPopular extends Model
     //获取当前抢购产品
     public static function getPopularByTime(int $productId, string $lan, $tim)
     {   
-       return self::alias('a')->where('a.start_time', '<=', $tim)->where('a.end_time', '>=', $tim)
-        ->join('product_list b', "a.product_id = b.id", "left")
-        ->field('a.*,b.is_area,'.$lan.'_name as name')
-        ->where('a.product_id', $productId)->find();  
+       return self::alias('a')
+           ->where('a.start_time', '<=', $tim)
+           ->where('a.end_time', '>=', $tim)
+           ->join('product_list b', "a.product_id = b.id", "left")
+           ->field('a.*,b.is_area,'.$lan.'_name as name')
+           ->where('a.product_id', $productId)->find();
     }
 
  

+ 13 - 3
application/common/model/UserAirdrop.php

@@ -34,6 +34,10 @@ class UserAirdrop extends Model
     const typeUser        = 0;
     const typeRwa         = 1;
     const typeAddr        = 2;
+    //超级福利
+    const typeSuperWeal   = 3;
+    //超级福利上级
+    const typeSuperParent   = 4;
 
     //状态
     public $status_list = [
@@ -44,12 +48,18 @@ class UserAirdrop extends Model
 
 
 
-    public function getStatusList(){
-        
+    public function updateDate($id, $status, $ramark = ''){
+        $data = [
+            'status'    =>  $status,
+            'start_time'=>  time(),
+        ];
+        if(!empty($ramark)){
+            $data['remark'] = $ramark;
+        }
+        UserAirdrop::where('id', $id)->update($data);
     }
 
 
-
     public function getCreateTimeTextAttr($value, $data)
     {
         $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');

+ 0 - 3
application/common/model/UserWelfare.php

@@ -31,9 +31,6 @@ class UserWelfare extends Model
     {
         return self::where('id', 1)->find();
     }
-    
-
-
 
     public function getCreateTimeTextAttr($value, $data)
     {