|
|
@@ -9,6 +9,7 @@ use App\Order;
|
|
|
use App\OrderCountingRecord;
|
|
|
use App\Warehouse;
|
|
|
use Carbon\Carbon;
|
|
|
+use Illuminate\Database\Eloquent\Builder;
|
|
|
use Illuminate\Support\Arr;
|
|
|
use Illuminate\Support\Collection;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
@@ -18,7 +19,9 @@ use App\Traits\ServiceAppAop;
|
|
|
class NewOrderCountingRecordService
|
|
|
{
|
|
|
use ServiceAppAop;
|
|
|
- protected $modelClass=NewOrderCountingRecord::class;
|
|
|
+
|
|
|
+ protected $modelClass = OrderCountingRecord::class;
|
|
|
+
|
|
|
public function orderCountingRecordsFromCache($start, $end, $unit, $ownerIds)
|
|
|
{
|
|
|
$dataList = collect();
|
|
|
@@ -86,22 +89,22 @@ class NewOrderCountingRecordService
|
|
|
{
|
|
|
$key = 'warehouseCountingRecords_' . $start . '_' . $end . '_' . json_encode($ownerIds);
|
|
|
return Cache::remember($key, config('cache.expirations.warehouseCountingRecords'), 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);
|
|
|
- $warehouse = Warehouse::query()->find($item[0]->warehouse_id);
|
|
|
+ $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);
|
|
|
+ $warehouse = Warehouse::query()->find($item[0]->warehouse_id);
|
|
|
|
|
|
- $dataList->push([
|
|
|
- 'value' => $counter,
|
|
|
- 'warehouse_id' => $item[0]->warehouse_id,
|
|
|
- 'name' => $warehouse ? $warehouse->name : '仓库为空',
|
|
|
- 'code' => $warehouse ? $warehouse->code : 'NULL',
|
|
|
- ]);
|
|
|
- });
|
|
|
- return $dataList;
|
|
|
+ $dataList->push([
|
|
|
+ 'value' => $counter,
|
|
|
+ 'warehouse_id' => $item[0]->warehouse_id,
|
|
|
+ 'name' => $warehouse ? $warehouse->name : '仓库为空',
|
|
|
+ 'code' => $warehouse ? $warehouse->code : 'NULL',
|
|
|
+ ]);
|
|
|
+ });
|
|
|
+ return $dataList;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@@ -476,11 +479,230 @@ class NewOrderCountingRecordService
|
|
|
{
|
|
|
if ($this->isNotCurrentDate($dateStr, $unit)) {
|
|
|
LogService::log('NewOrderCountingRecordService', '缓存设置为永久', $dateStr);
|
|
|
- $ttl = config('cache.expirations.forever');//非当前日期的缓存为永久
|
|
|
+ $ttl = config('cache.expirations.orderCountingRecord');//非当前日期的缓存为永久
|
|
|
} else {
|
|
|
LogService::log('NewOrderCountingRecordService', '缓存设置为临时', $dateStr);
|
|
|
$ttl = config('cache.expirations.orderCountingRecord');//当前日期缓存为1800s
|
|
|
}
|
|
|
return $ttl;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ //TODO 控制台重构
|
|
|
+
|
|
|
+ public function getWareHouseRecordsApi($start, $end, $ownerIds): array
|
|
|
+ {
|
|
|
+ $orderCountingRecords = OrderCountingRecord::query()
|
|
|
+ ->selectRaw("sum(amount) as value ,warehouse_id")
|
|
|
+ ->with('warehouse:id,name,code')
|
|
|
+ ->whereBetween('date_target', [$start, $end])
|
|
|
+ ->whereIn('owner_id', $ownerIds)
|
|
|
+ ->groupBy('warehouse_id')
|
|
|
+ ->get();
|
|
|
+ $result = [];
|
|
|
+ foreach ($orderCountingRecords as $orderCountingRecord) {
|
|
|
+ $result[] = [
|
|
|
+ 'logistic_id' => $orderCountingRecord->warehouse_id,
|
|
|
+ 'value' => $orderCountingRecord->value,
|
|
|
+ 'name' => $orderCountingRecord->warehouse->name ?? '',
|
|
|
+ 'code' => $orderCountingRecord->warehouse->code ?? '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getLogisticRecordsApi($start, $end, $ownerIds): array
|
|
|
+ {
|
|
|
+ $orderCountingRecords = OrderCountingRecord::query()
|
|
|
+ ->selectRaw("sum(amount) as value ,logistic_id")
|
|
|
+ ->with('logistic:id,name')
|
|
|
+ ->whereBetween('date_target', [$start, $end])
|
|
|
+ ->whereIn('owner_id', $ownerIds)
|
|
|
+ ->groupBy('logistic_id')
|
|
|
+ ->get();
|
|
|
+ $result = [];
|
|
|
+ foreach ($orderCountingRecords as $orderCountingRecord) {
|
|
|
+ $result[] = [
|
|
|
+ 'logistic_id' => $orderCountingRecord->logistic_id,
|
|
|
+ 'value' => $orderCountingRecord->value,
|
|
|
+ 'name' => $orderCountingRecord->logistic->name ?? '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询订单量趋势
|
|
|
+ * @param $start string
|
|
|
+ * @param $end string
|
|
|
+ * @param $unit string
|
|
|
+ * @param $ownerIds array
|
|
|
+ * @return Builder[]|\Illuminate\Database\Eloquent\Collection
|
|
|
+ */
|
|
|
+ public function getOrderCountingRecordsApi(string $start, string $end, string $unit, array $ownerIds)
|
|
|
+ {
|
|
|
+ $orderCountingRecords = OrderCountingRecord::query()
|
|
|
+ ->selectRaw("sum(amount) as counter ,date_target")
|
|
|
+ ->whereBetween('date_target', [$start, $end])
|
|
|
+ ->where('counting_unit', $unit)
|
|
|
+ ->whereIn('owner_id', $ownerIds)
|
|
|
+ ->groupBy('date_target')
|
|
|
+ ->get()->toArray();
|
|
|
+ if (now()->toDateString() == $end) {//查询时间包含当天
|
|
|
+ switch ($unit) {
|
|
|
+ case '日':
|
|
|
+ //查询当天统计
|
|
|
+ $startDateTime = Carbon::parse($end)->startOfDay()->toDateTimeString();
|
|
|
+ $endDateTime = Carbon::parse($end)->endOfDay()->toDateTimeString();
|
|
|
+ break;
|
|
|
+ case '月':
|
|
|
+ //查询当月统计
|
|
|
+ $startDateTime = Carbon::parse($end)->startOfMonth()->startOfDay()->toDateTimeString();
|
|
|
+ $endDateTime = Carbon::parse($end)->endOfMonth()->endOfDay()->toDateTimeString();
|
|
|
+ break;
|
|
|
+ case '年':
|
|
|
+ //查询当年统计
|
|
|
+ $startDateTime = Carbon::parse($end)->startOfYear()->startOfDay()->toDateTimeString();
|
|
|
+ $endDateTime = Carbon::parse($end)->endOfYear()->endOfDay()->toDateTimeString();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ $order = Order::query()
|
|
|
+ ->selectRaw("count(1) as amounts ,DATE_FORMAT(created_at,'%Y-%m-%d') as date_target")
|
|
|
+ ->whereBetween('created_at', [$startDateTime, $endDateTime])
|
|
|
+ ->where('wms_status', '订单完成')
|
|
|
+ ->whereIn('owner_id', $ownerIds)
|
|
|
+ ->groupBy('date_target')
|
|
|
+ ->first();
|
|
|
+ $orderCountingRecords[] = [
|
|
|
+ "counter" => $order->amounts,
|
|
|
+ "date_target" => $order->date_target,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return $orderCountingRecords;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计订单量 从$start 开始统计 默认截止到当前日期的前一天
|
|
|
+ * @param $start string
|
|
|
+ * @param null $end string
|
|
|
+ * @param $unit string
|
|
|
+ */
|
|
|
+ public function recordOrder(string $start, $end = null, string $unit = '日')
|
|
|
+ {
|
|
|
+ switch ($unit) {
|
|
|
+ case '日':
|
|
|
+ $this->recordByDay($start, $end, $unit);
|
|
|
+ break;
|
|
|
+ case'月':
|
|
|
+ $this->recordByMonth($start, $end, $unit);
|
|
|
+ break;
|
|
|
+ case'年':
|
|
|
+ $this->recordByYear($start, $end, $unit);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清空统计缓存
|
|
|
+ */
|
|
|
+ public function clearCacheOrderRecord()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日统计
|
|
|
+ * @param string $start
|
|
|
+ * @param $end
|
|
|
+ * @param string $unit
|
|
|
+ */
|
|
|
+ public function recordByDay(string $start, $end = null, string $unit = '日'): void
|
|
|
+ {
|
|
|
+ $startDateTime = Carbon::parse($start)->startOfDay()->toDateTimeString();
|
|
|
+ if (is_null($end)) {
|
|
|
+ $end = now()->subDay()->endOfDay();
|
|
|
+ }
|
|
|
+ $endDateTime = Carbon::parse($end)->endOfDay()->toDateTimeString();
|
|
|
+ $orders = Order::query()
|
|
|
+ ->selectRaw("owner_id,warehouse_id,logistic_id,count(1) as amounts ,DATE_FORMAT(created_at,'%Y-%m-%d') as date_target")
|
|
|
+ ->whereBetween('created_at', [$startDateTime, $endDateTime])
|
|
|
+ ->where('wms_status', '订单完成')
|
|
|
+ ->groupBy('owner_id', 'warehouse_id', 'logistic_id', 'date_target')
|
|
|
+ ->get();
|
|
|
+ $insertData = [];
|
|
|
+ foreach ($orders as $order) {
|
|
|
+ $insertData[] = [
|
|
|
+ 'owner_id' => $order->owner_id,
|
|
|
+ 'warehouse_id' => $order->warehouse_id,
|
|
|
+ 'logistic_id' => $order->logistic_id,
|
|
|
+ 'date_target' => $order->date_target,
|
|
|
+ 'counting_unit' => $unit,
|
|
|
+ 'amount' => $order->amounts,
|
|
|
+ 'year' => Carbon::parse($order->date_target)->year,
|
|
|
+ 'month' => Carbon::parse($order->date_target)->year . '-' . Carbon::parse($order->date_target)->month,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $insertDataChunked = array_chunk($insertData, 2000);
|
|
|
+ foreach ($insertDataChunked as $items) {
|
|
|
+ OrderCountingRecord::query()->insertOrIgnore($items);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function recordByMonth(string $start, $end = null, $unit = '月')
|
|
|
+ {
|
|
|
+ $startDate = Carbon::parse($start)->startOfMonth()->toDateString();
|
|
|
+ if (is_null($end)) {
|
|
|
+ $end = now()->subMonth()->endOfDay();
|
|
|
+ }
|
|
|
+ $endDate = Carbon::parse($end)->endOfDay()->toDateString();
|
|
|
+ $orderCountingRecords = OrderCountingRecord::query()
|
|
|
+ ->selectRaw("owner_id,warehouse_id,logistic_id,sum(amount) as amount_sum,month,year,date_target")
|
|
|
+ ->whereBetween('date_target', [$startDate, $endDate])
|
|
|
+ ->where('counting_unit', '日')
|
|
|
+ ->groupBy('owner_id', 'warehouse_id', 'logistic_id', 'month', 'date_target')
|
|
|
+ ->get();
|
|
|
+ $insertData = [];
|
|
|
+ foreach ($orderCountingRecords as $orderCountingRecord) {
|
|
|
+ $insertData[] = [
|
|
|
+ 'owner_id' => $orderCountingRecord->owner_id,
|
|
|
+ 'warehouse_id' => $orderCountingRecord->warehouse_id,
|
|
|
+ 'logistic_id' => $orderCountingRecord->logistic_id,
|
|
|
+ 'counting_unit' => $unit,
|
|
|
+ 'date_target' => Carbon::parse($orderCountingRecord->date_target)->startOfMonth()->toDateString(),
|
|
|
+ 'amount' => $orderCountingRecord->amount_sum,
|
|
|
+ 'year' => $orderCountingRecord->year,
|
|
|
+ 'month' => $orderCountingRecord->month,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ OrderCountingRecord::query()->insertOrIgnore($insertData);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function recordByYear(string $start, $end = null, $unit = '年')
|
|
|
+ {
|
|
|
+ $startYear = Carbon::parse($start)->year;
|
|
|
+ if (is_null($end)) {
|
|
|
+ $end = now()->subYear()->toDateString();
|
|
|
+ }
|
|
|
+ $endYear = Carbon::parse($end)->year;
|
|
|
+ $orderCountingRecords = OrderCountingRecord::query()
|
|
|
+ ->selectRaw("owner_id,warehouse_id,logistic_id,sum(amount) as amount_sum,year,date_target")
|
|
|
+ ->whereBetween('year', [$startYear, $endYear])
|
|
|
+ ->groupBy('owner_id', 'warehouse_id', 'logistic_id', 'year')
|
|
|
+ ->get();
|
|
|
+ $insertData = [];
|
|
|
+ foreach ($orderCountingRecords as $orderCountingRecord) {
|
|
|
+ $insertData[] = [
|
|
|
+ 'owner_id' => $orderCountingRecord->owner_id,
|
|
|
+ 'warehouse_id' => $orderCountingRecord->warehouse_id,
|
|
|
+ 'logistic_id' => $orderCountingRecord->logistic_id,
|
|
|
+ 'counting_unit' => $unit,
|
|
|
+ 'date_target' => Carbon::parse($orderCountingRecord->date_target)->startOfYear()->toDateString(),
|
|
|
+ 'amount' => $orderCountingRecord->amount_sum,
|
|
|
+ 'year' => $orderCountingRecord->year,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ OrderCountingRecord::query()->insertOrIgnore($insertData);
|
|
|
+ }
|
|
|
}
|