作者 何书鹏

商品列表总库存

@@ -3,7 +3,6 @@ @@ -3,7 +3,6 @@
3 3
4 namespace app\api\controller; 4 namespace app\api\controller;
5 5
6 -  
7 use app\api\model\Category; 6 use app\api\model\Category;
8 use app\api\model\Goods; 7 use app\api\model\Goods;
9 use app\api\model\GoodsSpec; 8 use app\api\model\GoodsSpec;
@@ -111,6 +110,8 @@ class Classification extends Api @@ -111,6 +110,8 @@ class Classification extends Api
111 ->find(); 110 ->find();
112 $item['price'] = $goods_spec['goods_price']; 111 $item['price'] = $goods_spec['goods_price'];
113 $item['line_price'] = $goods_spec['line_price']; 112 $item['line_price'] = $goods_spec['line_price'];
  113 + // 总库存
  114 + $item->append(['stock_num']);
114 }); 115 });
115 }else{ 116 }else{
116 $list = $model 117 $list = $model
@@ -132,6 +133,8 @@ class Classification extends Api @@ -132,6 +133,8 @@ class Classification extends Api
132 ->find(); 133 ->find();
133 $item['price'] = $goods_spec['goods_price']; 134 $item['price'] = $goods_spec['goods_price'];
134 $item['line_price'] = $goods_spec['line_price']; 135 $item['line_price'] = $goods_spec['line_price'];
  136 + // 总库存
  137 + $item->append(['stock_num']);
135 }); 138 });
136 } 139 }
137 $this->success('分类商品列表',['list'=>$list,'image'=>cdnurl(Config::get('site.advert'),true)]); 140 $this->success('分类商品列表',['list'=>$list,'image'=>cdnurl(Config::get('site.advert'),true)]);
@@ -161,6 +161,8 @@ class Index extends Api @@ -161,6 +161,8 @@ class Index extends Api
161 ->find(); 161 ->find();
162 $item['price'] = $goods_spec['goods_price']; 162 $item['price'] = $goods_spec['goods_price'];
163 $item['line_price'] = $goods_spec['line_price']; 163 $item['line_price'] = $goods_spec['line_price'];
  164 + // 总库存
  165 + $item->append(['stock_num']);
164 }); 166 });
165 }else{ 167 }else{
166 $list = $model->where('is_index','1') 168 $list = $model->where('is_index','1')
@@ -175,6 +177,8 @@ class Index extends Api @@ -175,6 +177,8 @@ class Index extends Api
175 ->find(); 177 ->find();
176 $item['price'] = $goods_spec['goods_price']; 178 $item['price'] = $goods_spec['goods_price'];
177 $item['line_price'] = $goods_spec['line_price']; 179 $item['line_price'] = $goods_spec['line_price'];
  180 + // 总库存
  181 + $item->append(['stock_num']);
178 }); 182 });
179 } 183 }
180 184
@@ -122,7 +122,9 @@ class Order extends Api @@ -122,7 +122,9 @@ class Order extends Api
122 $sum = 0; 122 $sum = 0;
123 foreach ($item->getRelation('goods')as $key => $value){ 123 foreach ($item->getRelation('goods')as $key => $value){
124 $sum += $value['total_num']; 124 $sum += $value['total_num'];
125 - $value->visible(['goods_id','goods_name','goods_attr','total_num','total_price']); 125 + // 总库存
  126 + $value['stock_num'] = GoodsSpec::where('goods_id',$value['goods_id'])->sum('stock_num');
  127 + $value->visible(['goods_id','goods_name','goods_attr','total_num','total_price'])->append(['stock_num']);
126 } 128 }
127 $item['total_sum'] = $sum; 129 $item['total_sum'] = $sum;
128 $item->visible([ 130 $item->visible([
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 namespace app\api\model; 4 namespace app\api\model;
5 5
6 6
  7 +use addons\litestore\model\Litestoregoodsspec;
7 use think\Model; 8 use think\Model;
8 9
9 class Goods extends Model 10 class Goods extends Model
@@ -49,4 +50,11 @@ class Goods extends Model @@ -49,4 +50,11 @@ class Goods extends Model
49 $value = !empty($data['down_image']) ? cdnurl($data['down_image'],true):''; 50 $value = !empty($data['down_image']) ? cdnurl($data['down_image'],true):'';
50 return $value; 51 return $value;
51 } 52 }
  53 +
  54 + // 总库存
  55 + public function getStockNumAttr($value,$data)
  56 + {
  57 + $value = GoodsSpec::where('goods_id',$data['goods_id'])->sum('stock_num');
  58 + return $value;
  59 + }
52 } 60 }