244 lines
6.9 KiB
PHP
244 lines
6.9 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Api\Driver;
|
|
|
|
use App\Constants\Admin\CateringCode;
|
|
use App\Constants\Common\GoodCode;
|
|
use App\Constants\Common\RoleCode;
|
|
use App\Exception\ErrException;
|
|
use App\Model\AdminUser;
|
|
use App\Model\DriverStatus;
|
|
use App\Model\OrderMealCateringLog;
|
|
use App\Model\OrderOptionCateringLog;
|
|
use App\Model\Site;
|
|
use App\Model\Sku;
|
|
use App\Service\Api\BaseService;
|
|
use App\Service\ServiceTrait\Common\CycleTrait;
|
|
use Hyperf\Database\Model\Builder;
|
|
use Hyperf\Database\Model\Model;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
abstract class BaseDriverService extends BaseService
|
|
{
|
|
use CycleTrait;
|
|
|
|
/**
|
|
* @var AdminUser
|
|
*/
|
|
#[Inject]
|
|
protected AdminUser $adminUserModel;
|
|
|
|
/**
|
|
* @var Site
|
|
*/
|
|
#[Inject]
|
|
protected Site $siteModel;
|
|
|
|
/**
|
|
* @var OrderMealCateringLog
|
|
*/
|
|
#[Inject]
|
|
protected OrderMealCateringLog $orderMealCateringLogModel;
|
|
|
|
/**
|
|
* @var OrderOptionCateringLog
|
|
*/
|
|
#[Inject]
|
|
protected OrderOptionCateringLog $orderOptionCateringLogModel;
|
|
|
|
/**
|
|
* @var DriverStatus
|
|
*/
|
|
#[Inject]
|
|
protected DriverStatus $driverStatusModel;
|
|
|
|
/**
|
|
* @var Sku
|
|
*/
|
|
#[Inject]
|
|
protected Sku $skuModel;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected array $siteIds;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected array $siteArr;
|
|
|
|
/**
|
|
* @var Builder|Model|AdminUser|null
|
|
*/
|
|
protected AdminUser|null|Builder|Model $adminInfo;
|
|
|
|
protected int $cycleId;
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
$this->cycleId = (int)$this->initTodayCycleId();
|
|
|
|
$this->adminInfo = $this->adminUserModel->getAdminInfoByBindUserId($this->userId);
|
|
if ($this->adminInfo->role_id != RoleCode::DRIVER) throw new ErrException('暂无权限,请联系客服并提供相关信息绑定账号');
|
|
|
|
$siteArr = $this->siteModel
|
|
->where('delivered_id',$this->adminInfo->id)
|
|
->orderBy('sequence')
|
|
->get();
|
|
if ($siteArr->isEmpty()) throw new ErrException('该司机并未绑定站点');
|
|
$this->siteArr = array_column($siteArr->toArray(),null,'id');
|
|
$this->siteIds = array_keys($siteArr->toArray());
|
|
}
|
|
|
|
abstract public function handle();
|
|
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getTodayList(): array
|
|
{
|
|
$info = $this->driverStatusModel
|
|
->where('driver_id',$this->adminInfo->id)
|
|
->where('cycle_id',$this->cycleId)
|
|
->whereIn('site_id',$this->siteIds)
|
|
->get();
|
|
|
|
if ($info->isEmpty()) return [];
|
|
$info = $info->toArray();
|
|
|
|
foreach ($info as $infoItem) {
|
|
$info['meal_list'] = json_decode($info['meal_list'], true);
|
|
}
|
|
|
|
return $info;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getMealData(): array
|
|
{
|
|
$mealArr = $this->orderMealCateringLogModel
|
|
->where('cycle_id',$this->cycleId)
|
|
->where('status',CateringCode::CATERING_STATUS_FINISH)
|
|
->whereIn('site_id', $this->siteIds)
|
|
->select(['quantity','site_id','sku_id'])
|
|
->get();
|
|
if ($mealArr->isEmpty()) $mealArr = [];
|
|
else $mealArr = $mealArr->toArray();
|
|
|
|
$skuIds = array_column($mealArr, 'sku_id');
|
|
$skuArr = $this->skuModel->getDataArrByIds($skuIds);
|
|
$skuArr = array_column($skuArr,null,'id');
|
|
|
|
|
|
$res = [];
|
|
foreach ($mealArr as $oneLog) {
|
|
if (empty($res[$oneLog['site_id']])) {
|
|
$res[$oneLog['site_id']] = [
|
|
'total_copies' => 0,
|
|
'add_staple_food_num' => 0,
|
|
'sku' => []
|
|
];
|
|
}
|
|
|
|
if (empty($res[$oneLog['site_id']]['sku'][$oneLog['sku_id']])) {
|
|
$res[$oneLog['site_id']]['sku'][$oneLog['sku_id']] = [
|
|
'sku_id' => $oneLog['sku_id'],
|
|
'sku_name' => $skuArr[$oneLog['sku_id']]['title'] ?? '',
|
|
'quantity' => 0,
|
|
];
|
|
}
|
|
|
|
if ($skuArr[$oneLog['sku_id']]['is_add_staple_food'] == GoodCode::IS_ADD_STAPLE_FOOD) {
|
|
$res[$oneLog['site_id']]['add_staple_food_num'] += $oneLog['quantity'];
|
|
} else {
|
|
$res[$oneLog['site_id']]['total_copies'] += $oneLog['quantity'];
|
|
}
|
|
|
|
$res[$oneLog['site_id']]['sku'][$oneLog['sku_id']]['quantity'] += $oneLog['quantity'];
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function getOptionData(): array
|
|
{
|
|
$optionArr = $this->orderOptionCateringLogModel
|
|
->where('cycle_id',$this->cycleId)
|
|
->where('status',CateringCode::CATERING_STATUS_FINISH)
|
|
->whereIn('site_id', $this->siteIds)
|
|
->select(['quantity','site_id','add_staple_food_num'])
|
|
->get();
|
|
if ($optionArr->isEmpty()) $optionArr = [];
|
|
else $optionArr = $optionArr->toArray();
|
|
|
|
$res = [];
|
|
foreach ($optionArr as $oneSite) {
|
|
if (empty($res[$oneSite['site_id']])) {
|
|
$res[$oneSite['site_id']] = [
|
|
'copies' => 0,
|
|
'add_staple_food_num' => 0
|
|
];
|
|
}
|
|
|
|
$res[$oneSite['site_id']]['copies'] = $oneSite['quantity'] + $res[$oneSite['site_id']]['copies'];
|
|
$res[$oneSite['site_id']]['add_staple_food_num'] = $oneSite['add_staple_food_num'] + $res[$oneSite['site_id']]['add_staple_food_num'];
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function buildListData(): array
|
|
{
|
|
$optionArr = $this->getOptionData();
|
|
|
|
$mealArr = $this->getMealData();
|
|
|
|
$res = [];
|
|
$dateTime = date('Y-m-d H:i:s');
|
|
|
|
foreach ($this->siteArr as $site) {
|
|
$res[] = [
|
|
'cycle_id' => $this->cycleId,
|
|
'site_id' => $site['id'],
|
|
'site_name' => $site['name'],
|
|
'driver_id' => $this->adminInfo->id,
|
|
'line_name' => '',
|
|
'meal_order_quantity' => $mealArr[$site['id']]['quantity'] ?? 0,
|
|
'meal_add_staple_food_num' => $mealArr[$site['id']]['add_staple_food_num'] ?? 0,
|
|
'meal_list' => $mealArr[$site['id']]['sku'] ?? [],
|
|
'option_order_quantity' => $optionArr[$site['id']]['copies'] ?? 0,
|
|
'option_add_staple_food_num' => $optionArr[$site['id']]['add_staple_food_num'] ?? 0,
|
|
'create_time' => $dateTime,
|
|
'update_time' => $dateTime,
|
|
];
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
} |