فهرست منبع

Merge branch 'yang' of ssh://was.baoshi56.com:10022/var/git/bswas

 Conflicts:
	app/Http/Controllers/TestController.php
	app/Providers/AppServiceProvider.php
LD 4 سال پیش
والد
کامیت
ec2831e2e8
40فایلهای تغییر یافته به همراه1199 افزوده شده و 64 حذف شده
  1. 60 0
      app/Console/Commands/OrderCountingRecordTask.php
  2. 1 0
      app/Console/Kernel.php
  3. 3 3
      app/Http/Controllers/ControlPanelController.php
  4. 85 0
      app/Http/Controllers/OwnerLogisticFeeDetailController.php
  5. 85 0
      app/Http/Controllers/OwnerLogisticFeeReportController.php
  6. 22 0
      app/Http/Controllers/TestController.php
  7. 1 1
      app/Jobs/LogisticSFSync.php
  8. 6 4
      app/Jobs/LogisticYDSync.php
  9. 2 2
      app/Jobs/LogisticYTOSync.php
  10. 1 2
      app/Jobs/LogisticZopSync.php
  11. 14 1
      app/OrderCountingRecord.php
  12. 12 0
      app/OwnerFeeDetail.php
  13. 6 0
      app/OwnerFeeDetailLogistic.php
  14. 26 0
      app/OwnerLogisticFeeDetail.php
  15. 14 0
      app/OwnerLogisticFeeReport.php
  16. 7 0
      app/Providers/AppServiceProvider.php
  17. 1 1
      app/Services/LogisticRouteInterface.php
  18. 18 5
      app/Services/LogisticYDService.php
  19. 8 2
      app/Services/LogisticYTOService.php
  20. 16 14
      app/Services/LogisticZopService.php
  21. 239 17
      app/Services/NewOrderCountingRecordService.php
  22. 11 7
      app/Services/OrderPackageReceivedSyncService.php
  23. 95 0
      app/Services/OwnerLogisticFeeDetailService.php
  24. 13 0
      app/Services/OwnerLogisticFeeReportService.php
  25. 2 2
      config/cache.php
  26. 0 1
      database/factories/OrderCountingRecordFactory.php
  27. 17 0
      database/factories/OwnerLogisticFeeDetailFactory.php
  28. 12 0
      database/factories/OwnerLogisticFeeReportFactory.php
  29. 39 0
      database/migrations/2021_06_03_115444_create_owner_logistic_fee_details_table.php
  30. 31 0
      database/migrations/2021_06_03_115532_create_owner_logistic_fee_reports_table.php
  31. 54 0
      database/migrations/2021_06_05_103627_change_order_counting_records.php
  32. 22 0
      database/seeds/OrderSeed.php
  33. 16 0
      database/seeds/OwnerLogisticFeeDetailSeeder.php
  34. 16 0
      database/seeds/OwnerLogisticFeeReportSeeder.php
  35. 1 1
      tests/Feature/LogisticZopSyncTest.php
  36. 1 1
      tests/Services/LogisticYDService/LogisticYDSyncTest.php
  37. 48 0
      tests/Services/NewOrderCountingRecordService/GetOrderCountingRecordsApiTest.php
  38. 59 0
      tests/Services/NewOrderCountingRecordService/RecordByMonthTest.php
  39. 73 0
      tests/Services/NewOrderCountingRecordService/RecordByYearTest.php
  40. 62 0
      tests/Services/NewOrderCountingRecordService/RecordOrderDayTest.php

+ 60 - 0
app/Console/Commands/OrderCountingRecordTask.php

@@ -0,0 +1,60 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Services\LogService;
+use App\Services\NewOrderCountingRecordService;
+use Illuminate\Console\Command;
+
+class OrderCountingRecordTask extends Command
+{
+    /**
+     * @var NewOrderCountingRecordService $service
+     */
+    public $service;
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'orderCountingRecordTask';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @throws \Exception
+     */
+    public function handle()
+    {
+        LogService::log(OrderCountingRecordTask::class, "订单量统计", '');
+        ini_set('memory_limit', '2226M');
+        $this->service = app('NewOrderCountingRecordService');
+        if (now()->toDateString() == now()->startOfMonth()->toDateString()) {//是否为月初
+            //统计上月的数据
+            $this->service->recordOrder(now()->subMonth()->startOfMonth()->toDateString(), now()->subMonth()->endOfMonth()->toDateString(), '月');
+        }
+        if (now()->toDateString() == now()->startOfYear()->toDateString()) {//是否为年初
+            //统计上年的数据
+            $this->service->recordOrder(now()->subYear()->startofYear()->toDateString(), now()->subYear()->endOfYear()->toDateString(), '年');
+        }
+        //统计前一天的数据
+        $this->service->recordOrder(now()->subDay()->toDateString(), now()->subDay()->toDateString(), '日');
+    }
+}

+ 1 - 0
app/Console/Kernel.php

@@ -81,6 +81,7 @@ class  Kernel extends ConsoleKernel
         $schedule->command('sync:carrier')->hourlyAt(1);
         $schedule->command('createProcurementTotalBill')->monthlyOn(1);
         $schedule->command('check:cacheRack')->everyMinute();
+        $schedule->command('orderCountingRecordTask')->dailyAt("1:00");
     }
 
     /**

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

@@ -51,7 +51,7 @@ class ControlPanelController extends Controller
         $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
         $ownerIds=$request->owner_ids;
         if (!$ownerIds || in_array('all',$ownerIds)) $ownerIds = $this->getCountingOwnerIds(null);
-        $orderCountingRecords = $orderCountingRecordService->orderCountingRecords($start, $end, $request->unit, $ownerIds);
+        $orderCountingRecords = $orderCountingRecordService->getOrderCountingRecordsApi($start, $end, $request->unit, $ownerIds);
         return compact('orderCountingRecords');
     }
 
@@ -65,7 +65,7 @@ class ControlPanelController extends Controller
         $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
         $ownerIds=$request->input('owner_ids');
         if (!$ownerIds || in_array('all',$ownerIds)) $ownerIds = $this->getCountingOwnerIds(null);
-        $logisticsCountingRecords = $orderCountingRecordService->logisticsCountingRecords($start, $end, $ownerIds);
+        $logisticsCountingRecords = $orderCountingRecordService->getLogisticRecordsApi($start, $end, $ownerIds);
         return compact('logisticsCountingRecords');
     }
 
@@ -78,7 +78,7 @@ class ControlPanelController extends Controller
         $start = Carbon::parse($request->start)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->start;
         $end = Carbon::parse($request->end)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $request->end;
         $ownerIds = $this->getCountingOwnerIds(null);
-        $warehouseCountingRecords = $orderCountingRecordService->warehouseCountingRecords($start, $end, $ownerIds);
+        $warehouseCountingRecords = $orderCountingRecordService->getWareHouseRecordsApi($start, $end, $ownerIds);
         return compact('warehouseCountingRecords');
     }
 

+ 85 - 0
app/Http/Controllers/OwnerLogisticFeeDetailController.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\OwnerLogisticFeeDetail;
+use Illuminate\Http\Request;
+
+class OwnerLogisticFeeDetailController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        //
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        //
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\OwnerLogisticFeeDetail  $ownerLogisticFeeDetail
+     * @return \Illuminate\Http\Response
+     */
+    public function show(OwnerLogisticFeeDetail $ownerLogisticFeeDetail)
+    {
+        //
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  \App\OwnerLogisticFeeDetail  $ownerLogisticFeeDetail
+     * @return \Illuminate\Http\Response
+     */
+    public function edit(OwnerLogisticFeeDetail $ownerLogisticFeeDetail)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\OwnerLogisticFeeDetail  $ownerLogisticFeeDetail
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, OwnerLogisticFeeDetail $ownerLogisticFeeDetail)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\OwnerLogisticFeeDetail  $ownerLogisticFeeDetail
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(OwnerLogisticFeeDetail $ownerLogisticFeeDetail)
+    {
+        //
+    }
+}

+ 85 - 0
app/Http/Controllers/OwnerLogisticFeeReportController.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\OwnerLogisticFeeReport;
+use Illuminate\Http\Request;
+
+class OwnerLogisticFeeReportController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        //
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        //
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\OwnerLogisticFeeReport  $ownerLogisticFeeReport
+     * @return \Illuminate\Http\Response
+     */
+    public function show(OwnerLogisticFeeReport $ownerLogisticFeeReport)
+    {
+        //
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  \App\OwnerLogisticFeeReport  $ownerLogisticFeeReport
+     * @return \Illuminate\Http\Response
+     */
+    public function edit(OwnerLogisticFeeReport $ownerLogisticFeeReport)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\OwnerLogisticFeeReport  $ownerLogisticFeeReport
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, OwnerLogisticFeeReport $ownerLogisticFeeReport)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\OwnerLogisticFeeReport  $ownerLogisticFeeReport
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(OwnerLogisticFeeReport $ownerLogisticFeeReport)
+    {
+        //
+    }
+}

+ 22 - 0
app/Http/Controllers/TestController.php

@@ -70,6 +70,7 @@ use App\Services\OrderPackageReceivedSyncService;
 use App\Services\OrderPackageService;
 use App\Services\OrderService;
 use App\Services\OrderTrackingService;
+use App\Services\OwnerLogisticFeeDetailService;
 use App\Services\RejectedBillService;
 use App\Services\StoreService;
 use App\Services\SupplierService;
@@ -1521,6 +1522,18 @@ TEXT;
         dd($headers);
     }
 
+    }
+
+    public function ownerLogisticFeeDetailService_getDetails()
+    {
+        /**
+         * @var $services OwnerLogisticFeeDetailService
+         */
+        $services = app('OwnerLogisticFeeDetailService');
+        $result = $services->getDetails('12','2021-05-30','2021-05-31');
+        dd($result);
+    }
+
     public function order_counting_records_cache_clear()
     {
         $owner_ids = Owner::query()->select('id')->pluck('id');
@@ -1532,4 +1545,13 @@ TEXT;
             }
         }
     }
+
+    public function order_counting_records_init()
+    {
+        \App\OrderCountingRecord::query()->truncate();
+        $start = '2021-05-14';
+        $end = '2021-06-07';
+        $service = app('NewOrderCountingRecordService');
+        $service->recordByDay($start, $end, '日');
+    }
 }

+ 1 - 1
app/Jobs/LogisticSFSync.php

@@ -44,7 +44,7 @@ class LogisticSFSync implements ShouldQueue
      */
     public function handle()
     {
-        LogService::log(LogisticSFSync::class, "{$this->logistic_number}-JOB-SF", '');
+        LogService::log(LogisticSFSync::class, "JOB-SF", $this->logistic_number);
         $this->logisticSFService = app('LogisticSFService');
         $formedData = $this->logisticSFService->get([$this->logistic_number]);
         $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');

+ 6 - 4
app/Jobs/LogisticYDSync.php

@@ -19,14 +19,16 @@ class LogisticYDSync implements ShouldQueue
 
     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
     /**
-     * @var $logisticYDService LogisticYDService
+     *
      * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
      * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
      * @var $logistic_number string
      */
 
     protected $logistic_number;
-
+    /**
+     * @var $logisticYDService LogisticYDService
+     */
     protected $logisticYDService;
     protected $orderPackageReceivedSyncService;
 
@@ -47,14 +49,14 @@ class LogisticYDSync implements ShouldQueue
      */
     public function handle()
     {
-        LogService::log(LogisticYDSync::class, "{$this->logistic_number}-JOB-YD", '');
+        LogService::log(LogisticYDSync::class, "JOB-YD", $this->logistic_number);
         $this->logisticYDService = app('LogisticYDService');
         //先订阅订单
         $this->logisticYDService->registerApi([$this->logistic_number]);
         //查询订单路由信息
         $nativeResponse = $this->logisticYDService->query($this->logistic_number);
         //格式化信息
-        $formattedData = $this->logisticYDService->format($nativeResponse);
+        $formattedData = $this->logisticYDService->format($nativeResponse,$this->logistic_number);
         $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
         //更新数据
         $this->orderPackageReceivedSyncService->update([$formattedData]);

+ 2 - 2
app/Jobs/LogisticYTOSync.php

@@ -46,10 +46,10 @@ class LogisticYTOSync implements ShouldQueue
      */
     public function handle()
     {
-        LogService::log(LogisticYTOSync::class, "{$this->logistic_number}-JOB-YTO", '');
+        LogService::log(LogisticYTOSync::class, "JOB-YTO", $this->logistic_number);
         $this->logisticYTOService = app('LogisticYTOService');
         $nativeResponse = $this->logisticYTOService->query($this->logistic_number);
-        $formattedData = $this->logisticYTOService->format($nativeResponse);
+        $formattedData = $this->logisticYTOService->format($nativeResponse,$this->logistic_number);
         $this->orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
         if (count($formattedData)>0 && $formattedData['logistic_number']??false){
             $this->orderPackageReceivedSyncService->update([$formattedData]);

+ 1 - 2
app/Jobs/LogisticZopSync.php

@@ -51,11 +51,10 @@ class LogisticZopSync implements ShouldQueue
      */
     public function handle()
     {
-        ini_set('max_execution_time', 60);
         LogService::log(LogisticZopSync::class, "JOB-ZOP", $this->logistic_number);
         $logistic_zop_service = new LogisticZopService();
         $nativeResponse = $logistic_zop_service->query($this->logistic_number);
-        $formatted_data = $logistic_zop_service->format($nativeResponse);
+        $formatted_data = $logistic_zop_service->format($nativeResponse,$this->logistic_number);
         $this->order_package_received_sync_service->update([$formatted_data]);
     }
 }

+ 14 - 1
app/OrderCountingRecord.php

@@ -5,12 +5,25 @@ namespace App;
 use Illuminate\Database\Eloquent\Model;
 
 use App\Traits\ModelLogChanging;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
 class OrderCountingRecord extends Model
 {
     use ModelLogChanging;
 
     //
-    protected $fillable = ['owner_id','shop_id' ,'warehouse_id' ,'logistic_id' ,'date_target' ,'counting_unit' ,'amount','year','month','week'];
+    protected $fillable = ['owner_id' ,'warehouse_id' ,'logistic_id' ,'date_target' ,'counting_unit' ,'amount','year','month'];
+
+    public  $timestamps = false;
+
+    public function logistic(): BelongsTo
+    {
+        return $this->belongsTo(Logistic::class);
+    }
+
+    public function warehouse(): BelongsTo
+    {
+        return $this->belongsTo(Warehouse::class);
+    }
 
 }

+ 12 - 0
app/OwnerFeeDetail.php

@@ -6,6 +6,8 @@ use App\Services\ProcessMethodService;
 use Illuminate\Database\Eloquent\Model;
 
 use App\Traits\ModelLogChanging;
+use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasOne;
 
 class OwnerFeeDetail extends Model
 {
@@ -61,4 +63,14 @@ class OwnerFeeDetail extends Model
     {   //快递费子项
         return $this->hasMany(OwnerFeeDetailLogistic::class,"owner_fee_detail_id","id");
     }
+
+    public function ownerLogisticFeeDetails(): HasMany
+    {
+        return $this->hasMany(OwnerLogisticFeeDetail::class);
+    }
+
+    public function ownerLogisticFeeDetail(): HasOne
+    {
+        return $this->hasOne(OwnerLogisticFeeDetail::class,'logistic_bill','logistic_bill');
+    }
 }

+ 6 - 0
app/OwnerFeeDetailLogistic.php

@@ -5,6 +5,7 @@ namespace App;
 use Illuminate\Database\Eloquent\Model;
 
 use App\Traits\ModelLogChanging;
+use Illuminate\Database\Eloquent\Relations\HasOne;
 
 class OwnerFeeDetailLogistic extends Model
 {
@@ -20,4 +21,9 @@ class OwnerFeeDetailLogistic extends Model
     {   //快递
         return $this->belongsTo(Logistic::class);
     }
+
+    public function ownerLogisticFeeDetail(): HasOne
+    {
+        return $this->hasOne(OwnerLogisticFeeDetail::class, 'logistic_bill', 'logistic_bill');
+    }
 }

+ 26 - 0
app/OwnerLogisticFeeDetail.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+use App\Traits\ModelLogChanging;
+use Illuminate\Database\Eloquent\Relations\HasOne;
+
+class OwnerLogisticFeeDetail extends Model
+{
+    use ModelLogChanging;
+
+    public $fillable = ['owner_fee_detail_id', 'logistic_bill', 'initial_weight', 'initial_weight_price', 'additional_weight', 'additional_price'];
+
+    public function ownerFeeDetail(): HasOne
+    {
+        return $this->hasOne(OwnerFeeDetail::class);
+    }
+
+
+    public function ownerFeeDetailLogistic(): HasOne
+    {
+        return $this->hasOne(OwnerFeeDetailLogistic::class,'logistic_bill', 'logistic_bill');
+    }
+}

+ 14 - 0
app/OwnerLogisticFeeReport.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+use App\Traits\ModelLogChanging;
+
+class OwnerLogisticFeeReport extends Model
+{
+    use ModelLogChanging;
+
+    //
+}

+ 7 - 0
app/Providers/AppServiceProvider.php

@@ -33,6 +33,7 @@ use App\Services\LogisticYTOService;
 use App\Services\LogisticZopService;
 use App\Services\LogService;
 use App\Services\MaterialBoxService;
+use App\Services\NewOrderCountingRecordService;
 use App\Services\OracleBasCustomerService;
 use App\Services\OracleBasSkuService;
 use App\Services\OracleDocAsnDetailService;
@@ -132,6 +133,8 @@ use App\Services\ForeignZhenCangService;
 use App\Services\StorageService;
 use App\Services\LogisticAliJiSuApiService;
 use App\Services\CommodityMaterialBoxModelService;
+use App\Services\OwnerLogisticFeeDetailService;
+use App\Services\OwnerLogisticFeeReportService;
 
 class AppServiceProvider extends ServiceProvider
 {
@@ -196,6 +199,7 @@ class AppServiceProvider extends ServiceProvider
     private function loadingService(){
         app()->singleton('AllInventoryService',AllInventoryService::class);
         app()->singleton('AuthorityService',AuthorityService::class);
+        app()->singleton('NewOrderCountingRecordService',NewOrderCountingRecordService::class);
         app()->singleton('BatchService',BatchService::class);
         app()->singleton('BatchUpdateService', BatchUpdateService::class);
         app()->singleton('CacheService',CacheService::class);
@@ -256,6 +260,9 @@ class AppServiceProvider extends ServiceProvider
         app()->singleton('OwnerAreaReportService', OwnerAreaReportService::class);
         app()->singleton('OwnerBillReportService', OwnerBillReportService::class);
         app()->singleton('OwnerFeeDetailService', OwnerFeeDetailService::class);
+        app()->singleton('OwnerLogisticFeeDetailService',OwnerLogisticFeeDetailService::class);
+        app()->singleton('OwnerLogisticFeeReportService',OwnerLogisticFeeReportService::class);
+        app()->singleton('OwnerMaterialService', OwnerMaterialService::class);
         app()->singleton('OwnerMaterialService', OwnerMaterialService::class);
         app()->singleton('OwnerPriceDirectLogisticService', OwnerPriceDirectLogisticService::class);
         app()->singleton('OwnerPriceExpressService', OwnerPriceExpressService::class);

+ 1 - 1
app/Services/LogisticRouteInterface.php

@@ -8,5 +8,5 @@ interface LogisticRouteInterface
 {
     public function query($logistic_number);
 
-    public function format($nativeResponse);
+    public function format($nativeResponse,$logistic_number);
 }

+ 18 - 5
app/Services/LogisticYDService.php

@@ -64,8 +64,12 @@ class LogisticYDService
             'req-time' => now()->timestamp,
             "Content-Type" => "application/json"
         ];
-        $response = Http::withHeaders($headers)->withBody($json_body, 'application/json')->post($this->url);
-        return json_decode($response);
+        try {
+            $response = Http::withHeaders($headers)->withBody($json_body, 'application/json')->post($this->url);
+            return json_decode($response);
+        } catch (\GuzzleHttp\Exception\RequestException $e) {
+            LogService::log(LogisticYDService::class, "韵达-registerApi", $logistic_numbers);
+        }
     }
 
     public function query($logistic_number)
@@ -83,14 +87,23 @@ class LogisticYDService
             'req-time' => now()->timestamp,
             "Content-Type" => "application/json"
         ];
-        $response = Http::withHeaders($headers)->withBody(json_encode($body, JSON_UNESCAPED_UNICODE), 'application/json')->post($this->url);
+        try {
+            $response = Http::withHeaders($headers)->withBody(json_encode($body, JSON_UNESCAPED_UNICODE), 'application/json')->post($this->url);
+        } catch (\GuzzleHttp\Exception\RequestException $e) {
+            LogService::log(LogisticYDService::class, "韵达-query", $logistic_number);
+            return null;
+        }
         return json_decode($response->body());
     }
 
-    public function format($nativeResponse)
+    public function format($nativeResponse, $logistic_number)
     {
         if (is_null($nativeResponse) || $nativeResponse->code != '0000' || $nativeResponse->data->result == "false") {
-            return [];
+            return [
+                'logistic_number' => $logistic_number,
+                'exception_type' => '揽件异常',
+                'exception' => '是',
+            ];
         } else {
             $nativeData = $nativeResponse->data;
             try {

+ 8 - 2
app/Services/LogisticYTOService.php

@@ -42,10 +42,16 @@ class LogisticYTOService
         return json_decode($response->body());
     }
 
-    public function format($response): array
+    public function format($response,$logistic_number): array
     {
         $result = [];
-        if (is_object($response) && $response->code=='1001') {return [];}
+        if (is_object($response) && $response->code=='1001') {
+            return [
+                'logistic_number' => $logistic_number,
+                'exception_type' => '揽件异常',
+                'exception' => '是',
+            ];
+        }
         else {
             try {
                 if (is_array($response))$result['logistic_number'] = $response[0]->waybill_No;

+ 16 - 14
app/Services/LogisticZopService.php

@@ -28,30 +28,32 @@ class LogisticZopService implements LogisticRouteInterface
         return json_decode($response->body());
     }
 
-    public function format($nativeResponse)
+    public function format($nativeResponse,$logistic_number)
     {
-        if (is_null($nativeResponse)||empty($nativeResponse->result)) {//返回的结果为空,或者路由为[] 直接返回就好
-            return [];
-        }
-        $nativeRoutes = $nativeResponse->result;
-        $this->logistic_number = $nativeRoutes[0]->billCode;
-        $order_package = OrderPackage::query()->where('logistic_number', $this->logistic_number)->first();
-
-        $lastRoute = $nativeRoutes[count($nativeRoutes) - 1];
-
+        $order_package = OrderPackage::query()->where('logistic_number', $logistic_number)->first();
         $result = [
-            'logistic_number' => $this->logistic_number,
+            'logistic_number' => $logistic_number,
             'exception_type' => $order_package->exception_type,
-            'exception'=>$order_package->exception,
+            'exception' => $order_package->exception,
             'status' => $order_package->status,
             'transfer_status' => $order_package->transfer_status,
-            'received_at' =>$order_package->received_at,
+            'received_at' => $order_package->received_at,
         ];
+        $nativeRoutes = $nativeResponse->result??[];
+        if (empty($nativeRoutes)) {
+            $result['exception_type'] = '揽件异常';
+            $result['exception'] = '是';
+            return $result;
+        }
+        $lastRoute = $nativeRoutes[count($nativeRoutes) - 1]??[];
         list($status, $received_at) = $this->getNormalStatusAndReceivedAt($lastRoute);
         $result['status'] = $status;
         $result['received_at'] = $received_at;
         $result['transfer_status'] = $this->getTransferStatus($nativeRoutes);
         $result['routes_length'] = count($result['transfer_status']);
+        /**
+         * @var $orderPackageReceivedSyncService OrderPackageReceivedSyncService
+         */
         $orderPackageReceivedSyncService = app('OrderPackageReceivedSyncService');
         $lastRouteDate = Carbon::parse($lastRoute->scanDate / 1000)->addHours(8)->toDateTimeString();
         $exceptionData = $orderPackageReceivedSyncService->setExceptionType($result, $lastRouteDate);
@@ -82,7 +84,7 @@ class LogisticZopService implements LogisticRouteInterface
             case 'SIGNED':
             case '签收':
                 $status = '已收件';
-                $received_at = Carbon::parse($lastRoute->scanDate / 1000)->toDateTimeString();
+                $received_at = Carbon::parse($lastRoute->scanDate / 1000)->addHours(8)->toDateTimeString();
                 break;
             default:
                 $status = '无';

+ 239 - 17
app/Services/NewOrderCountingRecordService.php

@@ -9,6 +9,7 @@ use App\Order;
 use App\OrderCountingRecord;
 use App\Warehouse;
 use Carbon\Carbon;
+use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Cache;
@@ -18,7 +19,9 @@ use App\Traits\ServiceAppAop;
 class NewOrderCountingRecordService
 {
     use ServiceAppAop;
-    protected $modelClass=NewOrderCountingRecord::class;
+
+    protected $modelClass = OrderCountingRecord::class;
+
     public function orderCountingRecordsFromCache($start, $end, $unit, $ownerIds)
     {
         $dataList = collect();
@@ -86,22 +89,22 @@ class NewOrderCountingRecordService
     {
         $key = 'warehouseCountingRecords_' . $start . '_' . $end . '_' . json_encode($ownerIds);
         return Cache::remember($key, config('cache.expirations.warehouseCountingRecords'), function () use ($start, $end, $ownerIds) {
-        $dataList = collect();
-        $resultOrders = $this->get($start, $end, '日', $ownerIds);
-        $resultOrders->groupBy('warehouse_id')->each(function ($item) use (&$dataList) {
-            $counter = $item->reduce(function ($sum, $item) {
-                return $sum + $item->amount;
-            }, 0);
-            $warehouse = Warehouse::query()->find($item[0]->warehouse_id);
+            $dataList = collect();
+            $resultOrders = $this->get($start, $end, '日', $ownerIds);
+            $resultOrders->groupBy('warehouse_id')->each(function ($item) use (&$dataList) {
+                $counter = $item->reduce(function ($sum, $item) {
+                    return $sum + $item->amount;
+                }, 0);
+                $warehouse = Warehouse::query()->find($item[0]->warehouse_id);
 
-            $dataList->push([
-                'value' => $counter,
-                'warehouse_id' => $item[0]->warehouse_id,
-                'name' => $warehouse ? $warehouse->name : '仓库为空',
-                'code' => $warehouse ? $warehouse->code : 'NULL',
-            ]);
-        });
-        return $dataList;
+                $dataList->push([
+                    'value' => $counter,
+                    'warehouse_id' => $item[0]->warehouse_id,
+                    'name' => $warehouse ? $warehouse->name : '仓库为空',
+                    'code' => $warehouse ? $warehouse->code : 'NULL',
+                ]);
+            });
+            return $dataList;
         });
     }
 
@@ -476,11 +479,230 @@ class NewOrderCountingRecordService
     {
         if ($this->isNotCurrentDate($dateStr, $unit)) {
             LogService::log('NewOrderCountingRecordService', '缓存设置为永久', $dateStr);
-            $ttl = config('cache.expirations.forever');//非当前日期的缓存为永久
+            $ttl = config('cache.expirations.orderCountingRecord');//非当前日期的缓存为永久
         } else {
             LogService::log('NewOrderCountingRecordService', '缓存设置为临时', $dateStr);
             $ttl = config('cache.expirations.orderCountingRecord');//当前日期缓存为1800s
         }
         return $ttl;
     }
+
+
+    //TODO 控制台重构
+
+    public function getWareHouseRecordsApi($start, $end, $ownerIds): array
+    {
+        $orderCountingRecords = OrderCountingRecord::query()
+            ->selectRaw("sum(amount) as value ,warehouse_id")
+            ->with('warehouse:id,name,code')
+            ->whereBetween('date_target', [$start, $end])
+            ->whereIn('owner_id', $ownerIds)
+            ->groupBy('warehouse_id')
+            ->get();
+        $result = [];
+        foreach ($orderCountingRecords as $orderCountingRecord) {
+            $result[] = [
+                'logistic_id' => $orderCountingRecord->warehouse_id,
+                'value' => $orderCountingRecord->value,
+                'name' => $orderCountingRecord->warehouse->name ?? '',
+                'code' => $orderCountingRecord->warehouse->code ?? '',
+            ];
+        }
+        return $result;
+    }
+
+    public function getLogisticRecordsApi($start, $end, $ownerIds): array
+    {
+        $orderCountingRecords = OrderCountingRecord::query()
+            ->selectRaw("sum(amount) as value ,logistic_id")
+            ->with('logistic:id,name')
+            ->whereBetween('date_target', [$start, $end])
+            ->whereIn('owner_id', $ownerIds)
+            ->groupBy('logistic_id')
+            ->get();
+        $result = [];
+        foreach ($orderCountingRecords as $orderCountingRecord) {
+            $result[] = [
+                'logistic_id' => $orderCountingRecord->logistic_id,
+                'value' => $orderCountingRecord->value,
+                'name' => $orderCountingRecord->logistic->name ?? '',
+            ];
+        }
+        return $result;
+    }
+
+    /**
+     * 查询订单量趋势
+     * @param $start string
+     * @param $end string
+     * @param $unit string
+     * @param $ownerIds array
+     * @return Builder[]|\Illuminate\Database\Eloquent\Collection
+     */
+    public function getOrderCountingRecordsApi(string $start, string $end, string $unit, array $ownerIds)
+    {
+        $orderCountingRecords = OrderCountingRecord::query()
+            ->selectRaw("sum(amount) as counter ,date_target")
+            ->whereBetween('date_target', [$start, $end])
+            ->where('counting_unit', $unit)
+            ->whereIn('owner_id', $ownerIds)
+            ->groupBy('date_target')
+            ->get()->toArray();
+        if (now()->toDateString() == $end) {//查询时间包含当天
+            switch ($unit) {
+                case '日':
+                    //查询当天统计
+                    $startDateTime = Carbon::parse($end)->startOfDay()->toDateTimeString();
+                    $endDateTime = Carbon::parse($end)->endOfDay()->toDateTimeString();
+                    break;
+                case '月':
+                    //查询当月统计
+                    $startDateTime = Carbon::parse($end)->startOfMonth()->startOfDay()->toDateTimeString();
+                    $endDateTime = Carbon::parse($end)->endOfMonth()->endOfDay()->toDateTimeString();
+                    break;
+                case '年':
+                    //查询当年统计
+                    $startDateTime = Carbon::parse($end)->startOfYear()->startOfDay()->toDateTimeString();
+                    $endDateTime = Carbon::parse($end)->endOfYear()->endOfDay()->toDateTimeString();
+                    break;
+            }
+            $order = Order::query()
+                ->selectRaw("count(1) as amounts ,DATE_FORMAT(created_at,'%Y-%m-%d') as date_target")
+                ->whereBetween('created_at', [$startDateTime, $endDateTime])
+                ->where('wms_status', '订单完成')
+                ->whereIn('owner_id', $ownerIds)
+                ->groupBy('date_target')
+                ->first();
+            $orderCountingRecords[] = [
+                "counter" => $order->amounts,
+                "date_target" => $order->date_target,
+            ];
+        }
+        return $orderCountingRecords;
+    }
+
+    /**
+     * 统计订单量 从$start 开始统计 默认截止到当前日期的前一天
+     * @param $start string
+     * @param null $end string
+     * @param $unit string
+     */
+    public function recordOrder(string $start, $end = null, string $unit = '日')
+    {
+        switch ($unit) {
+            case '日':
+                $this->recordByDay($start, $end, $unit);
+                break;
+            case'月':
+                $this->recordByMonth($start, $end, $unit);
+                break;
+            case'年':
+                $this->recordByYear($start, $end, $unit);
+                break;
+            default:
+        }
+    }
+
+    /**
+     * 清空统计缓存
+     */
+    public function clearCacheOrderRecord()
+    {
+
+    }
+
+    /**
+     * 日统计
+     * @param string $start
+     * @param $end
+     * @param string $unit
+     */
+    public function recordByDay(string $start, $end = null, string $unit = '日'): void
+    {
+        $startDateTime = Carbon::parse($start)->startOfDay()->toDateTimeString();
+        if (is_null($end)) {
+            $end = now()->subDay()->endOfDay();
+        }
+        $endDateTime = Carbon::parse($end)->endOfDay()->toDateTimeString();
+        $orders = Order::query()
+            ->selectRaw("owner_id,warehouse_id,logistic_id,count(1) as amounts ,DATE_FORMAT(created_at,'%Y-%m-%d') as date_target")
+            ->whereBetween('created_at', [$startDateTime, $endDateTime])
+            ->where('wms_status', '订单完成')
+            ->groupBy('owner_id', 'warehouse_id', 'logistic_id', 'date_target')
+            ->get();
+        $insertData = [];
+        foreach ($orders as $order) {
+            $insertData[] = [
+                'owner_id' => $order->owner_id,
+                'warehouse_id' => $order->warehouse_id,
+                'logistic_id' => $order->logistic_id,
+                'date_target' => $order->date_target,
+                'counting_unit' => $unit,
+                'amount' => $order->amounts,
+                'year' => Carbon::parse($order->date_target)->year,
+                'month' => Carbon::parse($order->date_target)->year . '-' . Carbon::parse($order->date_target)->month,
+            ];
+        }
+        $insertDataChunked = array_chunk($insertData, 2000);
+        foreach ($insertDataChunked as $items) {
+            OrderCountingRecord::query()->insertOrIgnore($items);
+        }
+    }
+
+
+    public function recordByMonth(string $start, $end = null, $unit = '月')
+    {
+        $startDate = Carbon::parse($start)->startOfMonth()->toDateString();
+        if (is_null($end)) {
+            $end = now()->subMonth()->endOfDay();
+        }
+        $endDate = Carbon::parse($end)->endOfDay()->toDateString();
+        $orderCountingRecords = OrderCountingRecord::query()
+            ->selectRaw("owner_id,warehouse_id,logistic_id,sum(amount) as amount_sum,month,year,date_target")
+            ->whereBetween('date_target', [$startDate, $endDate])
+            ->where('counting_unit', '日')
+            ->groupBy('owner_id', 'warehouse_id', 'logistic_id', 'month', 'date_target')
+            ->get();
+        $insertData = [];
+        foreach ($orderCountingRecords as $orderCountingRecord) {
+            $insertData[] = [
+                'owner_id' => $orderCountingRecord->owner_id,
+                'warehouse_id' => $orderCountingRecord->warehouse_id,
+                'logistic_id' => $orderCountingRecord->logistic_id,
+                'counting_unit' => $unit,
+                'date_target' => Carbon::parse($orderCountingRecord->date_target)->startOfMonth()->toDateString(),
+                'amount' => $orderCountingRecord->amount_sum,
+                'year' => $orderCountingRecord->year,
+                'month' => $orderCountingRecord->month,
+            ];
+        }
+        OrderCountingRecord::query()->insertOrIgnore($insertData);
+    }
+
+    public function recordByYear(string $start, $end = null, $unit = '年')
+    {
+        $startYear = Carbon::parse($start)->year;
+        if (is_null($end)) {
+            $end = now()->subYear()->toDateString();
+        }
+        $endYear = Carbon::parse($end)->year;
+        $orderCountingRecords = OrderCountingRecord::query()
+            ->selectRaw("owner_id,warehouse_id,logistic_id,sum(amount) as amount_sum,year,date_target")
+            ->whereBetween('year', [$startYear, $endYear])
+            ->groupBy('owner_id', 'warehouse_id', 'logistic_id', 'year')
+            ->get();
+        $insertData = [];
+        foreach ($orderCountingRecords as $orderCountingRecord) {
+            $insertData[] = [
+                'owner_id' => $orderCountingRecord->owner_id,
+                'warehouse_id' => $orderCountingRecord->warehouse_id,
+                'logistic_id' => $orderCountingRecord->logistic_id,
+                'counting_unit' => $unit,
+                'date_target' => Carbon::parse($orderCountingRecord->date_target)->startOfYear()->toDateString(),
+                'amount' => $orderCountingRecord->amount_sum,
+                'year' => $orderCountingRecord->year,
+            ];
+        }
+        OrderCountingRecord::query()->insertOrIgnore($insertData);
+    }
 }

+ 11 - 7
app/Services/OrderPackageReceivedSyncService.php

@@ -41,7 +41,7 @@ class OrderPackageReceivedSyncService
             $query = $query->where('sent_at', '>=', $initDate->subDays((int)config('api_logistic.days'))->toDateTimeString())
                 ->whereNull('received_at');
         } else {//查询20天以内的数据
-            $query = $query->where('sent_at', '>=', now()->subDays(20))
+            $query = $query->whereDate('sent_at','2021-06-02')
                 ->whereNull('received_at');
         }
         $query->chunk(2000, function ($orderPackages) {
@@ -49,7 +49,7 @@ class OrderPackageReceivedSyncService
             //sf
             if (array_key_exists('SF', $logisticNumbers)) {
                 $SFLogisticNumbers = $logisticNumbers['SF'];
-                LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-SF", '');
+                LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-SF", $SFLogisticNumbers);
                 foreach ($SFLogisticNumbers as $logisticNumber) {
                     LogisticSFSync::dispatch($logisticNumber);
                 }
@@ -57,7 +57,7 @@ class OrderPackageReceivedSyncService
             //更新中通
             if (array_key_exists('ZTO', $logisticNumbers)) {
                 $ZTOLogisticNumbers = $logisticNumbers['ZTO'];
-                LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-ZTO", '');
+                LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-ZTO", $ZTOLogisticNumbers);
                 foreach ($ZTOLogisticNumbers as $logisticNumber) {
                     LogisticZopSync::dispatch($logisticNumber);
                 }
@@ -65,7 +65,7 @@ class OrderPackageReceivedSyncService
             //更新韵达
             if (array_key_exists('YUNDA', $logisticNumbers)) {
                 $YDLogisticNumbers = $logisticNumbers['YUNDA'];
-                LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-YUNDA", '');
+                LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-YUNDA", $YDLogisticNumbers);
                 foreach ($YDLogisticNumbers as $logistic_number) {
                     LogisticYDSync::dispatch($logistic_number);
                 }
@@ -73,7 +73,7 @@ class OrderPackageReceivedSyncService
             //更新圆通
             if (array_key_exists('YTO', $logisticNumbers)) {
                 $YTOLogisticNumbers = $logisticNumbers['YTO'];
-                LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-YTO", '');
+                LogService::log(OrderPackageReceivedSyncService::class, "同步快递信息定时方法-YTO", $YTOLogisticNumbers);
                 foreach ($YTOLogisticNumbers as $logistic_number) {
                     LogisticYTOSync::dispatch($logistic_number);
                 }
@@ -190,6 +190,7 @@ class OrderPackageReceivedSyncService
             try {
                 $logisticCode = $orderPackage->order->logistic->code;
             } catch (Exception $e) {
+                LogService::log(OrderPackageReceivedSyncService::class, "快递同步按照承运商分组异常", $orderPackage->id);
                 continue;
             }
             $key = config('api_logistic.logistic.' . $logisticCode);
@@ -209,6 +210,9 @@ class OrderPackageReceivedSyncService
      */
     public function setExceptionType(array $data, $lastRouteDate): array
     {
+        //设置默认异常为否
+        $data['exception_type'] = '无';
+        $data['exception'] = '否';
         $logistic_number = $data['logistic_number'];
         /** @var OrderPackage $orderPackage */
         $orderPackage = OrderPackage::query()->with('order')->where('logistic_number', $logistic_number)->first();
@@ -321,7 +325,7 @@ class OrderPackageReceivedSyncService
             $conclusion |= ($last_routed_duration > $SHORT_RESPONSE_HOURS && $last_routed_duration < $LONG_RESPONSE_HOURS) ? $IS_SHORT_NO_RESPONSE : 0;
             $conclusion |= ($last_routed_duration > $LONG_RESPONSE_HOURS) ? $IS_LONG_NO_RESPONSE : 0;
             $conclusion |= ($last_routed_duration > $SENDING_RESPONSE_HOURS && $data['status'] == '派送中') ? $IS_SENDING_NO_RESPONSE : 0;
-            $conclusion |= ($delivered_duration > $HAVEN_SECOND_GOT_HOURS && $data['routes_length'] < 3) ? $IS_SECOND_ROUTE_HAVE : 0;//超过指定时间,路由信息小于两
+            $conclusion |= ($delivered_duration > $HAVEN_SECOND_GOT_HOURS && $data['routes_length'] < 3) ? $IS_SECOND_ROUTE_HAVE : 0;//和出库时间比较 超过指定时间,路由信息小于三
             return $conclusion;
         })();
         switch ($conclusion) {
@@ -348,7 +352,7 @@ class OrderPackageReceivedSyncService
         }
         if ($conclusion
             == ($conclusion | $IS_SECOND_ROUTE_HAVE)) {
-            $data['exception_type'] = '件异常';
+            $data['exception_type'] = '件异常';
             $data['exception'] = '是';
 
         }

+ 95 - 0
app/Services/OwnerLogisticFeeDetailService.php

@@ -0,0 +1,95 @@
+<?php
+
+namespace App\Services;
+
+use App\OwnerFeeDetail;
+use App\Traits\ServiceAppAop;
+use App\OwnerLogisticFeeDetail;
+use Carbon\Carbon;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Collection;
+
+class OwnerLogisticFeeDetailService
+{
+    use ServiceAppAop;
+
+    /**
+     * @var $modelClass  OwnerLogisticFeeDetail
+     */
+    protected $modelClass = OwnerLogisticFeeDetail::class;
+
+    //插入数据 ServiceAppAop的 insert 方法 支持批量
+
+    /**
+     * 根据货主查询 和时间段查询
+     * @param string $owner_id
+     * @param string $start
+     * @param string $end
+     * @return array
+     */
+    public function getDetails(string $owner_id, string $start, string $end): array
+    {
+        $ownerFeeDetailIds = OwnerFeeDetail::query()->selectRaw('id')
+            ->where('type', '发货')
+            ->where('outer_table_name', 'orders')
+            ->whereHas('logistic', function (Builder $query) {
+                $query->where('type', '快递');
+            })
+            ->where('owner_id', $owner_id)
+            ->where('worked_at', '>=', Carbon::parse($start)->startOfDay())
+            ->where('worked_at', '<=', Carbon::parse($end)->endOfDay())->pluck('id');
+        $ownerLogisticFeeDetails = OwnerLogisticFeeDetail::query()->with(['ownerFeeDetail.logistic', 'ownerFeeDetailLogistic'])->whereIn('owner_fee_detail_id', $ownerFeeDetailIds)->paginate();
+        dd($ownerLogisticFeeDetails);
+
+//         $ownerFeeDetails = OwnerFeeDetail::query()
+//            ->with(['ownerLogisticFeeDetail:id,logistic_bill,initial_weight_price,additional_price', 'items.logistic:id,name','items.ownerLogisticFeeDetail:id,logistic_bill,initial_weight_price,additional_price', 'logistic:id,name,type'])
+//            ->where('type', '发货')
+//            ->where('outer_table_name', 'orders')
+//            ->whereHas('logistic', function (Builder $query) {
+//                $query->where('type', '快递');
+//            })
+//            ->where('owner_id', $owner_id)
+//            ->where('worked_at', '>=', Carbon::parse($start)->startOfDay())
+//            ->where('worked_at', '<=', Carbon::parse($end)->endOfDay())
+//            ->get();
+//        $result = [];
+//        foreach ($ownerFeeDetails as $ownerFeeDetail) {
+//            if ($ownerFeeDetail->items->count()==0) {//只有一个包裹
+//                $result[] = [
+//                    'logistic_name' => $ownerFeeDetail->logistic->name ?? '',//快递公司
+//                    'province' => $ownerFeeDetail->province,//省份
+//                    'logistic_bill' => $ownerFeeDetail->logistic_bill ?? '',//快递单号
+//                    'weight' => $ownerFeeDetail->weight,//重量
+//                    'logistic_fee' => $ownerFeeDetail->logistic_fee,//快递费
+//                    'initial_weight_price' => $ownerFeeDetail->ownerLogisticFeeDetail->initial_weight_price ?? '',//首重价格
+//                    'additional_price' => $ownerFeeDetail->ownerLogisticFeeDetail->additional_price ?? '',//续重价格
+//                ];
+//            } else {//多个包裹
+//                foreach ($ownerFeeDetail->items as $ownerFeeDetailLogistic) {
+//                    $result[] = [
+//                        'logistic_name' => $ownerFeeDetail->logistic->name ?? '',//快递公司
+//                        'province' => $ownerFeeDetail->province,//省份
+//                        'logistic_bill' => $ownerFeeDetailLogistic->logistic_bill,//快递单号
+//                        'weight' => $ownerFeeDetailLogistic->weight,//重量
+//                        'logistic_fee' => $ownerFeeDetailLogistic->logistic_fee,//快递费
+//                        'initial_weight_price' => $ownerFeeDetailLogistic->ownerLogisticFeeDetail->initial_weight_price ?? '',//首重价格
+//                        'additional_price' => $ownerFeeDetailLogistic->ownerLogisticFeeDetail->additional_price ?? '',//续重价格
+//                    ];
+//                }
+//            }
+//        }
+        $result = [];
+        foreach ($ownerLogisticFeeDetails->items() as $ownerLogisticFeeDetail) {
+                $result[] = [
+                    'logistic_name' => $ownerLogisticFeeDetail->ownerFeeDetail->logistic->name ?? '',//快递公司
+                    'province' => $ownerLogisticFeeDetail->ownerFeeDetail->province,//省份
+                    'logistic_bill' => $ownerLogisticFeeDetail->logistic_bill ?? '',//快递单号
+                    'weight' => $ownerLogisticFeeDetail->ownerFeeDetailLogistic->weight,//重量
+                    'logistic_fee' => $ownerLogisticFeeDetail->ownerFeeDetailLogistic->logistic_fee,//快递费
+                    'initial_weight_price' => $ownerLogisticFeeDetail->initial_weight_price ?? '',//首重价格
+                    'additional_price' => $ownerLogisticFeeDetail->additional_price ?? '',//续重价格
+                ];
+        }
+        return $result;
+    }
+}

+ 13 - 0
app/Services/OwnerLogisticFeeReportService.php

@@ -0,0 +1,13 @@
+<?php 
+
+namespace App\Services;
+
+use App\Traits\ServiceAppAop;
+use App\OwnerLogisticFeeReport;
+
+class OwnerLogisticFeeReportService
+{
+    use ServiceAppAop;
+    protected $modelClass=OwnerLogisticFeeReport::class;
+
+}

+ 2 - 2
config/cache.php

@@ -16,8 +16,8 @@ return [
 
         'owners'=>20,           //模型Owner
         'orderCountingRecord'=>1800,           //模型Owner
-        'logisticsCountingRecords'=>86400,           //快递饼图缓存时间
-        'warehouseCountingRecords'=>86400,           //仓库饼图缓存时间
+        'logisticsCountingRecords'=>1800,           //快递饼图缓存时间
+        'warehouseCountingRecords'=>1800,           //仓库饼图缓存时间
     ],
     /*
     |--------------------------------------------------------------------------

+ 0 - 1
database/factories/OrderCountingRecordFactory.php

@@ -14,7 +14,6 @@ use Illuminate\Database\Eloquent\Factory;
 $factory->define(OrderCountingRecord::class, function (Faker $faker) {
     return [
         'owner_id' => $faker->numberBetween(0, 100),
-        'shop_id' => $faker->numberBetween(0, 100),
         'warehouse_id' => $faker->numberBetween(0, 100),
         'logistic_id' => $faker->numberBetween(0, 100),
         'date_target' => Carbon::now()->toDateString(),

+ 17 - 0
database/factories/OwnerLogisticFeeDetailFactory.php

@@ -0,0 +1,17 @@
+<?php
+
+/** @var \Illuminate\Database\Eloquent\Factory $factory */
+
+use App\OwnerLogisticFeeDetail;
+use Faker\Generator as Faker;
+
+$factory->define(OwnerLogisticFeeDetail::class, function (Faker $faker) {
+    return [
+        'owner_fee_detail_id'=>random_int(1,100),
+        'logistic_bill'=>$faker->uuid,
+        'initial_weight'=>random_int(1,100),
+        'initial_weight_price'=>random_int(1,100),
+        'additional_weight'=>random_int(1,100),
+        'additional_price' => random_int(1, 100),
+    ];
+});

+ 12 - 0
database/factories/OwnerLogisticFeeReportFactory.php

@@ -0,0 +1,12 @@
+<?php
+
+/** @var \Illuminate\Database\Eloquent\Factory $factory */
+
+use App\OwnerLogisticFeeReport;
+use Faker\Generator as Faker;
+
+$factory->define(OwnerLogisticFeeReport::class, function (Faker $faker) {
+    return [
+        //
+    ];
+});

+ 39 - 0
database/migrations/2021_06_03_115444_create_owner_logistic_fee_details_table.php

@@ -0,0 +1,39 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateOwnerLogisticFeeDetailsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('owner_logistic_fee_details', function (Blueprint $table) {
+            $table->id();
+            $table->unsignedBigInteger('owner_fee_detail_id')->comment('计费模型详情');
+            $table->string('logistic_bill')->comment('快递单号');
+            $table->decimal('initial_weight')->comment('首重');
+            $table->decimal('initial_weight_price')->comment('首重价格');
+            $table->decimal('additional_weight')->comment('续重');
+            $table->decimal('additional_price')->comment('续重价格');
+            $table->timestamps();
+            $table->index(['owner_fee_detail_id']);
+            $table->index(['logistic_bill']);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('owner_logistic_fee_details');
+    }
+}

+ 31 - 0
database/migrations/2021_06_03_115532_create_owner_logistic_fee_reports_table.php

@@ -0,0 +1,31 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateOwnerLogisticFeeReportsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('owner_logistic_fee_reports', function (Blueprint $table) {
+            $table->id();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('owner_logistic_fee_reports');
+    }
+}

+ 54 - 0
database/migrations/2021_06_05_103627_change_order_counting_records.php

@@ -0,0 +1,54 @@
+<?php
+
+use App\OrderCountingRecord;
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class ChangeOrderCountingRecords extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('order_counting_records', function (Blueprint $table) {
+            OrderCountingRecord::query()->truncate();
+            $table->dropColumn('week');
+            $table->dropColumn('shop_id');
+            $table->dropColumn('created_at');
+            $table->dropColumn('updated_at');
+
+            $table->dropIndex('order_counting_records_owner_id_index');
+            $table->dropIndex('order_counting_records_warehouse_id_index');
+            $table->dropIndex('order_counting_records_logistic_id_index');
+            $table->dropIndex('order_counting_records_date_target_index');
+            $table->dropIndex('order_counting_records_counting_unit_index');
+            $table->unique(['owner_id', 'warehouse_id', 'logistic_id', 'date_target', 'counting_unit'],'order_counting_records_unique');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('order_counting_records', function (Blueprint $table) {
+            $table->string('week');
+            $table->integer('shop_id');
+
+            $table->index('owner_id');
+            $table->index('shop_id');
+            $table->index('warehouse_id');
+            $table->index('logistic_id');
+            $table->index('date_target');
+            $table->index('counting_unit');
+
+            $table->dropIndex('order_counting_records_unique');
+        });
+    }
+}

+ 22 - 0
database/seeds/OrderSeed.php

@@ -0,0 +1,22 @@
+<?php
+
+use Illuminate\Database\Seeder;
+
+class OrderSeed extends Seeder
+{
+    /**
+     * Run the database seeds.
+     *
+     * @return void
+     */
+    public function run()
+    {
+        foreach (now()->subMonth()->daysUntil(now()) as $day) {
+            factory(\App\Order::class)->times(random_int(10,12))->create([
+                'created_at' => $day->toDateTimeString(),
+                'wms_status' => '订单完成',
+            ]);
+        }
+
+    }
+}

+ 16 - 0
database/seeds/OwnerLogisticFeeDetailSeeder.php

@@ -0,0 +1,16 @@
+<?php
+
+use Illuminate\Database\Seeder;
+
+class OwnerLogisticFeeDetailSeeder extends Seeder
+{
+    /**
+     * Run the database seeds.
+     *
+     * @return void
+     */
+    public function run()
+    {
+        //
+    }
+}

+ 16 - 0
database/seeds/OwnerLogisticFeeReportSeeder.php

@@ -0,0 +1,16 @@
+<?php
+
+use Illuminate\Database\Seeder;
+
+class OwnerLogisticFeeReportSeeder extends Seeder
+{
+    /**
+     * Run the database seeds.
+     *
+     * @return void
+     */
+    public function run()
+    {
+        //
+    }
+}

+ 1 - 1
tests/Feature/LogisticZopSyncTest.php

@@ -39,7 +39,7 @@ class LogisticZopSyncTest extends TestCase
 
     public function test_get()
     {
-        LogisticZopSync::dispatch('75600296655116');
+        LogisticZopSync::dispatch('75600272832573');
     }
 
 

+ 1 - 1
tests/Services/LogisticYDService/LogisticYDSyncTest.php

@@ -36,7 +36,7 @@ class LogisticYDSyncTest extends TestCase
      */
     public function sync_test()
     {
-        LogisticYDSync::dispatch('4314543143889');
+        LogisticYDSync::dispatch('4314960813161');
         $this->assertTrue(OrderPackage::query()->where('logistic_number','4314543143889')->first()->transfer_status);
     }
 }

+ 48 - 0
tests/Services/NewOrderCountingRecordService/GetOrderCountingRecordsApiTest.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace Tests\Services\NewOrderCountingRecordService;
+use App\Services\NewOrderCountingRecordService;
+use Tests\TestCase;
+use App\OrderCountingRecord;
+use App\Traits\TestMockSubServices;
+
+class GetOrderCountingRecordsApiTest extends TestCase
+{
+    use TestMockSubServices;
+    /** @var NewOrderCountingRecordService $service */
+    public $service;
+    private $data;
+
+    private $amount=1;
+    function setUp(): void
+    {
+        parent::setUp();
+        $this->service = app('NewOrderCountingRecordService');
+//        $this->data['newOrderCountingRecords']
+//            = factory(OrderCountingRecord::class, $this->amount)
+//            ->create();
+    }
+
+    public function testReturned()
+    {
+        $this->assertTrue(true);
+    }
+
+    function tearDown(): void
+    {
+        OrderCountingRecord::query()
+            ->whereIn('id',data_get($this->data['newOrderCountingRecords'],'*.id')??[])
+            ->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * @test
+     */
+    public function get_test()
+    {
+        $ownerIds = \App\Owner::query()->pluck('id')->toArray();
+        $result  =  $this->service->getOrderCountingRecordsApi('2021-05-01', '2021-06-08', '日', $ownerIds);
+        $this->assertTrue(true);
+    }
+}

+ 59 - 0
tests/Services/NewOrderCountingRecordService/RecordByMonthTest.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace Tests\Services\NewOrderCountingRecordService;
+
+use App\Services\NewOrderCountingRecordService;
+use Tests\TestCase;
+use App\OrderCountingRecord;
+use App\Traits\TestMockSubServices;
+
+class RecordByMonthTest extends TestCase
+{
+    use TestMockSubServices;
+
+    /** @var NewOrderCountingRecordService $service */
+    public $service;
+    private $data;
+    private $amount = 2;
+
+    function setUp(): void
+    {
+        parent::setUp();
+        $this->service = app('NewOrderCountingRecordService');
+//        $this->data['newOrderCountingRecords']
+//            = factory(OrderCountingRecord::class, $this->amount)
+//            ->create();
+    }
+
+    public function testReturned()
+    {
+        $this->assertTrue(true);
+    }
+
+    function tearDown(): void
+    {
+        OrderCountingRecord::query()
+            ->whereIn('id', data_get($this->data['newOrderCountingRecords'], '*.id') ?? [])
+            ->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * @test
+     */
+    public function get_test()
+    {
+        $start = '2021-05-01';
+        $startDate = $start;
+        $endDate = \Carbon\Carbon::parse($start)->endOfMonth()->toDateString();
+        $this->service->recordByMonth($start);
+        $this->assertEquals(OrderCountingRecord::query()
+            ->whereBetween('date_target', [$startDate, $endDate])
+            ->where('counting_unit','日')
+            ->sum('amount'), OrderCountingRecord::query()
+                ->where('counting_unit','月')
+                ->where('month','2021-5')
+                ->sum('amount'));
+        $this->assertTrue(true);
+    }
+}

+ 73 - 0
tests/Services/NewOrderCountingRecordService/RecordByYearTest.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace Tests\Services\NewOrderCountingRecordService;
+
+use App\Services\NewOrderCountingRecordService;
+use Carbon\Carbon;
+use Tests\TestCase;
+use App\OrderCountingRecord;
+use App\Traits\TestMockSubServices;
+
+class RecordByYearTest extends TestCase
+{
+    use TestMockSubServices;
+
+    /** @var NewOrderCountingRecordService $service */
+    public $service;
+    private $data;
+    private $amount = 1;
+
+    function setUp(): void
+    {
+        parent::setUp();
+        $this->service = app('NewOrderCountingRecordService');
+//        $this->data['newOrderCountingRecords']
+//            = factory(OrderCountingRecord::class, $this->amount)
+//            ->create();
+    }
+
+    public function testReturned()
+    {
+        $this->assertTrue(true);
+    }
+
+    function tearDown(): void
+    {
+        OrderCountingRecord::query()
+            ->whereIn('id', data_get($this->data['newOrderCountingRecords'], '*.id') ?? [])
+            ->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * @test
+     */
+    public function get_test()
+    {
+        $startYear = Carbon::parse(now()->subYear())->year;
+        if (is_null(null)) {
+            $end = now()->subYear()->toDateString();
+        }
+
+        $endYear = Carbon::parse($end)->year;
+        $aa = OrderCountingRecord::query()
+            ->whereBetween('year', [$startYear, $endYear])
+            ->where('counting_unit', '月')
+            ->sum('amount');
+
+        $this->data['newOrderCountingRecords']
+            = factory(OrderCountingRecord::class, $this->amount)
+            ->create([
+                'date_target' => now()->subYear()->startOfMonth()->toDateString(),
+                'counting_unit' => '月',
+                'amount' => 10,
+                'year' => now()->subYear()->year,
+            ]);
+        $this->service->recordByYear('2020-01-01');
+        $bb = OrderCountingRecord::query()
+            ->whereBetween('year', [$startYear, $endYear])
+            ->where('counting_unit', '月')
+            ->sum('amount');
+        $this->assertEquals($aa+10, $bb);
+    }
+}

+ 62 - 0
tests/Services/NewOrderCountingRecordService/RecordOrderDayTest.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace Tests\Services\NewOrderCountingRecordService;
+use App\Services\NewOrderCountingRecordService;
+use Carbon\Carbon;
+use Tests\TestCase;
+use App\OrderCountingRecord;
+use App\Traits\TestMockSubServices;
+
+class RecordOrderDayTest extends TestCase
+{
+    use TestMockSubServices;
+    /** @var NewOrderCountingRecordService $service */
+    public $service;
+    private $data;
+    private $amount=2;
+    function setUp(): void
+    {
+        parent::setUp();
+        $this->service = app('NewOrderCountingRecordService');
+//        $this->data['newOrderCountingRecords']
+//            = factory(OrderCountingRecord::class, $this->amount)
+//            ->create();
+    }
+
+    public function testReturned()
+    {
+        $this->assertTrue(true);
+    }
+
+    function tearDown(): void
+    {
+        OrderCountingRecord::query()
+            ->whereIn('id',data_get($this->data['newOrderCountingRecords'],'*.id')??[])
+            ->delete();
+        parent::tearDown();
+    }
+
+
+    /**
+     * @test
+     */
+    public function record_test()
+    {
+        $start = '2021-05-14';
+        $end = '2021-06-08';
+        $this->service->recordByDay($start, $end, '日');
+        $startDateTime = Carbon::parse($start)->startOfDay()->toDateTimeString();
+        $endDateTime = now()->subDay()->endOfDay()->toDateTimeString();
+
+        $orderCount = \App\Order::query()
+            ->whereBetween('created_at', [$startDateTime, $endDateTime])
+            ->where('wms_status', '订单完成')
+            ->count();
+        //TODO 只能使用Date!!!!
+        $sum = OrderCountingRecord::query()
+            ->whereBetween('date_target', [Carbon::parse($startDateTime)->toDateString(), Carbon::parse($endDateTime)->toDateString()])
+            ->sum('amount');
+        $this->assertEquals($sum,
+            $orderCount);
+    }
+}