feat : order

This commit is contained in:
2025-03-06 16:25:25 +08:00
parent 5ad53e55be
commit 88fdd218b3
10 changed files with 82 additions and 57 deletions

View File

@@ -60,4 +60,10 @@ class OrderCode
*/
const string ORDER_TYPE_GOOD_PREFIX = 'OG';
const string ORDER_TYPE_BALANCE_PREFIX = 'OB';
/**
* @var int 1=自选 2=套餐
*/
CONST INT ORDER_TYPE_OPTIONAL = 1;
CONST INT ORDER_TYPE_MEAL = 2;
}

View File

@@ -10,7 +10,7 @@ class ConfigCode
const string TODAY_CUT_OFF_TIME_KEY = 'TodayCutOffTime'; // 当日下单截止时间
const string ORDER_CANCEL_TIME_KEY = 'OrderCancelTime'; // 订单取消时间(下单后自动取消)
const string SUNDRY_UNIT_PRICE = 'SundryUnitPrice'; // 附加费单价 (服务费)
const string SUNDRY_PRICE_COMPUTE_TYPE = 'SundryPriceComputeType'; // 附加费计算方式 1 仅自选计算(默认值) 2 仅套餐计算 3 套餐+自选计算
const string SUNDRY_PRICE_COMPUTE_TYPE = 'SundryPriceComputeType'; // 附加费计算方式 1 仅自选计算(默认值) 2 仅套餐计算 3 套餐自选计算
const string NUMBER_OF_USER_ADDRESS_POOLS = 'NumberOfUserAddressPools'; // 用户可以设置的送达地址的数量
/**

View File

@@ -17,8 +17,8 @@ use Hyperf\DbConnection\Model\Model;
* @property int $site_id
* @property int $city_id
* @property int $coupon_id
* @property int $meal_copies
* @property int $optional_copies
* @property int $copies
* @property int $type
* @property string $total_price
* @property string $actual_price
* @property string $discount_price

View File

@@ -112,6 +112,11 @@ abstract class BaseOrderService extends BaseService
*/
protected int $orderId;
/**
* @var int 订单类型 (参考 SpuCode)
*/
protected int $orderType;
/**
* @var int 自动选择优惠券 (确认订单需要,下单不需要,用子类重置该值)
*/
@@ -133,6 +138,7 @@ abstract class BaseOrderService extends BaseService
$this->copies = 0;
$this->couponId = 0;
$this->siteId = 0;
$this->orderType = 0;
$this->orderRes = [
'total_price' => '0.00', //总价
@@ -151,8 +157,8 @@ abstract class BaseOrderService extends BaseService
'site' => [],
'good_ids' => [],
'good' => [],
'optional_copies' => 0,
'meal_copies' => 0,
'copies' => 0,
// 'meal_copies' => 0,
];
}
@@ -188,7 +194,7 @@ abstract class BaseOrderService extends BaseService
$this->computeSundryPrice();
$this->computeFavorable();
// $this->computeFavorable();
$this->computeFinallyPrice();
}

View File

@@ -23,6 +23,7 @@ class CheckCartService extends BaseOrderService
public function handle(): array
{
$this->siteId = (int)$this->request->input('site_id');
$this->orderType = (int)$this->request->input('order_type');
$this->check();

View File

@@ -32,6 +32,7 @@ class ConfirmationOrderService extends BaseOrderService
{
$this->siteId = (int)$this->request->input('site_id');
$this->couponId = (int)$this->request->input('coupon_id',0);
$this->orderType = (int)$this->request->input('order_type');
$this->check();

View File

@@ -107,7 +107,7 @@ class OrderInfoService extends BaseService
$imageList = $this->getOssObjects($imageIds);
$orderCopiesList = [];
for ($i = 1; $i <= ($orderInfo['meal_copies'] + $orderInfo['optional_copies']); $i++) {
for ($i = 1; $i <= ($orderInfo['copies'] ?? 0); $i++) {
$oneCopiesInfo = [
'total_price' => '0.00',
@@ -115,7 +115,7 @@ class OrderInfoService extends BaseService
'sku_list' => [],
'image_list' => [],
'take_food_code' => '', //todo 取餐码
'copies_type' => 0,
// 'copies_type' => 0,
];
foreach ($newOrderSkuList[$i] as $item) {
@@ -128,7 +128,7 @@ class OrderInfoService extends BaseService
$oneCopiesInfo['sku_list'][] = $skuInfo;
$oneCopiesInfo['image_list'][] = $imageList[$imageId] ?? [];
if ($oneCopiesInfo['copies_type'] == 0) $oneCopiesInfo['copies_type'] = $item['type'];
// if ($oneCopiesInfo['copies_type'] == 0) $oneCopiesInfo['copies_type'] = $item['type'];
}
// foreach ($newOrderSkuList as $item) {
// if ($item['order_id'] != $order['id'] || $item['copies'] != $i) continue;

View File

@@ -64,8 +64,8 @@ class OrderListService extends BaseService
'order_sno',
'user_id',
'site_id',
'meal_copies',
'optional_copies',
'copies',
'type',
'actual_price',
'total_price',
'status',
@@ -91,7 +91,6 @@ class OrderListService extends BaseService
private function buildData(array &$orderList): void
{
$orderIds = array_column($orderList, 'id');
$siteIds = array_column($orderList, 'site_id');
$orderSkuList = $this->orderGoodModel->whereIn('order_id', $orderIds)->get()->toArray();
$skuIds = [];
@@ -123,7 +122,7 @@ class OrderListService extends BaseService
foreach ($orderList as &$order) {
$orderCopiesList = [];
for ($i = 1; $i <= ($order['meal_copies'] + $order['optional_copies']); $i++) {
for ($i = 1; $i <= ($order['copies'] ?? 0); $i++) {
$oneCopiesInfo = [
'total_price' => '0.00',
@@ -131,7 +130,7 @@ class OrderListService extends BaseService
// 'sku_list' => [],
'image_list' => [],
'take_food_code' => [], //todo 取餐码
'copies_type' => 0,
// 'copies_type' => 0,
];
foreach ($newOrderSkuList[$order['id']][$i] as $item) {
@@ -144,7 +143,7 @@ class OrderListService extends BaseService
// $oneCopiesInfo['sku_list'][] = [];
$oneCopiesInfo['image_list'][] = $imageList[$imageId]['url'] ?? [];
if ($oneCopiesInfo['copies_type'] == 0) $oneCopiesInfo['copies_type'] = $item['type'];
// if ($oneCopiesInfo['copies_type'] == 0) $oneCopiesInfo['copies_type'] = $item['type'];
}
// foreach ($newOrderSkuList as $item) {
// if ($item['order_id'] != $order['id'] || $item['copies'] != $i) continue;

View File

@@ -53,6 +53,7 @@ class PlaceOrderService extends BaseOrderService
$this->siteId = (int)$this->request->input('site_id');
$this->couponId = (int)$this->request->input('coupon_id',0);
$this->orderType = (int)$this->request->input('order_type');
// 生成购物车信息 检测商品和库存等
$this->check();
@@ -191,8 +192,8 @@ class PlaceOrderService extends BaseOrderService
$orderInsertModel->site_id = $this->siteId;
$orderInsertModel->city_id = $this->orderRes['site_info']['city_id'];
$orderInsertModel->coupon_id = $this->couponId;
$orderInsertModel->meal_copies = $this->orderRes['meal_copies'];
$orderInsertModel->optional_copies = $this->orderRes['optional_copies'];
$orderInsertModel->copies = $this->orderRes['copies'];
$orderInsertModel->type = $this->orderType;
$orderInsertModel->total_price = $this->orderRes['total_price'];
$orderInsertModel->actual_price = $this->orderRes['actual_price'];
$orderInsertModel->discount_price = $this->orderRes['favorable_sundry_price'] + $this->orderRes['favorable_good_price'];

View File

@@ -102,8 +102,10 @@ trait OrderTrait
protected function checkGood(): void
{
foreach ($this->cartFirstData as $key => $one) {
if (in_array($key, $this->goodIds)) continue;
if ($this->orderType == 0) $this->orderType = $this->skuArr[$key]['type'];
if ($this->skuArr[$key]['type'] != $this->orderType) throw new ErrException('自选菜品跟套餐菜品请分开订单下单');
if (in_array($key, $this->goodIds)) continue;
throw new ErrException('商品不存在',ApiCode::ORDER_GOOD_IN_EXISTENCE);
}
}
@@ -154,42 +156,51 @@ trait OrderTrait
protected function getGoodInfo(int $kitchenId): void
{
$this->stockKey = ApiRedisKey::goodStockKey($this->cycleId,$kitchenId);
$mealGoodKey = ApiRedisKey::mealGoodListKey($this->cycleId,$kitchenId);
$optionalGoodKey = ApiRedisKey::optionalGoodListKey($this->cycleId,$kitchenId);
$mealGood = $this->redis->get($mealGoodKey);
$optionalGood = $this->redis->get($optionalGoodKey);
if (empty($mealGood) || empty($optionalGood)) throw new ErrException('商品不存在');
if (!$this->redis->exists($this->stockKey)) throw new ErrException('库存不足');
$mealGood = json_decode($mealGood, true);
$optionalGood = json_decode($optionalGood, true);
$skuArr = [];
$skuImageArr = [];
switch ($this->orderType) {
case OrderCode::ORDER_TYPE_OPTIONAL:
$optionalGoodKey = ApiRedisKey::optionalGoodListKey($this->cycleId,$kitchenId);
$optionalGood = $this->redis->get($optionalGoodKey);
if (empty($optionalGood)) throw new ErrException('商品不存在');
foreach ($mealGood as $one){
$newSkuList = array_map(function($sku) use($one) {
$sku['spu_title'] = $one['title'];
$sku['type'] = GoodCode::SPU_TYPE_MEAL;
return $sku;
}, $one['sku_list']);
$optionalGood = json_decode($optionalGood, true);
$skuImageArr = array_merge($skuImageArr,$one['image_list']);
$skuArr = array_merge($skuArr,$newSkuList);
}
foreach ($optionalGood as $one){
$newSkuList = array_map(function($sku) use($one) {
$sku['spu_title'] = strtolower($one['title']);
$sku['type'] = GoodCode::SPU_TYPE_OPTIONAL;
return $sku;
}, $one['sku_list']);
foreach ($optionalGood as $one){
$newSkuList = array_map(function($sku) use($one) {
$sku['spu_title'] = strtolower($one['title']);
$sku['type'] = GoodCode::SPU_TYPE_OPTIONAL;
return $sku;
}, $one['sku_list']);
$skuImageArr = array_merge($skuImageArr,$one['image_list']);
$skuArr = array_merge($skuArr,$newSkuList);
}
$skuImageArr = array_merge($skuImageArr,$one['image_list']);
$skuArr = array_merge($skuArr,$newSkuList);
break;
case OrderCode::ORDER_TYPE_MEAL:
$mealGoodKey = ApiRedisKey::mealGoodListKey($this->cycleId,$kitchenId);
$mealGood = $this->redis->get($mealGoodKey);
if (empty($mealGood)) throw new ErrException('商品不存在');
$mealGood = json_decode($mealGood, true);
foreach ($mealGood as $one){
$newSkuList = array_map(function($sku) use($one) {
$sku['spu_title'] = $one['title'];
$sku['type'] = GoodCode::SPU_TYPE_MEAL;
return $sku;
}, $one['sku_list']);
$skuImageArr = array_merge($skuImageArr,$one['image_list']);
$skuArr = array_merge($skuArr,$newSkuList);
}
break;
default:
throw new ErrException('还不支持下单');
}
$this->skuArr = array_column($skuArr,null,'id');
@@ -217,7 +228,7 @@ trait OrderTrait
foreach ($this->cartSecondData as $oneCopies) {
$oneCopiesTotalPrice = '0.00';
$copiesType = 1;
// $copiesType = 1;
$oneCopiesGoodInfo = [];
@@ -238,9 +249,9 @@ trait OrderTrait
$oneCopiesTotalPrice = bcadd($oneCopiesTotalPrice, bcmul($this->skuArr[$key]['price'],(string)$oneGood,2),2);
if ($copiesType == 1 && $this->skuArr[$key]['type'] == GoodCode::SPU_TYPE_MEAL) {
$copiesType = 2;
}
// if ($copiesType == 1 && $this->skuArr[$key]['type'] == GoodCode::SPU_TYPE_MEAL) {
// $copiesType = 2;
// }
}
$this->orderRes['good'][] = [
@@ -248,11 +259,11 @@ trait OrderTrait
'price' => $oneCopiesTotalPrice,
];
if ($copiesType == 1) {
$this->orderRes['optional_copies']++;
} else {
$this->orderRes['meal_copies']++;
}
// if ($copiesType == 1) {
// $this->orderRes['optional_copies']++;
// } else {
$this->orderRes['copies']++;
// }
$this->orderRes['total_good_price'] = bcadd($this->orderRes['total_good_price'],$oneCopiesTotalPrice,2);
}
@@ -268,8 +279,8 @@ trait OrderTrait
{
$this->orderRes['sundry_num'] = match ($this->configCache->getConfigValueByKey(ConfigCode::SUNDRY_PRICE_COMPUTE_TYPE))
{
1 => $this->orderRes['optional_copies'],
2 => $this->orderRes['meal_copies'],
1 => $this->orderType == OrderCode::ORDER_TYPE_OPTIONAL ? $this->copies : 0,
2 => $this->orderType == OrderCode::ORDER_TYPE_MEAL ? $this->copies : 0,
3 => $this->copies,
};