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(); $data=[]; foreach ($list as $k=>$v){ $data[$k]=$v; $data[$k]->order_num=$v->all_storage-$v->storage; $data[$k]->storage=$v->all_storage; } 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::forever('dwbs_good_num'.$storage->id,$num); }else{ $num=$val['总库存']; Cache::forever('dwbs_good_num'.$storage->id, $num); } if($storage->storage!=$val['总库存']){ $storage->storage=$val['总库存']; $storage->all_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()); } DB::beginTransaction(); try{ foreach($ids as $key=>$val){ $goods=Goodtest::where('id',$val)->first(); $s=$goods->storage; if (!Cache::has('dwbs_good_num'.$val)){ Cache::remember('dwbs_good_num'.$val,Carbon::now()->addDays(10),function ()use ($s){ return $s; }); } $numss=Cache::get('dwbs_good_num'.$val); if ($num<0){ if ($numss<-$num){ return $this->error(50023,'当前商品减少的数量大于库存数量'.$numss); }else{ $numm=-$num; Cache::decrement('dwbs_good_num'.$val,$numm); } }else{ Cache::increment('dwbs_good_num'.$val,$num); } $goods->storage=Cache::get('dwbs_good_num'.$val); $goods->all_storage+=$num; $log=new GoodsLog(); $log->admin_id=Auth::user()->id; $log->admin_name=Auth::user()->name; $log->goods_id=$val; $log->type=1;//修改库存 $log->name = $goods->name; $log->text='管理员:'.Auth::user()->name.',修改商品:'.$goods->name.'('.$goods->size.')库存,修改前库存:'.$numss.',修改后库存:'.Cache::get('dwbs_good_num'.$val); if (!$log->save()){ throw new \Exception('1'); } if (!$goods->save()){ throw new \Exception('1'); } } DB::commit(); return $this->success([]); }catch (\Exception $exception){ DB::rollBack(); Cache::decrement('dwbs_good_num'.$ids[0],$num); return $this->error(50021,$exception->getMessage()); } } 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 AddDataGood(ZtRequest $request){ // $re=User::where('level',3)->whereNull('deleted_at')->whereNotIn('mobile',['13837111367','15617601787'])->pluck('mobile')->toArray(); // if (in_array(13837111367,$re)){ // dd(1); // } // dd(2); // dump($request->isEmptyString('id')); // dd($request->inputs('id',2)); $good=Goodtest::where('is_deleted',0)->get(); foreach ($good as $k=>$v){ $num=$v->storage; // Cache::forget('dwbs_good_num'.$v->id); // Cache::put('dwbs_good_num'.$v->id,$num,3600*24*7); $ids=Ordertest::where('created_at','>','2021-08-24 00:00:00')->where('is_deleted',0)->pluck('id'); $nums=Orderdetail::whereIn('order_id',$ids)->where('goods_id',$v->id)->sum('num'); $n=$nums; dump(Cache::get('dwbs_good_num'.$v->id).'=========='.$v->name.$v->size.'('.$v->id.')'.'======='.$v->storage.'======'.$n); // dump(Cache::get('dwbs_good_num'.$v->id).'=========='.$v->name.$v->size.'('.$v->id.')'.'======='.$v->storage.'====='.$v->all_storage); } // Cache::forever('dwbs_good_num74',500); // 70 } public function GetGoodNum(){ $order=Ordertest::where('is_deleted',0)->where('created_at','>','2021-08-04 08:00:00')->pluck('id'); $orders_1=Orderdetail::whereIn('order_id',$order)->where('goods_id',86)->sum('num'); $orders_2=Orderdetail::whereIn('order_id',$order)->where('goods_id',87)->sum('num'); $orders_3=Orderdetail::whereIn('order_id',$order)->where('goods_id',88)->sum('num'); $orders_4=Orderdetail::whereIn('order_id',$order)->where('goods_id',89)->sum('num'); dump($orders_1); dump($orders_2); dump($orders_3); dump($orders_4); } }