From 1051e0c7a9a3c749bdeac53e3fa02d9dbd625741 Mon Sep 17 00:00:00 2001
From: liu <1873441552@qq.com>
Date: Thu, 20 Jun 2024 14:41:04 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=97=A8=E5=BA=97=E5=95=86?=
 =?UTF-8?q?=E5=93=81=E6=8E=92=E8=A1=8C=E7=9B=B8=E5=85=B3=E7=9A=84=E6=9F=A5?=
 =?UTF-8?q?=E8=AF=A2=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/store/controller/WorkbenchController.php |  31 ++++--
 app/store/logic/WorkbenchLogic.php           | 111 +++++++++++++++++++
 2 files changed, 134 insertions(+), 8 deletions(-)

diff --git a/app/store/controller/WorkbenchController.php b/app/store/controller/WorkbenchController.php
index 92188eed8..1e6fab099 100644
--- a/app/store/controller/WorkbenchController.php
+++ b/app/store/controller/WorkbenchController.php
@@ -130,11 +130,12 @@ class WorkbenchController extends BaseAdminController
     //     ApiDoc\Header(ref: [Definitions::class, "token"]),
     //     ApiDoc\ResponseSuccess("data", type: "array"),
     // ]
-    public function get_trend(\app\admin\controller\WorkbenchController $workbench)
+    public function get_trend()
     {
-        $params = $this->request->get();
-        $params['store_id'] = $this->request->adminInfo['store_id'];
-        return $workbench->get_trend();
+//        $params = $this->request->get();
+        $store_id = $this->request->adminInfo['store_id'];
+        $workbench = WorkbenchLogic::get_trend($store_id);
+        return $this->data($workbench);
     }
 
     // #[
@@ -148,11 +149,25 @@ class WorkbenchController extends BaseAdminController
     //     ApiDoc\Header(ref: [Definitions::class, "token"]),
     //     ApiDoc\ResponseSuccess("data", type: "array"),
     // ]
-    public function get_product_ranking(\app\admin\controller\WorkbenchController $workbench)
+    public function get_product_ranking()
     {
-        $params = $this->request->get();
-        $params['store_id'] = $this->request->adminInfo['store_id'];
-        return $workbench->get_product_ranking();
+//        $params = $this->request->get();
+        $dateRange = $this->request->get('date');
+        // 拆分日期范围
+        list($startDate, $endDate) = explode('-', $dateRange);
+        $startTime = str_replace('/', '-', $startDate);
+        $endTime = str_replace('/', '-', $endDate);
+        if (empty($startTime)) {
+            $startTime = strtotime(date('Y-m-d'));
+            $endTime = $startTime + 86400;
+        }
+        $where = [
+            ['create_time', 'between', [$startTime, $endTime]],
+            ['store_id','=',$this->request->adminInfo['store_id']]
+        ];
+
+        $workbench = WorkbenchLogic::get_product_ranking($where);
+        return $this->data($workbench);
     }
 
 
diff --git a/app/store/logic/WorkbenchLogic.php b/app/store/logic/WorkbenchLogic.php
index 5b7f8872e..e887a4f71 100644
--- a/app/store/logic/WorkbenchLogic.php
+++ b/app/store/logic/WorkbenchLogic.php
@@ -15,6 +15,7 @@
 namespace app\store\logic;
 
 
+use app\admin\logic\statistic\ProductStatisticLogic;
 use app\common\enum\OrderEnum;
 use app\common\enum\PayEnum;
 use app\common\enum\YesNoEnum;
@@ -725,6 +726,116 @@ class WorkbenchLogic extends BaseLogic
 
 
 
+    /**
+     * 商品趋势
+     */
+    public static function get_trend($store_id)
+    {
+        $dates = [];
+        $today = new \DateTime();
+        $thirtyDaysAgo = new \DateTime($today->format('Y-m-d'));
+        $thirtyDaysAgo->modify('-30 days');
+
+        for ($i = 0; $i < 31; $i++) {
+            $date = new \DateTime($thirtyDaysAgo->format('Y-m-d'));
+            $date->modify('+' . $i . ' days');
+            $dates[] = $date->format('Y-m-d');
+        }
+        $data = [
+            "xAxis" => $dates,
+            "series" => [
+                [
+                    "name" => "商品浏览量",
+                    "data" => self::store_visit_count($dates,$store_id),
+                    "type" => "line",
+                    "smooth" => "true",
+                    "yAxisIndex" => 1
+                ],
+                [
+                    "name" => "商品访客量",
+                    "data" => self::store_visit_user($dates,$store_id),
+                    "type" => "line",
+                    "smooth" => "true",
+                    "yAxisIndex" => 1
+                ],
+                [
+                    "name" => "支付金额",
+                    "data" => self::payPrice($dates,$store_id),
+                    "type" => "bar"
+                ],
+                [
+                    "name" => "退款金额",
+                    "data" => self::refundPrice($dates,$store_id),
+                    "type" => "bar"
+                ]
+            ]
+        ];
+        return  $data;
+
+    }
+
+
+
+    /**
+     * 商品浏览量
+     */
+    public static function store_visit_count($dates,$store_id)
+    {
+        $data = [];
+        foreach ($dates as $date) {
+            $data[] = StoreVisit::whereDay('create_time', $date)->where('store_id',$store_id)->cache('store_visit_count_' . $date, 300)->sum('count');
+        }
+        return $data;
+    }
+
+
+    /**
+     * 商品浏览量
+     */
+    public static function store_visit_user($dates,$store_id)
+    {
+        $data = [];
+        foreach ($dates as $date) {
+            $data[] = StoreVisit::whereDay('create_time', $date)->where('store_id',$store_id)->cache('store_visit_user_' . $date, 300)->count('uid');
+        }
+        return $data;
+    }
+
+
+    /**
+     * 支付金额
+     */
+    public static function payPrice($dates,$store_id)
+    {
+        $data = [];
+        foreach ($dates as $date) {
+            $data[] = StoreOrder::whereDay('create_time', $date)->where('store_id',$store_id)->cache('payPrice_' . $date, 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
+        }
+        return $data;
+    }
+
+
+    /**
+     * 退款金额
+     */
+    public static function refundPrice($dates,$store_id)
+    {
+        $data = [];
+        foreach ($dates as $date) {
+            $data[] = StoreOrder::whereDay('create_time', $date)->where('store_id',$store_id)->where('status', 'in', [-1, -2])->cache('refundPrice_' . $date, 300)->where('paid', 1)->sum('pay_price');
+        }
+        return $data;
+    }
+
+
+
+    /**
+     * 获取商品排名数据
+     */
+    public static function get_product_ranking($where)
+    {
+        return (new ProductStatisticLogic())->get_product_ranking($where);
+    }
 
 
 }