afa 10 сар өмнө
parent
commit
c3521ae247

+ 37 - 0
application/api/command/SendBonusRewards.php

@@ -0,0 +1,37 @@
+<?php
+
+
+namespace app\api\command;
+
+use think\console\Command;
+use think\console\Input;
+use think\console\Output;
+use app\common\logic\BonusRewards;
+use app\common\model\TimedTaskLogModel;
+use think\Model;
+
+
+class SendBonusRewards extends Command
+{
+    protected function configure()
+    {
+        $this->setName('SendBonusRewards')->setDescription('Here is the remark ');
+    }
+
+    protected function execute(Input $input, Output $output)
+    {
+        /* 永不超时 */
+//        ini_set('max_execution_time', 0);
+
+        // 记录开始运行的时间
+        $GLOBALS['_beginTime'] = microtime(TRUE);
+        $output->writeln('定时任务开始执行:' . date('Y-m-d H:i:s', time()));
+
+        /*** 发放团队算力奖励 ***/
+        $output->writeln("开始发放团队算力奖励:");
+
+        $log_id = (new BonusRewards())->powerRewards();
+
+        $output->info("SendTeamRewards ok!");
+    }
+}

+ 0 - 131
application/api/command/SendTeamRewards.php

@@ -1,131 +0,0 @@
-<?php
-
-
-namespace app\api\command;
-
-use think\console\Command;
-use think\console\Input;
-use think\console\Output;
-use app\common\logic\TeamRewards;
-use app\common\model\TimedTaskLogModel;
-use think\Model;
-
-
-class SendTeamRewards extends Command
-{
-    protected function configure()
-    {
-        $this->setName('SendTeamRewards')->setDescription('Here is the remark ');
-    }
-
-    protected function execute(Input $input, Output $output)
-    {
-        /* 永不超时 */
-//        ini_set('max_execution_time', 0);
-
-        // 记录开始运行的时间
-        $GLOBALS['_beginTime'] = microtime(TRUE);
-        $output->writeln('定时任务开始执行:' . date('Y-m-d H:i:s', time()));
-
-        /*** 发放团队算力奖励 ***/
-        $output->writeln("开始发放团队算力奖励:");
-
-        $log_id = (new TeamRewards())->powerRewards();
-
-        $time_total = round(microtime(true) - $GLOBALS['_beginTime'], 4);//计算耗时
-
-        $output->writeln('团队算力奖励发放结束:' . date('Y-m-d H:i:s', time()) . '; 耗时:' . $time_total . '秒');
-        //更新发放日志
-        if($log_id > 0){
-            (new TimedTaskLogModel())->update([
-                'id'            => $log_id,
-                'status'        => 1,
-                'end_time'      => date('Y-m-d H:i:s'),
-                'elapsed_time'  => $time_total
-            ]);
-        }
-
-        /*** 发放分红收益 ***/
-        $begin_usdt = microtime(TRUE);
-        $output->writeln("开始发放加权分红:");
-
-        $log_id_1 = (new TeamRewards())->usdtRewards();
-
-        $time_total = round(microtime(true) - $begin_usdt, 4);//计算耗时
-        $output->writeln('加权分红发放结束:' . date('Y-m-d H:i:s', time()) . '; 耗时:' . $time_total . '秒');
-        //更新发放日志
-        if($log_id_1 > 0){
-            (new TimedTaskLogModel())
-                ->where('id', $log_id_1)
-                ->update([
-                    'id'            => $log_id_1,
-                    'status'        => 1,
-                    'end_time'      => date('Y-m-d H:i:s'),
-                    'elapsed_time'  => $time_total
-                ]);
-        }
-
-        /*** 发放社区长、系统领导人加权分红 ***/
-        $begin_usdt = microtime(TRUE);
-        $output->writeln("开始发放社区长、系统领导人加权分红:");
-
-        $log_id_1 = (new TeamRewards())->marketRewards();
-
-        $time_total = round(microtime(true) - $begin_usdt, 4);//计算耗时
-        $output->writeln('社区长、系统领导人加权分红发放结束:' . date('Y-m-d H:i:s', time()) . '; 耗时:' . $time_total . '秒');
-        //更新发放日志
-        if($log_id_1 > 0){
-            (new TimedTaskLogModel())
-                ->where('id', $log_id_1)
-                ->update([
-                    'id'            => $log_id_1,
-                    'status'        => 1,
-                    'end_time'      => date('Y-m-d H:i:s'),
-                    'elapsed_time'  => $time_total
-                ]);
-        }
-
-        /*** 团队业绩分红 ***/
-        $begin_usdt = microtime(TRUE);
-        $output->writeln("团队业绩分红:");
-
-        $log_id_1 = (new TeamRewards())->teamRewards();
-
-        $time_total = round(microtime(true) - $begin_usdt, 4);//计算耗时
-        $output->writeln('团队业绩分红发放结束:' . date('Y-m-d H:i:s', time()) . '; 耗时:' . $time_total . '秒');
-        //更新发放日志
-        if($log_id_1 > 0){
-            (new TimedTaskLogModel())
-                ->where('id', $log_id_1)
-                ->update([
-                    'id'            => $log_id_1,
-                    'status'        => 1,
-                    'end_time'      => date('Y-m-d H:i:s'),
-                    'elapsed_time'  => $time_total
-                ]);
-        }
-
-        /*** 今日产出 ***/
-//        $begin_usdt = microtime(TRUE);
-//        $output->writeln("今日产出:");
-//
-//        $log_id_1 = (new TeamRewards())->AllocateEtc();
-//
-//        $time_total = round(microtime(true) - $begin_usdt, 4);//计算耗时
-//        $output->writeln('今日产出放结束:' . date('Y-m-d H:i:s', time()) . '; 耗时:' . $time_total . '秒');
-//        //更新发放日志
-//        if($log_id_1 > 0){
-//            (new TimedTaskLogModel())
-//                ->where('id', $log_id_1)
-//                ->update([
-//                    'id'            => $log_id_1,
-//                    'status'        => 1,
-//                    'end_time'      => date('Y-m-d H:i:s'),
-//                    'elapsed_time'  => $time_total
-//                ]);
-//        }
-
-        $time_total = round(microtime(true) - $GLOBALS['_beginTime'], 4);//计算耗时
-        $output->writeln('定时任务执行结束,总耗时' . date('Y-m-d H:i:s', time()) . '; 耗时:' . $time_total . '秒');
-    }
-}

+ 26 - 20
application/api/controller/Order.php

@@ -33,32 +33,38 @@ class Order extends Api
         if(!$validate->scene('add')->check($params)) $this->error($validate->getError());
         
         $order_info = $productPopular->where('id', $params['order_id'])->find();
-        if($order_info->num == $order_info->stock) $this->error(__("库存不足"));
         if(empty($order_info)) $this->error(__("参数有误,无可用产品"));
-        $order_data['order_id']  = $params['order_id'];
-        $order_data['product_id']= $params['product_id'];
-        $order_data['price']     = $order_info['price'];
-        $order_data['type_id']   = $productOrder::Popular;
-        $order_data['area_id']   = $params['area_id'];
-        $order_data['order_no']  = getOrderSN('R');
-        $order_data['user_id']   = $this->auth->id;
-        $order_data['status']    = $productOrder::Paid;
-        $order_data['num']       = 1;
+        $areaArr = explode(',', $params['area_id']);
+        $areaNum = count($areaArr);
+        if(($order_info->num + $areaNum) > $order_info->stock) $this->error(__("库存不足"));
+
         // 启动事务
         Db::startTrans();
         try {
             
             $amount = $ledgerWalletModel::getWalletChaBao($this->auth->id);
-            if(bccomp($order_info['price'], $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
+            $totalPrice = bcmul($order_info['price'], $areaNum, 2);
+            if(bccomp($totalPrice, $amount, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
             if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束"));
-
-            // 生成订单
-            $productOrder->create($order_data);
-            //修改区域状态
-            $productArea->where('id', $order_data['area_id'])->setField('status', ProductLists::STOP);
-
+            //批量地区添加
+            foreach ($areaArr as $item) {
+                       
+                $order_data['order_id']  = $params['order_id'];
+                $order_data['product_id']= $params['product_id'];
+                $order_data['price']     = $order_info['price'];
+                $order_data['type_id']   = $productOrder::Popular;
+                $order_data['area_id']   = $item;
+                $order_data['order_no']  = getOrderSN('R');
+                $order_data['user_id']   = $this->auth->id;
+                $order_data['status']    = $productOrder::Paid;
+                $order_data['num']       = 1;
+                // 生成订单
+                $productOrder->create($order_data);
+                //修改区域状态
+                $productArea->where('id', $item)->setField('status', ProductLists::STOP);
+            }
             //余额记录
-            $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$order_info['price'], $ledgerWalletModel::Popular, $this->auth->id);
+            $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$totalPrice, $ledgerWalletModel::Popular, $this->auth->id);
 
             //直推收益: pv* ×10%
             if($order_info['pv'] > 0 && $this->auth->parent_id > 0 && $productOrder::getEffectiveOrder($this->auth->parent_id) > 0){
@@ -66,8 +72,8 @@ class Order extends Api
                 $ledgerWalletModel->changeWalletAccount($this->auth->parent_id, Asset::TOKEN, $pv, $ledgerWalletModel::Direct, $this->auth->id);
             }
             //扣除库存
-            if($order_info->stock == 1 || time() >= $order_info->end_time) $order_info->status= $productPopular::STOP;
-            $order_info->num  += 1;
+            if(($order_info->stock - $areaNum) == 0 || time() >= $order_info->end_time) $order_info->status= $productPopular::STOP;
+            $order_info->num  += $areaNum;
             $order_info->save();
             // 提交事务
             Db::commit();

+ 1 - 1
application/command.php

@@ -21,6 +21,6 @@ return [
     'app\api\command\OrderPay',
     'app\api\command\Team',
     'app\api\command\RechargeTxHashVerify',
-    'app\api\command\SendTeamRewards',
+    'app\api\command\SendBonusRewards', //奖金
     'app\admin\command\Task',
 ];

+ 67 - 115
application/common/logic/TeamRewards.php → application/common/logic/BonusRewards.php

@@ -23,33 +23,25 @@ use fast\Http;
  * 团队奖励发放逻辑
  * 定时任务,定时发放,每天一次,一般在凌晨12一过开始
  */
-class TeamRewards
+class BonusRewards
 {
     /**
      * 奖励名称及类型ID
      * @var array
      */
     private array $rewards_type = [
-        'power' => [
+        'community' => [
             'type_id' => 1,
-            'type_name' => "团队算力奖励"
+            'type_name' => "社区建设津贴"
         ],
-        'usdt' => [
+        'service' => [
             'type_id' => 2,
-            'type_name' => "加权分红"
+            'type_name' => "服务津贴"
         ],
-        'market' => [
+        'together' => [
             'type_id' => 3,
-            'type_name' => "团队在、系统领导人加权分红"
-        ],
-        'team' => [
-            'type_id' => 4,
-            'type_name' => "社区长业绩分红"
-        ],
-        'allocate' => [
-            'type_id' => 5,
-            'type_name' => "算力拨币"
-        ],
+            'type_name' => "共创津贴"
+        ]
     ];
 
     /**
@@ -61,17 +53,15 @@ class TeamRewards
      */
     public function powerRewards()
     {
-        $date = $this->getExeDate($this->rewards_type['power']['type_id']);
-
-        if(empty($date)){
-            return "本次没有可用时间";
-        }
+        $date = $this->getExeDate($this->rewards_type['community']['type_id']);
+        if(empty($date))  return "本次没有可用时间";
+        
 
         //创建定时任务执行日志
         $TimedTaskLog = new TimedTaskLogModel();
         $log_id = $TimedTaskLog->insertGetId([
-            'type_id'       => $this->rewards_type['power']['type_id'],
-            'type_name'     => $this->rewards_type['power']['type_name'],
+            'type_id'       => $this->rewards_type['community']['type_id'],
+            'type_name'     => $this->rewards_type['community']['type_name'],
             'date_time'     => $date,
             'create_time'   => date('Y-m-d H:i:s'),
 
@@ -142,61 +132,7 @@ class TeamRewards
         return $log_id;
     }
 
-    /**
-     * 获取可用日期
-     * @var array
-     */
-    protected function getExeDate($type_id = 1)
-    {
-        $today = date('Y-m-d',strtotime("-1day"));//当前时间减一天为最后一次可用时间
-
-        $dateInfo = DB::table('timed_task_log')
-            ->where('type_id' ,$type_id)
-            ->where('status', 1)
-            ->order('date_time', 'desc')
-            ->find();
-        if(empty($dateInfo)){//首次
-            return $today;
-        }
 
-        $new_date = date('Y-m-d',strtotime("+1day",strtotime($dateInfo['date_time'])));//最后一次发放日期+1天为本次执行时间
-
-        if($new_date > $today){//超过今天
-            return "";
-        }
-        return $new_date;
-    }
-
-    /**
-     * 插入或更新收益统计信息
-     * @var array
-     */
-    protected function createData($uids, $amount, $date)
-    {
-        foreach ($uids as $user_id) {
-            $check = DB::table('team_rewards')
-                ->field('id')
-                ->where('user_id', $user_id)
-                ->where('date_time', $date)
-                ->find();
-            if ($check) {//存在,则更新
-                DB::table('team_rewards')
-                    ->where('id', $check['id'])
-                    ->update([
-                        'today_power' => Db::raw('today_power+' . $amount),
-                        'update_time' => date('Y-m-d H:i:s')
-                    ]);
-            } else {//新增
-                DB::table('team_rewards')
-                    ->insert([
-                        'user_id' => $user_id,
-                        'date_time' => $date,
-                        'today_power' => Db::raw('today_power+' . $amount),
-                        'create_time' => date('Y-m-d H:i:s')
-                    ]);
-            }
-        }
-    }
 
     /**
      * 计算理论收益
@@ -236,19 +172,6 @@ class TeamRewards
                     'update_time'   => date('Y-m-d H:i:s')
                 ]);
 
-//            //更新所有上级的 team_commission
-//            $path = (new UserPathModel())
-//                ->where('user_id', $info['user_id'])
-//                ->column('parent_id');
-//
-//            DB::table('team_rewards')
-//                ->where('user_id', 'in', $path)
-//                ->where('date_time', $info['date_time'])
-//                ->update([
-//                    'team_commission' => DB::raw("team_commission+" . $amount),
-//                    'update_time'   => date('Y-m-d H:i:s')
-//                ]);
-
         }
     }
 
@@ -316,7 +239,6 @@ class TeamRewards
                 }else{
                     Log::info("分红等级" . $level['level_id'] . "在" . $date . "分红金额为0;当日新增业绩:" . $amount . ",分红比例:" . $level['weight_dividend']. ",分红人数" . dump($levels_info[$level['level_id']]));
                 }
-
             }
         }
         return $log_id;
@@ -538,30 +460,6 @@ class TeamRewards
             }
         }
 
-
-//        foreach ($community_user_list as $info) {
-//            //取该社区长下新增服务器算力
-//            $next_ids = (new UserPathModel())->where('parent_id', $info['id'])->column('user_id');
-//            $recharge_power = OfflineRechargeRecordModel::where('user_id', 'in', $next_ids)
-//                //->where('order_type', 2)
-//                ->where('status', 1)
-//                ->whereBetween('create_time', $between_time)
-//                ->sum('power');
-//            $recharge_power = $community_rewards_arr[0] * $recharge_power;//取新增业绩的8%
-//            if($recharge_power < 1){
-//                (new Output())->writeln("会员:" . $info['id'] . ",本次没有可用新增数据:" . $date);
-//                continue;
-//            }
-//            $reward_power = $recharge_power * $community_rewards_arr[1];
-//            $reward_usdt = $recharge_power * $community_rewards_arr[2] * 8;//算力折算成U
-//            dump(['用户ID:' . $info['id'],'新增业绩:' . $recharge_power, '算力收益:' . $reward_power, 'U收益:' . $reward_usdt]);
-//
-//            //发放分红
-//            (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::POWER, $reward_power, Action::PowerCommunityBonusAward);
-//
-//            (new LedgerWalletModel)->changeWalletAccount($info['id'], Asset::USDT, $reward_usdt, Action::UsdtCmmunityBonus);
-//        }
-
         return $log_id;
     }
 
@@ -619,4 +517,58 @@ class TeamRewards
 
         return $log_id;
     }
+
+    /**
+     * 获取可用日期
+     * @var array
+     */
+    protected function getExeDate($type_id = 1)
+    {
+        $today = date('Y-m-d',strtotime("-1day"));//当前时间减一天为最后一次可用时间
+
+        $dateInfo = DB::table('timed_task_log')
+            ->where('type_id' ,$type_id)
+            ->where('status', 1)
+            ->order('date_time', 'desc')
+            ->find();
+        if(empty($dateInfo)){//首次
+            return $today;
+        }
+        $new_date = date('Y-m-d',strtotime("+1day",strtotime($dateInfo['date_time'])));//最后一次发放日期+1天为本次执行时间
+        if($new_date > $today){//超过今天
+            return "";
+        }
+        return $new_date;
+    }
+
+    /**
+     * 插入或更新收益统计信息
+     * @var array
+     */
+    protected function createData($uids, $amount, $date)
+    {
+        foreach ($uids as $user_id) {
+            $check = DB::table('team_rewards')
+                ->field('id')
+                ->where('user_id', $user_id)
+                ->where('date_time', $date)
+                ->find();
+            if ($check) {//存在,则更新
+                DB::table('team_rewards')
+                    ->where('id', $check['id'])
+                    ->update([
+                        'today_power' => Db::raw('today_power+' . $amount),
+                        'update_time' => date('Y-m-d H:i:s')
+                    ]);
+            } else {//新增
+                DB::table('team_rewards')
+                    ->insert([
+                        'user_id' => $user_id,
+                        'date_time' => $date,
+                        'today_power' => Db::raw('today_power+' . $amount),
+                        'create_time' => date('Y-m-d H:i:s')
+                    ]);
+            }
+        }
+    }
 }