From f75ba9fe78da145b78d8e0f2f4eff9079fb9312c Mon Sep 17 00:00:00 2001 From: Anyon Date: Wed, 9 Sep 2020 11:20:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=95=86=E5=93=81=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/controller/ShopGoods.php | 4 ++++ app/data/service/GoodsService.php | 23 +++++++++++------------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/data/controller/ShopGoods.php b/app/data/controller/ShopGoods.php index 04f13748d..ccb7c724e 100644 --- a/app/data/controller/ShopGoods.php +++ b/app/data/controller/ShopGoods.php @@ -179,10 +179,14 @@ class ShopGoods extends Controller /** * 表单结果处理 * @param boolean $result + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ protected function _form_result($result) { if ($result && $this->request->isPost()) { + GoodsService::instance()->syncStock(input('code')); $this->success('商品编辑成功!', 'javascript:history.back()'); } } diff --git a/app/data/service/GoodsService.php b/app/data/service/GoodsService.php index 01d0063a9..e6e63da2e 100644 --- a/app/data/service/GoodsService.php +++ b/app/data/service/GoodsService.php @@ -21,16 +21,16 @@ class GoodsService extends Service * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function syncStock($code) + public function syncStock(string $code) { // 商品入库统计 $query = $this->app->db->name('ShopGoodsStock'); $query->field('goods_code,goods_spec,ifnull(sum(number_stock),0) number_stock'); - $stockList = $query->where(['code' => $code])->group('goods_id,goods_spec')->select()->toArray(); + $stockList = $query->where(['goods_code' => $code])->group('goods_code,goods_spec')->select()->toArray(); // 商品销量统计 $query = $this->app->db->table('shop_order a')->field('b.goods_code,b.goods_spec,ifnull(sum(b.stock_sales),0) stock_sales'); - $query->leftJoin('shop_order_item b', 'a.order_no=b.order_no')->where([['b.code', '=', $code], ['a.status', 'in', [1, 2, 3, 4, 5]]]); - $salesList = $query->group('b.goods_id,b.goods_spec')->select()->toArray(); + $query->leftJoin('shop_order_item b', 'a.order_no=b.order_no')->where([['b.goods_code', '=', $code], ['a.status', 'in', [1, 2, 3, 4, 5]]]); + $salesList = $query->group('b.goods_code,b.goods_spec')->select()->toArray(); // 组装更新数据 $dataList = []; foreach (array_merge($stockList, $salesList) as $vo) { @@ -48,27 +48,26 @@ class GoodsService extends Service } // 更新商品主体销量及库存 $this->app->db->name('ShopGoods')->where(['code' => $code])->update([ - 'stock_total' => intval(array_sum(array_column($dataList, 'stock_total'))), - 'stock_sales' => intval(array_sum(array_column($dataList, 'stock_sales'))), + 'stock_total' => intval(array_sum(array_column($dataList, 'stock_total'))), + 'stock_sales' => intval(array_sum(array_column($dataList, 'stock_sales'))), + 'stock_virtual' => $this->app->db->name('ShopGoodsItem')->where(['goods_code' => $code])->sum('number_virtual'), ]); return true; } /** * 获取分类数据 - * @param string $type 数据类型 + * @param string $type 操作函数 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function getCateList($type = 'arr2tree'): array + public function getCateList(string $type = 'arr2tree'): array { $map = ['deleted' => 0, 'status' => 1]; - $query = $this->app->db->name('ShopGoodsCate'); - $query->where($map)->order('sort desc,id desc'); - $query->withoutField('sort,status,deleted,create_at'); - return DataExtend::$type($query->select()->toArray()); + $query = $this->app->db->name('ShopGoodsCate')->where($map)->order('sort desc,id desc'); + return DataExtend::$type($query->withoutField('sort,status,deleted,create_at')->select()->toArray()); } /**