Parcourir la source

临时工趋势

ANG YU il y a 5 ans
Parent
commit
f20e9abe47

+ 16 - 3
app/Http/Controllers/ControlPanelController.php

@@ -3,6 +3,7 @@
 namespace App\Http\Controllers;
 
 use App\Services\CheckActiveMenuService;
+use App\Services\LaborReportsCountingRecordService;
 use App\Services\OrderCountingRecordService;
 use App\Services\RealtimePendingOrdersService;
 use Carbon\Carbon;
@@ -17,6 +18,7 @@ class ControlPanelController extends Controller
          * @var CheckActiveMenuService $checkActiveMenuService
          * @var RealtimePendingOrdersService $realtimePendingOrdersService
          * @var OrderCountingRecordService $orderCountingRecords
+         * @var LaborReportsCountingRecordService $laborReportsCountingRecordService
          */
         $checkActiveMenuService = app(CheckActiveMenuService::class);
         $menus = $checkActiveMenuService->activeMenus();
@@ -29,7 +31,10 @@ class ControlPanelController extends Controller
         $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end);
         $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end);
         $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end);
-        return view('control.panel', compact('menus', 'warehousesOrders', 'orderCountingRecords', 'logisticsCountingRecords', 'warehouseCountingRecords'));
+
+        $laborReportsCountingRecordService = app(LaborReportsCountingRecordService::class);
+        $laborReportsCountingRecords = $laborReportsCountingRecordService->get($start, $end, '日');
+        return view('control.panel', compact('menus', 'warehousesOrders', 'orderCountingRecords', 'logisticsCountingRecords', 'warehouseCountingRecords', 'laborReportsCountingRecords'));
     }
 
     public function orderCountingRecordsApi(Request $request)
@@ -38,9 +43,7 @@ class ControlPanelController extends Controller
         $start = $request->start;
         $end = $request->end;
         $unit = $request->unit;
-//        var_dump($start,$end,$unit);
         $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end, null, $unit, null);
-//        var_dump($orderCountingRecords);
         return compact('orderCountingRecords');
     }
 
@@ -61,4 +64,14 @@ class ControlPanelController extends Controller
         $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end);
         return compact('warehouseCountingRecords');
     }
+
+    public function laborReportsCountingRecordApi(Request $request)
+    {
+        $laborReportsCountingRecordService = app(LaborReportsCountingRecordService::class);
+        $start = $request->start;
+        $end = $request->end;
+        $unit = $request->unit;
+        $laborReportsCountingRecords = $laborReportsCountingRecordService->get($start, $end, $unit);
+        return compact('laborReportsCountingRecords');
+    }
 }

+ 6 - 0
app/Providers/AppServiceProvider.php

@@ -14,6 +14,7 @@ use App\Services\CustomerService;
 use App\Services\DepositoryService;
 use App\Services\InventoryAccountMissionService;
 use App\Services\InventoryCompareService;
+use App\Services\LaborReportsCountingRecordService;
 use App\Services\OracleBasCustomerService;
 use App\Services\OracleBasSkuService;
 use App\Services\OracleDocAsnDetailService;
@@ -147,6 +148,7 @@ class AppServiceProvider extends ServiceProvider
         $this->loadingCheckActiveMenuService();
         $this->loadingRealtimePendingOrdersService();
         $this->loadingOrderCountingRecordService();
+        $this->loadingLaborReportsCountingRecordService();
     }
 
     private function loadingOrderModuleService(){
@@ -189,4 +191,8 @@ class AppServiceProvider extends ServiceProvider
     {
         app()->singleton('OrderCountingRecordService',OrderCountingRecordService::class);
     }
+    private function loadingLaborReportsCountingRecordService()
+    {
+        app()->singleton('LaborReportsCountingRecordService',LaborReportsCountingRecordService::class);
+    }
 }

+ 140 - 0
app/Services/LaborReportsCountingRecordService.php

@@ -0,0 +1,140 @@
+<?php
+
+
+namespace App\Services;
+
+
+use App\LaborReport;
+use Carbon\Carbon;
+use DateTime;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Str;
+use Ramsey\Uuid\Type\Integer;
+
+class LaborReportsCountingRecordService
+{
+    public function get($start, $end, $unit = '日')
+    {
+        $resultByCache = $this->getByCache($start, $end, $unit);
+        if ($resultByCache['dateList'] == []) {
+            return $resultByCache['dataList'];
+        } else {
+            $this->syncDBToCache($resultByCache['dateList'], $unit);
+            return $this->getByCache($start, $end, $unit)['dataList'];
+        }
+    }
+
+    public function getByCache($start, $end, $unit)
+    {
+        $dataList = collect();
+        $dateList = collect($this->periodDateToArray($start, $end, $unit));
+        $result = [];
+        foreach ($dateList as $date) {
+            $key = 'laborReportsCountingRecords_' . $date . '_' . $unit;
+            $item = Cache::get($key);
+            if ($item) {
+                $dataList->push($item);
+                $dateList = $dateList->reject(function ($value) use ($date) {
+                    return $value == $date;
+                });
+            }
+        }
+        $result['dataList'] = $dataList;
+        $result['dateList'] = $dateList->toArray();
+        return $result;
+    }
+
+
+    /**
+     * @param $start
+     * @param $end
+     * @param string $unit
+     * @return array
+     */
+    private function periodDateToArray($start, $end, $unit = '日')
+    {
+        $dataArray = [];
+        switch ($unit) {
+            case '日';
+                foreach (Carbon::parse($start)->daysUntil($end, 1)->toArray() as $item) {
+                    $dataArray[] = $item->toDateString();
+                }
+                break;
+            case '周';
+                foreach (Carbon::parse($start)->startOfWeek(1)->weeksUntil($end, 1)->toArray() as $item) {
+                    $dataArray[] = $item->year . '-' . $item->week . '';
+                }
+                break;
+            case '月';
+                foreach (Carbon::parse($start)->monthsUntil($end, 1)->toArray() as $item) {
+                    $dataArray[] = $item->year . '-' . $item->format('m') . '';
+                }
+                break;
+            case '年';
+                foreach (Carbon::parse($start)->yearsUntil($end, 1)->toArray() as $item) {
+                    $dataArray[] = $item->year . '-' . $item->month . '';
+                }
+                break;
+            default:
+                break;
+        }
+        return $dataArray;
+    }
+
+    private function syncDBToCache($dateList, $unit)
+    {
+        switch ($unit) {
+            case '日':
+                $query = LaborReport::query()->selectRaw("DATE_FORMAT(created_at,'%Y-%m-%d') as date_target, count(1) as counter");
+                foreach ($dateList as $startOfWeek) {
+                    $query->orWhere(function ($query) use ($startOfWeek) {
+                        $query->whereDate('created_at', $startOfWeek);
+                    });
+                }
+                $dataList = $query->groupBy('date_target')->get();
+                $dataList->each(function ($item) use ($unit) {
+                    $date = $item->date_target;
+                    $key = 'laborReportsCountingRecords_' . $date . '_' . $unit;
+                    Cache::put($key, $item);
+                });
+                break;
+            case '周':
+                $query = LaborReport::query()->selectRaw("DATE_FORMAT(created_at,'%x-%v') as date_target, count(1) as counter");
+                foreach ($dateList as $date) {
+                    $query->orWhere(function ($query) use ($date) {
+                        $year = Str::of($date)->explode('-')[0];
+                        $week = Str::of($date)->explode('-')[1];
+                        $startOfWeek = Carbon::now()->setISODate($year, $week)->startOfWeek()->toDateString();
+                        $endOfWeek = Carbon::parse($startOfWeek)->endOfWeek()->toDateString();
+                        $query->whereDate('created_at', '>=', $startOfWeek)->whereDate('created_at', '<=', $endOfWeek);
+                    });
+                }
+                $dataList = $query->groupBy('date_target')->get();
+                $dataList->each(function ($item) use ($unit) {
+                    $date = $item->date_target;
+                    $key = 'laborReportsCountingRecords_' . $date . '_' . $unit;
+                    Cache::put($key, $item);
+                });
+                break;
+            case '月':
+                $query = LaborReport::query()->selectRaw("DATE_FORMAT(created_at,'%Y-%m') as date_target, count(1) as counter");
+                foreach ($dateList as $date) {
+                    $query->orWhere(function ($query) use ($date) {
+                        $year = Str::of($date)->explode('-')[0];
+                        $month = Str::of($date)->explode('-')[1];
+                        $query->whereYear('created_at', $year)->whereMonth('created_at', $month);
+                    });
+                }
+                $dataList = $query->groupBy('date_target')->get();
+                $dataList->each(function ($item) use ($unit) {
+                    $date = $item->date_target;
+                    $key = 'laborReportsCountingRecords_' . $date . '_' . $unit;
+                    Cache::put($key, $item);
+                });
+                break;
+            default:
+                break;
+        }
+    }
+}

+ 5 - 5
app/Services/OrderCountingRecordService.php

@@ -15,7 +15,7 @@ use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Cache;
 use App\User;
 use Illuminate\Support\Str;
-use function MongoDB\BSON\toJSON;
+
 
 class OrderCountingRecordService
 {
@@ -221,7 +221,7 @@ class OrderCountingRecordService
                 $resultOrders = $this->getCreateByDatabaseUnitWeek($targetOwnerIdsUnderDates);
                 break;
             case'月':
-                $resultOrders = $this->getCreateByDatabaseUnitMounth($targetOwnerIdsUnderDates);
+                $resultOrders = $this->getCreateByDatabaseUnitMonth($targetOwnerIdsUnderDates);
                 break;
             default:
                 $resultOrders = collect();
@@ -255,13 +255,13 @@ class OrderCountingRecordService
                 }
                 break;
             case '周';
-                foreach (Carbon::parse($start)->daysUntil($end, 1)->toArray() as $item) {
+                foreach (Carbon::parse($start)->weeksUntil($end, 1)->toArray() as $item) {
                     $dataArray[] = $item->year . '-' . $item->week . '';
                 }
                 break;
             case '月';
                 foreach (Carbon::parse($start)->monthsUntil($end, 1)->toArray() as $item) {
-                    $dataArray[] = $item->year . '-' . $item->month . '';
+                    $dataArray[] = $item->year . '-' . $item->format('m') . '';
                 }
                 break;
             case '年';
@@ -325,7 +325,7 @@ class OrderCountingRecordService
         return $orderSqlBuilder->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id', 'date_target'])->get();
     }
 
-    private function getCreateByDatabaseUnitMounth($targetOwnerIdsUnderDates)
+    private function getCreateByDatabaseUnitMonth($targetOwnerIdsUnderDates)
     {
         $orderSqlBuilder = Order::query()->selectRaw("owner_id,warehouse_id,shop_id,logistic_id,count(1) as amounts ,DATE_FORMAT(created_at,'%Y-%m') as date_target");
         foreach ($targetOwnerIdsUnderDates as $dateStr => $ownerIds) {

+ 89 - 8
resources/views/control/panel.blade.php

@@ -64,9 +64,9 @@
                                 value-format="yyyy-MM-dd"
                                 :picker-options="pickerOptions">
                             </el-date-picker>
-                            <el-button type="primary" value="日" @click="orderCountingRecordApi('日')" v-model="unit">日</el-button>
-                            <el-button type="primary" value="周" @click="orderCountingRecordApi('周')" v-model="unit">周</el-button>
-                            <el-button type="primary" value="月" @click="orderCountingRecordApi('月')" v-model="unit">月</el-button>
+                            <el-button type="primary" value="日" @click="orderCountingRecordApi('日')" v-model="orderCountingRecordsUnit">日</el-button>
+                            <el-button type="primary" value="周" @click="orderCountingRecordApi('周')" v-model="orderCountingRecordsUnit">周</el-button>
+                            <el-button type="primary" value="月" @click="orderCountingRecordApi('月')" v-model="orderCountingRecordsUnit">月</el-button>
                     </div>
                     <div class="card-body row">
                         <div id="orderCountingRecords" class="col" style="width:600px;height:600px;"></div>
@@ -129,6 +129,30 @@
                     </div>
                 </div>
             </div>
+            <div class="col-5">
+                <div class="card">
+                    <div class="card-header">
+                        <span class="demonstration" ></span>
+                        <el-date-picker @blur="laborReportsCountingRecordApi('')"
+                                        v-model="laborReportsCountingRecordsDate"
+                                        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" value="日" @click="laborReportsCountingRecordApi('日')" v-model="laborReportsCountingRecordUnit">日</el-button>
+                        <el-button type="primary" value="周" @click="laborReportsCountingRecordApi('周')" v-model="laborReportsCountingRecordUnit">周</el-button>
+                        <el-button type="primary" value="月" @click="laborReportsCountingRecordApi('月')" v-model="laborReportsCountingRecordUnit">月</el-button>
+                    </div>
+                    <div class="card-body row">
+                        <div id="laborReportsCountingRecords" class="col" style="width:600px;height:600px;"></div>
+                    </div>
+                </div>
+            </div>
         </div>
     </div>
 @endsection
@@ -148,6 +172,7 @@
                 orderCountingRecords:{!! $orderCountingRecords !!},
                 logisticsCountingRecords:{!! $logisticsCountingRecords !!},
                 warehouseCountingRecords:{!! $warehouseCountingRecords !!},
+                laborReportsCountingRecords:{!! $laborReportsCountingRecords !!},
                 warehouses: {},
                 totalOrders: {
                     total: null,
@@ -159,6 +184,8 @@
                 },
                 orderCountingRecordsDateTarget: [],
                 orderCountingRecordsData: [],
+                laborReportsCountingRecordsDateTarget: [],
+                laborReportsCountingRecordsData: [],
                 pickerOptions: {
                     shortcuts: [{
                         text: '最近一周',
@@ -192,7 +219,10 @@
                     moment(new Date()).format('yyyy-MM-DD')],
                 warehouseCountingRecordsData: [moment(new Date(new Date().getTime() - 3600 * 1000 * 24 * 30)).format('yyyy-MM-DD'),
                     moment(new Date()).format('yyyy-MM-DD')],
-                unit: '日'
+                laborReportsCountingRecordsDate: [moment(new Date(new Date().getTime() - 3600 * 1000 * 24 * 30)).format('yyyy-MM-DD'),
+                    moment(new Date()).format('yyyy-MM-DD')],
+                orderCountingRecordsUnit: '日',
+                laborReportsCountingRecordUnit: '日'
             },
             mounted: function () {
                 let _this = this;
@@ -219,6 +249,10 @@
                 this.warehouseCountingRecordsChart = echarts.init(document.getElementById('warehouseCountingRecords'));
                 this.initWarehouseCountingRecordsChart();
 
+                this.initLaborReportsCountingRecords();
+                this.laborReportsCountingRecordsChart = echarts.init(document.getElementById('laborReportsCountingRecords'));
+                this.initLaborReportsCountingRecordsChart();
+
             },
             methods: {
                 getWareHouse: function (code) {
@@ -248,6 +282,23 @@
                         }]
                     });
                 },
+                initLaborReportsCountingRecordsChart() {
+                    this.laborReportsCountingRecordsChart.setOption({
+                        title: {text: '临时用工趋势'},
+                        tooltip: {},
+                        legend: {data: ['临时用工数']},
+                        xAxis: {
+                            type: 'category',
+                            data: this.laborReportsCountingRecordsDateTarget
+                        },
+                        yAxis: {type: 'value'},
+                        series: [{
+                            data: this.laborReportsCountingRecordsData,
+                            type: 'line',
+                            smooth: true
+                        }]
+                    });
+                },
                 initLogisticsCountingRecordsChart() {
                     this.logisticsCountingRecordsChart.setOption({
                         title: {
@@ -304,13 +355,23 @@
                         ]
                     });
                 },
-                orderCountingRecordApi(unit) {
-                    this.unit = unit;
+                initLaborReportsCountingRecords() {
+                    let _this = this;
+                    this.laborReportsCountingRecords.forEach(function (item) {
+                        _this.laborReportsCountingRecordsDateTarget.push(item.date_target);
+                        _this.laborReportsCountingRecordsData.push(item.counter);
+                    });
+                },
+
+                orderCountingRecordApi(orderCountingRecordsUnit) {
+                    if (orderCountingRecordsUnit === '') {
+                        orderCountingRecordsUnit = this.orderCountingRecordsUnit;
+                    }
+                    this.orderCountingRecordsUnit = orderCountingRecordsUnit;
                     let formData = new FormData();
                     formData.append('start', this.orderCountingRecordsDate[0]);
                     formData.append('end', this.orderCountingRecordsDate[1]);
-                    formData.append('unit', unit);
-                    console.log(formData.get('unit'));
+                    formData.append('unit', orderCountingRecordsUnit);
                     let _this = this;
                     axios.post('{{url('apiLocal/control/panel/menu/orderCountingRecordApi')}}', formData).then(function (res) {
                         if (res.status === 200) {
@@ -348,6 +409,26 @@
                     });
                 },
 
+                laborReportsCountingRecordApi(laborReportsCountingRecordUnit) {
+                    if (laborReportsCountingRecordUnit === '') {
+                        laborReportsCountingRecordUnit = this.laborReportsCountingRecordUnit;
+                    }
+                    this.laborReportsCountingRecordUnit = laborReportsCountingRecordUnit;
+                    let formData = new FormData();
+                    formData.append('start', this.laborReportsCountingRecordsDate[0]);
+                    formData.append('end', this.laborReportsCountingRecordsDate[1]);
+                    formData.append('unit', laborReportsCountingRecordUnit);
+                    let _this = this;
+                    axios.post('{{url('apiLocal/control/panel/menu/laborReportsCountingRecordApi')}}', formData).then(function (res) {
+                        if (res.status === 200) {
+                            _this.laborReportsCountingRecords = res.data.laborReportsCountingRecords;
+                            _this.laborReportsCountingRecordsDateTarget = [];
+                            _this.laborReportsCountingRecordsData = [];
+                            _this.initLaborReportsCountingRecords();
+                            _this.initLaborReportsCountingRecordsChart();
+                        }
+                    });
+                },
             }
         });
     </script>

+ 1 - 0
routes/apiLocal.php

@@ -90,4 +90,5 @@ Route::group(['prefix'=>'control'],function () {
     Route::post('panel/menu/orderCountingRecordApi','ControlPanelController@orderCountingRecordsApi') ;
     Route::post('panel/menu/logisticsCountingRecordsApi','ControlPanelController@logisticsCountingRecordsApi');
     Route::post('panel/menu/warehouseCountingRecordsApi','ControlPanelController@warehouseCountingRecordsApi');
+    Route::post('panel/menu/laborReportsCountingRecordApi','ControlPanelController@laborReportsCountingRecordApi');
 });

+ 41 - 0
tests/Services/LaborReportsCountingRecordService/ByCacheTest.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace LaborReportsCountingRecordService;
+
+use App\Services\CacheService;
+use App\Services\LaborReportsCountingRecordService;
+use App\Services\OrderCountingRecordService;
+use Illuminate\Support\Facades\Cache;
+use Tests\TestCase;
+
+class ByCacheTest extends TestCase
+{
+    /** @var LaborReportsCountingRecordService $laborReportsCountingRecordService */
+    public $laborReportsCountingRecordService;
+
+    public function setUp(): void
+    {
+        parent::setUp();
+        $this->laborReportsCountingRecordService = new LaborReportsCountingRecordService();
+    }
+
+    public function testSet()
+    {
+        $dateList = [
+            '2025-10-01',
+            '2025-10-02',
+            '2025-10-03',
+            '2025-10-04',
+            '2025-10-05',
+        ];
+        $unit = '日';
+        foreach ($dateList as $date) {
+            $key = 'laborReportsCountingRecords_' . $date . '_' . $unit;
+            Cache::put($key, '111111111111');
+        }
+        $result = $this->laborReportsCountingRecordService->getByCache($dateList, $unit);
+
+        self::assertTrue($result['dataList']->isnotEmpty());
+        self::assertTrue($result['dateList']==[]);
+    }
+}

+ 48 - 0
tests/Services/LaborReportsCountingRecordService/GetTest.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace LaborReportsCountingRecordService;
+
+use App\Services\CacheService;
+use App\Services\LaborReportsCountingRecordService;
+use App\Services\OrderCountingRecordService;
+use Illuminate\Support\Facades\Cache;
+use Tests\TestCase;
+
+class GetTest extends TestCase
+{
+    /** @var LaborReportsCountingRecordService $laborReportsCountingRecordService */
+    public $laborReportsCountingRecordService;
+
+    public function setUp(): void
+    {
+        parent::setUp();
+        $this->laborReportsCountingRecordService = new LaborReportsCountingRecordService();
+    }
+
+    public function testGet日()
+    {
+        $start = '2020-11-08';
+        $end = '2020-12-19';
+        $unit = '日';
+        $result =  $this->laborReportsCountingRecordService->get($start, $end, $unit);
+        self::assertTrue($result->isNotEmpty());
+    }
+
+    public function testGet周()
+    {
+        $start = '2020-11-08';
+        $end = '2020-12-19';
+        $unit = '周';
+        $result =  $this->laborReportsCountingRecordService->get($start, $end, $unit);
+        self::assertTrue($result->isNotEmpty());
+    }
+
+    public function testGet月()
+    {
+        $start = '2020-08-01';
+        $end = '2020-11-1';
+        $unit = '月';
+        $result =  $this->laborReportsCountingRecordService->get($start, $end, $unit);
+        self::assertTrue($result->isNotEmpty());
+    }
+}

+ 3 - 2
tests/Services/OrderCountingRecordService/DateTestTest.php

@@ -13,7 +13,8 @@ class DateTestTest extends TestCase
 {
     public function test01()
     {
-        $dateStr = '2020-11-13';
-        dd($dateStr == Carbon::now()->toDateString());
+        dd(Carbon::parse('2020-08-15')->monthsUntil('2020-11-13', 1)->toArray());
+
+
     }
 }