161 lines
4.7 KiB
PHP
161 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\Admin\Order;
|
|
|
|
use App\Cache\Redis\Api\SiteCache;
|
|
use App\Model\Order;
|
|
use App\Model\OrderGood;
|
|
use App\Model\Sku;
|
|
use App\Service\Admin\BaseService;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class OrderListService extends BaseService
|
|
{
|
|
/**
|
|
* @var Order
|
|
*/
|
|
#[Inject]
|
|
protected Order $orderModel;
|
|
|
|
/**
|
|
* @var OrderGood
|
|
*/
|
|
#[Inject]
|
|
protected OrderGood $orderGoodModel;
|
|
|
|
/**
|
|
* @var Sku
|
|
*/
|
|
#[Inject]
|
|
protected Sku $skuModel;
|
|
|
|
/**
|
|
* @var SiteCache
|
|
*/
|
|
#[Inject]
|
|
protected SiteCache $siteCache;
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
$limit = (int)$this->request->input('limit', 20);
|
|
|
|
//todo where
|
|
$orderList = $this->orderModel
|
|
->when($this->request->input('search_user_id'), function ($query) {
|
|
$query->where('user_id', $this->request->input('search_user_id'));
|
|
})
|
|
->when($this->request->input('search_status'), function ($query) {
|
|
$query->where('status', $this->request->input('search_status'));
|
|
})
|
|
->when($this->request->input('search_city_id'), function ($query) {
|
|
$query->where('city_id', $this->request->input('search_city_id'));
|
|
})
|
|
->when($this->request->input('search_kitchen_id'), function ($query) {
|
|
$query->where('kitchen_id', $this->request->input('search_kitchen_id'));
|
|
})
|
|
->select([
|
|
'id',
|
|
'cycle_id',
|
|
'order_sno',
|
|
'user_id',
|
|
'kitchen_id',
|
|
'site_id',
|
|
'copies',
|
|
'type',
|
|
'actual_price',
|
|
'total_price',
|
|
'status',
|
|
'create_time',
|
|
'pay_time'
|
|
])
|
|
->orderByDesc('id')
|
|
->paginate($limit)
|
|
->toArray();
|
|
|
|
if (!empty($orderList['data'])) {
|
|
$this->buildData($orderList['data']);
|
|
}
|
|
|
|
return $this->return->success('success', ['list' => $orderList]);
|
|
}
|
|
|
|
/**
|
|
* 构建订单列表数据
|
|
* @param array $orderList
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function buildData(array &$orderList): void
|
|
{
|
|
$orderIds = array_column($orderList, 'id');
|
|
|
|
$orderSkuList = $this->orderGoodModel->whereIn('order_id', $orderIds)->get()->toArray();
|
|
$skuIds = [];
|
|
$newOrderSkuList = [];
|
|
foreach ($orderSkuList as $orderSku) {
|
|
if (empty($newOrderSkuList[$orderSku['order_id']])) {
|
|
$newOrderSkuList[$orderSku['order_id']] = [];
|
|
}
|
|
|
|
if (empty($newOrderSkuList[$orderSku['order_id']][$orderSku['copies']])) {
|
|
$newOrderSkuList[$orderSku['order_id']][$orderSku['copies']] = [];
|
|
}
|
|
|
|
$newOrderSkuList[$orderSku['order_id']][$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');
|
|
|
|
|
|
foreach ($orderList as &$order) {
|
|
$orderCopiesList = [];
|
|
for ($i = 1; $i <= ($order['copies'] ?? 0); $i++) {
|
|
|
|
$oneCopiesInfo = [
|
|
'total_price' => '0.00',
|
|
'total_quantity' => 0,
|
|
'sku_list' => [],
|
|
'take_food_code' => [], //todo 取餐码
|
|
];
|
|
|
|
foreach ($newOrderSkuList[$order['id']][$i] as $item) {
|
|
if ($item['order_id'] != $order['id'] || $item['copies'] != $i) continue;
|
|
|
|
$skuInfo = $skuList[$item['sku_id']] ?? [];
|
|
$oneCopiesInfo['total_price'] = bcadd($oneCopiesInfo['total_price'], bcmul($item['unit_price'],(string)$item['quantity'],2), 2);
|
|
$oneCopiesInfo['total_quantity'] += $item['quantity'];
|
|
|
|
$oneCopiesInfo['sku_list'][] = $skuInfo;
|
|
}
|
|
|
|
$orderCopiesList[] = $oneCopiesInfo;
|
|
}
|
|
|
|
$order['copies_list'] = $orderCopiesList;
|
|
$order['site'] = $this->siteCache->getSiteInfo((int)$order['site_id']) ?? [];
|
|
}
|
|
}
|
|
} |