|
|
@@ -6,9 +6,9 @@ namespace App\Services;
|
|
|
|
|
|
use App\Order;
|
|
|
use App\OrderCountingRecord;
|
|
|
+use App\User;
|
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
-use Illuminate\Support\Str;
|
|
|
|
|
|
class NewOrderCountingRecordService
|
|
|
{
|
|
|
@@ -63,11 +63,12 @@ class NewOrderCountingRecordService
|
|
|
'lackingCondition' => $lackingCondition];
|
|
|
}
|
|
|
|
|
|
- public function dataFromMiddleTable($unQueryCondition, $unit)
|
|
|
+ public function dataFromMiddleTable($queryCondition)
|
|
|
{
|
|
|
$result = [];
|
|
|
$orderSqlBuilder = OrderCountingRecord::query();
|
|
|
- foreach ($unQueryCondition as $date => $owners) {
|
|
|
+ $unit = $queryCondition['unit'];
|
|
|
+ foreach ($queryCondition as $date => $owners) {
|
|
|
$orderSqlBuilder->orWhere(function ($query) use ($owners, $date, $unit) {
|
|
|
$query->whereIn('owner_id', $owners)->where('date_target', $date)->where('counting_unit', $unit);
|
|
|
});
|
|
|
@@ -75,8 +76,8 @@ class NewOrderCountingRecordService
|
|
|
$dataFromMiddleTable = $orderSqlBuilder->get();
|
|
|
$this->insertIntoCache($dataFromMiddleTable, $unit);
|
|
|
|
|
|
- $unQueryCondition = $this->unQueryCondition($dataFromMiddleTable, $unQueryCondition);
|
|
|
- $result['unQueryCondition'] = $unQueryCondition;
|
|
|
+ $queryCondition = $this->unQueryCondition($dataFromMiddleTable, $queryCondition);
|
|
|
+ $result['unQueryCondition'] = $queryCondition;
|
|
|
$result['dataFromMiddleTable'] = $dataFromMiddleTable;
|
|
|
return $result;
|
|
|
}
|
|
|
@@ -128,15 +129,13 @@ class NewOrderCountingRecordService
|
|
|
}
|
|
|
$conditionClone['data'] = $conditionClone['data']->toArray();
|
|
|
$orderCountingRecords_days = $this->getOrderCountingRecords($conditionClone);
|
|
|
- //TODO: func turnDaysToWeeks($orderCountingRecords_days): 变成 ..._weeks
|
|
|
$orderCountingRecords_combinedByLower = $this->turnDaysToWeeks($orderCountingRecords_days);
|
|
|
break;
|
|
|
case '日':
|
|
|
return [[], $queryCondition];
|
|
|
- break;
|
|
|
default:
|
|
|
}
|
|
|
- return [[], $orderCountingRecords_combinedByLower];
|
|
|
+ return [$orderCountingRecords_combinedByLower, []];
|
|
|
}
|
|
|
|
|
|
public function transfersToCondition($start, $end, $unit, $ownerIds)
|
|
|
@@ -180,10 +179,10 @@ class NewOrderCountingRecordService
|
|
|
return $condition;
|
|
|
}
|
|
|
|
|
|
- public function dataFromOrder($unQueryCondition, $unit)
|
|
|
+ public function dataFromOrder($queryCondition)
|
|
|
{
|
|
|
$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 ($unQueryCondition as $dateStr => $ownerIds) {
|
|
|
+ foreach ($queryCondition['date'] 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', '订单完成');
|
|
|
});
|
|
|
@@ -191,12 +190,12 @@ class NewOrderCountingRecordService
|
|
|
$dataFromOrder = $orderSqlBuilder->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id', 'date_target'])->get();
|
|
|
|
|
|
$dataFromMiddleTable = $this->ordersToOrderCountingRecords($dataFromOrder);
|
|
|
- $this->insertIntoMiddleTable($dataFromMiddleTable, $unit);
|
|
|
- $this->insertIntoCache($dataFromMiddleTable, $unit);
|
|
|
+ $this->insertIntoMiddleTable($dataFromMiddleTable);
|
|
|
+ $this->insertIntoCache($dataFromMiddleTable, $queryCondition['unit']);
|
|
|
return $dataFromMiddleTable;
|
|
|
}
|
|
|
|
|
|
- public function insertIntoMiddleTable($data, $unit)
|
|
|
+ public function insertIntoMiddleTable($data)
|
|
|
{
|
|
|
$data->filter(function ($item) {
|
|
|
return $this->isNotCurrentDate($item->date_target);
|
|
|
@@ -279,32 +278,26 @@ 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;
|
|
|
- }
|
|
|
+
|
|
|
+ $orderCountingRecords_days->groupBy('owner_id', 'shop_id', 'warehouse_id', 'logistic_id', 'week')->each(function ($ownerId, $ownerIds) use (&$orderCountingRecords_combinedByLower) {
|
|
|
+ $ownerIds->each(function ($shopId, $shopIds) use (&$orderCountingRecords_combinedByLower) {
|
|
|
+ $shopIds->each(function ($warehouseId, $warehouseIds) use (&$orderCountingRecords_combinedByLower) {
|
|
|
+ $warehouseIds->each(function ($logisticId, $logisticIds) use (&$orderCountingRecords_combinedByLower) {
|
|
|
+ $logisticIds->each(function ($week, $weeks) use (&$orderCountingRecords_combinedByLower) {
|
|
|
+ $orderCountingRecord = $weeks->first();
|
|
|
+ $orderCountingRecord->amount = $weeks->reduce(function ($carry, $item) {
|
|
|
+ return $carry + $item->amount;
|
|
|
+ });
|
|
|
+ $orderCountingRecords_combinedByLower->push($orderCountingRecord);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ return $orderCountingRecords_combinedByLower;
|
|
|
}
|
|
|
|
|
|
}
|