<?php
/*
 本代码由 口号帮 创建
 创建时间 2019-05-08 16:22:09
 技术支持 口号帮 
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace App\Http\Controllers\Admin;use App\Models\Dictionary;use App\Models\ProductLabel;use Illuminate\Http\Request;use App\Http\Controllers\Controller;use Illuminate\Support\Facades\DB;class ProductLabelController extends Controller{public function index(){$labels=ProductLabel::select()->orderBy('sort','desc')->get();$model=Dictionary::where(['slug'=>'product_label'])->first();return view('admin.dictionary.label',compact('labels','model'));}public function store(Request $request){$news=$request->input('new');$old=$request->input('old');$P2gAC0=array();$arr=$P2gAC0;if($news)goto P2gBodyx2;goto P2gNextx2;P2gBodyx2:$arr_new=array_pluck($news,'name');$arr=array_merge($arr,$arr_new);goto P2gx1;P2gNextx2:P2gx1:if($old)goto P2gBodyx4;goto P2gNextx4;P2gBodyx4:$arr_old=array_pluck($old,'name');$arr=array_merge($arr,$arr_old);goto P2gx3;P2gNextx4:P2gx3:$arr_unique=array_unique($arr);$A____AA_="count";$P2gAC0=$A____AA_($arr_unique);$A____AAA="count";$P2gAC1=$A____AAA($arr);$P2g0=$P2gAC0!=$P2gAC1;if($P2g0)goto P2gBodyx6;goto P2gNextx6;P2gBodyx6:return response()->json(['code'=>1,'msg'=>'存在重复的标签名称,请仔细检查']);goto P2gx5;P2gNextx6:P2gx5:$model=Dictionary::where(['slug'=>'product_label'])->first();$A___A___="count";$P2gAC0=$A___A___($arr);$P2g0=$P2gAC0>$model->max_num;if($P2g0)goto P2gBodyx8;goto P2gNextx8;P2gBodyx8:return response()->json(['code'=>1,'msg'=>"最多添加".$model->max_num."条标签"]);goto P2gx7;P2gNextx8:P2gx7:$old_ids=$request->input('ids',[]);DB::beginTransaction();try{ProductLabel::whereNotIn('id',$old_ids)->delete();if($old)goto P2gBodyxb;goto P2gNextxb;P2gBodyxb:foreach($old as $item){$id=$item['id'];unset($item['id']);ProductLabel::where(['id'=>$id])->update($item);}goto P2gxa;P2gNextxb:P2gxa:if($news)goto P2gBodyxd;goto P2gNextxd;P2gBodyxd:foreach($news as $value){ProductLabel::create($value);}goto P2gxc;P2gNextxd:P2gxc:$query=ProductLabel::select()->orderBy('sort','desc');$labels=$query->get();$P2gAC0=array();$names=$P2gAC0;if($labels)goto P2gBodyxf;goto P2gNextxf;P2gBodyxf:foreach($labels as $key=>$label){$P2g0='<span style="color: ' . $label->color;$P2g1=$P2g0 . '">';$P2g2=$P2g1 . $label->name;$P2g3=$P2g2 . '</span>';$names[]=$P2g3;$P2g4=$P2g3;}goto P2gxe;P2gNextxf:P2gxe:Dictionary::where(['slug'=>'product_label'])->update(['content'=>implode('、',$names),'model_ids'=>$query->pluck('id')->toJson()]);DB::commit();return response()->json(['code'=>0,'msg'=>'保存成功']);}catch(\Exception $exception){DB::rollBack();return response()->json(['code'=>1,'msg'=>'保存失败'.$exception->getMessage()]);}}}
?>