From 6c34f5bdd9b71a9e8db6cc7a2be028c1cb8ec861 Mon Sep 17 00:00:00 2001 From: devil Date: Fri, 4 Sep 2020 15:55:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=8F=96=E6=B6=88=E5=BA=93?= =?UTF-8?q?=E5=AD=98=E5=9B=9E=E6=BB=9A=E6=97=A0=E6=95=88=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/service/BuyService.php | 7 +++ application/service/WarehouseGoodsService.php | 50 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/application/service/BuyService.php b/application/service/BuyService.php index f7e93a928..a8294e7a0 100755 --- a/application/service/BuyService.php +++ b/application/service/BuyService.php @@ -1928,6 +1928,13 @@ class BuyService return $base; } + // 仓库库存回滚 + $we_ret = WarehouseGoodsService::WarehouseGoodsInventoryRollback($params['order_id'], $v['goods_id'], $spec, $v['buy_number']); + if($we_ret['code'] != 0) + { + return $we_ret; + } + // 回滚日志更新 $log_data = [ 'is_rollback' => 1, diff --git a/application/service/WarehouseGoodsService.php b/application/service/WarehouseGoodsService.php index cf5589408..0a0d0f325 100644 --- a/application/service/WarehouseGoodsService.php +++ b/application/service/WarehouseGoodsService.php @@ -870,5 +870,55 @@ class WarehouseGoodsService return DataReturn('扣除成功', 0); } + + /** + * 仓库库存回滚 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2020-08-02 + * @desc description + * @param [int] $order_id [订单id] + * @param [int] $goods_id [商品id] + * @param [string] $spec [商品规格] + * @param [int] $buy_number [扣除库存数量] + */ + public static function WarehouseGoodsInventoryRollback($order_id, $goods_id, $spec, $buy_number) + { + // 获取仓库id + $warehouse_id = Db::name('Order')->where(['id'=>$order_id])->value('warehouse_id'); + if(empty($warehouse_id)) + { + return DataReturn('订单仓库id有误', -1); + } + + // 规格key,空则默认default + $md5_key = 'default'; + if(!empty($spec)) + { + if(!is_array($spec)) + { + $spec = json_decode($spec, true); + } + $md5_key = implode('', array_column($spec, 'value')); + } + $md5_key = md5($md5_key); + + // 扣除仓库商品规格库存 + $where = ['warehouse_id'=>$warehouse_id, 'goods_id'=>$goods_id, 'md5_key'=>$md5_key]; + if(!Db::name('WarehouseGoodsSpec')->where($where)->setInc('inventory', $buy_number)) + { + return DataReturn('仓库商品规格库存回滚失败['.$warehouse_id.'-'.$goods_id.'('.$buy_number.')]', -11); + } + + // 扣除仓库商品库存 + $where = ['warehouse_id'=>$warehouse_id, 'goods_id'=>$goods_id]; + if(!Db::name('WarehouseGoods')->where($where)->setInc('inventory', $buy_number)) + { + return DataReturn('仓库商品库存回滚失败['.$warehouse_id.'-'.$goods_id.'('.$buy_number.')]', -12); + } + + return DataReturn('回滚成功', 0); + } } ?> \ No newline at end of file -- GitLab