作者 jinglong
1 个管道 的构建 通过 耗费 1 秒

修改库存不足

... ... @@ -51,12 +51,12 @@ class Cars extends Api
//检测库存
$stock_res = Common::findSoftWhereData('goods',['id'=>$data['goods_id']],'id,stock,style g_style');
if($stock_res){
//查看规格所属索引
$s_index = array_search($data['style'],Common::salePrice(trim($stock_res['g_style'])));
$stock = Common::salePrice($stock_res['stock'])[$s_index];
if(($stock <=> $data['goods_number']) == -1){
//检测库存
$check_stock = Common::checkStock($data['style'],$data['goods_number'],$stock_res['g_style'],$stock_res['stock']);
if(!$check_stock){
$this->error('库存不足');
}
}
//查询有无记录
$where = [
... ... @@ -130,13 +130,13 @@ class Cars extends Api
foreach ($res as &$value){
$value['image'] = $this->auth->absolutionUrlOne($value['image']);
$total_price += $value['price'] * $value['goods_number'];
//查看规格所属索引
$s_index = array_search($value['style'],Common::salePrice($value['g_style']));
$value['stock'] = Common::salePrice($value['stock'])[$s_index];
if(($value['stock'] <=> $value['goods_number']) == -1){
//购物车数量大于库存
//检测库存
$check_stock = Common::checkStock($value['style'],$value['goods_number'],$value['g_style'],$value['stock']);
if(!$check_stock){
$value['goods_number'] = $value['stock'];
}
}
// $total_price = array_sum(array_map(function ($product_row) {
// return $product_row['price'] * $product_row['goods_number'];
... ... @@ -324,10 +324,8 @@ class Cars extends Api
$value['price'] = $g_value['price'];//商品价格
//检测库存
//查看规格所属索引
$s_index = array_search($g_value['style'],Common::salePrice($value['g_style']));
$stock = Common::salePrice($value['stock'])[$s_index];
if(($stock <=> $g_value['goods_number']) == -1){
$check_stock = Common::checkStock($g_value['style'],$g_value['goods_number'],$value['g_style'],$value['stock']);
if(!$check_stock){
$this->error('库存不足');
}
$value['goods_number'] = $g_value['goods_number'];//商品数量
... ...
... ... @@ -402,6 +402,24 @@ class Common
}
/**
* @param $style 传递规格
* @param $goods_number 传递数量
* @param $g_style 数据库商品规格
* @param $g_stock 数据库商品库存
* @ApiInternal
* @return bool
*/
public static function checkStock($style,$goods_number,$g_style,$g_stock){
$s_index = array_search($style,self::salePrice($g_style));
$stock = self::salePrice($g_stock)[$s_index];
if(($stock <=> $goods_number) == -1){
//库存不足
return false;
}
return true;
}
/**
* 合并数组
* @ApiInternal
*/
... ...