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/Bargain.php

90 lines
3.4 KiB

<?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 Bargain
* @package app\api\service\order\source\checkout
*/
class Bargain extends Basics
{
/**
* 验证商品列表
* @return bool
*/
public function validateGoodsList(): bool
{
// 验证商品是否下架
// mix: 此处不验证砍价商品是否下架, 因为砍价任务完成才允许下单
// 判断商品库存
if (!$this->validateGoodsSeckillStock()) {
return false;
}
// 验证商品限购
if (!$this->validateLimitNum()) {
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;
// 获取用户已下单的件数(未取消 订单来源)
$alreadyBuyNum = OrderService::getGoodsBuyNum($this->user['user_id'], $goods['goods_id'], OrderSourceEnum::BARGAIN);
// 情况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;
}
}