|
|
@@ -4,11 +4,11 @@
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
+use App\Logistic;
|
|
|
use App\Order;
|
|
|
use App\OrderCountingRecord;
|
|
|
-use App\User;
|
|
|
+use App\Warehouse;
|
|
|
use Carbon\Carbon;
|
|
|
-use Illuminate\Support\Collection;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
class NewOrderCountingRecordService
|
|
|
@@ -16,12 +16,100 @@ class NewOrderCountingRecordService
|
|
|
public function orderCountingRecords($start, $end, $unit, $ownerIds)
|
|
|
{
|
|
|
$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);
|
|
|
+ return Cache::remember($key, 60, function () use ($start, $end, $unit, $ownerIds) {
|
|
|
+ $orders = $this->get($start, $end, $unit, $ownerIds);
|
|
|
+ $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;
|
|
|
+ $dataList->push([
|
|
|
+ 'counter' => $counter,
|
|
|
+ 'date_target' => $date_target,
|
|
|
+ ]);
|
|
|
+ });
|
|
|
+ return $dataList->sortBy("date_target");
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public function logisticsCountingRecords($start, $end, $ownerIds)
|
|
|
+ {
|
|
|
+ $key = 'logisticsCountingRecords_' . $start . '_' . $end . '_' . json_encode($ownerIds);
|
|
|
+ return Cache::remember($key, 600, function () use ($start, $end, $ownerIds) {
|
|
|
+ $dataList = collect();
|
|
|
+ $resultOrders = $this->get($start, $end, '日', $ownerIds);
|
|
|
+ $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)
|
|
|
+ {
|
|
|
+ $key = 'warehouseCountingRecords_' . $start . '_' . $end . '_' . json_encode($ownerIds);
|
|
|
+ return Cache::remember($key, 600, function () use ($start, $end, $ownerIds) {
|
|
|
+ $dataList = collect();
|
|
|
+ $resultOrders = $this->get($start, $end, '日', $ownerIds);
|
|
|
+ $resultOrders->groupBy('warehouse_id')->each(function ($item) use (&$dataList) {
|
|
|
+ $counter = $item->reduce(function ($sum, $item) {
|
|
|
+ return $sum + $item->amount;
|
|
|
+ }, 0);
|
|
|
+ $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;
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function get($start, $end, $unit, $ownerIds)
|
|
|
+ {
|
|
|
+ $queryCondition = $this->transfersToCondition($start, $end, $unit, $ownerIds);
|
|
|
+ return $this->getOrderCountingRecords($queryCondition);
|
|
|
+ }
|
|
|
+
|
|
|
public function getFromCache($queryCondition)
|
|
|
{
|
|
|
$lackingCondition = [];
|
|
|
@@ -78,10 +166,8 @@ class NewOrderCountingRecordService
|
|
|
if (empty($lackingCondition['data'])) {
|
|
|
return $orderCountingRecords_fromCache->concat($orderCountingRecords_fromSelfTable);
|
|
|
}
|
|
|
- if ($queryCondition['unit'] != '日') {
|
|
|
- list($orderCountingRecords_combinedByLower, $lackingCondition)
|
|
|
- = $this->getByLowerUnit($lackingCondition);
|
|
|
- }
|
|
|
+ list($orderCountingRecords_combinedByLower, $lackingCondition)
|
|
|
+ = $this->getByLowerUnit($lackingCondition);
|
|
|
|
|
|
if (!empty($lackingCondition)) {
|
|
|
$orderCountingRecords_FromOrder = $this->dataFromOrder($lackingCondition);
|
|
|
@@ -97,13 +183,11 @@ class NewOrderCountingRecordService
|
|
|
{
|
|
|
switch ($queryCondition['unit']) {
|
|
|
case '年':
|
|
|
- break;
|
|
|
- case '月':
|
|
|
- $lowUnit = '周';
|
|
|
+ $lowUnit = '月';
|
|
|
$conditionClone = ['unit' => $lowUnit, 'data' => []];
|
|
|
foreach ($queryCondition['data'] as $date => $ownerIds) {
|
|
|
$startAt = $date;
|
|
|
- $endAt = Carbon::parse($date)->endOfMonth()->toDateString();
|
|
|
+ $endAt = Carbon::parse($date)->endOfYear()->toDateString();
|
|
|
$items = $this->transfersToCondition(
|
|
|
$startAt, $endAt, $lowUnit, $ownerIds
|
|
|
)['data'];
|
|
|
@@ -113,14 +197,14 @@ class NewOrderCountingRecordService
|
|
|
}
|
|
|
}
|
|
|
$orderCountingRecords_days = $this->getOrderCountingRecords($conditionClone);
|
|
|
- $orderCountingRecords_combinedByLower = $this->turnGradingUpToLow($orderCountingRecords_days, $lowUnit, 'month');
|
|
|
+ $orderCountingRecords_combinedByLower = $this->turnGradingUpToLow($orderCountingRecords_days, $lowUnit, 'year');
|
|
|
break;
|
|
|
- case '周':
|
|
|
+ case '月':
|
|
|
$lowUnit = '日';
|
|
|
$conditionClone = ['unit' => $lowUnit, 'data' => []];
|
|
|
foreach ($queryCondition['data'] as $date => $ownerIds) {
|
|
|
$startAt = $date;
|
|
|
- $endAt = Carbon::parse($date)->endOfWeek()->toDateString();
|
|
|
+ $endAt = Carbon::parse($date)->endOfMonth()->toDateString();
|
|
|
$items = $this->transfersToCondition(
|
|
|
$startAt, $endAt, $lowUnit, $ownerIds
|
|
|
)['data'];
|
|
|
@@ -130,7 +214,7 @@ class NewOrderCountingRecordService
|
|
|
}
|
|
|
}
|
|
|
$orderCountingRecords_days = $this->getOrderCountingRecords($conditionClone);
|
|
|
- $orderCountingRecords_combinedByLower = $this->turnGradingUpToLow($orderCountingRecords_days, $lowUnit, 'week');
|
|
|
+ $orderCountingRecords_combinedByLower = $this->turnGradingUpToLow($orderCountingRecords_days, $lowUnit, 'month');
|
|
|
break;
|
|
|
case '日':
|
|
|
return [[], $queryCondition];
|
|
|
@@ -159,12 +243,6 @@ class NewOrderCountingRecordService
|
|
|
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) {
|
|
|
@@ -226,7 +304,6 @@ class NewOrderCountingRecordService
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
@@ -246,6 +323,9 @@ class NewOrderCountingRecordService
|
|
|
unset($unQueryCondition['data'][$dateStr][$key]);
|
|
|
}
|
|
|
}
|
|
|
+ if (empty($unQueryCondition['data'][$dateStr])) {
|
|
|
+ unset($unQueryCondition['data'][$dateStr]);
|
|
|
+ }
|
|
|
}
|
|
|
return $unQueryCondition;
|
|
|
}
|
|
|
@@ -299,16 +379,12 @@ class NewOrderCountingRecordService
|
|
|
if ($isEnd) {
|
|
|
switch ($unit) {
|
|
|
case "日":
|
|
|
- $item['date_target'] = Carbon::parse($item['date_target'])->startOfWeek()->toDateString();
|
|
|
- break;
|
|
|
- case "周":
|
|
|
$item['date_target'] = Carbon::parse($item['date_target'])->firstOfMonth()->toDateString();
|
|
|
break;
|
|
|
case "月":
|
|
|
$item['date_target'] = Carbon::parse($item['date_target'])->firstOfYear()->toDateString();
|
|
|
break;
|
|
|
}
|
|
|
-
|
|
|
$item['amount'] = $amount;
|
|
|
$item['counting_unit'] = $unit;
|
|
|
return $result->push($item);
|