feat: place Order

This commit is contained in:
2025-02-06 14:40:10 +08:00
parent fda042da54
commit e7ec144539
5 changed files with 50 additions and 5 deletions

View File

@@ -4,11 +4,15 @@ namespace App\Constants;
class ConfigCode class ConfigCode
{ {
const string TODAY_CUT_OFF_TIME_KEY = 'TodayCutOffTime'; const string TODAY_CUT_OFF_TIME_KEY = 'TodayCutOffTime'; // 当日下单截止时间
const string ORDER_CANCEL_TIME_KEY = 'OrderCancelTime'; const string ORDER_CANCEL_TIME_KEY = 'OrderCancelTime'; // 订单取消时间(下单后自动取消)
const string SUNDRY_UNIT_PRICE = 'SundryUnitPrice'; // 附加费单价 (服务费)
const string SUNDRY_PRICE_COMPUTE_TYPE = 'SundryPriceComputeType'; // 附加费计算方式 1 仅自选计算(默认值) 2 仅套餐计算 3 套餐+自选计算
const array DEFAULT_VALUE = [ const array DEFAULT_VALUE = [
self::TODAY_CUT_OFF_TIME_KEY => '15:00:00', self::TODAY_CUT_OFF_TIME_KEY => '15:00:00',
self::ORDER_CANCEL_TIME_KEY => 5, self::ORDER_CANCEL_TIME_KEY => 5,
self::SUNDRY_UNIT_PRICE => '3',
self::SUNDRY_PRICE_COMPUTE_TYPE => 1,
]; ];
} }

View File

@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace App\Service\Api\Order; namespace App\Service\Api\Order;
use App\Cache\Redis\Common\ConfigCache;
use App\Cache\Redis\RedisCache; use App\Cache\Redis\RedisCache;
use App\Service\Api\BaseService; use App\Service\Api\BaseService;
use App\Service\ServiceTrait\Api\OrderTrait; use App\Service\ServiceTrait\Api\OrderTrait;
@@ -43,6 +44,12 @@ abstract class BaseOrderService extends BaseService
#[Inject] #[Inject]
protected RedisCache $redis; protected RedisCache $redis;
/**
* @var ConfigCache
*/
#[Inject]
protected ConfigCache $configCache;
/** /**
* @var array * @var array
*/ */
@@ -86,6 +93,11 @@ abstract class BaseOrderService extends BaseService
*/ */
protected int $orderId; protected int $orderId;
/**
* @var int 自动选择优惠券 (确认订单需要,下单不需要,用子类重置该值)
*/
protected int $isAutoSelectCoupon = 2;
/** /**
* 构造方法 * 构造方法
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
@@ -147,6 +159,8 @@ abstract class BaseOrderService extends BaseService
/** /**
* 计算 * 计算
* @return void * @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public function compute(): void public function compute(): void
{ {

View File

@@ -15,6 +15,13 @@ use Psr\Container\NotFoundExceptionInterface;
class ConfirmationOrderService extends BaseOrderService class ConfirmationOrderService extends BaseOrderService
{ {
public function __construct()
{
parent::__construct();
$this->isAutoSelectCoupon = 1;
}
/** /**
* @return array * @return array
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface

View File

@@ -27,6 +27,13 @@ use Psr\Container\NotFoundExceptionInterface;
class PlaceOrderService extends BaseOrderService class PlaceOrderService extends BaseOrderService
{ {
public function __construct()
{
parent::__construct();
$this->isAutoSelectCoupon = 2;
}
/** /**
* 统一下单逻辑 * 统一下单逻辑
* @return array * @return array

View File

@@ -15,6 +15,7 @@ use App\Cache\Redis\Api\SiteCache;
use App\Cache\Redis\RedisCache; use App\Cache\Redis\RedisCache;
use App\Constants\ApiCode; use App\Constants\ApiCode;
use App\Constants\Common\GoodCode; use App\Constants\Common\GoodCode;
use App\Constants\ConfigCode;
use App\Exception\ErrException; use App\Exception\ErrException;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
@@ -204,11 +205,19 @@ trait OrderTrait
/** /**
* 计算服务费 * 计算服务费
* @return void * @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
protected function computeSundryPrice(): void protected function computeSundryPrice(): void
{ {
$this->orderRes['sundry_num'] = $this->orderRes['optional_copies']; $this->orderRes['sundry_num'] = match ($this->configCache->getConfigValue(ConfigCode::SUNDRY_PRICE_COMPUTE_TYPE))
$this->orderRes['sundry_price'] = '1.00'; //todo 设置 {
1 => $this->orderRes['optional_copies'],
2 => $this->orderRes['meal_copies'],
3 => $this->copies,
};
$this->orderRes['sundry_price'] = $this->configCache->getConfigValue(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'],$this->orderRes['sundry_num'],2);
} }
@@ -218,11 +227,15 @@ trait OrderTrait
*/ */
protected function computeFavorable(): void protected function computeFavorable(): void
{ {
if ($this->couponId <= 0) { if ($this->couponId <= 0 && $this->isAutoSelectCoupon == 1) {
$this->couponId = $this->getAutoCouponId(); $this->couponId = $this->getAutoCouponId();
} }
if ($this->couponId <= 0) { if ($this->couponId <= 0) {
$this->orderRes['coupon_id'] = 0;
$this->orderRes['coupon'] = [];
$this->orderRes['favorable_good_price'] = '0';
$this->orderRes['favorable_sundry_price'] = '0';
return; return;
} }