|
@@ -91,13 +91,13 @@ class Orders extends Api{
|
|
|
if( $skusList[$buyInfo['product_skuid']]['product_id'] != $buyInfo['product_id'] ) return json_send(['code'=>'error','msg'=>'该产品规格不存在或已下架','data'=>['error'=>'SKU不匹配=>'.$buyInfo['product_skuid']]]);
|
|
|
// 如果SKU存在,合并产品信息
|
|
|
$productInfo = array_merge($productInfo,$skusList[$buyInfo['product_skuid']]);
|
|
|
- // 扣除库存
|
|
|
- $skusList[$buyInfo['product_skuid']]['stock'] = $skusList[$buyInfo['product_skuid']]['stock'] - $buyInfo['buy_num'];
|
|
|
+ // 需要扣除的库存
|
|
|
+ $skusList[$buyInfo['product_skuid']]['decr'] = empty($skusList[$buyInfo['product_skuid']]['decr']) ? $buyInfo['buy_num'] : $skusList[$buyInfo['product_skuid']]['decr'] + $buyInfo['buy_num'];
|
|
|
}
|
|
|
// 判断库存
|
|
|
- if( $productInfo['stock'] < $buyInfo['buy_num'] ) return json_send(['code'=>'error','msg'=>'产品库存不足','data'=>['error'=>'产品库存不足=>'.$buyInfo['product_id']]]);
|
|
|
- // 扣除库存
|
|
|
- $productList[$buyInfo['product_id']]['stock'] = $productList[$buyInfo['product_id']]['stock'] - $buyInfo['buy_num'];
|
|
|
+ if( $productInfo['stock'] - $buyInfo['buy_num'] < $buyInfo['buy_num'] ) return json_send(['code'=>'error','msg'=>'产品库存不足','data'=>['error'=>'产品库存不足=>'.$buyInfo['product_id']]]);
|
|
|
+ // 需要扣除的库存
|
|
|
+ $productList[$buyInfo['product_id']]['decr'] = empty($productList[$buyInfo['product_id']]['decr']) ? $buyInfo['buy_num'] : $productList[$buyInfo['product_id']]['decr'] + $buyInfo['buy_num'];
|
|
|
// 计算价值
|
|
|
$priceTotal = $buyInfo['buy_num'] * $productInfo['price'];
|
|
|
// 购买信息
|
|
@@ -121,13 +121,6 @@ class Orders extends Api{
|
|
|
$productPrice = $couponRebate['product_price'];
|
|
|
// 获取优惠券赠品信息
|
|
|
$rebateProduct = $couponRebate['rebate_product'];
|
|
|
- // 库存处理
|
|
|
- foreach ($productList as $key => $value) {
|
|
|
- $productList[$key] = ['id'=>$value['id'],'stock'=>$value['stock']];
|
|
|
- }
|
|
|
- foreach ($skusList as $key => $value) {
|
|
|
- $skusList[$key] = ['id'=>$value['sku_id'],'stock'=>$value['stock']];
|
|
|
- }
|
|
|
// 组合订单数据
|
|
|
foreach ($orderProduct as $key => $order) {
|
|
|
// 判断哪一家的赠品
|
|
@@ -174,10 +167,30 @@ class Orders extends Api{
|
|
|
DB::beginTransaction();
|
|
|
// 写入数据
|
|
|
try {
|
|
|
- // 更新库存
|
|
|
- $Product->updateBatch(array_values($productList));
|
|
|
- // 更新库存
|
|
|
- $ProductSkus->updateBatch(array_values($skusList));
|
|
|
+ // 扣减商品库存
|
|
|
+ foreach ($productList as $key => $value) {
|
|
|
+ // 扣减库存
|
|
|
+ $result = $Product->edit($value['id'],['stock'=>DB::raw('stock+-'.$value['decr'])]);
|
|
|
+ // 判断结果
|
|
|
+ if( !$result ) {
|
|
|
+ // 回退数据
|
|
|
+ DB::rollBack();
|
|
|
+ // 错误提示
|
|
|
+ return json_send(['code'=>'error','msg'=>'库存扣减失败','data'=>['error'=>'库存扣减失败']]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 扣减商品库存
|
|
|
+ foreach ($skusList as $key => $value) {
|
|
|
+ // 扣减库存
|
|
|
+ $result = $ProductSkus->edit($value['sku_id'],['stock'=>DB::raw('stock+-'.$value['decr'])]);
|
|
|
+ // 判断结果
|
|
|
+ if( !$result ) {
|
|
|
+ // 回退数据
|
|
|
+ DB::rollBack();
|
|
|
+ // 错误提示
|
|
|
+ return json_send(['code'=>'error','msg'=>'库存扣减失败','data'=>['error'=>'库存扣减失败']]);
|
|
|
+ }
|
|
|
+ }
|
|
|
// 循环订单数据
|
|
|
foreach ($orderProduct as $order) {
|
|
|
// 先获取产品列表,并去除key
|
|
@@ -241,7 +254,7 @@ class Orders extends Api{
|
|
|
// 回退数据
|
|
|
DB::rollBack();
|
|
|
// 判断结果,如果库存扣减失败的话
|
|
|
- if( stripos($th->getMessage(),'UNSIGNED') ) return json_send(['code'=>'error','msg'=>'下单失败','data'=>['error'=>'产品库存扣减失败']]);
|
|
|
+ if( stripos($th->getMessage(),'UNSIGNED') ) return json_send(['code'=>'error','msg'=>'库存不足','data'=>['error'=>'产品库存扣减失败']]);
|
|
|
// 下单失败提示
|
|
|
return json_send(['code'=>'error','msg'=>'下单失败','data'=>['error'=>$th->getMessage().$th->getLine()]]);
|
|
|
}
|