Bladeren bron

订单量趋势 同步数据到统计表

ANG YU 5 jaren geleden
bovenliggende
commit
e98a7fca02

+ 11 - 0
app/OrderCountingRecord.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class OrderCountingRecord extends Model
+{
+    //
+    protected $fillable = ['owner_id','shop_id' ,'warehouse_id' ,'logistics_id' ,'date_target' ,'counting_unit' ,'amount'];
+}

+ 6 - 1
app/Providers/AppServiceProvider.php

@@ -18,6 +18,7 @@ use App\Services\OracleBasCustomerService;
 use App\Services\OracleBasSkuService;
 use App\Services\OracleDocAsnDetailService;
 use App\Services\OracleDOCOrderHeaderService;
+use App\Services\OrderCountingRecordService;
 use App\Services\OrderIssuePerformanceService;
 use App\Services\AllInventoryService;
 use App\Services\InventoryDailyLogService;
@@ -145,6 +146,7 @@ class AppServiceProvider extends ServiceProvider
         $this->loadingRejectedModuleService();
         $this->loadingCheckActiveMenuService();
         $this->loadingRealtimePendingOrdersService();
+        $this->loadingOrderCountingRecordService();
     }
 
     private function loadingOrderModuleService(){
@@ -183,5 +185,8 @@ class AppServiceProvider extends ServiceProvider
     {
         app()->singleton('RealtimePendingOrdersService',RealtimePendingOrdersService::class);
     }
-
+    private function loadingOrderCountingRecordService()
+    {
+        app()->singleton('OrderCountingRecordService',OrderCountingRecordService::class);
+    }
 }

+ 78 - 0
app/Services/OrderCountingRecordService.php

@@ -0,0 +1,78 @@
+<?php
+
+
+namespace App\Services;
+
+
+use App\Order;
+use App\OrderCountingRecord;
+use Illuminate\Support\Facades\Cache;
+use App\Services\OwnerService;
+
+class OrderCountingRecordService
+{
+    public function get($start, $end, $unit)
+    {
+        $result = collect();$emptyKeys = collect();$emptyKeys2 = collect();
+        $ownerIds = app(OwnerService::class)->getSelectionId();
+        if ($ownerIds->isEmpty()) {
+            $ownerIds = [10, 35, 70];
+        }
+        collect($this->periodDate($start, $end))->each(function($item)use(&$ownerIds,&$result,&$emptyKeys){
+            foreach ($ownerIds as $ownerId) {
+                $key = 'order_counting_records_' . $item . '_' . $ownerId;
+                $value = Cache::get($key);
+                if (empty($value)) $emptyKeys->push( ['owner_id' => $ownerId, 'date' => $item]);
+                else $result->push($value);
+            }
+        });
+
+        $emptyKeys->each(function($key)use(&$emptyKeys2,&$result){
+            $owner_id = $key['owner_id'];
+            $date = $key['date'];
+            $orderCountingRecords = OrderCountingRecord::query()->where('owner_id', $owner_id)->where('date_target', $date)->get();
+            $orderCountingRecords->each(function ($item)use(&$result) {
+                $result->push($item);
+            });
+            if ($orderCountingRecords->isEmpty()) {
+                $emptyKeys2->push(['owner_id' => $owner_id, 'date' => $date]);
+            }
+        });
+        $emptyKeys2->each(function($item)use(&$result){
+            $owner_id = $item['owner_id'];
+            $date = $item['date'];
+            $orders = Order::query()->selectRaw('owner_id,warehouse_id,shop_id,logistic_id as logistics_id,count(1) as amounts')
+                ->where('owner_id', $owner_id)
+                ->where('created_at', 'like', $date . '%')
+                ->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id'])->get();
+            $orders->each(function($order)use(&$result,$date){
+                $order->date = $date;
+                $result->push(OrderCountingRecord::query()->create([
+                        'owner_id' => $order->owner_id,
+                        'shop_id' => $order->shop_id,
+                        'warehouse_id' => $order->warehouse_id,
+                        'logistics_id' => $order->logistics_id,
+                        'date_target' => $order->date,
+                        'counting_unit' => '日',
+                        'amount' =>  $order->amounts,
+                ]));
+            });
+        });
+
+        return $result;
+    }
+
+    public function periodDate($start, $end)
+    {
+        $start_time = strtotime($start);
+        $end_time = strtotime($end);
+        $i = 0;
+        $arr = [];
+        while ($start_time <= $end_time) {
+            $arr[$i] = date('Y-m-d', $start_time);
+            $start_time = strtotime('+1 day', $start_time);
+            $i++;
+        }
+        return $arr;
+    }
+}

+ 38 - 0
database/migrations/2020_11_06_114606_create_order_counting_records_table.php

@@ -0,0 +1,38 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateOrderCountingRecordsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('order_counting_records', function (Blueprint $table) {
+            $table->id();
+            $table->integer('owner_id')->index()->comment('外键货主');
+            $table->integer('shop_id')->index()->comment('外键商铺');
+            $table->integer('warehouse_id')->index()->comment('外键仓库');
+            $table->integer('logistics_id')->index()->comment('外键快递');
+            $table->date('date_target')->index()->comment('该时间为时间, 根据单位扔掉部分信息');
+            $table->enum('counting_unit',['日','周','月','年'])->index()->comment('时间粒度');
+            $table->integer('amount')->comment('该货主下某天订单总数量');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('order_counting_records');
+    }
+}

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

@@ -0,0 +1,30 @@
+<?php
+
+namespace Tests\Services\OrderCountingRecordService;
+
+use App\Services\CacheService;
+use App\Services\OrderCountingRecordService;
+use Tests\TestCase;
+
+class OrderCountingRecordServiceTest extends TestCase
+{
+    /** @var OrderCountingRecordService $orderCountingRecordService */
+    public $orderCountingRecordService;
+
+    public function setUp(): void
+    {
+        parent::setUp();
+        app()->singleton('CacheService', CacheService::class);
+        $this->orderCountingRecordService = app(OrderCountingRecordService::class);
+    }
+
+
+    public function testGet()
+    {
+        $start = '2020-11-05';
+        $end = '2020-11-06';
+        ($this->orderCountingRecordService->get($start, $end, null));
+
+
+    }
+}