feat : order

This commit is contained in:
2025-03-04 15:41:51 +08:00
parent 319f0c03d4
commit 11b4496fa7
7 changed files with 69 additions and 29 deletions

View File

@@ -64,7 +64,7 @@ class ConfigCache
* @param $key * @param $key
* @param int $isRedis * @param int $isRedis
* @param int $expire * @param int $expire
* @return false|HigherOrderTapProxy|mixed|\Redis|string|null * @return false|HigherOrderTapProxy|mixed|\Redis|string
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
*/ */
@@ -77,10 +77,10 @@ class ConfigCache
if ($isRedis == 1) { if ($isRedis == 1) {
$this->redis->setEx($redisKey, $value, $expire,RedisCode::SYSTEM_DB); $this->redis->setEx($redisKey, $value, $expire,RedisCode::SYSTEM_DB);
} }
return $value; return $value ?? ConfigCode::DEFAULT_VALUE[$key];
} }
return $this->redis->get($key,RedisCode::SYSTEM_DB) ?? ConfigCode::DEFAULT_VALUE[$key]; return $this->redis->get($redisKey,RedisCode::SYSTEM_DB) ?? ConfigCode::DEFAULT_VALUE[$key];
} }
/** /**

View File

@@ -542,6 +542,20 @@ class RedisCache
return $this->getRedis($poolName)->zRem($key, $value); return $this->getRedis($poolName)->zRem($key, $value);
} }
/**
* @param $key
* @param $score
* @param $value
* @param string $poolName
* @return Redis|float|false
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function zIncrBy($key,$score, $value, string $poolName = RedisCode::DEFAULT_DB): Redis|float|false
{
return $this->getRedis($poolName)->zIncrBy($key, $score, $value);
}
/** /**
* @param $key * @param $key
* @param $value * @param $value

View File

@@ -219,8 +219,8 @@ abstract class LoginBaseService extends BaseService
*/ */
protected function addCoupon(): void protected function addCoupon(): void
{ {
$couponTemplateId = $this->configCache->getConfigValue(ConfigCode::COUPONS_FOR_NEWCOMERS); $couponTemplateId = $this->configCache->getConfigValueByKey(ConfigCode::COUPONS_FOR_NEWCOMERS);
$couponValidity = $this->configCache->getConfigValue(ConfigCode::NEWBIE_COUPON_VALIDITY); $couponValidity = $this->configCache->getConfigValueByKey(ConfigCode::NEWBIE_COUPON_VALIDITY);
// 随便一个为0代表不赠送 // 随便一个为0代表不赠送
if ($couponValidity == 0 || $couponTemplateId == 0) return; if ($couponValidity == 0 || $couponTemplateId == 0) return;

View File

@@ -70,6 +70,11 @@ abstract class BaseOrderService extends BaseService
*/ */
protected array $skuArr; protected array $skuArr;
/**
* @var array
*/
protected array $skuImageArr;
/** /**
* 订单结果 * 订单结果
* @var array * @var array

View File

@@ -67,7 +67,7 @@ class PlaceOrderService extends BaseOrderService
$this->joinCancelDelayQueue( $this->joinCancelDelayQueue(
$this->orderId, $this->orderId,
OrderCode::ORDER_TYPE_GOOD, OrderCode::ORDER_TYPE_GOOD,
(int)$this->configCache->getConfigValue(ConfigCode::ORDER_CANCEL_TIME_KEY) * DateUtil::MINUTE * DateUtil::MS (int)$this->configCache->getConfigValueByKey(ConfigCode::ORDER_CANCEL_TIME_KEY) * DateUtil::MINUTE * DateUtil::MS
); );
$this->sendStockMq($this->orderId,OrderCode::WAIT_PAY); $this->sendStockMq($this->orderId,OrderCode::WAIT_PAY);
@@ -84,10 +84,11 @@ class PlaceOrderService extends BaseOrderService
private function reduceStock(): void private function reduceStock(): void
{ {
$this->rollbackStockCache = []; $this->rollbackStockCache = [];
// var_dump($this->cartFirstData);
foreach ($this->cartFirstData as $goodId => $stock) { foreach ($this->cartFirstData as $goodId => $stock) {
$this->rollbackStockCache[$goodId] = $stock; $this->rollbackStockCache[$goodId] = $stock;
if (!($this->redis->zAdd($this->stockKey,'-'.$stock,$goodId))) { if (!($this->redis->zIncrBy($this->stockKey,-$stock,$goodId))) {
echo '123';
throw new Exception('cache error'); throw new Exception('cache error');
} }
} }
@@ -103,7 +104,7 @@ class PlaceOrderService extends BaseOrderService
if (empty($this->rollbackStockCache)) return; if (empty($this->rollbackStockCache)) return;
foreach ($this->rollbackStockCache as $goodId => $stock) { foreach ($this->rollbackStockCache as $goodId => $stock) {
$this->redis->zAdd($this->stockKey,$stock,$goodId); $this->redis->zIncrBy($this->stockKey,$stock,$goodId);
} }
} }
@@ -126,6 +127,7 @@ class PlaceOrderService extends BaseOrderService
Db::commit(); Db::commit();
} catch (Exception $e){ } catch (Exception $e){
echo $e->getMessage();
//回滚数据库 和 缓存 //回滚数据库 和 缓存
Db::rollBack(); Db::rollBack();
$this->rollbackStock(); $this->rollbackStock();
@@ -181,7 +183,7 @@ class PlaceOrderService extends BaseOrderService
$orderInsertModel->user_id = $this->userId; $orderInsertModel->user_id = $this->userId;
$orderInsertModel->cycle_id = $this->cycleId; $orderInsertModel->cycle_id = $this->cycleId;
$orderInsertModel->site_id = $this->siteId; $orderInsertModel->site_id = $this->siteId;
$orderInsertModel->city_id = $this->orderRes['site']['city_id']; $orderInsertModel->city_id = $this->orderRes['site_info']['city_id'];
$orderInsertModel->coupon_id = $this->couponId; $orderInsertModel->coupon_id = $this->couponId;
$orderInsertModel->meal_copies = $this->orderRes['meal_copies']; $orderInsertModel->meal_copies = $this->orderRes['meal_copies'];
$orderInsertModel->optional_copies = $this->orderRes['optional_copies']; $orderInsertModel->optional_copies = $this->orderRes['optional_copies'];

View File

@@ -77,7 +77,7 @@ class SiteService extends BaseService
if (in_array($id, $siteIds)) throw new ErrException('已添加该地点'); if (in_array($id, $siteIds)) throw new ErrException('已添加该地点');
$poolNumber = $this->configCache->getConfigValue(ConfigCode::NUMBER_OF_USER_ADDRESS_POOLS); $poolNumber = $this->configCache->getConfigValueByKey(ConfigCode::NUMBER_OF_USER_ADDRESS_POOLS);
if (count($siteIds) >= $poolNumber) throw new ErrException('已达到地址最大添加数量,请删除后再添加'); if (count($siteIds) >= $poolNumber) throw new ErrException('已达到地址最大添加数量,请删除后再添加');
$insertModel = new UserSite(); $insertModel = new UserSite();

View File

@@ -167,15 +167,32 @@ trait OrderTrait
$optionalGood = json_decode($optionalGood, true); $optionalGood = json_decode($optionalGood, true);
$skuArr = []; $skuArr = [];
$skuImageArr = [];
foreach ($mealGood as $one){ foreach ($mealGood as $one){
$skuArr = array_merge($skuArr,$one['sku_list']); $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);
} }
foreach ($optionalGood as $one){ foreach ($optionalGood as $one){
$skuArr = array_merge($skuArr,$one['sku_list']); $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);
} }
$this->skuArr = array_column($skuArr,null,'id'); $this->skuArr = array_column($skuArr,null,'id');
$this->skuImageArr = array_column($skuArr,null,'id');
$this->goodIds = array_column($skuArr,'id'); $this->goodIds = array_column($skuArr,'id');
unset($skuArr); unset($skuArr);
} }
@@ -202,29 +219,31 @@ trait OrderTrait
$copiesType = 1; $copiesType = 1;
$oneCopiesGoodInfo = []; $oneCopiesGoodInfo = [];
foreach ($oneCopies as $oneGood) {
if (empty($oneCopiesGoodInfo[$oneGood])) { foreach ($oneCopies as $key => $oneGood) {
$oneCopiesGoodInfo[$oneGood] = [ if (empty($oneCopiesGoodInfo[$key])) {
'num' => 1, $oneCopiesGoodInfo[$key] = [
'good_name' => '1', 'num' => $oneGood,
'good_url' => '1', 'good_name' => $this->skuArr[$key]['spu_title'] ?? '' . $this->skuArr[$key]['title'] ?? '',
'unit_price' => $this->skuArr[$oneGood]['price'], 'good_url' => $this->skuImageArr[$this->skuArr[$key]['image_ids']]['url'] ?? '',
'type' => $this->skuArr[$oneGood]['type'], 'unit_price' => $this->skuArr[$key]['price'],
'type' => $this->skuArr[$key]['type'],
'id' => $key,
'spu_id' => $this->skuArr[$key]['spu_id'],
]; ];
} else { } else {
$oneCopiesGoodInfo[$oneGood]['num'] += 1; $oneCopiesGoodInfo[$key]['num'] += $oneGood;
} }
$oneCopiesTotalPrice = bcadd($oneCopiesTotalPrice, $this->skuArr[$oneGood]['price'],2); $oneCopiesTotalPrice = bcadd($oneCopiesTotalPrice, bcmul($this->skuArr[$key]['price'],(string)$oneGood,2),2);
if ($copiesType == 1 && $this->skuArr[$oneGood]['type'] == GoodCode::SPU_TYPE_MEAL) { if ($copiesType == 1 && $this->skuArr[$key]['type'] == GoodCode::SPU_TYPE_MEAL) {
$copiesType = 2; $copiesType = 2;
} }
} }
$this->orderRes['good'][] = [ $this->orderRes['good'][] = [
'good_ids' => $oneCopies, 'good_info' => array_values($oneCopiesGoodInfo),
'good_info' => $oneCopiesGoodInfo,
'price' => $oneCopiesTotalPrice, 'price' => $oneCopiesTotalPrice,
]; ];
@@ -246,15 +265,15 @@ trait OrderTrait
*/ */
protected function computeSundryPrice(): void protected function computeSundryPrice(): void
{ {
$this->orderRes['sundry_num'] = match ($this->configCache->getConfigValue(ConfigCode::SUNDRY_PRICE_COMPUTE_TYPE)) $this->orderRes['sundry_num'] = match ($this->configCache->getConfigValueByKey(ConfigCode::SUNDRY_PRICE_COMPUTE_TYPE))
{ {
1 => $this->orderRes['optional_copies'], 1 => $this->orderRes['optional_copies'],
2 => $this->orderRes['meal_copies'], 2 => $this->orderRes['meal_copies'],
3 => $this->copies, 3 => $this->copies,
}; };
$this->orderRes['sundry_price'] = $this->configCache->getConfigValue(ConfigCode::SUNDRY_UNIT_PRICE); $this->orderRes['sundry_price'] = $this->configCache->getConfigValueByKey(ConfigCode::SUNDRY_UNIT_PRICE);
$this->orderRes['total_sundry_price'] = bcmul($this->orderRes['sundry_price'],$this->orderRes['sundry_num'],2); $this->orderRes['total_sundry_price'] = bcmul($this->orderRes['sundry_price'],(string)$this->orderRes['sundry_num'],2);
} }
/** /**