|
|
@@ -7,34 +7,17 @@ namespace App\Services;
|
|
|
use App\Order;
|
|
|
use App\OrderCountingRecord;
|
|
|
use Carbon\Carbon;
|
|
|
-use Illuminate\Support\Arr;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
|
class NewOrderCountingRecordService
|
|
|
{
|
|
|
- public function orderCountingRecords($start, $end, $unit)
|
|
|
+ public function orderCountingRecords($start, $end, $unit, $ownerIds)
|
|
|
{
|
|
|
- $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . auth()->id();
|
|
|
- return \cache()->remember($key, 600, function () use ($start, $end, $unit) {
|
|
|
- $result = collect();
|
|
|
- switch ($unit) {
|
|
|
- case '日':
|
|
|
- $result = $this->orderCountingRecordsDay($start, $end, $unit);
|
|
|
- break;
|
|
|
- case '周':
|
|
|
- $result = $this->orderCountingRecordsWeek($start, $end, $unit);
|
|
|
- break;
|
|
|
- case '月':
|
|
|
- $result = $this->orderCountingRecordsMonth($start, $end, $unit);
|
|
|
- break;
|
|
|
- case '年':
|
|
|
- $result = $this->orderCountingRecordsYear($start, $end, $unit);
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
-
|
|
|
- }
|
|
|
- return $result;
|
|
|
+ $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . json_encode($ownerIds);
|
|
|
+ $queryCondition = $this->transfersToCondition($start, $end, $unit, $ownerIds);
|
|
|
+ return cache()->remember($key, 600, function () use ($queryCondition) {
|
|
|
+ return $this->getOrderCountingRecords($queryCondition);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@@ -57,27 +40,27 @@ class NewOrderCountingRecordService
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- public function dataFromCache($start, $end, $unit)
|
|
|
+ public function getFromCache($queryCondition)
|
|
|
{
|
|
|
- $result = [];
|
|
|
- $unQueryCondition = [];
|
|
|
- $dataFromCache = collect();
|
|
|
- $queryCondition = $this->dateUtils($start, $end, $unit);
|
|
|
- foreach ($queryCondition as $dateStr => $ownerIds) {
|
|
|
+ $lackingCondition = [];
|
|
|
+ $orderCountingRecords_FromCache = collect();
|
|
|
+ $lackingCondition['unit'] = $queryCondition['unit'];
|
|
|
+ foreach ($queryCondition['data'] as $dateStr => $ownerIds) {
|
|
|
foreach ($ownerIds as $ownerId) {
|
|
|
- $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit . '_' . auth()->id();
|
|
|
+ $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $queryCondition['unit'];
|
|
|
$items = cache()->get($key);
|
|
|
if (empty($items)) {
|
|
|
- if (empty($unQueryCondition[$dateStr])) $unQueryCondition[$dateStr] = [];
|
|
|
- $unQueryCondition[$dateStr][] = $ownerId;
|
|
|
+ if (empty($lackingCondition['data'][$dateStr])) {
|
|
|
+ $lackingCondition['data'][$dateStr] = [];
|
|
|
+ }
|
|
|
+ $lackingCondition['data'][$dateStr][] = $ownerId;
|
|
|
} else {
|
|
|
- $dataFromCache = $dataFromCache->concat($items);
|
|
|
+ $orderCountingRecords_FromCache = $orderCountingRecords_FromCache->concat($items);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- $result['dataFromCache'] = $dataFromCache;
|
|
|
- $result['unQueryCondition'] = $unQueryCondition;
|
|
|
- return $result;
|
|
|
+ return ['orderCountingRecords_FromCache' => $orderCountingRecords_FromCache,
|
|
|
+ 'lackingCondition' => $lackingCondition];
|
|
|
}
|
|
|
|
|
|
public function dataFromMiddleTable($unQueryCondition, $unit)
|
|
|
@@ -98,54 +81,103 @@ class NewOrderCountingRecordService
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
- public function orderCountingRecordsDay($start, $end, $unit)
|
|
|
+ public function getOrderCountingRecords($queryCondition)
|
|
|
{
|
|
|
- $dataFromCacheResult = $this->dataFromCache($start, $end, $unit);
|
|
|
- $dataFromCache = $dataFromCacheResult['dataFromCache'];
|
|
|
- $unQueryCondition = $dataFromCacheResult['unQueryCondition'];
|
|
|
- if (!empty($unQueryCondition)) {
|
|
|
- $dataFromMiddleTableResult = $this->dataFromMiddleTable($unQueryCondition, $unit);
|
|
|
- $dataFromMiddleTable = $dataFromMiddleTableResult['dataFromMiddleTable'];
|
|
|
- $unQueryCondition = $dataFromMiddleTableResult['unQueryCondition'];
|
|
|
- if (!empty($unQueryCondition)) {
|
|
|
- $dataFromOrder = $this->dataFromOrder($unQueryCondition, $unit);
|
|
|
- }
|
|
|
+
|
|
|
+ list($orderCountingRecords_fromCache, $lackingCondition)
|
|
|
+ = $this->getFromCache($queryCondition);
|
|
|
+ if (empty($lackingCondition['data'])) {
|
|
|
+ return $orderCountingRecords_fromCache;
|
|
|
}
|
|
|
- $dataFromCache = $dataFromCache ?? collect();
|
|
|
- $dataFromMiddleTable = $dataFromMiddleTable ?? collect();
|
|
|
- $dataFromOrder = $dataFromOrder ?? collect();
|
|
|
- return $dataFromCache->concat($dataFromMiddleTable)->concat($dataFromOrder);
|
|
|
+
|
|
|
+ list($orderCountingRecords_fromSelfTable, $lackingCondition)
|
|
|
+ = $this->dataFromMiddleTable($lackingCondition);
|
|
|
+
|
|
|
+ if (empty($lackingCondition['data'])) {
|
|
|
+ return $orderCountingRecords_fromCache->concat($orderCountingRecords_fromSelfTable);
|
|
|
+ }
|
|
|
+ list($orderCountingRecords_combinedByLower, $lackingCondition)
|
|
|
+ = $this->getByLowerUnit($lackingCondition);
|
|
|
+
|
|
|
+ $orderCountingRecords_FromOrder = $this->dataFromOrder($lackingCondition);
|
|
|
+
|
|
|
+ return $orderCountingRecords_FromOrder
|
|
|
+ ->concat($orderCountingRecords_fromCache)
|
|
|
+ ->concat($orderCountingRecords_fromSelfTable)
|
|
|
+ ->concat($orderCountingRecords_combinedByLower);
|
|
|
}
|
|
|
|
|
|
- public function dateUtils($start, $end, $unit)
|
|
|
+
|
|
|
+ public function getByLowerUnit($queryCondition)
|
|
|
{
|
|
|
- $ownerIds = $this->getCountingOwnerIds();
|
|
|
- $dataArray = [];
|
|
|
- switch ($unit) {
|
|
|
- case '日';
|
|
|
- foreach (Carbon::parse($start)->daysUntil($end, 1)->toArray() as $item) {
|
|
|
- $dataArray[$item->toDateString()] = $ownerIds;
|
|
|
- }
|
|
|
+ switch ($queryCondition['unit']) {
|
|
|
+ case '年':
|
|
|
break;
|
|
|
- case '周';
|
|
|
- foreach (Carbon::parse($start)->weeksUntil($end, 1)->toArray() as $item) {
|
|
|
- $dataArray[$item->year . '-' . $item->week . ''] = $ownerIds;
|
|
|
- }
|
|
|
+ case '月':
|
|
|
break;
|
|
|
- case '月';
|
|
|
- foreach (Carbon::parse($start)->monthsUntil($end, 1)->toArray() as $item) {
|
|
|
- $dataArray[$item->year . '-' . $item->format('m') . ''] = $ownerIds;
|
|
|
+ case '周':
|
|
|
+ $conditionClone = ['unit' => '日', 'data' => collect([])];
|
|
|
+ foreach ($queryCondition['data'] as $date => $ownerIds) {
|
|
|
+ $startAt = $date;
|
|
|
+ $endAt = Carbon::parse($date)->endOfWeek()->toDateString();
|
|
|
+ $conditionClone['data']->concat(
|
|
|
+ $this->transfersToCondition(
|
|
|
+ $startAt, $endAt, $queryCondition['unit'], $ownerIds
|
|
|
+ )['data']
|
|
|
+ );
|
|
|
}
|
|
|
+ $conditionClone['data'] = $conditionClone['data']->toArray();
|
|
|
+ $orderCountingRecords_days = $this->getOrderCountingRecords($conditionClone);
|
|
|
+ //TODO: func turnDaysToWeeks($orderCountingRecords_days): 变成 ..._weeks
|
|
|
+ $orderCountingRecords_combinedByLower = $this->turnDaysToWeeks($orderCountingRecords_days);
|
|
|
break;
|
|
|
- case '年';
|
|
|
- foreach (Carbon::parse($start)->yearsUntil($end, 1)->toArray() as $item) {
|
|
|
- $dataArray[$item->year] = $ownerIds;
|
|
|
- }
|
|
|
+ case '日':
|
|
|
+ return [[], $queryCondition];
|
|
|
break;
|
|
|
default:
|
|
|
+ }
|
|
|
+ return [[], $orderCountingRecords_combinedByLower];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function transfersToCondition($start, $end, $unit, $ownerIds)
|
|
|
+ {
|
|
|
+ $condition = [
|
|
|
+ 'data' => [],
|
|
|
+ 'unit' => $unit,];
|
|
|
+ $startAt = Carbon::parse($start);
|
|
|
+ $dates = [];
|
|
|
+ switch ($unit) {
|
|
|
+ case '年':
|
|
|
+ $dates = collect($startAt->yearsUntil($end, 1)->toArray())
|
|
|
+ ->map(function (Carbon $date) {
|
|
|
+ return $date->firstOfYear();
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case '月':
|
|
|
+ $dates = collect($startAt->monthsUntil($end, 1)->toArray())
|
|
|
+ ->map(function (Carbon $date) {
|
|
|
+ return $date->firstOfMonth();
|
|
|
+ });
|
|
|
break;
|
|
|
+ case '周':
|
|
|
+ $dates = collect($startAt->weeksUntil($end, 1)->toArray())
|
|
|
+ ->map(function (Carbon $date) {
|
|
|
+ return $date->startOfWeek();
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ case '日':
|
|
|
+ $dates = collect($startAt->daysUntil($end, 1)->toArray())
|
|
|
+ ->map(function (Carbon $date) {
|
|
|
+ return $date->startOfDay();
|
|
|
+ });
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($dates as $day) {
|
|
|
+ $condition['data'][$day->toDateString()] = $ownerIds;
|
|
|
}
|
|
|
- return $dataArray;
|
|
|
+ return $condition;
|
|
|
}
|
|
|
|
|
|
public function dataFromOrder($unQueryCondition, $unit)
|
|
|
@@ -164,24 +196,6 @@ class NewOrderCountingRecordService
|
|
|
return $dataFromMiddleTable;
|
|
|
}
|
|
|
|
|
|
- public function orderCountingRecordsWeek($start, $end, $unit)
|
|
|
- {
|
|
|
- //TODO
|
|
|
- return collect();
|
|
|
- }
|
|
|
-
|
|
|
- public function orderCountingRecordsMonth($start, $end, $unit)
|
|
|
- {
|
|
|
- //TODO
|
|
|
- return collect();
|
|
|
- }
|
|
|
-
|
|
|
- public function orderCountingRecordsYear($start, $end, $unit)
|
|
|
- {
|
|
|
- //TODO
|
|
|
- return collect();
|
|
|
- }
|
|
|
-
|
|
|
public function insertIntoMiddleTable($data, $unit)
|
|
|
{
|
|
|
$data->filter(function ($item) {
|
|
|
@@ -243,6 +257,10 @@ class NewOrderCountingRecordService
|
|
|
//当日当周当月当年的数据不能插入到中间表
|
|
|
$dateTime = Carbon::now()->toDateTimeString();
|
|
|
foreach ($dataFromOrder as $order) {
|
|
|
+ $carbon = Carbon::parse($order->date_target);
|
|
|
+ $year = $carbon->year;
|
|
|
+ $week = $carbon->week;
|
|
|
+ $month = $carbon->month;
|
|
|
$dataFromMiddleTable->push(new OrderCountingRecord([
|
|
|
'owner_id' => $order->owner_id,
|
|
|
'shop_id' => $order->shop_id,
|
|
|
@@ -251,6 +269,9 @@ class NewOrderCountingRecordService
|
|
|
'date_target' => $order->date_target,
|
|
|
'counting_unit' => $order->counting_unit,
|
|
|
'amount' => $order->amounts,
|
|
|
+ 'week' => $year . '-' . $week,
|
|
|
+ 'month' => $year . '-' . $month,
|
|
|
+ 'year' => $year,
|
|
|
'created_at' => $dateTime,
|
|
|
'updated_at' => $dateTime,
|
|
|
]));
|
|
|
@@ -258,4 +279,32 @@ class NewOrderCountingRecordService
|
|
|
return $dataFromMiddleTable;
|
|
|
}
|
|
|
|
|
|
+ public function dateSwitch($unQueryCondition, $currentUnit, $targetUnit)
|
|
|
+ {
|
|
|
+ foreach ($unQueryCondition as $date => $ownerIds) {
|
|
|
+ switch ($currentUnit . '-' . $targetUnit) {
|
|
|
+ case '周-日':
|
|
|
+ $week = Str::of($date)->after('-');
|
|
|
+ $year = Str::of($date)->before('-');
|
|
|
+
|
|
|
+ break;
|
|
|
+ case '月-周':
|
|
|
+ break;
|
|
|
+ case '年-月':
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function turnDaysToWeeks($orderCountingRecords_days)
|
|
|
+ {
|
|
|
+ $orderCountingRecords_combinedByLower = collect();
|
|
|
+ foreach ($orderCountingRecords_days as $orderCountingRecord) {
|
|
|
+ $date_target = $orderCountingRecord->date_target;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|