You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/api/service/order/source/checkout/Groupon.php

111 lines
4.0 KiB

11 months ago
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\api\service\order\source\checkout;
use app\api\service\Order as OrderService;
use app\common\enum\order\OrderSource as OrderSourceEnum;
/**
* 订单结算台-拼团商品扩展类
* Class Groupon
* @package app\api\service\order\source\checkout
*/
class Groupon extends Basics
{
/**
* 验证商品列表
* @return bool
*/
public function validateGoodsList(): bool
{
// 验证商品是否下架
if (!$this->validateGoodsStatus()) {
return false;
}
// 判断商品库存
if (!$this->validateGoodsSeckillStock()) {
return false;
}
// 验证商品限购
if (!$this->validateLimitNum()) {
return false;
}
return true;
}
/**
* 判断商品是否下架
* @return bool
*/
private function validateGoodsStatus(): bool
{
foreach ($this->goodsList as $goods) {
if ($goods['is_delete'] || !$goods['status']) {
$this->error = "很抱歉,商品 [{$goods['goods_name']}] 已下架";
return false;
}
}
return true;
}
/**
* 判断商品库存数量
* @return bool
*/
private function validateGoodsSeckillStock(): bool
{
foreach ($this->goodsList as $goods) {
if ($goods['total_num'] > $goods['skuInfo']['stock_num']) {
$this->error = "很抱歉,商品 [{$goods['goods_name']}] 库存不足";
return false;
}
}
return true;
}
/**
* 验证商品限购
* @return bool
*/
public function validateLimitNum(): bool
{
foreach ($this->goodsList as $goods) {
// 不限购
if (!$goods['is_restrict']) return true;
// 拼团商品单次限购数量
if ($goods['total_num'] > $goods['restrict_single']) {
$this->error = "很抱歉,该商品限购{$goods['restrict_single']}件,请修改购买数量";
return false;
}
// 获取用户已下单的件数(未取消 订单来源)
$alreadyBuyNum = OrderService::getGoodsBuyNum($this->user['user_id'], $goods['goods_id'], OrderSourceEnum::GROUPON);
// 情况1: 已购买0件, 实际想购买5件
if ($alreadyBuyNum == 0 && $goods['total_num'] > $goods['restrict_total']) {
$this->error = "很抱歉,该商品限购{$goods['restrict_total']}件,请修改购买数量";
return false;
}
// 情况2: 已购买3件, 实际想购买1件
if ($alreadyBuyNum >= $goods['restrict_total']) {
$this->error = "很抱歉,该商品限购{$goods['restrict_total']}件,您当前已下单{$alreadyBuyNum}件,无法购买";
return false;
}
// 情况3: 已购买2件, 实际想购买2件
if (($alreadyBuyNum + $goods['total_num']) > $goods['restrict_total']) {
$diffNum = ($alreadyBuyNum + $goods['total_num']) - $goods['restrict_total'];
$this->error = "很抱歉,该商品限购{$goods['restrict_total']}件,您最多能再购买{$diffNum}件";
return false;
}
}
return true;
}
}