作者 何书鹏

商品列表总库存

... ... @@ -3,7 +3,6 @@
namespace app\api\controller;
use app\api\model\Category;
use app\api\model\Goods;
use app\api\model\GoodsSpec;
... ... @@ -111,6 +110,8 @@ class Classification extends Api
->find();
$item['price'] = $goods_spec['goods_price'];
$item['line_price'] = $goods_spec['line_price'];
// 总库存
$item->append(['stock_num']);
});
}else{
$list = $model
... ... @@ -132,6 +133,8 @@ class Classification extends Api
->find();
$item['price'] = $goods_spec['goods_price'];
$item['line_price'] = $goods_spec['line_price'];
// 总库存
$item->append(['stock_num']);
});
}
$this->success('分类商品列表',['list'=>$list,'image'=>cdnurl(Config::get('site.advert'),true)]);
... ...
... ... @@ -161,6 +161,8 @@ class Index extends Api
->find();
$item['price'] = $goods_spec['goods_price'];
$item['line_price'] = $goods_spec['line_price'];
// 总库存
$item->append(['stock_num']);
});
}else{
$list = $model->where('is_index','1')
... ... @@ -175,6 +177,8 @@ class Index extends Api
->find();
$item['price'] = $goods_spec['goods_price'];
$item['line_price'] = $goods_spec['line_price'];
// 总库存
$item->append(['stock_num']);
});
}
... ...
... ... @@ -122,7 +122,9 @@ class Order extends Api
$sum = 0;
foreach ($item->getRelation('goods')as $key => $value){
$sum += $value['total_num'];
$value->visible(['goods_id','goods_name','goods_attr','total_num','total_price']);
// 总库存
$value['stock_num'] = GoodsSpec::where('goods_id',$value['goods_id'])->sum('stock_num');
$value->visible(['goods_id','goods_name','goods_attr','total_num','total_price'])->append(['stock_num']);
}
$item['total_sum'] = $sum;
$item->visible([
... ...
... ... @@ -4,6 +4,7 @@
namespace app\api\model;
use addons\litestore\model\Litestoregoodsspec;
use think\Model;
class Goods extends Model
... ... @@ -49,4 +50,11 @@ class Goods extends Model
$value = !empty($data['down_image']) ? cdnurl($data['down_image'],true):'';
return $value;
}
// 总库存
public function getStockNumAttr($value,$data)
{
$value = GoodsSpec::where('goods_id',$data['goods_id'])->sum('stock_num');
return $value;
}
}
\ No newline at end of file
... ...