|
|
@@ -0,0 +1,78 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+
|
|
|
+use App\Order;
|
|
|
+use App\OrderCountingRecord;
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
+use App\Services\OwnerService;
|
|
|
+
|
|
|
+class OrderCountingRecordService
|
|
|
+{
|
|
|
+ public function get($start, $end, $unit)
|
|
|
+ {
|
|
|
+ $result = collect();$emptyKeys = collect();$emptyKeys2 = collect();
|
|
|
+ $ownerIds = app(OwnerService::class)->getSelectionId();
|
|
|
+ if ($ownerIds->isEmpty()) {
|
|
|
+ $ownerIds = [10, 35, 70];
|
|
|
+ }
|
|
|
+ collect($this->periodDate($start, $end))->each(function($item)use(&$ownerIds,&$result,&$emptyKeys){
|
|
|
+ foreach ($ownerIds as $ownerId) {
|
|
|
+ $key = 'order_counting_records_' . $item . '_' . $ownerId;
|
|
|
+ $value = Cache::get($key);
|
|
|
+ if (empty($value)) $emptyKeys->push( ['owner_id' => $ownerId, 'date' => $item]);
|
|
|
+ else $result->push($value);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ $emptyKeys->each(function($key)use(&$emptyKeys2,&$result){
|
|
|
+ $owner_id = $key['owner_id'];
|
|
|
+ $date = $key['date'];
|
|
|
+ $orderCountingRecords = OrderCountingRecord::query()->where('owner_id', $owner_id)->where('date_target', $date)->get();
|
|
|
+ $orderCountingRecords->each(function ($item)use(&$result) {
|
|
|
+ $result->push($item);
|
|
|
+ });
|
|
|
+ if ($orderCountingRecords->isEmpty()) {
|
|
|
+ $emptyKeys2->push(['owner_id' => $owner_id, 'date' => $date]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ $emptyKeys2->each(function($item)use(&$result){
|
|
|
+ $owner_id = $item['owner_id'];
|
|
|
+ $date = $item['date'];
|
|
|
+ $orders = Order::query()->selectRaw('owner_id,warehouse_id,shop_id,logistic_id as logistics_id,count(1) as amounts')
|
|
|
+ ->where('owner_id', $owner_id)
|
|
|
+ ->where('created_at', 'like', $date . '%')
|
|
|
+ ->groupBy(['owner_id', 'warehouse_id', 'shop_id', 'logistic_id'])->get();
|
|
|
+ $orders->each(function($order)use(&$result,$date){
|
|
|
+ $order->date = $date;
|
|
|
+ $result->push(OrderCountingRecord::query()->create([
|
|
|
+ 'owner_id' => $order->owner_id,
|
|
|
+ 'shop_id' => $order->shop_id,
|
|
|
+ 'warehouse_id' => $order->warehouse_id,
|
|
|
+ 'logistics_id' => $order->logistics_id,
|
|
|
+ 'date_target' => $order->date,
|
|
|
+ 'counting_unit' => '日',
|
|
|
+ 'amount' => $order->amounts,
|
|
|
+ ]));
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function periodDate($start, $end)
|
|
|
+ {
|
|
|
+ $start_time = strtotime($start);
|
|
|
+ $end_time = strtotime($end);
|
|
|
+ $i = 0;
|
|
|
+ $arr = [];
|
|
|
+ while ($start_time <= $end_time) {
|
|
|
+ $arr[$i] = date('Y-m-d', $start_time);
|
|
|
+ $start_time = strtotime('+1 day', $start_time);
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
+ return $arr;
|
|
|
+ }
|
|
|
+}
|