212 lines
7.0 KiB
PHP
212 lines
7.0 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\Constants\Common\GoodCode;
|
|
use App\Constants\Common\OrderCode;
|
|
use App\Model\Order;
|
|
use App\Model\OrderGood;
|
|
use App\Model\PickupCode;
|
|
use App\Model\Sku;
|
|
use App\Service\Api\BaseService;
|
|
use App\Service\ServiceTrait\Common\OssTrait;
|
|
use App\Service\ServiceTrait\Common\PickupCodeTrait;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class OrderListService extends BaseService
|
|
{
|
|
|
|
use OssTrait;
|
|
use PickupCodeTrait;
|
|
|
|
/**
|
|
* @var int 当前订单/历史订单
|
|
*/
|
|
CONST int CURRENT_ORDER = 1;
|
|
|
|
CONST int HISTORY_ORDER = 2;
|
|
|
|
/**
|
|
* @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);
|
|
|
|
$type = (int)$this->request->input('type',self::CURRENT_ORDER);
|
|
|
|
$status = match ($type){
|
|
self::CURRENT_ORDER => [OrderCode::WAIT_PAY,OrderCode::PAYED,OrderCode::PLAN,OrderCode::DEPART,OrderCode::ARRIVE],
|
|
self::HISTORY_ORDER => [OrderCode::FINISH,OrderCode::CANCEL,OrderCode::FINISH_REFUND,OrderCode::UNCOMPLETED_REFUND]
|
|
};
|
|
|
|
$orderList = $this->orderModel
|
|
->where('user_id', $this->userId)
|
|
->whereIn('status', $status)
|
|
// ->when($this->request->input('status'), function ($query) {
|
|
// $query->where('status', $this->request->input('status'));
|
|
// })
|
|
->select([
|
|
'id',
|
|
'order_sno',
|
|
'user_id',
|
|
'site_id',
|
|
'copies',
|
|
'type',
|
|
'actual_price',
|
|
'total_price',
|
|
'status',
|
|
])
|
|
->orderByDesc('id')
|
|
->paginate($limit)
|
|
->toArray();
|
|
|
|
if (!empty($orderList['data'])) {
|
|
$this->buildData($orderList['data']);
|
|
}
|
|
|
|
return $this->return->success('success', ['list' => $orderList]);
|
|
}
|
|
|
|
/**
|
|
* @var PickupCode
|
|
*/
|
|
#[Inject]
|
|
protected PickupCode $pickupCodeModel;
|
|
|
|
|
|
/**
|
|
* 构建订单列表数据
|
|
* @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);
|
|
$imageIdArr = array_column($skuList,'image_ids');
|
|
$skuList = array_column($skuList, null,'id');
|
|
$imageIds = array_unique(explode(',',implode(',',$imageIdArr)));
|
|
$imageList = $this->getOssObjects($imageIds);
|
|
|
|
$pickupCodeList = $this->pickupCodeModel
|
|
->whereIn('order_id',$orderIds)
|
|
->get(['pickup_code', 'order_id', 'copies', 'heapsort', 'id'])
|
|
->toArray();
|
|
|
|
$pickupCodeArrList = $this->buildPickupCodeListData($pickupCodeList);
|
|
|
|
foreach ($orderList as &$order) {
|
|
$orderCopiesList = [];
|
|
$addStapleFood = [];
|
|
if ($order['type'] == OrderCode::ORDER_TYPE_MEAL) {
|
|
$addStapleFood = [
|
|
'total_price' => '0.00',
|
|
'total_quantity' => 0,
|
|
'pickup_code' => '', //这个没有取餐码
|
|
'image_list' => [],
|
|
'is_add_staple_food' => GoodCode::IS_ADD_STAPLE_FOOD,
|
|
];
|
|
}
|
|
|
|
for ($i = 1; $i <= (count($newOrderSkuList[$order['id']]) ?? 0); $i++) {
|
|
$breakFlag = 0;
|
|
$oneCopiesInfo = [
|
|
'total_price' => '0.00',
|
|
'total_quantity' => 0,
|
|
'image_list' => [],
|
|
'pickup_code' => $pickupCodeArrList[$order['id']][$i]['pickup_code'] ?? '', // 取餐码
|
|
'is_add_staple_food' => GoodCode::IS_NOT_ADD_STAPLE_FOOD,
|
|
];
|
|
|
|
foreach ($newOrderSkuList[$order['id']][$i] as $item) {
|
|
if ($item['order_id'] != $order['id'] || $item['copies'] != $i) continue;
|
|
|
|
$skuInfo = $skuList[$item['sku_id']] ?? [];
|
|
$imageId = !empty($skuInfo) && !empty(explode(',',$skuInfo['image_ids'])[0]) ? explode(',',$skuInfo['image_ids'])[0] : [];
|
|
|
|
if ($skuInfo['is_add_staple_food'] == GoodCode::IS_ADD_STAPLE_FOOD) {
|
|
$addStapleFood['total_price'] = bcadd((string)$oneCopiesInfo['total_price'], bcmul((string)$item['unit_price'],(string)$item['quantity'],2), 2);
|
|
$addStapleFood['total_quantity'] += $item['quantity'];
|
|
$addStapleFood['image_list'][] = $imageList[$imageId]['url'] ?? '';
|
|
$addStapleFood['title'] = $skuInfo['title'] ?? '';
|
|
$addStapleFood['unit_price'] = $item['unit_price'];
|
|
$breakFlag = 1;
|
|
continue;
|
|
}
|
|
|
|
$oneCopiesInfo['total_price'] = bcadd($oneCopiesInfo['total_price'], bcmul($item['unit_price'],(string)$item['quantity'],2), 2);
|
|
$oneCopiesInfo['total_quantity'] += $item['quantity'];
|
|
|
|
$oneCopiesInfo['image_list'][] = $imageList[$imageId]['url'] ?? [];
|
|
}
|
|
|
|
if ($breakFlag == 0) $orderCopiesList[] = $oneCopiesInfo;
|
|
}
|
|
|
|
if (!empty($addStapleFood) && $addStapleFood['total_quantity'] >= 0) $orderCopiesList[] = $addStapleFood;
|
|
$order['copies_list'] = $orderCopiesList;
|
|
$order['site'] = $this->siteCache->getSiteInfo((int)$order['site_id']) ?? [];
|
|
}
|
|
}
|
|
} |