Переглянути джерело

订单量趋势 快递分布查询

ANG YU 5 роки тому
батько
коміт
ce9bd50dcc

+ 11 - 1
app/Http/Controllers/ControlPanelController.php

@@ -27,7 +27,9 @@ class ControlPanelController extends Controller
         $start = (new Carbon())->subMonth()->toDateString();
         $end = (new Carbon())->toDateString();
         $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end);
-        return view('control.panel', compact('menus', 'warehousesOrders', 'orderCountingRecords'));
+        $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end);
+        $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end);
+        return view('control.panel', compact('menus', 'warehousesOrders', 'orderCountingRecords', 'logisticsCountingRecords', 'warehouseCountingRecords'));
     }
 
     public function orderCountingRecordsApi(Request $request)
@@ -39,4 +41,12 @@ class ControlPanelController extends Controller
         $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end, $unit);
         return compact('orderCountingRecords');
     }
+    public function logisticsCountingRecordsApi(Request $request)
+    {
+        $orderCountingRecordService = app(OrderCountingRecordService::class);
+        $start = $request->start;
+        $end = $request->end;
+        $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end);
+        return compact('logisticsCountingRecords');
+    }
 }

+ 2 - 1
app/Http/Controllers/CustomerController.php

@@ -8,6 +8,7 @@ use Exception;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
+use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Gate;
 use Illuminate\Support\Facades\Http;
 
@@ -82,7 +83,7 @@ class CustomerController extends Controller
         if(!Gate::allows('客户管理-项目-查询')){ return redirect('denied');  }
         /** @var OwnerService $service */
         $service = app('OwnerService');
-        $customers = $service->get(['customer_id'=>true],['customer']);
+        $customers = $service->get(Auth::user(),['customer_id'=>true],['customer']);
         return response()->view('customer.project.index');
     }
 

+ 76 - 13
app/Services/OrderCountingRecordService.php

@@ -4,14 +4,17 @@
 namespace App\Services;
 
 
+use App\Logistic;
 use App\Order;
 use App\OrderCountingRecord;
+use App\Warehouse;
 use Carbon\Carbon;
 use DateTime;
 use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Support\Facades\Cache;
 use App\User;
 use Illuminate\Support\Str;
+use function MongoDB\BSON\toJSON;
 
 class OrderCountingRecordService
 {
@@ -23,20 +26,28 @@ class OrderCountingRecordService
         $this->ownerService = app(OwnerService::class);
     }
 
-    public function get($start, $end, $ownerIds = null, $unit = '日')
+    /**
+     * @param $start
+     * @param $end
+     * @param null $ownerIds
+     * @param string $unit
+     * @param $user
+     * @return mixed
+     */
+    public function get($start, $end, $ownerIds = null, $unit = '日', $user)
     {
-        $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit);
+        $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit, $user);
         if (!($resultByCache['unExistingOrders'])) return $resultByCache['resultOrders'];
         $resultByOrderCountingRecords = $this->getByDatabase($resultByCache['unExistingOrders'], $unit);
         $this->createByDatabase($resultByOrderCountingRecords['unExistingOrders'], $unit);
         $this->getByDatabase($resultByCache['unExistingOrders'], $unit);
-        $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit);
+        $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit, $user);
         return $resultByCache['resultOrders'];
     }
 
     public function orderCountingRecords($start, $end, $unit = '日')
     {
-        $orders = $this->get($start, $end, null, $unit);
+        $orders = $this->get($start, $end, null, $unit, null);
         $dataList = collect();
         $orders->groupBy('date_target')->each(function ($items) use (&$dataList, $unit) {
             $counter = $items->reduce(function ($sum, $item) {
@@ -54,24 +65,74 @@ class OrderCountingRecordService
         return $dataList->sortBy("date_target");
     }
 
-    public function logisticsCountingRecords($start, $end, $ownerIds = null)
+    /**
+     * @param $start
+     * @param $end
+     * @param null $ownerIds
+     * @param null $user
+     * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection
+     */
+    public function logisticsCountingRecords($start, $end, $ownerIds = null, $user = null)
     {
         $dataList = collect();
-        $resultOrders = $this->get($start, $end, $ownerIds);
-        $resultOrders->groupBy('logistic_id')->each(function ($item)use(&$dataList) {
+        $resultOrders = $this->get($start, $end, $ownerIds, '日', $user);
+        $resultOrders->groupBy('logistic_id')->each(function ($item) use (&$dataList) {
             $counter = $item->reduce(function ($sum, $item) {
                 return $sum + $item->amount;
             }, 0);
             $dataList->push([
-                'counter' => $counter,
-                'logistic_id' => $date_target,
+                'value' => $counter,
+                'logistic_id' => $item[0]->logistic_id,
             ]);
         });
+        $map = [];
+        $logistics =  Logistic::query()->whereIn('id',data_get($dataList,'*.logistic_id'))->get();
+        $logistics->each(function($logistic)use(&$map){
+            $map[$logistic->id] = $logistic;
+        });
+        return $dataList->map(function (&$item)use($map){
+            $logistic = $map[$item['logistic_id']] ?? '';
+            $item['name'] = $logistic->name ??'';
+            return $item;
+        });
     }
 
-    public function getByCache($start, $end, $ownerIds = null, $unit = '日')
+    public function warehouseCountingRecords($start, $end, $ownerIds = null, $user = null)
+    {
+        $dataList = collect();
+        $resultOrders = $this->get($start, $end, $ownerIds, '日', $user);
+        $resultOrders->groupBy('warehouse_id')->each(function ($item) use (&$dataList) {
+            $counter = $item->reduce(function ($sum, $item) {
+                return $sum + $item->amount;
+            }, 0);
+            $dataList->push([
+                'value' => $counter,
+                'warehouse_id' => $item[0]->warehouse_id,
+            ]);
+        });
+        $map = [];
+        $logistics =  Warehouse::query()->whereIn('id',data_get($dataList,'*.warehouse_id'))->get();
+        $logistics->each(function($warehouse)use(&$map){
+            $map[$warehouse->id] = $warehouse;
+        });
+        return $dataList->map(function (&$item)use($map){
+            $warehouse = $map[$item['warehouse_id']] ?? '';
+            $item['name'] = $warehouse->name ??'';
+            return $item;
+        });
+    }
+
+    /**
+     * @param $start
+     * @param $end
+     * @param null $ownerIds
+     * @param string $unit
+     * @param $user
+     * @return array
+     */
+    public function getByCache($start, $end, $ownerIds = null, $unit = '日', $user)
     {// Order[] results, Array [$date=>[$ownerIds]]
-        $countingOwnerIds = $this->getCountingOwnerIds($ownerIds);
+        $countingOwnerIds = $this->getCountingOwnerIds($ownerIds, $user);
         $resultOrders = collect();
         $unExistingOrders = [];
         $carbonInterfaces = $this->periodDateToArray($start, $end, $unit);
@@ -190,9 +251,11 @@ class OrderCountingRecordService
      * @param $ownerIds
      * @return array
      */
-    public function getCountingOwnerIds($ownerIds = null): array
+    public function getCountingOwnerIds($ownerIds = null, $user): array
     {
-        $user = auth()->user();
+        if ($user == null) {
+            $user = auth()->user();
+        }
         /** @var UserService $userService */
         $userService = app('UserService');
         $permittingOwnerIds = $userService->getPermittingOwnerIds($user);

+ 4 - 2
app/Services/OwnerService.php

@@ -151,10 +151,12 @@ Class OwnerService
         return Owner::query()->whereIn('id',app('UserService')->getPermittingOwnerIds($user)??[])->get();
     }
 
-    public function get(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false)
+    public function get(array $params, array $withs = null, bool $authority = true, bool $notShowSoftDelete = false,$user)
     {
         /** @var User $user */
-        $user = Auth::user();
+        if ($user==null) {
+            $user = Auth::user();
+        }
         $query = Owner::query();
         if ($authority){
             $ids = $user->getPermittingOwnerIdsAttribute();

+ 117 - 47
resources/views/control/panel.blade.php

@@ -48,53 +48,71 @@
                 </div>
             </div>
         </div>
-        <div class="row">
-            <div class="col-6 card-body">
-                <div class="row">
-                    <div class="col-6">
-                        <span class="demonstration"></span>
-                        <el-date-picker
-                            v-model="orderCountingRecordsDate"
-                            type="daterange"
-                            align="right"
-                            unlink-panels
-                            range-separator="-"
-                            start-placeholder="开始日期"
-                            end-placeholder="结束日期"
-                            value-format="yyyy-MM-dd"
-                            :picker-options="pickerOptions">
-                        </el-date-picker>
-                    </div>
-                    <div class="col-6">
-                        <div>
-                            <el-button type="primary" value="日" @click="unit= '日'" v-model="unit">日</el-button>
-                            <el-button type="primary" value="周" @click="unit= '周'" v-model="unit">周</el-button>
-                            <el-button type="primary" value="月" @click="unit= '月'" v-model="unit">月</el-button>
-                            <el-button type="primary" icon="el-icon-search" @click="orderCountingRecordApi()">搜索
-                            </el-button>
+        <div class="row my-3">
+            <div class="col-5">
+                <div class="card">
+                    <div class="card-header">
+                        <div class="col-5 row">
+                            <span class="demonstration"></span>
+                            <el-date-picker
+                                v-model="orderCountingRecordsDate"
+                                type="daterange"
+                                align="right"
+                                unlink-panels
+                                range-separator="-"
+                                start-placeholder="开始日期"
+                                end-placeholder="结束日期"
+                                value-format="yyyy-MM-dd"
+                                :picker-options="pickerOptions">
+                            </el-date-picker>
+                            <div class="row">
+                                <el-button type="primary" value="日" @click="unit= '日'" v-model="unit">日</el-button>
+                                <el-button type="primary" value="周" @click="unit= '周'" v-model="unit">周</el-button>
+                                <el-button type="primary" value="月" @click="unit= '月'" v-model="unit">月</el-button>
+                                <el-button type="primary" icon="el-icon-search" @click="orderCountingRecordApi()">搜索
+                                </el-button>
+                            </div>
                         </div>
                     </div>
+                    <div class="card-body row">
+                        <div id="orderCountingRecords" class="col" style="width:600px;height:600px;"></div>
+                    </div>
                 </div>
-                <div id="orderCountingRecords" class="row" style="width: 600px;height:400px;"></div>
             </div>
-            <div class="col-6 card-body">
-                <div class="block">
-                    <span class="demonstration"></span>
-                    <el-date-picker
-                        v-model="logisticsCountingRecordsDate"
-                        type="daterange"
-                        align="right"
-                        unlink-panels
-                        range-separator="-"
-                        start-placeholder="开始日期"
-                        end-placeholder="结束日期"
-                        value-format="yyyy-MM-dd"
-                        :picker-options="pickerOptions">
-                    </el-date-picker>
-                    <div>
-                        <el-button type="primary" icon="el-icon-search" @click="logisticsCountingRecordsApi()">搜索</el-button>
+            <div class="col-7">
+                <div class="row">
+                    <div class="col-6">
+                        <div class="card">
+                            <div class="card-header">
+                                <div class="col-5 row">
+                                    <div class="block">
+                                        <span class="demonstration"></span>
+                                        <el-date-picker
+                                            v-model="logisticsCountingRecordsData"
+                                            type="daterange"
+                                            align="right"
+                                            unlink-panels
+                                            range-separator="-"
+                                            start-placeholder="开始日期"
+                                            end-placeholder="结束日期"
+                                            value-format="yyyy-MM-dd"
+                                            :picker-options="pickerOptions">
+                                        </el-date-picker>
+                                        <el-button type="primary" icon="el-icon-search"
+                                                   @click="logisticsCountingRecordsApi()">搜索
+                                        </el-button>
+                                    </div>
+                                </div>
+                            </div>
+                            <div class="card-body row">
+                                <div id="logisticsCountingRecords" class="col"
+                                     style="width: 600px;height:600px;"></div>
+                            </div>
+                        </div>
+                    </div>
+                    <div class="col-6">
+
                     </div>
-                    <div id="logisticsCountingRecords" class="row" style="width: 600px;height:400px;"></div>
                 </div>
             </div>
         </div>
@@ -114,6 +132,7 @@
                 menus:{!! $menus !!},
                 warehousesOrders:{!! $warehousesOrders !!},
                 orderCountingRecords:{!! $orderCountingRecords !!},
+                logisticsCountingRecords:{!! $logisticsCountingRecords !!},
                 warehouses: {},
                 totalOrders: {
                     total: null,
@@ -156,7 +175,7 @@
                     moment(new Date(new Date().getTime() - 3600 * 1000 * 24 * 30)).format('yyyy-MM-DD'),
                     moment(new Date()).format('yyyy-MM-DD')
                 ],
-                logisticsCountingRecordsDate: [moment(new Date(new Date().getTime() - 3600 * 1000 * 24 * 30)).format('yyyy-MM-DD'),
+                logisticsCountingRecordsData: [moment(new Date(new Date().getTime() - 3600 * 1000 * 24 * 30)).format('yyyy-MM-DD'),
                     moment(new Date()).format('yyyy-MM-DD')],
                 unit: '日'
             },
@@ -176,8 +195,13 @@
                     _this.totalOrders.sowComplete += item.sowComplete;
                 });
                 this.initOrderCountingRecords();
-                this.OrderCountingRecordsChart = echarts.init(document.getElementById('orderCountingRecords'));
+                this.orderCountingRecordsChart = echarts.init(document.getElementById('orderCountingRecords'));
                 this.initOrderCountingRecordsChart();
+
+                // this.initLogisticsCountingRecords();
+                this.logisticsCountingRecordsChart = echarts.init(document.getElementById('logisticsCountingRecords'));
+                this.initLogisticsCountingRecordsChart();
+
             },
             methods: {
                 getWareHouse: function (code) {
@@ -190,8 +214,15 @@
                         _this.orderCountingRecordsData.push(item.counter);
                     });
                 },
+                initLogisticsCountingRecords() {
+                    let _this = this;
+                    this.logisticsCountingRecords.forEach(function (item) {
+                        _this.orderCountingRecordsDateTarget.push(item.date_target);
+                        _this.orderCountingRecordsData.push(item.counter);
+                    });
+                },
                 initOrderCountingRecordsChart() {
-                    this.OrderCountingRecordsChart.setOption({
+                    this.orderCountingRecordsChart.setOption({
                         title: {text: '实时待处理订单'},
                         tooltip: {},
                         legend: {data: ['订单数']},
@@ -207,15 +238,42 @@
                         }]
                     });
                 },
+                initLogisticsCountingRecordsChart() {
+                    this.logisticsCountingRecordsChart.setOption({
+                        title: {
+                            text: '快递分布',
+                            left: 'left'
+                        },
+                        tooltip: {
+                            trigger: 'item',
+                            formatter: '{a} <br/>{b} : {c} ({d}%)'
+                        },
+                        series: [
+                            {
+                                name: '访问来源',
+                                type: 'pie',
+                                radius: '55%',
+                                center: ['50%', '60%'],
+                                data: this.logisticsCountingRecords,
+                                emphasis: {
+                                    itemStyle: {
+                                        shadowBlur: 10,
+                                        shadowOffsetX: 0,
+                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
+                                    }
+                                }
+                            }
+                        ]
+                    });
+                },
                 orderCountingRecordApi() {
                     let formData = new FormData();
-                    console.log(this.orderCountingRecordsDate[0]);
                     formData.append('start', this.orderCountingRecordsDate[0]);
                     formData.append('end', this.orderCountingRecordsDate[1]);
                     formData.append('unit', this.unit);
                     console.log(this.unit);
                     let _this = this;
-                    axios.post('{{url('apiLocal/control/panel/menu')}}', formData).then(function (res) {
+                    axios.post('{{url('apiLocal/control/panel/menu/orderCountingRecordApi')}}', formData).then(function (res) {
                         if (res.status === 200) {
                             _this.orderCountingRecords = res.data.orderCountingRecords;
                             _this.orderCountingRecordsDateTarget = [];
@@ -224,6 +282,18 @@
                             _this.initOrderCountingRecordsChart();
                         }
                     });
+                },
+                logisticsCountingRecordsApi() {
+                    let formData = new FormData();
+                    formData.append('start', this.logisticsCountingRecordsData[0]);
+                    formData.append('end', this.logisticsCountingRecordsData[1]);
+                    let _this = this;
+                    axios.post('{{url('apiLocal/control/panel/menu/logisticsCountingRecordsApi')}}', formData).then(function (res) {
+                        if (res.status === 200) {
+                            _this.logisticsCountingRecords = res.data.logisticsCountingRecords;
+                            _this.initLogisticsCountingRecordsChart();
+                        }
+                    });
                 }
             }
         });

+ 2 - 1
routes/apiLocal.php

@@ -87,5 +87,6 @@ Route::group(['prefix'=>'maintenance'],function (){
 
 /** 控制台 */
 Route::group(['prefix'=>'control'],function () {
-    Route::post('panel/menu','ControlPanelController@orderCountingRecordsApi') ;
+    Route::post('panel/menu/orderCountingRecordApi','ControlPanelController@orderCountingRecordsApi') ;
+    Route::post('panel/menu/logisticsCountingRecordsApi','ControlPanelController@logisticsCountingRecordsApi') ;
 });

+ 30 - 0
tests/Services/OrderCountingRecordService/OrderCountingRecordServiceLogisticsCountingRecordsTest.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace Tests\Services\OrderCountingRecordService;
+
+use App\Services\CacheService;
+use App\Services\OrderCountingRecordService;
+use App\User;
+use Tests\TestCase;
+
+class OrderCountingRecordServiceLogisticsCountingRecordsTest extends TestCase
+{
+    /** @var OrderCountingRecordService $orderCountingRecordService */
+    public $orderCountingRecordService;
+
+    public function setUp(): void
+    {
+        parent::setUp();
+        $this->orderCountingRecordService = app(OrderCountingRecordService::class);
+    }
+
+    public function testLogisticsCountingRecords()
+    {
+        $user =  User::query()->find(1)->first();
+
+        $dataList =  $this->orderCountingRecordService->logisticsCountingRecords('2020-10-01', '2020-11-09',null,$user);
+        self::assertTrue($dataList->isNotEmpty());
+        dd($dataList);
+
+    }
+}

+ 30 - 0
tests/Services/OrderCountingRecordService/OrderCountingRecordServiceWarehouseCountingRecordsTest.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace Tests\Services\OrderCountingRecordService;
+
+use App\Services\CacheService;
+use App\Services\OrderCountingRecordService;
+use App\User;
+use Tests\TestCase;
+
+class OrderCountingRecordServiceWarehouseCountingRecordsTest extends TestCase
+{
+    /** @var OrderCountingRecordService $orderCountingRecordService */
+    public $orderCountingRecordService;
+
+    public function setUp(): void
+    {
+        parent::setUp();
+        $this->orderCountingRecordService = app(OrderCountingRecordService::class);
+    }
+
+    public function testLogisticsCountingRecords()
+    {
+        $user =  User::query()->find(1)->first();
+
+        $dataList =  $this->orderCountingRecordService->warehouseCountingRecords('2020-10-01', '2020-11-09',null,$user);
+        self::assertTrue($dataList->isNotEmpty());
+        dd($dataList);
+
+    }
+}