作者 何书鹏

供货商库存优化

... ... @@ -36,6 +36,7 @@ class Payed
->where('a.goods_id',$item['goods_id'])
->where('a.sku_price_id',$item['goods_sku_price_id'])
->where('a.stock','>',0)
->where('a.deletetime',null)
->orderRaw('rand()')
->field('a.id,a.dealer_id')
->limit(1)
... ...
... ... @@ -304,10 +304,57 @@ class Dealer extends Backend
*/
public function goods()
{
$dealer_id = $this->request->param('dealer_id');
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if($this->request->isAjax()){
(new \app\admin\controller\shopro\goods\Goods)->index();
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$goods_model = new \app\admin\model\shopro\goods\Goods;
$where = [];
// 经销商
$goods_ids = [];
if(isset($dealer_id) && $dealer_id != 0) {
$goods_ids = \app\admin\model\dealer\DealerStock::alias('a')
->join('shopro_goods_sku_price b','a.sku_price_id = b.id')
->where('dealer_id',$dealer_id)
->column('a.goods_id');
}
if ($goods_ids) {
$where['id'] = ['in',$goods_ids];
// 删除无用的供货商库存
\app\admin\model\dealer\DealerStock::where('goods_id','not in',$goods_ids)
->where('dealer_id',$dealer_id)
->delete();
}
$total = $goods_model->where($where)->count();
$subsql = \app\admin\model\shopro\goods\SkuPrice::where('status', 'up')->field('sum(stock) as stock, goods_id')->group('goods_id')->buildSql();
$goodsTableName = $goods_model->getQuery()->getTable();
// 关联规格表,获取总库存
$list = $goods_model->join([$subsql => 'w'], $goodsTableName . '.id = w.goods_id', 'left');
// 关联查询当前商品的活动,一个商品可能存在多条活动记录,关联条件 只有 find_in_set 会存在一个商品出现多次,所以使用 group
$actSubSql = \app\admin\model\shopro\activity\Activity::field('type as activity_type, id as activity_id, goods_ids')->buildSql();
$list = $list->join([$actSubSql => 'act'], "find_in_set(" . $goodsTableName . ".id, goods_ids)", 'left')->group('id');
$list = $list->orderRaw($sort . ' ' . $order)
->where($where)
->limit($offset, $limit)
->select();
foreach ($list as $row) {
$row->visible(['id', 'type', 'activity_id', 'activity_type', 'app_type', 'title', 'status', 'weigh', 'category_ids', 'image', 'price', 'likes', 'views', 'sales', 'stock', 'show_sales', 'dispatch_type', 'updatetime']);
}
$list = collection($list)->toArray();
return json(["total" => $total, "rows" => $list]);
}
$this->assignconfig('dealer_id', $this->request->param('dealer_id'));
$this->assignconfig('dealer_id', $dealer_id);
return $this->view->fetch();
}
... ... @@ -379,7 +426,7 @@ class Dealer extends Backend
// 更新规格库存
foreach ($list as $v) {
$sum_stock = $act->where('sku_price_id',$v['sku_price_id'])->sum('stock');
$sum_stock = $act->where('sku_price_id',$v['sku_price_id'])->where('deletetime',null)->sum('stock');
\app\admin\model\shopro\goods\SkuPrice::where('id',$v['sku_price_id'])->update(['stock'=>$sum_stock]);
}
Db::commit();
... ...
... ... @@ -114,7 +114,7 @@ class DealerStock extends Backend
}
$result = $this->model->allowField(true)->save($params);
// 更新规格库存
$skuPrice->stock = $this->model->where('sku_price_id',$skuPrice['id'])->sum('stock');
$skuPrice->stock = $this->model->where('sku_price_id',$skuPrice['id'])->where('deletetime',null)->sum('stock');
$skuPrice->save();
Db::commit();
} catch (ValidateException $e) {
... ...
... ... @@ -167,7 +167,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'shopro/goods/goods/index' + location.search,
index_url: 'dealer/dealer/goods' + location.search,
dragsort_url: '',
table: 'shopro_goods',
}
... ...