12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php namespace App\Http\Controllers\Api;
- use App\Goods;
- use App\GoodsSkus;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- class GoodsController extends Controller {
- public function index()
- {
- $result = Goods::select('id as goods_id', 'name','picUrl','goodsCategoryName')->where('is_del', 0)->orderBy('goods.id', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- $data = array();
- foreach($result as $value){
- # 获取sku
- $skuList = GoodsSkus::select('id as sku_id','propsName','price','is_white','quantity','is_weigh')->where('goodsCode', $value['goods_id'])->where('is_del', 0)->get();
- $skuList = json_decode(json_encode($skuList),true);
- $sku = array();
- foreach($skuList as $v){
- $quantity = GoodsSkus::actualQuantityByRedis($v['sku_id']);
- if($v['is_white'] == 0 && $quantity['quantity'] <= 0){
- $v['stock'] = 0;
- } else {
- $v['stock'] = 1;
- }
- unset($v['is_white']);
- unset($v['quantity']);
- $sku[] = $v;
- }
- $value['sku_num'] = count($sku);
- if(1 == $value['sku_num']){
- $value['stock'] = $sku[0]['stock'];
- $value['is_weigh'] = $sku[0]['is_weigh'];
- }
- $value['sku_list'] = $sku;
- if('0' == $value['goodsCategoryName']) {
- $data['其它'][] = $value;
- } else {
- $data[$value['goodsCategoryName']][] = $value;
- }
- }
- return self::returnValue($data,200);
- }
- public function searchGoods(Request $request)
- {
- $name = trim($request->input('name'));
- $result = Goods::select('id as goods_id', 'name','picUrl','goodsCategoryName')->where('name', 'like', '%'.$name.'%')->where('is_del', 0)->orderBy('id', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- $data = array();
- foreach($result as $value){
- # 获取sku
- $skuList = GoodsSkus::select('id as sku_id','propsName','price','is_white','quantity','is_weigh')->where('goodsCode', $value['goods_id'])->where('is_del', 0)->get();
- $skuList = json_decode(json_encode($skuList),true);
- $sku = array();
- foreach($skuList as $v){
- $quantity = GoodsSkus::actualQuantityByRedis($v['sku_id']);
- if($v['is_white'] == 0 && $quantity['quantity'] <= 0){
- $v['stock'] = 0;
- } else {
- $v['stock'] = 1;
- }
- unset($v['is_white']);
- unset($v['quantity']);
- $sku[] = $v;
- }
- $value['sku_num'] = count($sku);
- if(1 == $value['sku_num']){
- $value['stock'] = $sku[0]['stock'];
- $value['is_weigh'] = $sku[0]['is_weigh'];
- }
- $value['sku_list'] = $sku;
- $data[] = $value;
- }
- return self::returnValue($data);
- }
- }
|