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.

59 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\integral\service;
use app\integral\model\IntegralProduct;
class Product extends Base
{
/**
* 判断库存是否充足
* @param int $product_id 产品ID
* @param int $product_sku 产品SKU
* @param int $number 数量
* @date 2022-11-15
*/
public function isStockEnough($product_id, $sku = '', $number = 1)
{
$product_model = new IntegralProduct();
// 获取商品详情
$product = $product_model->getProduct([
['id', '=', $product_id]
],'id,stock,is_publish,is_spec_open');
if (empty($product)) {
return sendErrorArray(2001, '商品不存在');
}
if ($product['is_publish'] == 0) {
return sendErrorArray(2002, '商品已下架');
}
// 开启规格时没有SKU抛出异常
if ($product['is_spec_open'] == 1) {
if (empty($sku)) {
return sendErrorArray(2003, 'sku参数错误');
}
}
// 没有规格时,购买件数超过总库存
if ($product['is_spec_open'] == 0) {
if ($number > $product['stock']) {
return sendErrorArray(2004, '商品加购件数(含已加购件数)超过库存');
}
} else {
foreach ($product['integralProductSku'] as $k => $v) {
if ($v['sku'] == $sku) {
if ($number > $v['stock']) {
return sendErrorArray(2005, '商品加购件数(含已加购件数)超过库存');
}
}
}
}
return sendSuccessArray();
}
}