/**
  * --------------------------------------------------------
  * ajax属性列表
  * --------------------------------------------------------
  */
 public function ajaxList(Request $request, $typeId, $goodsId = 0)
 {
     if ($request->ajax()) {
         if ($goodsId && Goods::find($goodsId)) {
             $type = GoodsType::find($typeId);
             $attrs = Attribute::where('type_id', $type->id)->get();
             $goods = Goods::find($goodsId);
             $goodsAttrs = $goods->goodsAttrs;
             foreach ($attrs as $v) {
                 $v->attr_value = unserialize($v->attr_value);
                 $v->list = GoodsAttr::where(['attr_id' => $v['id'], 'goods_id' => $goodsId])->select('attr_value', 'attr_price')->get();
                 //[['attr_value'=>'', 'attr_price'=>''],...];
                 if (!$v->list->first()) {
                     $v->list = [['attr_value' => '', 'attr_price' => 0]];
                 }
             }
             //dump($attrs);
             /************************
                $goods = Goods::find($goodsId);
                $goodsAttrs = $goods->goodsAttrs;
                foreach($goodsAttrs as $v){
                    $attr = Attribute::find($v['attr_id']);
                    $v->attr_value_list = unserialize($attr->attr_value);
                    $v->attr_name = $attr->attr_name;
                    $v->attr_type = $attr->attr_type;
                    $v->attr_input_type = $attr->attr_input_type;
                }
                //$goodsAttrs = GoodsAttr::where(['goods_id'=>$goodsId])->select('id', 'attr_id', 'attr_value as _value', 'attr_price')->get()->toArray();
                ******************************/
             return response()->json($attrs);
         } else {
             $attrs = [];
             $type = GoodsType::find($typeId);
             $attrs = Attribute::where('type_id', $type->id)->get();
             //属性值去序列化
             foreach ($attrs as $attr) {
                 $attr->attr_value = unserialize($attr->attr_value);
             }
             return response()->json($attrs);
         }
     } else {
         $attrs = ['error'];
         return response()->json($attrs);
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update(Request $request, $id)
 {
     $this->validate($request, ['goods_name' => 'required|max:255', 'store_price' => 'required|numeric|min:0']);
     $goods = Goods::find($id);
     $goods->goods_name = $request->input('goods_name');
     $goods->store_price = $request->input('store_price');
     //$post->slug = Str::slug(Input::get('title'));
     $goods->cat_id = Input::get('cat_id');
     $goods->content = Input::get('content');
     $goods->user_id = Auth::user()->id;
     $goods->type_id = Input::get('type_id');
     //to-do
     $goods->brand_id = 1;
     //to-do
     if ($file = Input::file('thumb')) {
         $allowed_extensions = ["png", "jpg", "gif"];
         if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
             return ['error' => 'You may only upload png, jpg or gif.'];
         }
         $fileName = $file->getClientOriginalName();
         $extension = $file->getClientOriginalExtension() ?: 'png';
         $folderName = 'uploads/images/' . date("Ym", time()) . '/' . date("d", time());
         $destinationPath = public_path() . '/' . $folderName;
         $safeName = str_random(10) . '.' . $extension;
         $file->move($destinationPath, $safeName);
         $goods->thumb = $folderName . '/' . $safeName;
     }
     //开始事务
     DB::beginTransaction();
     try {
         if ($goods->save()) {
             $thumbUrls = Input::get('thumbUrls');
             $originalUrls = Input::get('originalUrls');
             if (is_array($originalUrls)) {
                 foreach ($originalUrls as $k => $originalUrl) {
                     $photo = new Photo();
                     $photo->goods_id = $goods->id;
                     $photo->original_url = $originalUrls[$k];
                     $photo->thumb_url = $thumbUrls[$k];
                     $photo->save();
                 }
             }
             //商品关联属性
             //删除重复,goods_id,attr_id,attr_value
             $attrIdList = Input::get('attr_id_list');
             $attrValueList = Input::get('attr_value_list');
             $attrPriceList = Input::get('attr_price_list');
             $attrs = array_map(null, $attrIdList, $attrValueList, $attrPriceList);
             for ($i = 0; $i < count($attrs); $i++) {
                 for ($j = $i + 1; $j < count($attrs); $j++) {
                     if ($attrs[$i][0] == $attrs[$j][0] && $attrs[$i][1] == $attrs[$j][1]) {
                         unset($attrs[$i]);
                     }
                 }
             }
             //数据标记
             foreach ($attrs as $_k => $_v) {
                 if (!GoodsAttr::where(['goods_id' => $goods->id, 'attr_id' => $_v[0], 'attr_value' => $_v[1]])->first()) {
                     $attrs[$_k]['sign'] = 'insert';
                 } elseif (GoodsAttr::where(['goods_id' => $goods->id, 'attr_id' => $_v[0], 'attr_value' => $_v[1]])->first()) {
                     $attrs[$_k]['sign'] = 'update';
                 }
             }
             //数据标记
             foreach ($goods->goodsAttrs as $v) {
                 $isDel = true;
                 foreach ($attrs as $_v) {
                     if ($v['attr_id'] == $_v[0] && $v['attr_value'] == $_v[1]) {
                         $isDel = false;
                     }
                 }
                 if ($isDel) {
                     $attrs[] = [$v['attr_id'], $v['attr_value'], 'sign' => 'delete'];
                 }
             }
             //数据操作
             foreach ($attrs as $_v) {
                 if ($_v['sign'] == 'insert') {
                     $attr = new GoodsAttr();
                     $attr->goods_id = $goods->id;
                     $attr->attr_id = $_v[0];
                     $attr->attr_value = $_v[1];
                     $attr->attr_price = $_v[2];
                     //如果为字符串attr_price存入数据库为0
                     if ($attr->goods_id && $attr->attr_id && $attr->attr_value) {
                         //删除存在的
                         //$attrs::where(['goods_id'=>$attr->goods_id, 'attr_id'=>$attr->attr_id, 'attr_value'=>$attr->attr_value]);
                         //保存
                         $attr->save();
                     }
                 } elseif ($_v['sign'] == 'update') {
                     GoodsAttr::where(['goods_id' => $goods->id, 'attr_id' => $_v[0], 'attr_value' => $_v[1]])->update(['attr_price' => $_v[2]]);
                 } elseif ($_v['sign'] == 'delete') {
                     $attr = GoodsAttr::where(['goods_id' => $goods->id, 'attr_id' => $_v[0], 'attr_value' => $_v[1]])->delete();
                 }
             }
             DB::commit();
             return Redirect::to('admin/goods');
         } else {
             return Redirect::back()->withInput()->withErrors('保存失败!');
         }
     } catch (\Exception $e) {
         DB::rollBack();
         throw $e;
     }
 }