From 3342603af019e162976d08c09cde9913730a9a19 Mon Sep 17 00:00:00 2001
From: lizhilin <1007915129@qq.com>
Date: Wed, 9 Oct 2024 17:17:57 +0800
Subject: [PATCH] 更新

---
 app/Admin/Forms/VerifierCodeForm.php           |  1 +
 app/Http/Controllers/Api/CommentController.php | 12 +++++++++---
 app/Http/Controllers/Api/IncomeController.php  |  7 ++++++-
 app/Http/Controllers/Api/SystemController.php  | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 app/Http/Controllers/Api/UserController.php    | 17 +++++++++++------
 app/Models/OrderDivideRecord.php               |  1 +
 routes/api.php                                 |  4 ++++
 7 files changed, 90 insertions(+), 10 deletions(-)

diff --git a/app/Admin/Forms/VerifierCodeForm.php b/app/Admin/Forms/VerifierCodeForm.php
index 44a7f1c..5d689dd 100644
--- a/app/Admin/Forms/VerifierCodeForm.php
+++ b/app/Admin/Forms/VerifierCodeForm.php
@@ -30,6 +30,7 @@ public function handle(array $input)
         $order = OrderInfo::find($this->payload['id']);
         $order->order_status = 2; //待领取状态
         $order->verification_code = $code;
+        $order->verifi_code_at = date("Y-m-d H:i:s");
         $order->save();
         return $this->response()->success('确认成功')->refresh();
     }
diff --git a/app/Http/Controllers/Api/CommentController.php b/app/Http/Controllers/Api/CommentController.php
index d1f7977..bd91ed7 100644
--- a/app/Http/Controllers/Api/CommentController.php
+++ b/app/Http/Controllers/Api/CommentController.php
@@ -44,14 +44,20 @@ public function add(Request $request)
                 $ogObj->is_comment = 1;
                 $ogObj->save();
             }
-            DB::commit();
+
             //2分钟执行订单分佣
             $count = OrderGoods::where('order_id', $oid)->where('is_comment', 0)->count();
             Log::add('订单商品评论' . $og_id, ['num' => $count]);
             if ($count == 0) {
-                Log::add('调用分佣', $orderObj->toArray());
-                $this->dispatch(new AutoCompleteOrder($orderObj, 10));
+                if ($useObj->spuid || $useObj->merchant_id) {
+                    $orderObj->is_commission = 1;
+                    $orderObj->order_status = 4;
+                    $orderObj->save();
+                }
+                // Log::add('调用分佣', $orderObj->toArray());
+                // $this->dispatch(new AutoCompleteOrder($orderObj, 10));
             }
+            DB::commit();
             return $this->JsonResponse('');
         } catch (\Exception $exception) {
             Log::add('添加评论失败', $exception->getMessage());
diff --git a/app/Http/Controllers/Api/IncomeController.php b/app/Http/Controllers/Api/IncomeController.php
index 6a7a94d..4deb80f 100644
--- a/app/Http/Controllers/Api/IncomeController.php
+++ b/app/Http/Controllers/Api/IncomeController.php
@@ -72,7 +72,12 @@ public function add(Request $request)
         if ($balance < $money) {
             return $this->JsonResponse('', '余额不足', 500);
         }
-
+        $exist = Income::where(['user_type' => $type, 'um_id' => $um_id])
+            ->orderBy('id', 'DESC')
+            ->first();
+        if ($exist) {
+            return $this->JsonResponse('', '提现审核中!', 500);
+        }
         DB::beginTransaction();
         try {
             $comObj = new Income();
diff --git a/app/Http/Controllers/Api/SystemController.php b/app/Http/Controllers/Api/SystemController.php
index 76b4ac5..8a89211 100644
--- a/app/Http/Controllers/Api/SystemController.php
+++ b/app/Http/Controllers/Api/SystemController.php
@@ -3,6 +3,8 @@
 namespace App\Http\Controllers\Api;
 
 use App\Command\Log;
+use App\Models\OrderInfo as OrderInfoModel;
+use App\Models\OrderDivideRecord;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\DB;
 
@@ -22,4 +24,60 @@ public function update(Request $request)
 
         return $this->JsonResponse('');
     }
+
+    //定时任务--待领取状态下七天未领取,自动到待评价状态
+    public function autoChangeReceiveStatus()
+    {
+        $chunkSize = 50; // 每次处理的订单数量
+        $orders = OrderInfoModel::where(["order_status" => 2])->whereNull('verification_at')->whereNotNull('verifi_code_at')->orderBy('id')->take($chunkSize);
+        while ($orders->count() > 0) {
+            $orders->chunk($chunkSize, function ($batch) {
+                foreach ($batch as $order) {
+                    $nowtime = time();
+                    if ($order->verifi_code_at) {
+                        $wait_time = strtotime($order->verifi_code_at);
+                        $diff_time = $nowtime - $wait_time;
+                        $d = $diff_time / 86400;
+                        if ($d >= 7) {
+                            $order->order_status = 3;
+                            $order->save();
+                        }
+                    }
+                }
+            });
+
+            // 移动游标到下一批
+            $orders->skip($chunkSize);
+            $orders = $orders->getQuery();
+            sleep(2);
+        }
+
+        return '--ok--';
+    }
+
+    //定时任务--订单分佣
+    public function autoOrderCommission()
+    {
+        $chunkSize = 50; // 每次处理的订单数量
+        $orders = OrderInfoModel::where(["order_status" => 4, 'is_commission' => 1])->orderBy('id')->take($chunkSize);
+        while ($orders->count() > 0) {
+            $orders->chunk($chunkSize, function ($batch) {
+                foreach ($batch as $order) {
+                    //佣金分配
+                    $res = OrderDivideRecord::divide($order->id);
+                    if ($res) {
+                        $order->is_commission = 2;
+                        $order->save();
+                    }
+                }
+            });
+
+            // 移动游标到下一批
+            $orders->skip($chunkSize);
+            $orders = $orders->getQuery();
+            sleep(3);
+        }
+
+        return '--ok--';
+    }
 }
diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php
index 526e73d..9564733 100644
--- a/app/Http/Controllers/Api/UserController.php
+++ b/app/Http/Controllers/Api/UserController.php
@@ -369,14 +369,19 @@ public function share(Request $request)
         if (!$spObj) {
             return $this->JsonResponse('', '参数错误', 201);
         }
+        //分享人注册时间
+        $sp_time = strtotime($spObj->created_at);
 
         $userObj = $request->user();
-        $user_id = $userObj->id;
-        $spuid = $userObj->spuid; //是否已有推荐人
-        if (!$spuid && $user_id != $shuid) {
-            $userObj->spuid = $shuid; //直推
-            $userObj->second_spuid = $spObj->spuid; //间推
-            $userObj->save();
+        $user_time = strtotime($userObj->created_at);
+        if ($user_time > $sp_time) {
+            $user_id = $userObj->id;
+            $spuid = $userObj->spuid; //是否已有推荐人
+            if (!$spuid && $user_id != $shuid) {
+                $userObj->spuid = $shuid; //直推
+                $userObj->second_spuid = $spObj->spuid; //间推
+                $userObj->save();
+            }
         }
         return $this->JsonResponse('');
     }
diff --git a/app/Models/OrderDivideRecord.php b/app/Models/OrderDivideRecord.php
index 01840ce..1737949 100644
--- a/app/Models/OrderDivideRecord.php
+++ b/app/Models/OrderDivideRecord.php
@@ -69,6 +69,7 @@ public static function divide($order_id)
                 self::addRecord($item->id, $order_id, $goods_amount, $divide_price, $merchant_commission, $merchant_id, $user_id, 3);
             }
         }
+        return true;
     }
 
     public static function addRecord($og_id, $order_id, $goods_amount, $divide_price, $commission, $um_id, $user_id, $sh_type)
diff --git a/routes/api.php b/routes/api.php
index 5a5313f..3149959 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -74,6 +74,10 @@
 
     Route::get('send/config/update', 'SystemController@update');
 
+    Route::get('auto-to-commentstatus', 'SystemController@autoChangeReceiveStatus');  //定时任务--待领取状态下七天未领取,自动到待评价状态
+
+    Route::get('auto-order-commission', 'SystemController@autoOrderCommission'); //定时完成--订单完成后分佣
+
     Route::middleware('auth:sanctum')->group(function () {
 
         Route::get('user-info', 'UserController@info');  //获取小程序端用户资料
--
libgit2 0.26.0