159 lines
4.7 KiB
PHP
159 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Api\Order;
|
|
|
|
use App\Cache\Redis\Api\SiteCache;
|
|
use App\Exception\ErrException;
|
|
use App\Model\AdminUser;
|
|
use App\Model\Chef;
|
|
use App\Model\Order;
|
|
use App\Model\OrderGood;
|
|
use App\Model\Sku;
|
|
use App\Model\Spu;
|
|
use App\Service\Api\BaseService;
|
|
use App\Service\ServiceTrait\Common\OssTrait;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class OrderInfoService extends BaseService
|
|
{
|
|
use OssTrait;
|
|
|
|
/**
|
|
* @var Order
|
|
*/
|
|
#[Inject]
|
|
protected Order $orderModel;
|
|
|
|
/**
|
|
* @var OrderGood
|
|
*/
|
|
#[Inject]
|
|
protected OrderGood $orderGoodModel;
|
|
|
|
/**
|
|
* @var Sku
|
|
*/
|
|
#[Inject]
|
|
protected Sku $skuModel;
|
|
|
|
/**
|
|
* @var Spu
|
|
*/
|
|
#[Inject]
|
|
protected Spu $spuModel;
|
|
|
|
/**
|
|
* @var AdminUser
|
|
*/
|
|
#[Inject]
|
|
protected AdminUser $adminUserModel;
|
|
|
|
/**
|
|
* @var SiteCache
|
|
*/
|
|
#[Inject]
|
|
protected SiteCache $siteCache;
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
$orderId = (int)$this->request->input('order_id');
|
|
|
|
$orderInfo = $this->orderModel->getInfoById($orderId);
|
|
|
|
if (empty($orderInfo)) throw new ErrException('订单不存在');
|
|
|
|
$orderInfo = $orderInfo->toArray();
|
|
$this->buildData($orderInfo);
|
|
|
|
return $this->return->success('success', $orderInfo);
|
|
}
|
|
|
|
/**
|
|
* @param array $orderInfo
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function buildData(array &$orderInfo): void
|
|
{
|
|
$orderSkuList = $this->orderGoodModel->where('order_id', $orderInfo['id'])->get()->toArray();
|
|
|
|
$skuIds = [];
|
|
$newOrderSkuList = [];
|
|
foreach ($orderSkuList as $orderSku) {
|
|
if (empty($newOrderSkuList[$orderSku['copies']])) {
|
|
$newOrderSkuList[$orderSku['copies']] = [];
|
|
}
|
|
|
|
$newOrderSkuList[$orderSku['copies']][] = $orderSku;
|
|
|
|
if (in_array($orderSku['sku_id'], $skuIds)) continue;
|
|
|
|
$skuIds[] = $orderSku['sku_id'];
|
|
}
|
|
unset($orderSkuList);
|
|
|
|
$skuList = $this->skuModel->getDataArrByIds($skuIds);
|
|
$skuList = array_column($skuList, null,'id');
|
|
$imageList = $this->getOssObjects(array_column($skuList,'image_ids'));
|
|
|
|
$spuIds = array_unique(array_column($skuList,'spu_id'));
|
|
$spuList = $this->spuModel->getDataArrByIds($spuIds);
|
|
$spuList = array_column($spuList, null,'id');
|
|
|
|
$chefId = array_unique(array_column($spuList,'chef_id'));
|
|
$chefList = $this->adminUserModel->getDataArrByIds($chefId);
|
|
$chefList = array_column($chefList, null,'id');
|
|
|
|
$orderCopiesList = [];
|
|
for ($i = 1; $i <= ($orderInfo['copies'] ?? 0); $i++) {
|
|
|
|
$oneCopiesInfo = [
|
|
'total_price' => '0.00',
|
|
'total_quantity' => 0,
|
|
'sku_list' => [],
|
|
'take_food_code' => '', //todo 取餐码
|
|
];
|
|
|
|
foreach ($newOrderSkuList[$i] as $item) {
|
|
if ($item['order_id'] != $orderInfo['id'] || $item['copies'] != $i) continue;
|
|
|
|
$skuInfo = $skuList[$item['sku_id']] ?? [];
|
|
$imageId = !empty($skuInfo) && !empty(explode(',',$skuInfo['image_ids'])[0]) ? explode(',',$skuInfo['image_ids'])[0] : [];
|
|
// $skuInfo['url'] = $imageList[$imageId]['url'] ?? '';
|
|
// $oneCopiesInfo['image_list'][] = $imageList[$imageId] ?? [];
|
|
|
|
$oneCopiesInfo['sku_list'][] = [
|
|
'url' => $imageList[$imageId]['url'] ?? '',
|
|
'title' => $skuList[$item['sku_id']]['title'] ?? '',
|
|
'unit_price' => $item['unit_price'],
|
|
'quantity' => $item['quantity'],
|
|
'price' => bcmul((string)$item['unit_price'], (string)$item['quantity'], 2),
|
|
'chef_name' => $chefList[$spuList[$skuList[$item['sku_id']]['spu_id']]['chef_id']]['chinese_name'] ?? '',
|
|
];
|
|
$oneCopiesInfo['total_price'] = bcadd((string)$oneCopiesInfo['total_price'], bcmul((string)$item['unit_price'],(string)$item['quantity'],2), 2);
|
|
$oneCopiesInfo['total_quantity'] += $item['quantity'];
|
|
}
|
|
|
|
$orderCopiesList[] = $oneCopiesInfo;
|
|
}
|
|
|
|
$orderInfo['copies_list'] = $orderCopiesList;
|
|
$orderInfo['site'] = $this->siteCache->getSiteInfo($orderInfo['site_id']);
|
|
}
|
|
} |