|
|
@@ -4,14 +4,17 @@
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
+use App\Logistic;
|
|
|
use App\Order;
|
|
|
use App\OrderCountingRecord;
|
|
|
+use App\Warehouse;
|
|
|
use Carbon\Carbon;
|
|
|
use DateTime;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
use App\User;
|
|
|
use Illuminate\Support\Str;
|
|
|
+use function MongoDB\BSON\toJSON;
|
|
|
|
|
|
class OrderCountingRecordService
|
|
|
{
|
|
|
@@ -23,20 +26,28 @@ class OrderCountingRecordService
|
|
|
$this->ownerService = app(OwnerService::class);
|
|
|
}
|
|
|
|
|
|
- public function get($start, $end, $ownerIds = null, $unit = '日')
|
|
|
+ /**
|
|
|
+ * @param $start
|
|
|
+ * @param $end
|
|
|
+ * @param null $ownerIds
|
|
|
+ * @param string $unit
|
|
|
+ * @param $user
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function get($start, $end, $ownerIds = null, $unit = '日', $user)
|
|
|
{
|
|
|
- $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit);
|
|
|
+ $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit, $user);
|
|
|
if (!($resultByCache['unExistingOrders'])) return $resultByCache['resultOrders'];
|
|
|
$resultByOrderCountingRecords = $this->getByDatabase($resultByCache['unExistingOrders'], $unit);
|
|
|
$this->createByDatabase($resultByOrderCountingRecords['unExistingOrders'], $unit);
|
|
|
$this->getByDatabase($resultByCache['unExistingOrders'], $unit);
|
|
|
- $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit);
|
|
|
+ $resultByCache = $this->getByCache($start, $end, $ownerIds, $unit, $user);
|
|
|
return $resultByCache['resultOrders'];
|
|
|
}
|
|
|
|
|
|
public function orderCountingRecords($start, $end, $unit = '日')
|
|
|
{
|
|
|
- $orders = $this->get($start, $end, null, $unit);
|
|
|
+ $orders = $this->get($start, $end, null, $unit, null);
|
|
|
$dataList = collect();
|
|
|
$orders->groupBy('date_target')->each(function ($items) use (&$dataList, $unit) {
|
|
|
$counter = $items->reduce(function ($sum, $item) {
|
|
|
@@ -54,24 +65,74 @@ class OrderCountingRecordService
|
|
|
return $dataList->sortBy("date_target");
|
|
|
}
|
|
|
|
|
|
- public function logisticsCountingRecords($start, $end, $ownerIds = null)
|
|
|
+ /**
|
|
|
+ * @param $start
|
|
|
+ * @param $end
|
|
|
+ * @param null $ownerIds
|
|
|
+ * @param null $user
|
|
|
+ * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection
|
|
|
+ */
|
|
|
+ public function logisticsCountingRecords($start, $end, $ownerIds = null, $user = null)
|
|
|
{
|
|
|
$dataList = collect();
|
|
|
- $resultOrders = $this->get($start, $end, $ownerIds);
|
|
|
- $resultOrders->groupBy('logistic_id')->each(function ($item)use(&$dataList) {
|
|
|
+ $resultOrders = $this->get($start, $end, $ownerIds, '日', $user);
|
|
|
+ $resultOrders->groupBy('logistic_id')->each(function ($item) use (&$dataList) {
|
|
|
$counter = $item->reduce(function ($sum, $item) {
|
|
|
return $sum + $item->amount;
|
|
|
}, 0);
|
|
|
$dataList->push([
|
|
|
- 'counter' => $counter,
|
|
|
- 'logistic_id' => $date_target,
|
|
|
+ '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 getByCache($start, $end, $ownerIds = null, $unit = '日')
|
|
|
+ public function warehouseCountingRecords($start, $end, $ownerIds = null, $user = null)
|
|
|
+ {
|
|
|
+ $dataList = collect();
|
|
|
+ $resultOrders = $this->get($start, $end, $ownerIds, '日', $user);
|
|
|
+ $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['name'] = $warehouse->name ??'';
|
|
|
+ return $item;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $start
|
|
|
+ * @param $end
|
|
|
+ * @param null $ownerIds
|
|
|
+ * @param string $unit
|
|
|
+ * @param $user
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getByCache($start, $end, $ownerIds = null, $unit = '日', $user)
|
|
|
{// Order[] results, Array [$date=>[$ownerIds]]
|
|
|
- $countingOwnerIds = $this->getCountingOwnerIds($ownerIds);
|
|
|
+ $countingOwnerIds = $this->getCountingOwnerIds($ownerIds, $user);
|
|
|
$resultOrders = collect();
|
|
|
$unExistingOrders = [];
|
|
|
$carbonInterfaces = $this->periodDateToArray($start, $end, $unit);
|
|
|
@@ -190,9 +251,11 @@ class OrderCountingRecordService
|
|
|
* @param $ownerIds
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function getCountingOwnerIds($ownerIds = null): array
|
|
|
+ public function getCountingOwnerIds($ownerIds = null, $user): array
|
|
|
{
|
|
|
- $user = auth()->user();
|
|
|
+ if ($user == null) {
|
|
|
+ $user = auth()->user();
|
|
|
+ }
|
|
|
/** @var UserService $userService */
|
|
|
$userService = app('UserService');
|
|
|
$permittingOwnerIds = $userService->getPermittingOwnerIds($user);
|