all(); $page_size=$input['page_size']; $page_index=$input['page_index']; $num = ($page_index - 1) * $page_size; $search_name=$input['search_name']; $status=$input['status']; $where=[]; if ($status==1){ $where=function ($query){ $query->whereIn('is_show',[2,3]); }; }elseif($status==-1){ $where=[]; }elseif($status==0){ $where['is_show']=1; }elseif($status==2){ $where['is_show']=4; } $data=Goodtest::where($where)->where('is_deleted',0); if($search_name){ $data->where('name','like','%'.$search_name.'%'); } $count=$data->count(); if($count==0){ $this->error('450001','没有数据'); } $list=$data->skip($num)->take($page_size) ->get(); return $this->success_list($list,'success',$count); } public function exportGoodsInventory(Request $request){ $input=$request->all(); $search_name=$input['search_name']; $status=$input['status']; $where=[]; if ($status==1){ $where=function ($query){ $query->whereIn('is_show',[2,3]); }; }elseif($status==-1){ $where=[]; }elseif($status==0){ $where['is_show']=1; }elseif($status==2){ $where['is_show']=4; } $data=Goodtest::where($where)->where('is_deleted',0); if($search_name){ $data->where('name','like','%'.$search_name.'%'); } $count=$data->count(); if($count==0){ $this->error('450001','没有数据'); } $list=$data->get(); return $this->success_list($list,'success',$count); } public function importGoodsInventory(Request $request){ $input=$request->all(); $data=$input['data']; DB::beginTransaction(); try{ foreach($data as $key => $val){ if(empty($val['商品id'])){ return $this->error('450001','第'.($key+2).'行附近,商品id不能为空'); } if(empty($val['商品名称'])){ return $this->error('450001','第'.($key+2).'行附近,商品名称不能为空'); } if(empty($val['款式'])){ return $this->error('450001','第'.($key+2).'行附近,款式不能为空'); } if(empty($val['总库存']) && $val['总库存']!=0){ return $this->error('450001','第'.($key+2).'行附近,总库存不能为空'); } if(!is_numeric($val['总库存'])){ return $this->error('450001','第'.($key+2).'行附近,总库存数据类型有误'); } $storage=Goodtest::where('id',$val['商品id'])->lockForUpdate()->first(); if (!Cache::has('dwbs_good_num'.$storage->id)){ $num=$val['总库存']; Cache::remember('dwbs_good_num'.$storage->id,Carbon::now()->addDays(5),function ()use ($num){ return $num; }); } if($storage->storage!=$val['总库存']){ $storage->storage=$val['总库存']; $storage->save(); GoodsLog::create([ 'admin_id'=> Auth::user()->id, 'admin_name'=>Auth::user()->name, 'goods_id'=>$val['商品id'], 'type'=>1,//修改库存 'name' => $val['商品名称'], 'text'=>'管理员:'.Auth::user()->name.',修改商品:'.$val['商品名称'].'('.$val['款式'].')库存,修改前库存:'.$storage.',修改后库存:'.$val['总库存'] ]); } } DB::commit(); return $this->success([]); }catch (\Exception $e){ DB::rollback(); return $e->getMessage(); return $this->error(); } } public function updateGoodsInventory(Request $request){ $input=$request->all(); $ids=$input['ids']; $num=$input['num']; $rules=[ 'ids'=>'required|array', 'num'=>'required|numeric', ]; $messages=[ 'ids.required'=>'商品id不能为空.', 'ids.array'=>'商品id需为数组.', 'num.required'=>'库存数量不能为空.', 'num.numeric'=>'库存数量格式有误.', ]; $validator = Validator::make($input, $rules, $messages); if($validator->fails()){ return $this->error('400013',$validator->errors()->first()); } foreach($ids as $key=>$val){ DB::beginTransaction(); try{ $goods=Goodtest::where('id',$val)->lockForUpdate()->first(); $count=$goods->storage; $goods->storage+=$num; GoodsLog::create([ 'admin_id'=> Auth::user()->id, 'admin_name'=>Auth::user()->name, 'goods_id'=>$val, 'type'=>1,//修改库存 'name' => $goods->name, 'text'=>'管理员:'.Auth::user()->name.',修改商品:'.$goods->name.'('.$goods->size.')库存,修改前库存:'.$count.',修改后库存:'.$goods->storage ]); $goods->save(); DB::commit(); if (!Cache::has('dwbs_good_num'.$val)){ $nums=$goods->storage; Cache::remember('dwbs_good_num'.$val,Carbon::now()->addDays(5),function ()use ($nums){ return $nums; }); }else{ Cache::increment('dwbs_good_num'.$val,$num); } }catch (\Exception $exception){ DB::rollBack(); } } return $this->success([]); } public function getGoodsInventoryLog(Request $request){ $input=$request->all(); $id=$input['id']; $list=GoodsLog::where('goods_id',$id)->where('type',1)->orderBy('id','desc')->get(); if(count($list)<=0){ $this->error('450001','没有数据'); } return $this->success($list); } //获取商品规格数据 public function getSku($id){ $path=GoodsSku::withTrashed()->find($id)->sku_path; $arr=explode('/',$path); $goodsSku=[]; foreach($arr as $k=>$v){ $upSpec=GoodsSpec::withTrashed()->where('id',$v)->first(); $upSpec['uptitle']=GoodsSpec::withTrashed()->where('id',$upSpec->pid)->value('title'); $goodsSku[$upSpec['uptitle']]=$upSpec->title; } return $goodsSku; } //皇冠库存 public function getCrownInventoryInfo(Request $request){ $input=$request->all(); $page_size=$input['page_size']; $page_index=$input['page_index']; $num = ($page_index - 1) * $page_size; $search_name=$input['search_name']; $where=[]; $ll=Inventory::with('user:id,nickname,mobile') ->whereHas('user',function ($query) { $query->where('level', 3); }) ->whereHas('user', function (Builder $query) use ($search_name) { $query->where('nickname', 'like', '%' . $search_name . '%'); $query->orwhere('mobile', 'like', '%' . $search_name . '%'); }) ->where($where) ->groupBy('user_id','goods_id') ->get(); if($ll){ $count=sizeof($ll); }else{ $this->error('400001','没有数据'); } $list=Inventory::with('user:id,nickname,mobile','goods:id,name') ->whereHas('user',function ($query) { $query->where('level', 3); }) ->whereHas('user', function (Builder $query) use ($search_name) { $query->where('nickname', 'like', '%' . $search_name . '%'); $query->orwhere('mobile', 'like', '%' . $search_name . '%'); }) ->select('user_id','goods_id') ->where($where) ->groupBy('user_id','goods_id') ->orderBy('user_id','desc') ->orderBy('goods_id','desc') ->skip($num)->take($page_size) ->get(); if(empty($list)){ return $this->error('400002','没有获取到数据'); } foreach($list as $key=>$val){ $sku= Inventory::where('goods_id',$val['goods_id'])->where('user_id',$val['user_id'])->select('sku_id','total')->get(); foreach($sku as $k=>$v){ $price=GoodsSkuPrice::withTrashed()->where('goods_id',$val['goods_id'])->where('sku_id',$v->sku_id)->where('user_level','3')->value('price'); $sku[$k]->price=$price?$price:0; $sku[$k]->style=$this->getSku($v->sku_id); } $list[$key]->style=$sku; } return $this->success_list($list,'success',$count); } public function Add(){ // Cache::rememberForever('ddd',function (){ // return 12; // }); dd(Cache::get('ddd')); } }