Parcourir la source

update statistics

xielin il y a 6 ans
Parent
commit
2bd41df02b

+ 53 - 0
app/Http/Controllers/V1/IndexController.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace App\Http\Controllers\V1;
+
+use App\Repositories\StatisticsRepository;
+use Carbon\Carbon;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
+
+class IndexController extends Controller
+{
+    public function __construct(StatisticsRepository $statisticsRepository)
+    {
+        $this->statisticsRepository = $statisticsRepository;
+    }
+
+    /**
+     * 获取统计信息
+     * @param Request $request
+     */
+    public function index(Request $request)
+    {
+//        $user = Auth::user();
+//        $shopId = $user->shop_id;
+        $shopId = 1;
+        $request = $request->all();
+        $start = Carbon::parse(isset($request['start']) ? $request['start'] : Carbon::now())->startOfDay()->format('Y-m-d\TH:i:s\Z');
+        $end = Carbon::parse(isset($request['end']) ? $request['end'] : Carbon::now())->endOfDay()->format('Y-m-d\TH:i:s\Z');
+        $data['paid'] = $this->statisticsRepository->getOrderPayInfo($start, $end, $shopId);
+        $data['dfh_order'] = $this->statisticsRepository->getDfhOrder($start, $end, $shopId);
+        $data['finish_order'] = $this->statisticsRepository->getFinishOrder($start, $end, $shopId);
+        $data['total_order'] = $this->statisticsRepository->getOrderNum($start, $end, $shopId);
+        $data['refund_order'] = $this->statisticsRepository->getRefundOrderNum($start, $end, $shopId);
+        $data['feedback_order'] = $this->statisticsRepository->getFeedBackOrderNum($start, $end, $shopId);
+        return $data;
+    }
+
+    /**
+     * 获取销售额
+     * @param Request $request
+     * @return array
+     */
+    public function sales(Request $request)
+    {
+//        $user = Auth::user();
+//        $shopId = $user->shop_id;
+        $shopId = 1;
+        $request = $request->all();
+        $start = Carbon::parse(isset($request['start']) ? $request['start'] : '-6 days')->startOfDay()->format('Y-m-d\TH:i:s\Z');
+        $end = Carbon::parse(isset($request['end']) ? $request['end'] : Carbon::now())->endOfDay()->format('Y-m-d\TH:i:s\Z');
+        return $this->statisticsRepository->getSaleMoney($start, $end, $shopId);
+    }
+}

+ 453 - 0
app/Repositories/StatisticsRepository.php

@@ -0,0 +1,453 @@
+<?php
+
+namespace App\Repositories;
+
+
+use Carbon\Carbon;
+use Illuminate\Support\Facades\Log;
+
+class StatisticsRepository
+{
+    public function __construct()
+    {
+    }
+
+    /**
+     * 获取付款订单总金额,总支付人数,总订单数
+     * @param string $start
+     * @param string $end
+     * @return array
+     */
+    public function getOrderPayInfo($start = '', $end = '', $shopId = '')
+    {
+        $params = [
+            "index" => "platform_order_statistics",
+            "type" => "_doc",
+            "body" => [
+                "size" => 0,
+                "query" => [
+                    "bool" => [
+                        "must" => [
+                            [
+                                "terms" => [
+                                    "status" => [1, 2, 3, 4, 5, 6]
+                                ]
+                            ],
+                            [
+                                "term" => [
+                                    "shop_id" => $shopId
+                                ]
+                            ],
+                            [
+                                "range" => [
+                                    "created_at" => [
+                                        "from" => $start,
+                                        "to" => $end,
+                                        "include_lower" => true,
+                                        "include_upper" => true,
+                                    ]
+                                ]
+                            ]
+                        ]
+                    ]
+                ],
+                "aggs" => [
+                    "total_fee" => [
+                        "sum" => [
+                            "field" => "real_price"
+                        ]
+                    ],
+                    "pay_user_num" => [
+                        "cardinality" => [
+                            "field" => "uid"
+                        ]
+                    ],
+                    "pay_order_num" => [
+                        "cardinality" => [
+                            "field" => "id"
+                        ]
+                    ]
+                ]
+            ]
+        ];
+
+        $result = \Elasticsearch::search($params);
+        Log::debug('get-order-payinfo:' . json_encode($params));
+        return [
+            'total_fee' => $result['aggregations']['total_fee']['value'],
+            'pay_user_num' => $result['aggregations']['pay_user_num']['value'],
+            'pay_order_num' => $result['aggregations']['pay_order_num']['value'],
+        ];
+    }
+
+    /**
+     * 获取待发货订单数
+     * @param string $start
+     * @param string $end
+     * @return array
+     */
+    public function getDfhOrder($start = '', $end = '', $shopId = '')
+    {
+        $params = [
+            "index" => "platform_order_statistics",
+            "type" => "_doc",
+            "body" => [
+                "size" => 0,
+                "query" => [
+                    "bool" => [
+                        "must" => [
+                            [
+                                "term" => [
+                                    "status" => [
+                                        "value" => 1
+                                    ]
+                                ]
+                            ],
+                            [
+                                "term" => [
+                                    "shop_id" => $shopId
+                                ]
+                            ],
+                            [
+                                "range" => [
+                                    "created_at" => [
+                                        "from" => $start,
+                                        "to" => $end,
+                                        "include_lower" => true,
+                                        "include_upper" => true,
+                                    ]
+                                ]
+                            ]
+                        ]
+                    ]
+                ],
+                "aggs" => [
+                    "dfh_order_num" => [
+                        "cardinality" => [
+                            "field" => "id"
+                        ]
+                    ]
+                ]
+            ]
+        ];
+
+        $result = \Elasticsearch::search($params);
+        Log::debug('get-dfh-order:' . json_encode($params));
+        return [
+            'dfh_order_num' => $result['aggregations']['dfh_order_num']['value']
+        ];
+    }
+
+    /**
+     * 获取完成订单数
+     * @param string $start
+     * @param string $end
+     * @return array
+     */
+    public function getFinishOrder($start = '', $end = '', $shopId = '')
+    {
+        $params = [
+            "index" => "platform_order_statistics",
+            "type" => "_doc",
+            "body" => [
+                "size" => 0,
+                "query" => [
+                    "bool" => [
+                        "must" => [
+                            [
+                                "terms" => [
+                                    "status" => [5, 6]
+                                ]
+                            ],
+                            [
+                                "term" => [
+                                    "shop_id" => $shopId
+                                ]
+                            ],
+                            [
+                                "range" => [
+                                    "created_at" => [
+                                        "from" => $start,
+                                        "to" => $end,
+                                        "include_lower" => true,
+                                        "include_upper" => true,
+                                    ]
+                                ]
+                            ]
+                        ]
+                    ]
+                ],
+                "aggs" => [
+                    "total_fee" => [
+                        "sum" => [
+                            "field" => "real_price"
+                        ]
+                    ],
+                    "finish_order_num" => [
+                        "cardinality" => [
+                            "field" => "id"
+                        ]
+                    ]
+                ]
+            ]
+        ];
+
+        $result = \Elasticsearch::search($params);
+        Log::debug('get-finish-order:' . json_encode($params));
+        return [
+            'finish_total_fee' => $result['aggregations']['total_fee']['value'],
+            'finish_order_num' => $result['aggregations']['finish_order_num']['value']
+        ];
+    }
+
+    /**
+     * 获取总订单数
+     * @param string $start
+     * @param string $end
+     * @return array
+     */
+    public function getOrderNum($start = '', $end = '', $shopId = '')
+    {
+        $params = [
+            "index" => "platform_order_statistics",
+            "type" => "_doc",
+            "body" => [
+                "size" => 0,
+                "query" => [
+                    "bool" => [
+                        "must" => [
+                            [
+                                "term" => [
+                                    "shop_id" => $shopId
+                                ]
+                            ],
+                            [
+                                "range" => [
+                                    "created_at" => [
+                                        "from" => $start,
+                                        "to" => $end,
+                                        "include_lower" => true,
+                                        "include_upper" => true,
+                                    ]
+                                ]
+                            ]
+                        ]
+                    ]
+                ],
+                "aggs" => [
+                    "total_order_num" => [
+                        "cardinality" => [
+                            "field" => "id"
+                        ]
+                    ]
+                ]
+            ]
+        ];
+
+        $result = \Elasticsearch::search($params);
+        Log::debug('get-order-num:' . json_encode($params));
+        return [
+            'total_order_num' => $result['aggregations']['total_order_num']['value']
+        ];
+    }
+
+    /**
+     * 获取总退单数
+     * @param string $start
+     * @param string $end
+     * @return array
+     */
+    public function getRefundOrderNum($start = '', $end = '', $shopId = '')
+    {
+        $params = [
+            "index" => "platform_order_statistics",
+            "type" => "_doc",
+            "body" => [
+                "size" => 0,
+                "query" => [
+                    "bool" => [
+                        "must" => [
+                            [
+                                "terms" => [
+                                    "feedback_status" => [1, 2]
+                                ]
+                            ],
+                            [
+                                "term" => [
+                                    "shop_id" => $shopId
+                                ]
+                            ],
+                            [
+                                "range" => [
+                                    "created_at" => [
+                                        "from" => $start,
+                                        "to" => $end,
+                                        "include_lower" => true,
+                                        "include_upper" => true,
+                                    ]
+                                ]
+                            ]
+                        ]
+                    ]
+                ],
+                "aggs" => [
+                    "refund_order_num" => [
+                        "cardinality" => [
+                            "field" => "id"
+                        ]
+                    ]
+                ]
+            ]
+        ];
+
+        $result = \Elasticsearch::search($params);
+        Log::debug('get-refund-order:' . json_encode($params));
+        return [
+            'refund_order_num' => $result['aggregations']['refund_order_num']['value']
+        ];
+    }
+
+    /**
+     * 获取维权订单数
+     * @param string $start
+     * @param string $end
+     * @return array
+     */
+    public function getFeedBackOrderNum($start = '', $end = '', $shopId = '')
+    {
+        $params = [
+            "index" => "platform_order_statistics",
+            "type" => "_doc",
+            "body" => [
+                "size" => 0,
+                "query" => [
+                    "bool" => [
+                        "must" => [
+                            [
+                                "terms" => [
+                                    "feedback_status" => [1, 2, 3]
+                                ]
+                            ],
+                            [
+                                "term" => [
+                                    "shop_id" => $shopId
+                                ]
+                            ],
+                            [
+                                "range" => [
+                                    "created_at" => [
+                                        "from" => $start,
+                                        "to" => $end,
+                                        "include_lower" => true,
+                                        "include_upper" => true,
+                                    ]
+                                ]
+                            ]
+                        ]
+                    ]
+                ],
+                "aggs" => [
+                    "feedback_order_num" => [
+                        "cardinality" => [
+                            "field" => "id"
+                        ]
+                    ]
+                ]
+            ]
+        ];
+
+        $result = \Elasticsearch::search($params);
+        Log::debug('get-feedback-ordernum:' . json_encode($params));
+        return [
+            'feedback_order_num' => $result['aggregations']['feedback_order_num']['value']
+        ];
+    }
+
+    /**
+     * 获取销售额
+     * @param string $start
+     * @param string $end
+     * @return array
+     */
+    public function getSaleMoney($start = '', $end = '', $shopId = '')
+    {
+        $params = [
+            "index" => "platform_order_statistics",
+            "type" => "_doc",
+            "body" => [
+                "size" => 0,
+                "query" => [
+                    "bool" => [
+                        "must" => [
+                            [
+                                "terms" => [
+                                    "status" => [1, 2, 3, 4, 5, 6]
+                                ]
+                            ],
+                            [
+                                "term" => [
+                                    "shop_id" => $shopId
+                                ]
+                            ],
+                            [
+                                "range" => [
+                                    "created_at" => [
+                                        "from" => $start,
+                                        "to" => $end,
+                                        "include_lower" => true,
+                                        "include_upper" => true,
+                                    ]
+                                ]
+                            ]
+                        ]
+                    ]
+                ],
+                "aggs" => [
+                    "sale_per_day" => [
+                        "date_histogram" => [
+                            "field" => "created_at",
+                            "time_zone" => "Asia/Shanghai",
+                            "interval" => "1d",
+//                            "offset" => 0,
+//                            "order" => [
+//                                "_key" => "asc"
+//                            ],
+//                            "keyed" => false,
+                            "min_doc_count" => 0,
+                            "format" => "yyyy-MM-dd",
+                            "extended_bounds" => [
+                                "min" => Carbon::parse($start)->format('Y-m-d'),
+                                "max" => Carbon::parse($end)->format('Y-m-d'),
+                            ]
+                        ],
+                        "aggs" => [
+                            "total_fee" => [
+                                "sum" => [
+                                    "field" => "real_price"
+                                ]
+                            ],
+                            "pay_user_num" => [
+                                "cardinality" => [
+                                    "field" => "uid"
+                                ]
+                            ]
+                        ]
+                    ]
+
+                ]
+            ]
+        ];
+
+        $result = \Elasticsearch::search($params);
+        Log::debug('get-sale-money:' . json_encode($params));
+        $tmp = [];
+        foreach ($result['aggregations']['sale_per_day']['buckets'] as $row) {
+            $tmpDate = Carbon::parse($row['key_as_string'])->format('m-d');
+            $tmp[$tmpDate] = [
+                'pay_user_num' => $row['pay_user_num']['value'],
+                'total_fee' => $row['total_fee']['value'],
+            ];
+        }
+        return $tmp;
+    }
+}

+ 2 - 2
routes/api.php

@@ -16,9 +16,7 @@ $api = app('Dingo\Api\Routing\Router');
 $api->version('v1', [
     'namespace' => 'App\Http\Controllers\V1',
 ], function ($api) {
-    //登录
 
-    //$api->post('login', 'AuthController@authenticate');
     //新增编辑
     $api->post('addShop', 'ShopController@addShop');
     //商户详情
@@ -30,6 +28,8 @@ $api->version('v1', [
     //获取商户信息
     $api->post('getShopInfo', 'ShopController@getTokenShop');
 
+    $api->get('statistics', 'IndexController@index');
+    $api->get('statistics/sales', 'IndexController@sales');
 
     $api->group(['middleware' => 'auth:api'], function ($api) {
         //注册