diff --git a/app/Constants/ConfigCode.php b/app/Constants/ConfigCode.php index 020a15e..97950bb 100644 --- a/app/Constants/ConfigCode.php +++ b/app/Constants/ConfigCode.php @@ -4,11 +4,15 @@ namespace App\Constants; class ConfigCode { - const string TODAY_CUT_OFF_TIME_KEY = 'TodayCutOffTime'; - const string ORDER_CANCEL_TIME_KEY = 'OrderCancelTime'; + 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 array DEFAULT_VALUE = [ self::TODAY_CUT_OFF_TIME_KEY => '15:00:00', self::ORDER_CANCEL_TIME_KEY => 5, + self::SUNDRY_UNIT_PRICE => '3', + self::SUNDRY_PRICE_COMPUTE_TYPE => 1, ]; } \ No newline at end of file diff --git a/app/Service/Api/Order/BaseOrderService.php b/app/Service/Api/Order/BaseOrderService.php index 3abf547..f6aadf9 100644 --- a/app/Service/Api/Order/BaseOrderService.php +++ b/app/Service/Api/Order/BaseOrderService.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace App\Service\Api\Order; +use App\Cache\Redis\Common\ConfigCache; use App\Cache\Redis\RedisCache; use App\Service\Api\BaseService; use App\Service\ServiceTrait\Api\OrderTrait; @@ -43,6 +44,12 @@ abstract class BaseOrderService extends BaseService #[Inject] protected RedisCache $redis; + /** + * @var ConfigCache + */ + #[Inject] + protected ConfigCache $configCache; + /** * @var array */ @@ -86,6 +93,11 @@ abstract class BaseOrderService extends BaseService */ protected int $orderId; + /** + * @var int 自动选择优惠券 (确认订单需要,下单不需要,用子类重置该值) + */ + protected int $isAutoSelectCoupon = 2; + /** * 构造方法 * @throws ContainerExceptionInterface @@ -147,6 +159,8 @@ abstract class BaseOrderService extends BaseService /** * 计算 * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function compute(): void { diff --git a/app/Service/Api/Order/ConfirmationOrderService.php b/app/Service/Api/Order/ConfirmationOrderService.php index 5c31944..66a4510 100644 --- a/app/Service/Api/Order/ConfirmationOrderService.php +++ b/app/Service/Api/Order/ConfirmationOrderService.php @@ -15,6 +15,13 @@ use Psr\Container\NotFoundExceptionInterface; class ConfirmationOrderService extends BaseOrderService { + + public function __construct() + { + parent::__construct(); + $this->isAutoSelectCoupon = 1; + } + /** * @return array * @throws ContainerExceptionInterface diff --git a/app/Service/Api/Order/PlaceOrderService.php b/app/Service/Api/Order/PlaceOrderService.php index d67384c..a0cf322 100644 --- a/app/Service/Api/Order/PlaceOrderService.php +++ b/app/Service/Api/Order/PlaceOrderService.php @@ -27,6 +27,13 @@ use Psr\Container\NotFoundExceptionInterface; class PlaceOrderService extends BaseOrderService { + public function __construct() + { + parent::__construct(); + $this->isAutoSelectCoupon = 2; + } + + /** * 统一下单逻辑 * @return array diff --git a/app/Service/ServiceTrait/Api/OrderTrait.php b/app/Service/ServiceTrait/Api/OrderTrait.php index 84c2e5d..2dabcd9 100644 --- a/app/Service/ServiceTrait/Api/OrderTrait.php +++ b/app/Service/ServiceTrait/Api/OrderTrait.php @@ -15,6 +15,7 @@ use App\Cache\Redis\Api\SiteCache; use App\Cache\Redis\RedisCache; use App\Constants\ApiCode; use App\Constants\Common\GoodCode; +use App\Constants\ConfigCode; use App\Exception\ErrException; use Hyperf\Di\Annotation\Inject; use Psr\Container\ContainerExceptionInterface; @@ -204,11 +205,19 @@ trait OrderTrait /** * 计算服务费 * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ protected function computeSundryPrice(): void { - $this->orderRes['sundry_num'] = $this->orderRes['optional_copies']; - $this->orderRes['sundry_price'] = '1.00'; //todo 设置 + $this->orderRes['sundry_num'] = match ($this->configCache->getConfigValue(ConfigCode::SUNDRY_PRICE_COMPUTE_TYPE)) + { + 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); } @@ -218,11 +227,15 @@ trait OrderTrait */ protected function computeFavorable(): void { - if ($this->couponId <= 0) { + if ($this->couponId <= 0 && $this->isAutoSelectCoupon == 1) { $this->couponId = $this->getAutoCouponId(); } 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; }