库存扣除优化

This commit is contained in:
devil 2020-08-05 23:03:43 +08:00
parent fdf4c9fb5d
commit 4848be3633
4 changed files with 169 additions and 64 deletions

View File

@ -77,7 +77,7 @@ class Cart extends Common
$this->error('非法访问');
}
$params = $_POST;
$params = $this->data_post;
$params['user'] = $this->user;
return BuyService::CartSave($params);
}
@ -98,7 +98,7 @@ class Cart extends Common
return $this->error('非法访问');
}
$params = $_POST;
$params = $this->data_post;
$params['user'] = $this->user;
return BuyService::CartDelete($params);
}
@ -119,7 +119,7 @@ class Cart extends Common
$this->error('非法访问');
}
$params = $_POST;
$params = $this->data_post;
$params['user'] = $this->user;
return BuyService::CartStock($params);
}

View File

@ -102,17 +102,29 @@ class BuyService
return $goods_base;
}
// 获取商品规格图片
// 是否存在规格
if(!empty($spec))
{
// 获取商品规格图片
$images = self::BuyGoodsSpecImages($goods_id, $spec);
if(!empty($images))
{
$goods['images'] = $images;
$goods['images_old'] = ResourcesService::AttachmentPathViewHandle($images);
}
// 从规格获取库存
$base = GoodsService::GoodsSpecDetail(['id'=>$goods_id, 'spec'=>$spec]);
if($base['code'] == 0)
{
// 规格库存赋值
$goods['inventory'] = $base['data']['spec_base']['inventory'];
} else {
return $base;
}
}
// 数量
$stock = ($goods['buy_max_number'] > 0 && $params['stock'] > $goods['buy_max_number']) ? $goods['buy_max_number'] : $params['stock'];
@ -1477,7 +1489,7 @@ class BuyService
}
/**
* 订单支付前校验
* 单个订单支付前校验
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
@ -1485,7 +1497,7 @@ class BuyService
* @desc description
* @param [array] $params [输入参数]
*/
public static function OrderPayBeginCheck($params = [])
public static function SingleOrderPayBeginCheck($params = [])
{
// 请求参数
$p = [
@ -1494,16 +1506,6 @@ class BuyService
'key_name' => 'order_id',
'error_msg' => '订单id有误',
],
[
'checked_type' => 'empty',
'key_name' => 'order_data',
'error_msg' => '订单更新数据不能为空',
],
[
'checked_type' => 'is_array',
'key_name' => 'order_data',
'error_msg' => '订单更新数据有误',
]
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
@ -1512,8 +1514,7 @@ class BuyService
}
// 是否扣除库存
$common_is_deduction_inventory = MyC('common_is_deduction_inventory', 0);
if($common_is_deduction_inventory != 1)
if(MyC('common_is_deduction_inventory', 0) != 1)
{
return DataReturn('未开启扣除库存', 0);
}
@ -1524,42 +1525,10 @@ class BuyService
{
foreach($order_detail as $v)
{
// 获取商品
$goods = Db::name('Goods')->field('is_shelves,is_deduction_inventory,inventory,title')->find($v['goods_id']);
if(empty($goods))
$ret = self::BuyOrderPayBeginGoodsCheck($v);
if($ret['code'] != 0)
{
return DataReturn('商品不存在', -10);
}
// 商品状态
if($goods['is_shelves'] != 1)
{
return DataReturn('商品已下架['.$goods['title'].']', -10);
}
// 库存
if(isset($goods['is_deduction_inventory']) && $goods['is_deduction_inventory'] == 1)
{
// 先判断商品库存是否不足
if($goods['inventory'] < $v['buy_number'])
{
return DataReturn('库存不足['.$goods['title'].'('.$goods['inventory'].'<'.$v['buy_number'].')]', -10);
}
// 规格库存
$spec = empty($v['spec']) ? '' : json_decode($v['spec'], true);
$base = GoodsService::GoodsSpecDetail(['id'=>$v['goods_id'], 'spec'=>$spec]);
if($base['code'] == 0)
{
// 先判断商品规格库存是否不足
$inventory = Db::name('GoodsSpecBase')->where(['id'=>$base['data']['spec_base']['id'], 'goods_id'=>$v['goods_id']])->value('inventory');
if($inventory < $v['buy_number'])
{
return DataReturn('库存不足['.$goods['title'].'('.$inventory.'<'.$v['buy_number'].')]', -10);
}
} else {
return $base;
}
return $ret;
}
}
return DataReturn('校验成功', 0);
@ -1567,6 +1536,138 @@ class BuyService
return DataReturn('没有需要扣除库存的数据', 0);
}
/**
* 多个订单下单库存校验
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-08-04
* @desc description
* @param [array] $params [输入参数]
*/
public static function MoreOrderPayBeginCheck($params = [])
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'order_data',
'error_msg' => '订单数据不能为空',
],
[
'checked_type' => 'is_array',
'key_name' => 'order_data',
'error_msg' => '订单数据有误',
]
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 是否扣除库存
if(MyC('common_is_deduction_inventory', 0) != 1)
{
return DataReturn('未开启扣除库存', 0);
}
// 数据集合
$detail = Db::name('OrderDetail')->field('id,order_id,goods_id,buy_number,spec')->where(['order_id'=>array_column($params['order_data'], 'id')])->select();
if(empty($detail))
{
return DataReturn('订单详情有误', -1);
}
// 订单集合
$order_group = [];
foreach($params['order_data'] as $o)
{
$order_group[$o['id']] = $o['warehouse_id'];
}
// 订单详情
$data = [];
foreach($detail as $d)
{
$key = md5(empty($d['spec']) ? 'default' : $d['spec']);
if(!isset($data[$order_group[$d['order_id']]][$d['goods_id']][$key]))
{
$data[$order_group[$d['order_id']]][$d['goods_id']][$key] = $d;
} else {
$data[$order_group[$d['order_id']]][$d['goods_id']][$key]['buy_number'] += $d['buy_number'];
}
}
// 数据校验
foreach($data as $w)
{
foreach($w as $g)
{
foreach($g as $v)
{
$ret = self::BuyOrderPayBeginGoodsCheck($v);
if($ret['code'] != 0)
{
return $ret;
}
}
}
}
return DataReturn('校验成功', 0);
}
/**
* 订单支付前商品校验
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-08-05
* @desc description
* @param [array] $detail [订单详情]
*/
public static function BuyOrderPayBeginGoodsCheck($detail)
{
// 获取商品
$goods = Db::name('Goods')->field('is_shelves,is_deduction_inventory,inventory,title')->find($detail['goods_id']);
if(empty($goods))
{
return DataReturn('商品不存在', -10);
}
// 商品状态
if($goods['is_shelves'] != 1)
{
return DataReturn('商品已下架['.$goods['title'].']', -10);
}
// 库存
if(isset($goods['is_deduction_inventory']) && $goods['is_deduction_inventory'] == 1)
{
// 先判断商品库存是否不足
if($goods['inventory'] < $detail['buy_number'])
{
return DataReturn('库存不足['.$goods['title'].'('.$goods['inventory'].'<'.$detail['buy_number'].')]', -10);
}
// 规格库存
$spec = empty($detail['spec']) ? '' : json_decode($detail['spec'], true);
$base = GoodsService::GoodsSpecDetail(['id'=>$detail['goods_id'], 'spec'=>$spec]);
if($base['code'] == 0)
{
// 先判断商品规格库存是否不足
if($base['data']['spec_base']['inventory'] < $detail['buy_number'])
{
return DataReturn('库存不足['.$goods['title'].'('.$base['data']['spec_base']['inventory'].'<'.$detail['buy_number'].')]', -10);
}
} else {
return $base;
}
}
return DataReturn('校验成功', 0);
}
/**
* 库存扣除
* @author Devil
@ -1669,10 +1770,9 @@ class BuyService
if($base['code'] == 0)
{
// 先判断商品规格库存是否不足
$inventory = Db::name('GoodsSpecBase')->where(['id'=>$base['data']['spec_base']['id'], 'goods_id'=>$v['goods_id']])->value('inventory');
if($inventory < $v['buy_number'])
if($base['data']['spec_base']['inventory'] < $v['buy_number'])
{
return DataReturn('商品规格库存不足['.$goods['title'].'('.$inventory.'<'.$v['buy_number'].']', -10);
return DataReturn('商品规格库存不足['.$goods['title'].'('.$base['data']['spec_base']['inventory'].'<'.$v['buy_number'].']', -10);
}
// 扣除规格操作

View File

@ -80,6 +80,7 @@ class OrderService
$order_nos = [];
// 循环处理
$order_data = [];
foreach($ids as $k=>$order_id)
{
// 获取订单信息
@ -95,13 +96,6 @@ class OrderService
return DataReturn('状态不可操作['.$status_text.']'.$order['order_no'], -1);
}
// 订单支付前校验
$ret = BuyService::OrderPayBeginCheck(['order_id'=>$order['id'], 'order_data'=>$order]);
if($ret['code'] != 0)
{
return $ret;
}
// 金额为0、走直接支付成功
if($order['total_price'] <= 0.00)
{
@ -130,6 +124,16 @@ class OrderService
$client_type = $order['client_type'];
$order_payment_id = $order['payment_id'];
}
// 订单数据集合
$order_data[] = $order;
}
// 订单支付前校验订单商品
$ret = BuyService::MoreOrderPayBeginCheck(['order_data'=>$order_data]);
if($ret['code'] != 0)
{
return $ret;
}
// 支付方式
@ -357,7 +361,7 @@ class OrderService
}
// 订单支付前校验
$ret = BuyService::OrderPayBeginCheck(['order_id'=>$order['id'], 'order_data'=>$order]);
$ret = BuyService::SingleOrderPayBeginCheck(['order_id'=>$order['id'], 'order_data'=>$order]);
if($ret['code'] != 0)
{
return $ret;

View File

@ -68,6 +68,7 @@
font-size: 24rpx;
padding: 5rpx 20rpx;
background: #f0f0f0;
border-radius: 6rpx;
}
.goods-title, .goods-spec {
margin-bottom: 5rpx;