Update GoodsService.php

This commit is contained in:
邹景立 2021-03-17 10:52:21 +08:00
parent 7a9b190eb4
commit 35ee0d95cc

View File

@ -13,48 +13,6 @@ use think\admin\Service;
class GoodsService extends Service class GoodsService extends Service
{ {
/**
* 更新商品库存数据
* @param string $code
* @return boolean
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function syncStock(string $code): bool
{
// 商品入库统计
$query = $this->app->db->name('ShopGoodsStock');
$query->field('goods_code,goods_spec,ifnull(sum(goods_stock),0) stock_total');
$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.goods_code='{$code}' and a.status>0 and a.deleted_status<0");
$salesList = $query->group('b.goods_code,b.goods_spec')->select()->toArray();
// 组装更新数据
$dataList = [];
foreach (array_merge($stockList, $salesList) as $vo) {
$key = "{$vo['goods_code']}@@{$vo['goods_spec']}";
$dataList[$key] = isset($dataList[$key]) ? array_merge($dataList[$key], $vo) : $vo;
if (empty($dataList[$key]['stock_sales'])) $dataList[$key]['stock_sales'] = 0;
if (empty($dataList[$key]['stock_total'])) $dataList[$key]['stock_total'] = 0;
}
unset($salesList, $stockList);
// 更新商品规格销量及库存
foreach ($dataList as $vo) {
$map = ['goods_code' => $code, 'goods_spec' => $vo['goods_spec']];
$set = ['stock_total' => $vo['stock_total'], 'stock_sales' => $vo['stock_sales']];
$this->app->db->name('ShopGoodsItem')->where($map)->update($set);
}
// 更新商品主体销量及库存
$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_virtual' => $this->app->db->name('ShopGoodsItem')->where(['goods_code' => $code])->sum('number_virtual'),
]);
return true;
}
/** /**
* 获取分类数据 * 获取分类数据
* @return array * @return array
@ -115,6 +73,48 @@ class GoodsService extends Service
return 3; return 3;
} }
/**
* 更新商品库存数据
* @param string $code
* @return boolean
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function syncStock(string $code): bool
{
// 商品入库统计
$query = $this->app->db->name('ShopGoodsStock');
$query->field('goods_code,goods_spec,ifnull(sum(goods_stock),0) stock_total');
$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.goods_code='{$code}' and a.status>0 and a.deleted_status<0");
$salesList = $query->group('b.goods_code,b.goods_spec')->select()->toArray();
// 组装更新数据
$dataList = [];
foreach (array_merge($stockList, $salesList) as $vo) {
$key = "{$vo['goods_code']}@@{$vo['goods_spec']}";
$dataList[$key] = isset($dataList[$key]) ? array_merge($dataList[$key], $vo) : $vo;
if (empty($dataList[$key]['stock_sales'])) $dataList[$key]['stock_sales'] = 0;
if (empty($dataList[$key]['stock_total'])) $dataList[$key]['stock_total'] = 0;
}
unset($salesList, $stockList);
// 更新商品规格销量及库存
foreach ($dataList as $vo) {
$map = ['goods_code' => $code, 'goods_spec' => $vo['goods_spec']];
$set = ['stock_total' => $vo['stock_total'], 'stock_sales' => $vo['stock_sales']];
$this->app->db->name('ShopGoodsItem')->where($map)->update($set);
}
// 更新商品主体销量及库存
$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_virtual' => $this->app->db->name('ShopGoodsItem')->where(['goods_code' => $code])->sum('number_virtual'),
]);
return true;
}
/** /**
* 商品数据绑定 * 商品数据绑定
* @param array $data 商品主数据 * @param array $data 商品主数据