Forráskód Böngészése

控制台代码优化

ANG YU 5 éve
szülő
commit
2a3705c237
21 módosított fájl, 516 hozzáadás és 1427 törlés
  1. 0 46
      app/Console/Commands/SyncOrderCountingRecordTask.php
  2. 0 1
      app/Http/Controllers/ControlPanelController.php
  3. 0 2
      app/Providers/AppServiceProvider.php
  4. 49 31
      app/Services/NewOrderCountingRecordService.php
  5. 0 409
      app/Services/OrderCountingRecordService.php
  6. 7 18
      resources/views/control/panel.blade.php
  7. 0 41
      tests/Services/LaborReportsCountingRecordService/ByCacheTest.php
  8. 0 53
      tests/Services/LaborReportsCountingRecordService/GetTest.php
  9. 0 27
      tests/Services/LaborReportsCountingRecordService/UserGroupsCountTest.php
  10. 106 0
      tests/Services/NewOrderCountingRecordService/GetFromCacheTest.php
  11. 203 0
      tests/Services/NewOrderCountingRecordService/GetFromLowerUnitTest.php
  12. 116 0
      tests/Services/NewOrderCountingRecordService/GetFromMiddleTableTest.php
  13. 35 56
      tests/Services/NewOrderCountingRecordService/GetOrderCountingRecordsTest.php
  14. 0 337
      tests/Services/NewOrderCountingRecordService/NewOrderCountingRecordServiceTest.php
  15. 0 1
      tests/Services/NewOrderCountingRecordService/OrderCountingRecordsTest.php
  16. 0 4
      tests/Services/NewOrderCountingRecordService/TransfersToConditionsTest.php
  17. 0 59
      tests/Services/OrderCountingRecordService/DateTestTest.php
  18. 0 251
      tests/Services/OrderCountingRecordService/OrderCountingRecordServiceGetTest.php
  19. 0 30
      tests/Services/OrderCountingRecordService/OrderCountingRecordServiceLogisticsCountingRecordsTest.php
  20. 0 31
      tests/Services/OrderCountingRecordService/OrderCountingRecordServiceOrderCountingRecordsTest.php
  21. 0 30
      tests/Services/OrderCountingRecordService/OrderCountingRecordServiceWarehouseCountingRecordsTest.php

+ 0 - 46
app/Console/Commands/SyncOrderCountingRecordTask.php

@@ -1,46 +0,0 @@
-<?php
-
-namespace App\Console\Commands;
-
-use App\Services\OrderCountingRecordService;
-use Carbon\Carbon;
-use Illuminate\Console\Command;
-
-class SyncOrderCountingRecordTask extends Command
-{
-    /**
-     * The name and signature of the console command.
-     *
-     * @var string
-     */
-    protected $signature = 'syncOrderCountingRecordTask';
-
-    /**
-     * 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.
-     *
-     */
-    public function handle()
-    {
-        $orderCountingRecordService = new OrderCountingRecordService;
-        $orderCountingRecordService->orderCountingRecords(Carbon::now()->toDateString(), Carbon::now()->toDateString(), null, '日');
-        $orderCountingRecordService->orderCountingRecords(Carbon::now()->toDateString(), Carbon::now()->toDateString(), null, '周');
-        $orderCountingRecordService->orderCountingRecords(Carbon::now()->toDateString(), Carbon::now()->toDateString(), null, '月');
-    }
-}

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

@@ -5,7 +5,6 @@ namespace App\Http\Controllers;
 use App\Services\CheckActiveMenuService;
 use App\Services\LaborReportsCountingRecordService;
 use App\Services\NewOrderCountingRecordService;
-use App\Services\OrderCountingRecordService;
 use App\Services\RealtimePendingOrdersService;
 use App\Services\UserService;
 use App\User;

+ 0 - 2
app/Providers/AppServiceProvider.php

@@ -28,7 +28,6 @@ use App\Services\OracleDocAsnDetailService;
 use App\Services\OracleDOCOrderHeaderService;
 use App\Services\OrderCommodityService;
 use App\Services\OrderCommodityAssignService;
-use App\Services\OrderCountingRecordService;
 use App\Services\OrderIssuePerformanceService;
 use App\Services\AllInventoryService;
 use App\Services\InventoryDailyLogService;
@@ -156,7 +155,6 @@ class AppServiceProvider extends ServiceProvider
         app()->singleton('OrderIssueProcessLogService',OrderIssueProcessLogService::class);
         app()->singleton('OrderCommodityService',OrderCommodityService::class);
         app()->singleton('OrderCommodityAssignService',OrderCommodityAssignService::class);
-        app()->singleton('OrderCountingRecordService',OrderCountingRecordService::class);
         app()->singleton('OrderIssuePerformanceService',OrderIssuePerformanceService::class);
         app()->singleton('OrderIssueService',OrderIssueService::class);
         app()->singleton('OrderIssueWorkLoadService',OrderIssueWorkLoadService::class);

+ 49 - 31
app/Services/NewOrderCountingRecordService.php

@@ -33,7 +33,7 @@ class NewOrderCountingRecordService
     public function logisticsCountingRecords($start, $end, $ownerIds)
     {
         $key = 'logisticsCountingRecords_' . $start . '_' . $end . '_' . json_encode($ownerIds);
-        return Cache::remember($key, 600, function () use ($start, $end, $ownerIds) {
+        return Cache::remember($key, config('cache.expirations.oftenChange'), function () use ($start, $end, $ownerIds) {
             $dataList = collect();
             $resultOrders = $this->get($start, $end, '日', $ownerIds);
             $resultOrders->groupBy('logistic_id')->each(function ($item) use (&$dataList) {
@@ -61,7 +61,7 @@ class NewOrderCountingRecordService
     public function warehouseCountingRecords($start, $end, $ownerIds)
     {
         $key = 'warehouseCountingRecords_' . $start . '_' . $end . '_' . json_encode($ownerIds);
-        return Cache::remember($key, 600, function () use ($start, $end, $ownerIds) {
+        return Cache::remember($key, config('cache.expirations.oftenChange'), function () use ($start, $end, $ownerIds) {
             $dataList = collect();
             $resultOrders = $this->get($start, $end, '日', $ownerIds);
             $resultOrders->groupBy('warehouse_id')->each(function ($item) use (&$dataList) {
@@ -107,7 +107,7 @@ class NewOrderCountingRecordService
         return $this->getOrderCountingRecords($queryCondition);
     }
 
-    public function getFromCache($queryCondition)
+    public function getFromCache($queryCondition): array
     {
         $lackingCondition = [];
         $orderCountingRecords_FromCache = collect();
@@ -131,9 +131,8 @@ class NewOrderCountingRecordService
         return [$orderCountingRecords_FromCache, $lackingCondition];
     }
 
-    public function dataFromMiddleTable($queryCondition)
+    public function getFromMiddleTable($queryCondition): array
     {
-        $result = [];
         $orderSqlBuilder = OrderCountingRecord::query();
         $unit = $queryCondition['unit'];
         foreach ($queryCondition['data'] as $date => $owners) {
@@ -151,7 +150,6 @@ class NewOrderCountingRecordService
 
     public function getOrderCountingRecords($queryCondition)
     {
-
         list($orderCountingRecords_fromCache, $lackingCondition)
             = $this->getFromCache($queryCondition);
         if (empty($lackingCondition['data'])) {
@@ -159,34 +157,37 @@ class NewOrderCountingRecordService
         }
 
         list($orderCountingRecords_fromSelfTable, $lackingCondition)
-            = $this->dataFromMiddleTable($lackingCondition);
+            = $this->getFromMiddleTable($lackingCondition);
         if (empty($lackingCondition['data'])) {
             return $orderCountingRecords_fromCache->concat($orderCountingRecords_fromSelfTable);
         }
-        list($orderCountingRecords_combinedByLower, $lackingCondition)
-            = $this->getByLowerUnit($lackingCondition);
-
-        if (!empty($lackingCondition)) {
+        /**
+         * 1 如果查询粒度大于日 则进入getFromLowerUnit()
+         * 2 如果查询粒度为日 则进入dataFromOrder()
+         */
+        if ($lackingCondition['unit'] != '日') {//日为最低粒度,不用进入此方法
+            $orderCountingRecords_combinedByLower = $this->getFromLowerUnit($lackingCondition);
+        }else{
             $orderCountingRecords_FromOrder = $this->dataFromOrder($lackingCondition);
         }
         $result = collect();
-        if (isset($orderCountingRecords_FromOrder)  && !$orderCountingRecords_FromOrder->isEmpty()) {
-            $result =  $result->concat($orderCountingRecords_FromOrder);
+        if (isset($orderCountingRecords_FromOrder) && !$orderCountingRecords_FromOrder->isEmpty()) {
+            $result = $result->concat($orderCountingRecords_FromOrder);
         }
         if (!$orderCountingRecords_fromCache->isEmpty()) {
-            $result =  $result->concat($orderCountingRecords_fromCache);
+            $result = $result->concat($orderCountingRecords_fromCache);
         }
         if (!$orderCountingRecords_fromSelfTable->isEmpty()) {
             $result = $result->concat($orderCountingRecords_fromSelfTable);
         }
-        if (count($orderCountingRecords_combinedByLower) != 0) {
+        if (isset($orderCountingRecords_combinedByLower)) {
             $result = $result->concat($orderCountingRecords_combinedByLower);
         }
         return $result;
     }
 
 
-    public function getByLowerUnit($queryCondition)
+    public function getFromLowerUnit($queryCondition)
     {
         switch ($queryCondition['unit']) {
             case '年':
@@ -205,6 +206,8 @@ class NewOrderCountingRecordService
                 }
                 $orderCountingRecords_days = $this->getOrderCountingRecords($conditionClone);
                 $orderCountingRecords_combinedByLower = $this->turnGradingUpToLow($orderCountingRecords_days, $lowUnit, 'year');
+                $this->insertIntoMiddleTable($orderCountingRecords_combinedByLower, '年');
+                $this->insertIntoCache(collect($orderCountingRecords_combinedByLower),'年');
                 break;
             case '月':
                 $lowUnit = '日';
@@ -222,15 +225,18 @@ class NewOrderCountingRecordService
                 }
                 $orderCountingRecords_days = $this->getOrderCountingRecords($conditionClone);
                 $orderCountingRecords_combinedByLower = $this->turnGradingUpToLow($orderCountingRecords_days, $lowUnit, 'month');
+                $this->insertIntoMiddleTable($orderCountingRecords_combinedByLower, '月');
+                $this->insertIntoCache(collect($orderCountingRecords_combinedByLower),'月');
+
                 break;
             case '日':
                 return [[], $queryCondition];
             default:
         }
-        return [$orderCountingRecords_combinedByLower, []];
+        return $orderCountingRecords_combinedByLower;
     }
 
-    public function transfersToCondition($start, $end, $unit, $ownerIds)
+    public function transfersToCondition($start, $end, $unit, $ownerIds): array
     {
         $condition = [
             'data' => [],
@@ -270,24 +276,27 @@ class NewOrderCountingRecordService
         $orderSqlBuilder = Order::query()->selectRaw("owner_id,warehouse_id,shop_id,logistic_id,count(1) as amounts ,DATE_FORMAT(created_at,'%Y-%m-%d') as date_target");
         foreach ($queryCondition['data'] as $dateStr => $ownerIds) {
             $orderSqlBuilder->orWhere(function ($query) use ($ownerIds, $dateStr) {
-                $query->whereIn('owner_id', $ownerIds)->where('created_at', '>', $dateStr)->where('created_at', '<', Carbon::parse($dateStr)->addDay()->toDateString())->where('wms_status', '订单完成');
+                $query->whereIn('owner_id', $ownerIds)->where('created_at', '>=', $dateStr)->where('created_at', '<', Carbon::parse($dateStr)->addDay()->toDateString())->where('wms_status', '订单完成');
             });
         }
         $dataFromOrder = $orderSqlBuilder->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id', 'date_target'])->get();
         $dataFromMiddleTable = $this->ordersToOrderCountingRecords($dataFromOrder, $queryCondition['unit']);
         $this->setDefultData($dataFromMiddleTable, $queryCondition);
-        $this->insertIntoMiddleTable($dataFromMiddleTable);
+        $this->insertIntoMiddleTable($dataFromMiddleTable, $queryCondition['unit']);
         $this->insertIntoCache($dataFromMiddleTable, $queryCondition['unit']);
         return $dataFromMiddleTable;
     }
 
-    public function insertIntoMiddleTable($data)
+    public function insertIntoMiddleTable($data, $unit)
     {
-        $data->filter(function ($item) {
-            return $this->isNotCurrentDate($item->date_target);
-        })->chunk(1000)->each(function ($item) {
+
+        $filter = $data->filter(function ($item) use ($unit) {
+            return $this->isNotCurrentDate($item->date_target, $unit);
+        });
+        $filter->chunk(1000)->each(function ($item) {
             OrderCountingRecord::query()->insert($item->toArray());
         });
+
     }
 
     public function insertIntoCache($collect, $unit)
@@ -300,18 +309,25 @@ class NewOrderCountingRecordService
         });
 
         foreach ($map as $key => $orders) {
-            $ttl = 3600 * 24;
-            if (!$this->isNotCurrentDate($orders[0]['date_target'])) {
-                $ttl = 70;
+
+            $ttl = config('cache.expirations.forever');
+            if (!$this->isNotCurrentDate($orders[0]['date_target'], $unit)) {
+                $ttl = config('cache.expirations.oftenChange');
             }
             Cache::put($key, collect($orders), $ttl);
         }
     }
 
-    public function isNotCurrentDate($dateStr): bool
+    public function isNotCurrentDate($dateStr, $unit): bool
     {
-        return $dateStr != Carbon::now()->format('Y-m-d')
-            && $dateStr != Carbon::now()->year . '-' . Carbon::now()->month;
+        switch ($unit) {
+            case '月':
+                return !Carbon::parse($dateStr)->isCurrentMonth();
+            case '年':
+                return !Carbon::parse($dateStr)->isCurrentYear();
+            default:
+                return !Carbon::parse($dateStr)->isCurrentDay();
+        }
     }
 
 
@@ -387,13 +403,15 @@ class NewOrderCountingRecordService
             switch ($unit) {
                 case "日":
                     $item['date_target'] = Carbon::parse($item['date_target'])->firstOfMonth()->toDateString();
+                    $item['counting_unit'] = '月';
                     break;
                 case "月":
                     $item['date_target'] = Carbon::parse($item['date_target'])->firstOfYear()->toDateString();
+                    $item['counting_unit'] = '年';
                     break;
             }
             $item['amount'] = $amount;
-            $item['counting_unit'] = $unit;
+
             return $result->push($item);
         }
         return $result;

+ 0 - 409
app/Services/OrderCountingRecordService.php

@@ -1,409 +0,0 @@
-<?php
-
-
-namespace App\Services;
-
-
-use App\Logistic;
-use App\Order;
-use App\OrderCountingRecord;
-use App\Warehouse;
-use Carbon\Carbon;
-use DateTime;
-use Illuminate\Database\Eloquent\Builder;
-use Illuminate\Database\Eloquent\Collection;
-use Illuminate\Support\Facades\Auth;
-use Illuminate\Support\Facades\Cache;
-use App\User;
-use Illuminate\Support\Str;
-
-
-class OrderCountingRecordService
-{
-    /** @var $ownerService OwnerService */
-    private $ownerService = null;
-
-    function __construct()
-    {
-        $this->ownerService = app(OwnerService::class);
-    }
-
-    /**
-     * @param $start
-     * @param $end
-     * @param null $ownerIds ;
-     * @param string $unit
-     * @param $user
-     * @return mixed
-     */
-    public function get($start, $end, $ownerIds = null, $unit = '日', $user)
-    {
-        $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit, $user);
-        if (!($resultByCache['unExistingOrders'])) return $resultByCache['resultOrders'];
-        $resultByOrderCountingRecords = $this->getByDatabase($resultByCache['unExistingOrders'], $unit);
-        $this->createByDatabase($resultByOrderCountingRecords['unExistingOrders'], $unit);
-        $this->getByDatabase($resultByCache['unExistingOrders'], $unit);
-        $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit, $user);
-        return $resultByCache['resultOrders'];
-    }
-
-    public function orderCountingRecords($start, $end, $ownerIds = null, $unit = '日', $user = null)
-    {
-        $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . Auth::user()->id;
-        return Cache::remember($key, 60, function () use ($start, $end, $unit, $ownerIds, $user) {
-            $orders = $this->get($start, $end, null, $unit, null);
-            $dataList = collect();
-            $orders->groupBy('date_target')->each(function ($items) use (&$dataList, $unit) {
-                $counter = $items->reduce(function ($sum, $item) {
-                    return $sum + $item->amount;
-                }, 0);
-                $date_target = $items[0]->date_target;
-                if ($unit == '周') {
-                    $date_target = (new DateTime())->setISODate(Str::of($date_target)->explode('-')[0], Str::of($date_target)->explode('-')[1])->format('yy-m-d');
-                }
-                $dataList->push([
-                    'counter' => $counter,
-                    'date_target' => $date_target,
-                ]);
-            });
-            return $dataList->sortBy("date_target");
-        });
-    }
-
-    /**
-     * @param $start
-     * @param $end
-     * @param null $ownerIds
-     * @param null $user
-     * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection
-     */
-    public function logisticsCountingRecords($start, $end, $ownerIds = null, $user = null)
-    {
-        $key = 'logisticsCountingRecords_' . $start . '_' . $end . '_' . Auth::user()->id;
-        return Cache::remember($key, 600, function () use ($start, $end, $ownerIds, $user) {
-            $dataList = collect();
-            $resultOrders = $this->get($start, $end, $ownerIds, '日', $user);
-            $resultOrders->groupBy('logistic_id')->each(function ($item) use (&$dataList) {
-                $counter = $item->reduce(function ($sum, $item) {
-                    return $sum + $item->amount;
-                }, 0);
-                $dataList->push([
-                    'value' => $counter,
-                    'logistic_id' => $item[0]->logistic_id,
-                ]);
-            });
-            $map = [];
-            $logistics = Logistic::query()->whereIn('id', data_get($dataList, '*.logistic_id'))->get();
-            $logistics->each(function ($logistic) use (&$map) {
-                $map[$logistic->id] = $logistic;
-            });
-            return $dataList->map(function (&$item) use ($map) {
-                $logistic = $map[$item['logistic_id']] ?? '';
-                $item['name'] = $logistic->name ?? '';
-                return $item;
-            });
-        });
-    }
-
-    public function warehouseCountingRecords($start, $end, $ownerIds = null, $user = null)
-    {
-        $key = 'warehouseCountingRecords_' . $start . '_' . $end . '_' . Auth::user()->id;
-        return Cache::remember($key, 600, function () use ($start, $end, $ownerIds, $user) {
-            $dataList = collect();
-            $resultOrders = $this->get($start, $end, $ownerIds, '日', $user);
-            $resultOrders->groupBy('warehouse_id')->each(function ($item) use (&$dataList) {
-                $counter = $item->reduce(function ($sum, $item) {
-                    return $sum + $item->amount;
-                }, 0);
-                $dataList->push([
-                    'value' => $counter,
-                    'warehouse_id' => $item[0]->warehouse_id,
-                ]);
-            });
-            $map = [];
-            $logistics = Warehouse::query()->whereIn('id', data_get($dataList, '*.warehouse_id'))->get();
-            $logistics->each(function ($warehouse) use (&$map) {
-                $map[$warehouse->id] = $warehouse;
-            });
-            return $dataList->map(function (&$item) use ($map) {
-                $warehouse = $map[$item['warehouse_id']] ?? '';
-                $item['code'] = $warehouse->name ?? '';
-                switch ($item['code']) {
-                    case 'WH01':
-                        $item['name'] = '松江一仓';
-                        break;
-                    case 'WH02':
-                        $item['name'] = '松江二仓';
-                        break;
-                    case 'WH03':
-                        $item['name'] = '嘉定一仓';
-                        break;
-                    default:
-                        $item['name'] = '仓库为空';
-                        break;
-                }
-                return $item;
-            });
-        });
-    }
-
-    /**
-     * @param $start
-     * @param $end
-     * @param null $ownerIds
-     * @param string $unit
-     * @param $user
-     * @return array
-     */
-    public function getByCache($start, $end, $ownerIds = null, $unit = '日', $user)
-    {// Order[] results, Array [$date=>[$ownerIds]]
-        $countingOwnerIds = $this->getCountingOwnerIds($ownerIds, $user);
-        $resultOrders = collect();
-        $unExistingOrders = [];
-        $dateArray = $this->periodDateToArray($start, $end, $unit);
-        foreach ($dateArray as $dateStr) {
-            foreach ($countingOwnerIds as $ownerId) {
-                $key = "order_counting_records_{$dateStr}_{$ownerId}_{$unit}";
-                $orders = Cache::get($key);
-                if ($orders) {
-                    $orders->each(function ($item) use (&$resultOrders) {
-                        $resultOrders->push($item);
-                    });
-                    continue;
-                }
-                $unExistingOrders[$dateStr][] = $ownerId;
-            }
-        }
-        return ['resultOrders' => $resultOrders, 'unExistingOrders' => $unExistingOrders];
-    }
-
-    public function getByDatabase($targetOwnerIdsUnderDates, $unit = '日')
-    {// Order[] results, Array [$date=>[$ownerIds]]
-        $orderSqlBuilder = OrderCountingRecord::query();
-        foreach ($targetOwnerIdsUnderDates as $date => $owners) {
-            $orderSqlBuilder->orWhere(function ($query) use ($owners, $date, $unit) {
-                $query->whereIn('owner_id', $owners)->where('date_target', $date)->where('counting_unit', $unit);
-            });
-        }
-        $resultOrders = $orderSqlBuilder->get();
-        $resultOrders->groupBy(['date_target', 'owner_id'])->each(function ($item, $dateStr) use ($unit) {
-            $item->each(function ($item, $owner_id) use ($dateStr, $unit) {
-                $key = "order_counting_records_{$dateStr}_{$owner_id}_{$unit}";
-                $ttl = 3600 * 24;
-                if (!$this->isNotCurrentDate($dateStr)) {
-                    $ttl = 70;
-                }
-                Cache::put($key, $item, $ttl);
-            });
-        });
-        $map = [];
-        $resultOrders->each(function ($item) use (&$map) {
-            $key = 'owner_id=' . $item->owner_id . ' date_target' . $item->date_target;
-            $map[$key] = true;
-        });
-        foreach ($targetOwnerIdsUnderDates as $dateStr => $ownerIds) {
-            foreach ($ownerIds as $key => $ownerId) {
-                $key1 = 'owner_id=' . $ownerId . ' date_target' . $dateStr;
-                if (isset($map[$key1]) && $this->isNotCurrentDate($dateStr)) {
-                    //中间表查找到数据,并且时间不是当前日期
-                    unset($targetOwnerIdsUnderDates[$dateStr][$key]);
-                }
-            }
-        }
-        return ['resultOrders' => $resultOrders, 'unExistingOrders' => $targetOwnerIdsUnderDates];
-    }
-
-    public function createByDatabase($targetOwnerIdsUnderDates, $unit = '日')
-    {// Order[] results]
-        switch ($unit) {
-            case '日':
-                $resultOrders = $this->getCreateByDatabaseUnitDay($targetOwnerIdsUnderDates);
-                break;
-            case'周':
-                $resultOrders = $this->getCreateByDatabaseUnitWeek($targetOwnerIdsUnderDates);
-                break;
-            case'月':
-                $resultOrders = $this->getCreateByDatabaseUnitMonth($targetOwnerIdsUnderDates);
-                break;
-            default:
-                $resultOrders = collect();
-                break;
-        }
-        $result = collect();
-        $isHasCurrentDate = false;
-        $currentDate = null;
-        foreach ($targetOwnerIdsUnderDates as $dateStr => $ownerIds) {
-            if ($this->isNotCurrentDate($dateStr)) {
-                foreach ($ownerIds as $ownerId) {
-                    if ($resultOrders->where('date_target', $dateStr)->where('owner_id', $ownerId)->count() == 0) {
-                        $result->push([
-                            'owner_id' => $ownerId,
-                            'shop_id' => null,
-                            'warehouse_id' => null,
-                            'logistic_id' => null,
-                            'date_target' => $dateStr,
-                            'counting_unit' => $unit,
-                            'amount' => 0,
-                        ]);
-                    }
-                }
-            } else {
-                //查询范围包含当天
-                $isHasCurrentDate = true;
-                $currentDate = $dateStr;
-            }
-        }
-        $resultOrders->each(function ($order) use (&$result, $unit) {
-            $orderCountingRecord = new OrderCountingRecord([
-                'owner_id' => $order->owner_id,
-                'shop_id' => $order->shop_id,
-                'warehouse_id' => $order->warehouse_id,
-                'logistic_id' => $order->logistic_id,
-                'date_target' => $order->date_target,
-                'counting_unit' => $unit,
-                'amount' => $order->amounts,
-            ]);
-            $result->push($orderCountingRecord);
-        });
-        $this->insertDataToMiddle($isHasCurrentDate, $currentDate, $unit, $result);
-        return ['resultOrders' => $result];
-    }
-
-
-    public 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)->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;
-    }
-
-    /**
-     * @param $ownerIds
-     * @return array
-     */
-    public function getCountingOwnerIds($ownerIds = null, $user): array
-    {
-        if ($user == null) {
-            $user = auth()->user();
-        }
-        /** @var UserService $userService */
-        $userService = app('UserService');
-        $permittingOwnerIds = $userService->getPermittingOwnerIds($user);
-        if (!$ownerIds) {
-            return $permittingOwnerIds;
-        }
-        return Cache::remember(
-            'PermittingOwnerIds' . '_' . auth()->user()->id . '_' . implode('_', $ownerIds),
-            600, function () use ($ownerIds, $permittingOwnerIds) {
-            /** @var User $user */
-            return array_intersect($ownerIds, $permittingOwnerIds);
-        });
-    }
-
-    /**
-     * @param $targetOwnerIdsUnderDates
-     * @return Builder[]|Collection
-     */
-    private function getCreateByDatabaseUnitDay($targetOwnerIdsUnderDates)
-    {
-        $orderSqlBuilder = Order::query()->selectRaw("owner_id,warehouse_id,shop_id,logistic_id,count(1) as amounts ,DATE_FORMAT(created_at,'%Y-%m-%d') as date_target");
-        foreach ($targetOwnerIdsUnderDates as $dateStr => $ownerIds) {
-            $orderSqlBuilder->orWhere(function ($query) use ($ownerIds, $dateStr) {
-                $query->whereIn('owner_id', $ownerIds)->where('created_at', '>', $dateStr)->where('created_at', '<', Carbon::parse($dateStr)->addDay()->toDateString())->where('wms_status', '订单完成');
-            });
-        }
-        return $orderSqlBuilder->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id', 'date_target'])->get();
-    }
-
-    private function getCreateByDatabaseUnitWeek($targetOwnerIdsUnderDates)
-    {
-        $orderSqlBuilder = Order::query()->selectRaw("owner_id,warehouse_id,shop_id,logistic_id,count(1) as amounts ,DATE_FORMAT(created_at,'%x-%v') as date_target");
-        foreach ($targetOwnerIdsUnderDates as $dateStr => $ownerIds) {
-            $orderSqlBuilder->orWhere(function ($query) use ($ownerIds, $dateStr) {
-                $dateStr = (new DateTime())->setISODate(Str::of($dateStr)->explode('-')[0], Str::of($dateStr)->explode('-')[1])->format('yy-m-d');
-                $query->whereIn('owner_id', $ownerIds)->where('created_at', '>', $dateStr)->where('created_at', '<=', Carbon::parse($dateStr)->addWeek()->toDateString())->where('wms_status', '订单完成');
-            });
-        }
-        return $orderSqlBuilder->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id', 'date_target'])->get();
-    }
-
-    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) {
-            $orderSqlBuilder->orWhere(function ($query) use ($ownerIds, $dateStr) {
-                $year = Str::of($dateStr)->explode('-')[0];
-                $month = Str::of($dateStr)->explode('-')[1];
-                $dateStr = $year . '-' . $month . '-01';
-                $query->whereIn('owner_id', $ownerIds)->where('created_at', '>', $dateStr)->where('created_at', '<', Carbon::parse($dateStr)->addMonth()->toDateString())->where('wms_status', '订单完成');
-            });
-        }
-        return $orderSqlBuilder->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id', 'date_target'])->get();
-    }
-
-    /**
-     * @param $dateStr
-     * @return bool
-     */
-    public function isNotCurrentDate($dateStr): bool
-    {
-        return $dateStr != Carbon::now()->format('Y-m-d')
-            && $dateStr != Carbon::now()->year . '-' . Carbon::now()->week
-            && $dateStr != Carbon::now()->year . '-' . Carbon::now()->month;
-    }
-
-    protected function insertDataToMiddle($isHasCurrentDate, $currentDate, $unit, $result)
-    {
-        $orderCountingRecordsClock = cache()->get('orderCountingRecordsClock');
-        switch ($orderCountingRecordsClock) {
-            case true:
-            case null:
-                $this->setClockAndDeleteCurrentDateAndInsert($isHasCurrentDate, $currentDate, $unit, $result);
-                break;
-            default:
-                break;
-        }
-    }
-
-
-    protected function deleteMiddleWhereCurrentDate($isHasCurrentDate, $currentDate, $unit)
-    {
-        if ($isHasCurrentDate) {
-            //删除OrderCountingRecord表中与当前日期相关的数据,之后的批量插入重新插入这部分数据
-            OrderCountingRecord::query()->where('date_target', $currentDate)->where('counting_unit', $unit)->delete();
-        }
-    }
-
-    protected function setClockAndDeleteCurrentDateAndInsert($isHasCurrentDate, $currentDate, $unit, $result)
-    {
-        cache()->put('orderCountingRecordsClock', false, 10);
-        $this->deleteMiddleWhereCurrentDate($isHasCurrentDate, $currentDate, $unit);
-        $result->chunk(1000)->each(function ($item) {
-            OrderCountingRecord::query()->insert($item->toArray());
-        });
-        cache()->put('orderCountingRecordsClock', true, 10);
-    }
-}

+ 7 - 18
resources/views/control/panel.blade.php

@@ -26,11 +26,6 @@
                 <div class="col-sm col-lg-2 col-xl-2 col-md-2">
                     <div class="card">
                         <div class="card-header text-dark h5">
-                            {{--                            <p><select name="" id="">--}}
-                            {{--                                    <option value="i+1980"  v-for="i in (new Array(30).keys())" :selected="year(widgets.orderAmount.startAt)==i+1980">--}}
-                            {{--                                        @{{i+1980}}--}}
-                            {{--                                    </option>--}}
-                            {{--                                </select></p>--}}
                             <p>实时待处理订(总):@{{ totalOrders.total }}</p>
                         </div>
                         <div class="card-body">
@@ -209,18 +204,6 @@
                         <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>--}}
-
                                 <div class="block">
                                     <span v-show="laborReportsCountingRecordsDayShow" class="demonstration">起始日期</span>
                                     <el-date-picker
@@ -415,7 +398,7 @@
                 orderCountingRecordsEnd: moment(new Date()).format('yyyy-MM-DD'),
                 laborReportsCountingRecordsDayShow: true,
                 laborReportsCountingRecordsMonthShow: false,
-                laborReportsCountingRecordsYearShow:false ,
+                laborReportsCountingRecordsYearShow: false,
                 laborReportsCountingRecordsStart: moment().subtract('1', 'month').format('yyyy-MM-DD'),
                 laborReportsCountingRecordsEnd: moment(new Date()).format('yyyy-MM-DD'),
             },
@@ -598,16 +581,19 @@
                             this.orderCountingRecordsDayShow = true;
                             this.orderCountingRecordsMonthShow = false;
                             this.orderCountingRecordsYearShow = false;
+                            this.orderCountingRecordsStart = moment().subtract('1', 'month').format('yyyy-MM-DD');
                             break;
                         case '月':
                             this.orderCountingRecordsDayShow = false;
                             this.orderCountingRecordsMonthShow = true;
                             this.orderCountingRecordsYearShow = false;
+                            this.orderCountingRecordsStart = moment().subtract('12', 'month').format('yyyy-MM-DD');
                             break;
                         case '年':
                             this.orderCountingRecordsDayShow = false;
                             this.orderCountingRecordsMonthShow = false;
                             this.orderCountingRecordsYearShow = true;
+                            this.orderCountingRecordsStart = moment().subtract('12', 'month').format('yyyy-MM-DD');
                             break;
                     }
                     this.orderCountingRecordsUnit = orderCountingRecordsUnit;
@@ -670,16 +656,19 @@
                             this.laborReportsCountingRecordsDayShow = true;
                             this.laborReportsCountingRecordsMonthShow = false;
                             this.laborReportsCountingRecordsYearShow = false;
+                            this.laborReportsCountingRecordsStart = moment().subtract('1', 'month').format('yyyy-MM-DD');
                             break;
                         case '月':
                             this.laborReportsCountingRecordsDayShow = false;
                             this.laborReportsCountingRecordsMonthShow = true;
                             this.laborReportsCountingRecordsYearShow = false;
+                            this.laborReportsCountingRecordsStart = moment().subtract('12', 'month').format('yyyy-MM-DD');
                             break;
                         case '年':
                             this.laborReportsCountingRecordsDayShow = false;
                             this.laborReportsCountingRecordsMonthShow = false;
                             this.laborReportsCountingRecordsYearShow = true;
+                            this.laborReportsCountingRecordsStart = moment().subtract('12', 'month').format('yyyy-MM-DD');
                             break;
                     }
 

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

@@ -1,41 +0,0 @@
-<?php
-
-namespace LaborReportsCountingRecordService;
-
-use App\Services\LaborReportsCountingRecordService;
-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()
-    {
-        $start = '2025-10-01';
-        $end = '2025-10-05';
-        $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($start,$end, $unit);
-
-        self::assertTrue($result['dataList']->isnotEmpty());
-        self::assertTrue($result['dateList'] == []);
-    }
-}

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

@@ -1,53 +0,0 @@
-<?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()
-    {
-        $this->assertTrue(true);
-    }
-
-//    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());
-//    }
-}

+ 0 - 27
tests/Services/LaborReportsCountingRecordService/UserGroupsCountTest.php

@@ -1,27 +0,0 @@
-<?php
-
-namespace LaborReportsCountingRecordService;
-
-use App\Services\CacheService;
-use App\Services\LaborReportsCountingRecordService;
-use App\Services\OrderCountingRecordService;
-use Illuminate\Support\Facades\Cache;
-use Tests\TestCase;
-
-class UserGroupsCountTest extends TestCase
-{
-    /** @var LaborReportsCountingRecordService $laborReportsCountingRecordService */
-    public $laborReportsCountingRecordService;
-
-    public function setUp(): void
-    {
-        parent::setUp();
-        $this->laborReportsCountingRecordService = new LaborReportsCountingRecordService();
-    }
-
-    public function testUserGroupsCount()
-    {
-        $result =  $this->laborReportsCountingRecordService->userGroupsCount('2020-10-17','2020-11-16');
-        self::assertTrue(true);
-    }
-}

+ 106 - 0
tests/Services/NewOrderCountingRecordService/GetFromCacheTest.php

@@ -0,0 +1,106 @@
+<?php
+
+namespace NewOrderCountingRecordService;
+
+
+use App\Order;
+use App\OrderCountingRecord;
+use App\Owner;
+use App\Services\NewOrderCountingRecordService;
+use App\User;
+use Carbon\Carbon;
+use Tests\TestCase;
+
+class GetFromCacheTest extends TestCase
+{
+    protected $newOrderCountingRecordService;
+    protected $queryConditionDay;
+    protected $queryConditionWeek;
+    protected $queryConditionMonth;
+    protected $queryConditionYear;
+    protected $ownerIds;
+    protected $cache_key = 'order_counting_records_';
+    protected $step_length = 1;
+    protected $orderCountingRecordIds = [];
+    protected $units = ['日', '月', '年'];
+    protected $orderIds;
+
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        cache()->flush();
+        $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
+        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
+        $owners = factory(Owner::class)->times(2)->create();
+        $this->ownerIds = array_column($owners->toArray(), 'id');
+        $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
+        $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
+        $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
+    }
+
+    protected function tearDown(): void
+    {
+        Owner::destroy($this->ownerIds);
+        OrderCountingRecord::destroy($this->orderCountingRecordIds);
+        Order::destroy($this->orderIds);
+        OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * 缓存没有任何数据
+     * @test
+     */
+    public function cache_data_empty()
+    {
+        $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
+        $this->assertEquals([
+            0 => collect(),
+            ['unit' => '日',
+                'data' => [
+                    Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
+                    Carbon::now()->subDays(0)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
+                ]]], $result);
+    }
+
+
+    /**
+     * 缓存命中全部数据
+     * @test
+     */
+    public function cache_data_all()
+    {
+        foreach ($this->ownerIds as $owner_id) {
+            for ( $i = 1; $i >= 0; $i--) {
+                $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日';
+                cache()->put($key, collect(['aaa']));
+            }
+        }
+        $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
+        $this->assertEquals([
+            0 => collect(['aaa','aaa','aaa','aaa']),
+            ['unit' => '日']], $result);
+    }
+
+    /**
+     * 缓存命中部分数据
+     * @test
+     */
+    public function cache_data_some()
+    {
+        foreach ($this->ownerIds as $owner_id) {
+            for ( $i = 0; $i >= 0; $i--) {
+                $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日';
+                cache()->put($key, collect(['aaa']));//缓存中放入当天的数据
+            }
+        }
+        $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
+        $this->assertEquals([
+            0 => collect(['aaa','aaa']),
+            ['unit' => '日',
+                'data' => [
+                    Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回
+                ]]], $result);
+    }
+}

+ 203 - 0
tests/Services/NewOrderCountingRecordService/GetFromLowerUnitTest.php

@@ -0,0 +1,203 @@
+<?php
+
+namespace NewOrderCountingRecordService;
+
+
+use App\Order;
+use App\OrderCountingRecord;
+use App\Owner;
+use App\Services\NewOrderCountingRecordService;
+use App\User;
+use Carbon\Carbon;
+use Tests\TestCase;
+
+class GetFromLowerUnitTest extends TestCase
+{
+    protected $newOrderCountingRecordService;
+    protected $queryConditionDay;
+    protected $queryConditionWeek;
+    protected $queryConditionMonth;
+    protected $queryConditionYear;
+    protected $ownerIds;
+    protected $cache_key = 'order_counting_records_';
+    protected $step_length = 1;
+    protected $orderCountingRecordIds = [];
+    protected $units = ['日', '月', '年'];
+    protected $orderIds;
+
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        cache()->flush();
+        $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
+        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
+        $owners = factory(Owner::class)->times(2)->create();
+        $this->ownerIds = array_column($owners->toArray(), 'id');
+        $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
+        $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
+        $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
+    }
+
+    protected function tearDown(): void
+    {
+        Owner::destroy($this->ownerIds);
+        OrderCountingRecord::destroy($this->orderCountingRecordIds);
+        Order::destroy($this->orderIds);
+        OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * unit为月,中间表日为空,查询orders
+     * @test
+     */
+    public function unit_month_from_order()
+    {
+        $orders = collect();
+        foreach ($this->ownerIds as $ownerId) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orders->push(factory(Order::class)->create([
+                    'created_at' => Carbon::now()->subMonths($i)->toDateString(),
+                    'owner_id' => $ownerId,
+                    'wms_status' => '订单完成',
+                ]));
+            }
+        }
+        $this->orderIds = array_column($orders->toArray(), 'id');
+
+        $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
+        $this->assertEquals([1, 1, 1, 1],$result->pluck('amount')->toArray());
+    }
+
+    /**
+     * unit为年,中间表日为空,查询orders
+     * @test
+     */
+    public function unit_year_from_order()
+    {
+        $orders = collect();
+        foreach ($this->ownerIds as $ownerId) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orders->push(factory(Order::class)->create([
+                    'created_at' => Carbon::now()->subYears($i)->toDateString(),
+                    'owner_id' => $ownerId,
+                    'wms_status' => '订单完成',
+                ]));
+            }
+        }
+        $this->orderIds = array_column($orders->toArray(), 'id');
+
+        $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
+        $this->assertEquals([1, 1, 1, 1],$result->pluck('amount')->toArray());
+    }
+
+    /**
+     * 插入中间表测试 月
+     * @test
+     */
+    public function unit_month_from_order_insert()
+    {
+        $orders = collect();
+        foreach ($this->ownerIds as $ownerId) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orders->push(factory(Order::class)->create([
+                    'created_at' => Carbon::now()->subMonths($i)->toDateString(),
+                    'owner_id' => $ownerId,
+                    'wms_status' => '订单完成',
+                ]));
+            }
+        }
+        $this->orderIds = array_column($orders->toArray(), 'id');
+
+        $this->assertDatabaseMissing('order_counting_records', [
+            'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
+            'counting_unit' => '月',
+            'owner_id' => $this->ownerIds[0],
+        ]);
+        $this->assertDatabaseMissing('order_counting_records', [
+            'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
+            'counting_unit' => '月',
+            'owner_id' => $this->ownerIds[1],
+        ]);
+
+        $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
+
+        $this->assertDatabaseHas('order_counting_records', [
+            'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
+            'counting_unit' => '月',
+            'owner_id' => $this->ownerIds[0],
+        ]);
+
+        $this->assertDatabaseHas('order_counting_records', [
+            'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
+            'counting_unit' => '月',
+            'owner_id' => $this->ownerIds[1],
+        ]);
+    }
+
+    /**
+     * 插入中间表测试 年
+     * @test
+     */
+    public function unit_year_from_order_insert()
+    {
+        $orders = collect();
+        foreach ($this->ownerIds as $ownerId) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orders->push(factory(Order::class)->create([
+                    'created_at' => Carbon::now()->subYears($i)->toDateString(),
+                    'owner_id' => $ownerId,
+                    'wms_status' => '订单完成',
+                ]));
+            }
+        }
+        $this->orderIds = array_column($orders->toArray(), 'id');
+
+        $this->assertDatabaseMissing('order_counting_records', [
+            'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
+            'counting_unit' => '年',
+            'owner_id' => $this->ownerIds[0],
+        ]);
+        $this->assertDatabaseMissing('order_counting_records', [
+            'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
+            'counting_unit' => '年',
+            'owner_id' => $this->ownerIds[1],
+        ]);
+
+        $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
+        $this->assertDatabaseHas('order_counting_records', [
+            'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
+            'counting_unit' => '年',
+            'owner_id' => $this->ownerIds[0],
+        ]);
+
+        $this->assertDatabaseHas('order_counting_records', [
+            'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
+            'counting_unit' => '年',
+            'owner_id' => $this->ownerIds[1],
+        ]);
+    }
+
+    /**
+     * @test
+     */
+    public function currentDateTest()
+    {
+        $result =  $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'日');
+        $this->assertFalse($result);
+        $result =  $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'月');
+        $this->assertFalse($result);
+        $result =  $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'年');
+        $this->assertFalse($result);
+
+        $result =  $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->toDateString(),'日');
+        $this->assertTrue($result);
+        $result =  $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth() ->toDateString(),'月');
+        $this->assertTrue($result);
+        $result =  $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subYear() ->toDateString(),'年');
+        $this->assertTrue($result);
+        $result =  $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subYears()->toDateString(),'月');
+        $this->assertTrue($result);
+    }
+}

+ 116 - 0
tests/Services/NewOrderCountingRecordService/GetFromMiddleTableTest.php

@@ -0,0 +1,116 @@
+<?php
+
+namespace NewOrderCountingRecordService;
+
+
+use App\Order;
+use App\OrderCountingRecord;
+use App\Owner;
+use App\Services\NewOrderCountingRecordService;
+use App\User;
+use Carbon\Carbon;
+use Illuminate\Database\Eloquent\Collection;
+use Tests\TestCase;
+
+class GetFromMiddleTableTest extends TestCase
+{
+    protected $newOrderCountingRecordService;
+    protected $queryConditionDay;
+    protected $queryConditionWeek;
+    protected $queryConditionMonth;
+    protected $queryConditionYear;
+    protected $ownerIds;
+    protected $cache_key = 'order_counting_records_';
+    protected $step_length = 1;
+    protected $orderCountingRecordIds = [];
+    protected $units = ['日', '月', '年'];
+    protected $orderIds;
+
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        cache()->flush();
+        $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
+        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
+        $owners = factory(Owner::class)->times(2)->create();
+        $this->ownerIds = array_column($owners->toArray(), 'id');
+        $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
+        $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
+        $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
+    }
+
+    protected function tearDown(): void
+    {
+        Owner::destroy($this->ownerIds);
+        OrderCountingRecord::destroy($this->orderCountingRecordIds);
+        Order::destroy($this->orderIds);
+        OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
+        parent::tearDown();
+    }
+
+    /**
+     * 中间表没有任何数据
+     * @test
+     */
+    public function data_empty()
+    {
+        $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
+        $this->assertEquals([
+            0 => new Collection(),
+            ['unit' => '日',
+                'data' => [
+                    Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
+                    Carbon::now()->subDays(0)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
+                ]]], $result);
+    }
+
+
+    /**
+     * 中间表命中全部数据
+     * @test
+     */
+    public function data_all()
+    {
+        $orderCountingRecords = collect();
+        foreach ($this->ownerIds as $owner_id) {
+            for ($i = 1; $i >= 0; $i--) {
+                $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([
+                    'date_target' => Carbon::now()->subDays($i)->toDateString(),
+                    'owner_id' => $owner_id,
+                ]));
+            }
+        }
+        $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id');
+        $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
+        $orderCountingRecords = OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->orderByDesc('owner_id')->get();
+
+        $this->assertEquals([
+            0 => $orderCountingRecords,
+            ['unit' => '日','data'=>[]]], $result);
+    }
+
+    /**
+     * 中间表命中部分数据
+     * @test
+     */
+    public function data_some()
+    {
+        $orderCountingRecords = collect();
+        foreach ($this->ownerIds as $owner_id) {
+            for ($i = 0; $i >= 0; $i--) {
+                $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([
+                    'date_target' => Carbon::now()->subDays($i)->toDateString(),
+                    'owner_id' => $owner_id,
+                ]));
+            }
+        }
+        $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id');
+
+        $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
+        $this->assertEquals(
+            ['unit' => '日','data' => [
+                Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回
+            ]], $result[1]);
+    }
+}

+ 35 - 56
tests/Services/NewOrderCountingRecordService/GetOrderCountingRecordsTest.php

@@ -16,8 +16,6 @@ use Tightenco\Collect\Support\Arr;
 
 class GetOrderCountingRecordsTest extends TestCase
 {
-    use RefreshDatabase;
-
     protected $newOrderCountingRecordService;
     protected $queryConditionDay;
     protected $queryConditionWeek;
@@ -40,7 +38,6 @@ class GetOrderCountingRecordsTest extends TestCase
         $owners = factory(Owner::class)->times(2)->create();
         $this->ownerIds = array_column($owners->toArray(), 'id');
         $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
-        $this->queryConditionWeek = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subWeeks($this->step_length)->toDateString(), Carbon::now()->toDateString(), '周', $this->ownerIds);
         $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
         $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
     }
@@ -49,62 +46,12 @@ class GetOrderCountingRecordsTest extends TestCase
     {
         cache()->flush();
         Owner::destroy($this->ownerIds);
-        orderCountingRecord::destroy($this->orderCountingRecordIds);
+        OrderCountingRecord::destroy($this->orderCountingRecordIds);
+        Order::destroy($this->orderIds);
+        OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
         parent::tearDown();
     }
 
-
-//    /**
-//     * @test
-//     */
-//    public function get_all_from_cache()
-//    {
-//        for ($i = 0; $i <= $this->step_length; $i++) {
-//            foreach ($this->ownerIds as $ownerId) {
-//                foreach ($this->units as $unit) {
-//                    switch ($unit) {
-//                        case "日":
-//                            $dateStr = Carbon::now()->subDays($i)->toDateString();
-//                            break;
-//                        case "月":
-//                            $dateStr = Carbon::now()->subMonths($i)->toDateString();
-//                            break;
-//                        default:
-//                            break;
-//                    }
-//                    if ($dateStr) {
-//                        $orderCountingRecord = factory(OrderCountingRecord::class)->create([
-//                            'date_target' => $dateStr,
-//                            'owner_id' => $ownerId,
-//                            'counting_unit' => $unit,
-//                        ]);
-//                        $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit;
-//                        cache()->put($key, collect()->push($orderCountingRecord));
-//                        if (empty($this->orderCountingRecordIds[$unit])) $this->orderCountingRecordIds[$unit] = [];
-//                        $this->orderCountingRecordIds[$unit][] = $orderCountingRecord->id;
-//                        $dateStr = null;
-//                    }
-//                }
-//            }
-//        }
-//        $resultDay = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
-//        $resultWeek = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionWeek);
-////        $resultMonth = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
-//        foreach ($this->units as $unit) {
-//            if (!empty($this->orderCountingRecordIds[$unit])) {
-//                switch ($unit) {
-//                    case "日":
-//                        $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultDay->sortBy('id')->toArray(), 'id'));
-//                        break;
-//                    case "月":
-////                        $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultMonth->sortBy('id')->toArray(), 'id'));
-//                        break;
-//                }
-//            }
-//        }
-//
-//    }
-
     /**
      * @test
      */
@@ -186,4 +133,36 @@ class GetOrderCountingRecordsTest extends TestCase
         $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionYear);
         $this->assertEquals([1, 1, 1, 1], array_column($result->toArray(), 'amount'));
     }
+
+    /**
+     * 按照月份查询插入中间表的测试
+     * @test
+     */
+    public function insert_monthly_order_counting_records()
+    {
+        //前一个月的订单2个
+        $carbon = Carbon::now()->subMonths(1);
+        $orders1 = factory(Order::class)->times(2)->create(['created_at' => $carbon, 'owner_id' => $this->ownerIds[0], 'wms_status' => '订单完成']);
+        //本月本日的订单2个
+        $orders2 = factory(Order::class)->times(2)->create(['created_at' => Carbon::now(), 'owner_id' => $this->ownerIds[0], 'wms_status' => '订单完成']);
+        $orders = $orders1->merge($orders2);
+
+        $this->orderIds = array_column($orders->toArray(), 'id');
+        //判断中间表中没有上一个月,'counting_unit' => '月'   'owner_id' => $this->ownerIds[0], 的数据
+        $this->assertDatabaseMissing('order_counting_records', [
+            'date_target' => $carbon->startOfMonth()->toDateString(),
+            'owner_id' => $this->ownerIds[0],
+            'counting_unit' => '月',
+            'amount' => '2',
+        ]);
+        $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
+
+        $this->assertDatabaseHas('order_counting_records', [
+            'date_target' => $carbon->startOfMonth()->toDateString(),
+            'owner_id' => $this->ownerIds[0],
+            'counting_unit' => '月',
+            'amount' => '2',
+        ]);
+        dump($result->toArray());
+    }
 }

+ 0 - 337
tests/Services/NewOrderCountingRecordService/NewOrderCountingRecordServiceTest.php

@@ -1,337 +0,0 @@
-<?php
-
-namespace NewOrderCountingRecordService;
-
-use App\Order;
-use App\OrderCountingRecord;
-use App\Owner;
-use App\Services\NewOrderCountingRecordService;
-use App\User;
-use Carbon\Carbon;
-use Illuminate\Foundation\Testing\RefreshDatabase;
-use Illuminate\Support\Arr;
-use Tests\TestCase;
-
-class NewOrderCountingRecordServiceTest extends TestCase
-{
-    use RefreshDatabase;
-
-    protected $newOrderCountingRecordService;
-
-    protected function setUp(): void
-    {
-        parent::setUp(); // TODO: Change the autogenerated stub
-        cache()->flush();
-        $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
-        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
-
-    }
-
-    protected function tearDown(): void
-    {
-        cache()->flush();
-        OrderCountingRecord::truncate();
-        Order::truncate();
-        Owner::truncate();
-        parent::tearDown(); // TODO: Change the autogenerated stub
-    }
-
-    /**
-     * @test
-     */
-    public function transfersToConditions()
-    {
-        factory(Owner::class)->times(2)->create();
-        $start = Carbon::now()->subDays(2)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-        $this->assertEquals([
-            Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2],
-            Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2],
-            Carbon::now()->toDateString() => [0 => 1, 1 => 2],
-        ], $dateArray);
-    }
-
-    /**
-     * @test
-     */
-    public function dateUtils_week()
-    {
-        factory(Owner::class)->times(2)->create();
-        $start = Carbon::now()->subWeeks(2)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '周';
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-        dd($dateArray);
-//        $this->assertEquals([
-//            Carbon::now()->subWeeks(2)->toDateString() => [0 => 1, 1 => 2],
-//            Carbon::now()->subWeeks(1)->toDateString() => [0 => 1, 1 => 2],
-//            Carbon::now()->toDateString() => [0 => 1, 1 => 2],
-//        ], $dateArray);
-    }
-
-
-    /**
-     * @test
-     */
-    public function orderCountingRecords_remember_day()
-    {
-        $start = Carbon::now()->subDays(2)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . auth()->id();
-
-        cache()->put($key, 'This is a test value');
-
-        $result = $this->newOrderCountingRecordService->get($start, $end, $unit);
-        $this->assertEquals('This is a test value', $result);
-    }
-
-
-    /**
-     * @test
-     */
-    public function dataFromCache_all_day()
-    {
-        $start = Carbon::now()->subDays(2)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        factory(Owner::class)->times(2)->create();
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-        $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-
-        foreach ($queryCondition as $dateStr => $ownerIds) {
-            foreach ($ownerIds as $ownerId) {
-                $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit . '_' . auth()->id();
-                cache()->put($key, collect()->push(factory(Owner::class)->create()));
-            }
-        }
-        $result = $this->newOrderCountingRecordService->getFromCache($queryCondition, $unit);
-        $this->assertCount(6, $result['dataFromCache']);
-    }
-
-    /**
-     * @test
-     */
-    public function dataFromCache_empty_day()
-    {
-        $start = Carbon::now()->subDays(2)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        factory(Owner::class)->times(2)->create();
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-        $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-
-
-        $result = $this->newOrderCountingRecordService->getFromCache($queryCondition, $unit);
-        $this->assertEquals([
-            Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2],
-            Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2],
-            Carbon::now()->toDateString() => [0 => 1, 1 => 2],
-        ], $result['unQueryCondition']);
-    }
-
-    /**
-     * @test
-     */
-    public function dataFromMiddleTable_all_day()
-    {
-        $start = Carbon::now()->subDays(1)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        $owner1 = factory(Owner::class)->create();
-        $owner2 = factory(Owner::class)->create();
-        factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
-        factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner2->id]);
-        factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner1->id]);
-        factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner2->id]);
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-
-        $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-        $result = $this->newOrderCountingRecordService->dataFromMiddleTable($queryCondition, $unit);
-
-        $this->assertCount(8, $result['dataFromMiddleTable']);
-        $this->assertCount(0, $result['unQueryCondition'][Carbon::now()->toDateString()]);
-        $this->assertCount(0, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]);
-    }
-
-    /**
-     * @test
-     */
-    public function dataFromMiddleTable_empty_day()
-    {
-        $start = Carbon::now()->subDays(1)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        factory(Owner::class)->create();
-        factory(Owner::class)->create();
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-
-        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-        $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
-
-        $this->assertCount(0, $result['dataFromMiddleTable']);
-        $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->toDateString()]);
-        $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]);
-    }
-
-    /**
-     * @test
-     */
-    public function dataFromMiddleTable_common_day()
-    {
-        $start = Carbon::now()->subDays(1)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        $owner1 = factory(Owner::class)->create();
-        $owner2 = factory(Owner::class)->create();
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-
-        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-        factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
-
-        $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
-
-        $this->assertCount(2, $result['dataFromMiddleTable']);
-        $this->assertCount(1, $result['unQueryCondition'][Carbon::now()->toDateString()]);
-        $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]);
-    }
-
-    /**
-     * @test
-     */
-    public function insertIntoCache_day()
-    {
-        $start = Carbon::now()->subDays(1)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        $owner1 = factory(Owner::class)->create();
-        $owner2 = factory(Owner::class)->create();
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-
-        $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-        $orderCountingRecord = factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
-
-        $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
-
-        $this->newOrderCountingRecordService->insertIntoCache($result['dataFromMiddleTable'], $unit);
-        $key = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner1->id . '_' . $unit;
-        $this->assertNotNull(cache()->get($key));
-        $this->assertNotNull($orderCountingRecord->toArray());
-        $this->assertEquals($orderCountingRecord->toArray(), cache()->get($key)->toArray());
-    }
-
-    /**
-     * @test
-     */
-    public function dataFromOrder_day()
-    {
-        $start = Carbon::now()->subDays(1)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        $owner1 = factory(Owner::class)->create();
-        $owner2 = factory(Owner::class)->create();
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-
-        $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-
-        factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
-        factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
-
-        factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
-        factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
-
-        $result = $this->newOrderCountingRecordService->dataFromOrder($unQueryCondition, $unit);
-        $this->assertEquals(1, $result->where('date_target', Carbon::now()->toDateString())->where('owner_id', $owner1->id)->first()->amount);
-        $this->assertEquals(2, $result->where('date_target', Carbon::now()->toDateString())->where('owner_id', $owner2->id)->first()->amount);
-        $this->assertEquals(3, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->where('owner_id', $owner1->id)->first()->amount);
-        $this->assertEquals(4, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->where('owner_id', $owner2->id)->first()->amount);
-    }
-
-    /**
-     * @test
-     */
-    public function dataFromOrder_insertMiddleTable_day()
-    {
-        $start = Carbon::now()->subDays(1)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        $owner1 = factory(Owner::class)->create();
-        $owner2 = factory(Owner::class)->create();
-        $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
-        $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
-
-        factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
-        factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
-
-        factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
-        factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
-
-        $this->newOrderCountingRecordService->dataFromOrder($unQueryCondition, $unit);
-        //当天的数据不入中间表
-        $this->assertCount(0, OrderCountingRecord::query()->where('date_target', Carbon::now()->toDateString())->get());
-        $this->assertCount(2, OrderCountingRecord::query()->where('date_target', Carbon::now()->subDays(1)->toDateString())->get());
-
-    }
-
-    /**
-     * @test
-     */
-    public function dataFromOrder_insertCache_day()
-    {
-        $start = Carbon::now()->subDays(1)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        $owner1 = factory(Owner::class)->create();
-        $owner2 = factory(Owner::class)->create();
-
-        $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit,$this->newOrderCountingRecordService->getCountingOwnerIds());
-
-        factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
-        factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
-
-        factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
-        factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
-
-        $this->newOrderCountingRecordService->dataFromOrder($unQueryCondition, $unit);
-        $key1 = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner1->id . '_' . $unit;
-        $key2 = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner2->id . '_' . $unit;
-        $key3 = 'order_counting_records_' . Carbon::now()->subDays(1)->toDateString() . '_' . $owner1->id . '_' . $unit;
-        $key4 = 'order_counting_records_' . Carbon::now()->subDays(1)->toDateString() . '_' . $owner2->id . '_' . $unit;
-        $this->assertCount(1, cache()->get($key1));
-        $this->assertCount(1, cache()->get($key2));
-        $this->assertCount(1, cache()->get($key3));
-        $this->assertCount(1, cache()->get($key4));
-    }
-
-    /**
-     * @test
-     */
-    public function orderCountingRecordsDay_day()
-    {
-        $start = Carbon::now()->subDays(1)->toDateString();
-        $end = Carbon::now()->toDateString();
-        $unit = '日';
-        $owner1 = factory(Owner::class)->create();
-        $owner2 = factory(Owner::class)->create();
-
-
-        factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
-        factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
-
-        factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
-        factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
-        $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit,$this->newOrderCountingRecordService->getCountingOwnerIds());
-
-        $result = $this->newOrderCountingRecordService->getOrderCountingRecords($unQueryCondition, $unit);
-        $this->assertEquals(3, $result->where('date_target', Carbon::now()->toDateString())->reduce(function ($carry, $item) {
-            return $carry + $item->amount;
-        }));
-        $this->assertEquals(7, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->reduce(function ($carry, $item) {
-            return $carry + $item->amount;
-        }));
-    }
-}

+ 0 - 1
tests/Services/NewOrderCountingRecordService/OrderCountingRecordsTest.php

@@ -14,7 +14,6 @@ use Tests\TestCase;
 
 class OrderCountingRecordsTest extends TestCase
 {
-    use RefreshDatabase;
 
     protected $newOrderCountingRecordService;
     protected $queryConditionDay;

+ 0 - 4
tests/Services/NewOrderCountingRecordService/TransfersToConditionsTest.php

@@ -88,10 +88,6 @@ class TransfersToConditionsTest extends TestCase
                         ->map(function(Carbon $date){
                             return $date->firstOfMonth();
                         })->toArray();break;
-                    case '周': $datesExpected=collect($date['startAt']->weeksUntil($date['endAt'], 1)->toArray())
-                        ->map(function(Carbon $date){
-                            return $date->startOfWeek();
-                        })->toArray();break;
                     case '日': $datesExpected=collect($date['startAt']->daysUntil($date['endAt'], 1)->toArray())
                         ->map(function(Carbon $date){
                             return $date->startOfDay();

+ 0 - 59
tests/Services/OrderCountingRecordService/DateTestTest.php

@@ -1,59 +0,0 @@
-<?php
-
-namespace OrderCountingRecordService;
-
-
-use App\Services\CacheService;
-use App\Services\OrderCountingRecordService;
-use Carbon\Carbon;
-use DateInterval;
-use DatePeriod;
-use DateTime;
-use Tests\TestCase;
-
-class DateTestTest extends TestCase
-{
-    /** @var OrderCountingRecordService $orderCountingRecordService */
-    public $orderCountingRecordService;
-
-    public function setUp(): void
-    {
-        parent::setUp();
-        $this->orderCountingRecordService = app(OrderCountingRecordService::class);
-    }
-
-    /**
-     * @test
-     */
-    public function carbon_date_equals()
-    {
-        $this->assertTrue('2020-11-25' == Carbon::now()->format('Y-m-d'));
-        $this->assertTrue('2020-48' == Carbon::now()->year . '-' . Carbon::now()->week);
-        $this->assertTrue('2020-11' == Carbon::now()->year . '-' . Carbon::now()->month);
-    }
-
-    /**
-     * @test
-     */
-    public function compare_date()
-    {
-        $dateStr = '2020-12-30';
-
-        $end = Carbon::parse($dateStr)->gt(Carbon::now()) ? Carbon::now()->toDateString() : $dateStr;
-
-        dd($end);
-    }
-
-    /**
-     * @test
-     */
-    public function for_date()
-    {
-        $start =  Carbon::now()->subMonth()->toDateString();
-        $end =  Carbon::now()->toDateString();
-        $unit = '月';
-        dump($start, $end, $unit);
-        $dateArray =$this->orderCountingRecordService->periodDateToArray($start, $end, $unit);
-        dd($dateArray);
-    }
-}

+ 0 - 251
tests/Services/OrderCountingRecordService/OrderCountingRecordServiceGetTest.php

@@ -1,251 +0,0 @@
-<?php
-
-namespace Tests\Services\OrderCountingRecordService;
-
-use App\Order;
-use App\Owner;
-use App\Services\CacheService;
-use App\Services\OrderCountingRecordService;
-use App\User;
-use Carbon\Carbon;
-use Illuminate\Foundation\Testing\RefreshDatabase;
-use Tests\TestCase;
-
-class OrderCountingRecordServiceGetTest extends TestCase
-{
-    use  RefreshDatabase;
-
-    /** @var OrderCountingRecordService $orderCountingRecordService */
-    public $orderCountingRecordService;
-
-    public function setUp(): void
-    {
-        parent::setUp();
-        app()->singleton('CacheService', CacheService::class);
-        $this->orderCountingRecordService = app(OrderCountingRecordService::class);
-    }
-
-
-    /**
-     * @test
-     */
-    public function counting_unit_date()
-    {
-        cache()->flush();
-        $start = Carbon::now()->subDays(2)->format('Y-m-d');
-        $end = (new Carbon())->toDateString();
-
-        $this->actingAs($user = factory(User::class)->create(['name' => 'yang']));
-
-        $owner = factory(Owner::class)->create();
-        $this->createCurrentDateOrders($owner);
-
-        $this->createSubDayOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
-        $this->assertEquals([0, 10, 20], array_column($result, 'counter'));
-        cache()->flush();
-        $this->createCurrentDateOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
-        $this->assertEquals([0, 10, 40], array_column($result, 'counter'));
-        cache()->flush();
-    }
-
-    /**
-     * @test
-     */
-    public function counting_unit_week()
-    {
-        cache()->flush();
-        $start = Carbon::now()->subWeek()->format('Y-m-d');
-        $end = (new Carbon())->toDateString();
-        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
-        $owner = factory(Owner::class)->create();
-        $this->createCurrentDateOrders($owner);
-
-        $this->createSubWeekOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
-        $this->assertEquals([20, 20], array_column($result, 'counter'));
-        cache()->flush();
-        $this->createCurrentDateOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
-        $this->assertEquals([20, 40], array_column($result, 'counter'));
-
-    }
-
-    /**
-     * @test
-     */
-    public function counting_unit_month()
-    {
-        cache()->flush();
-        $start = Carbon::now()->subMonth()->format('Y-m-d');
-        $end = (new Carbon())->toDateString();
-        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
-        $owner = factory(Owner::class)->create();
-        $this->createCurrentDateOrders($owner);
-
-        $this->createSubMonthOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
-        $this->assertEquals([20, 20], array_column($result, 'counter'));
-        cache()->flush();
-        $this->createCurrentDateOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
-        $this->assertEquals([20, 40], array_column($result, 'counter'));
-
-    }
-
-    /**
-     * @test
-     */
-    public function current_date_ttl_day()
-    {
-        cache()->flush();
-        $start = Carbon::now()->subDays(2)->format('Y-m-d');
-        $end = (new Carbon())->toDateString();
-
-        $this->actingAs($user = factory(User::class)->create(['name' => 'yang']));
-
-        $owner = factory(Owner::class)->create();
-        $this->createCurrentDateOrders($owner);
-
-        $this->createSubDayOrders($owner);
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
-        $this->assertEquals([0, 10, 20], array_column($result, 'counter'));
-        sleep(4);
-        $this->createCurrentDateOrders($owner);
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end)->toArray();
-        $this->assertEquals([0, 10, 40], array_column($result, 'counter'));
-        cache()->flush();
-
-    }
-
-    /**
-     * @test
-     */
-    public function current_date_ttl_week()
-    {
-        cache()->flush();
-        $start = Carbon::now()->subWeek()->format('Y-m-d');
-        $end = (new Carbon())->toDateString();
-        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
-        $owner = factory(Owner::class)->create();
-        $this->createCurrentDateOrders($owner);
-
-        $this->createSubWeekOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
-        $this->assertEquals([20, 20], array_column($result, 'counter'));
-
-        sleep(4);
-
-        $this->createCurrentDateOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '周')->toArray();
-        $this->assertEquals([20, 40], array_column($result, 'counter'));
-
-    }
-
-    /**
-     * @test
-     */
-    public function current_date_ttl_month()
-    {
-        cache()->flush();
-        $start = Carbon::now()->subMonth()->format('Y-m-d');
-        $end = (new Carbon())->toDateString();
-        $this->actingAs(factory(User::class)->create(['name' => 'yang']));
-        $owner = factory(Owner::class)->create();
-        $this->createCurrentDateOrders($owner);
-
-        $this->createSubMonthOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
-        $this->assertEquals([20, 20], array_column($result, 'counter'));
-        sleep(4);
-        $this->createCurrentDateOrders($owner);
-
-        $result = $this->orderCountingRecordService->orderCountingRecords($start, $end, null, '月')->toArray();
-        $this->assertEquals([20, 40], array_column($result, 'counter'));
-
-    }
-
-
-    /**
-     * @test
-     */
-    public function set_null()
-    {
-        $dateStr = '2020-11-26';
-        $this->assertTrue($dateStr != Carbon::now()->format('Y-m-d'));
-    }
-
-    /**
-     * @test
-     */
-    public function isNotCurrentDate()
-    {
-        $day_ture = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->format('Y-m-d'));
-        $day_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->format('Y-m-d'));
-
-        $week_ture = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subWeek()->year . '-' . Carbon::now()->subWeek()->week);
-        $week_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->week);
-
-        $month_true = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth()->year . '-' . Carbon::now()->subMonth()->month);
-        $month_false = $this->orderCountingRecordService->isNotCurrentDate(Carbon::now()->year . '-' . Carbon::now()->month);
-
-        $this->assertTrue($day_ture);
-        $this->assertFalse($day_false);
-
-        $this->assertTrue($week_ture);
-        $this->assertFalse($week_false);
-
-        $this->assertTrue($month_true);
-        $this->assertFalse($month_false);
-    }
-
-    public function tearDown(): void
-    {
-        Order::query()->delete();
-        Owner::query()->delete();
-        User::query()->delete();
-    }
-
-
-    protected function createCurrentDateOrders($owner): void
-    {
-        factory(Order::class)->times(20)->create([
-            'created_at' => Carbon::now(),
-            'owner_id' => $owner->id,
-            'wms_status' => '订单完成']);
-    }
-
-    protected function createSubWeekOrders($owner): void
-    {
-        factory(Order::class)->times(20)->create([
-            'created_at' => Carbon::now()->subWeek(),
-            'owner_id' => $owner->id,
-            'wms_status' => '订单完成']);
-    }
-
-    protected function createSubMonthOrders($owner): void
-    {
-        factory(Order::class)->times(20)->create([
-            'created_at' => Carbon::now()->subMonth(),
-            'owner_id' => $owner->id,
-            'wms_status' => '订单完成']);
-    }
-
-    protected function createSubDayOrders($owner): void
-    {
-        factory(Order::class)->times(10)->create([
-            'created_at' => Carbon::now()->subDays(1),
-            'owner_id' => $owner->id,
-            'wms_status' => '订单完成']);
-    }
-}

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

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

+ 0 - 31
tests/Services/OrderCountingRecordService/OrderCountingRecordServiceOrderCountingRecordsTest.php

@@ -1,31 +0,0 @@
-<?php
-
-namespace Tests\Services\OrderCountingRecordService;
-
-use App\Services\CacheService;
-use App\Services\OrderCountingRecordService;
-use Tests\TestCase;
-
-class OrderCountingRecordServiceOrderCountingRecordsTest 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-03';
-        $end = '2020-11-09';
-        dd($this->orderCountingRecordService->orderCountingRecords($start, $end, null));
-
-
-    }
-}

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

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