model = new Cat(); } /** * @param cat_name 分类名称 * @param pid 父级分类id * @param status 状态 0正常 1禁用 * 获取分类列表 * @return \think\Response|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function list(){ $param=$this->request->param(["pid"=>0,"cat_name"=>"","status"=>"","level"=>"","search"=>"","page"=>1,"size"=>10],"post","trim"); $where=[["is_del","=",0]]; if($param["pid"]!=="")$where[]=["pid","=",$param["pid"]]; if($param["cat_name"]!=="")$where[]=["cat_name","like","%".$param["cat_name"]."%"]; if($param["status"]!=="")$where[]=["status","=",$param["status"]]; if($param["level"]!=="")$where[]=["level","=",$param["level"]]; if($param["search"]!=="")$where[]=["search","like","%".$param["search"]."%"]; $data=$this->model->where($where)->order("id","desc")->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]) ->each(function(&$item){ $item["im"]=CatSpecs::with(["specs"])->where("cat_id",$item["id"])->field(["specs_id"])->select(); $depart = \app\user\model\AccountItem::with(["ItemName"])->where(["account_id"=>$item["createrid"]])->findOrEmpty(); $item["company_name"]=$depart->depart_name??""; $search_arr= Cat::GetCatListByChildId($item['id']); $item['item'] = array_column($search_arr,"id") ; }); return success("获取成功",["list"=>$data->items(),"count"=>$data->total()]); } /** * @param cat_name 分类名称 * @param pid 父级分类id * @param cat_desc 分类描述 * @param fund_code 财务代码 * @param specs_id 规格id * @param weight 权重 * @param status 状态 0正常 1禁用 * @return * */ public function create(){ $param=$this->request->param(['cat_name', 'pid', 'cat_desc' => '', 'fund_code' => '', 'is_del' => '0', 'specs_id'=>[], 'weight' => 0, 'status' => 0],"post","trim"); $valid=Validate::rule([ "cat_name|分类名称"=>"require|max:255|unique:app\admin\model\Cat,cat_name^is_del", "pid|父级分类"=>"require|integer|egt:0", "cat_desc|分类描述"=>"max:255", "fund_code|财务代码"=>"max:255", "specs_id|规格id"=>"require|array", "weight|权重"=>"integer", "status|状态"=>"integer|in:0,1", ]); if(!$valid->check($param))return error($valid->getError()); if($param['pid']!=0){ $parent = $this->model->where('id',$param['pid'])->findOrEmpty(); if($parent->isEmpty())return error('父级分类不存在'); } if($parent->isEmpty())return error("父级分类不存在"); $this->model->startTrans(); try{ $create=[ "cat_name"=>$param['cat_name'], "pid"=>$param["pid"], "cat_desc"=>$param["cat_desc"], "fund_code"=>$param["fund_code"], "weight"=>$param["weight"], "status"=>$param["status"], 'creater' => $this->uname, 'createrid' =>$this->uid, ]; $data=$this->model->create($create); if($data->isEmpty())throw new \Exception("添加失败"); $spec=$data->specs()->saveAll(array_map(function($item)use($data){return ['specs_id'=>$item];},$param['specs_id'])); if(!$spec)throw new \Exception("添加失败"); $this->model->commit(); }catch (\Exception $e){ $this->model->rollback(); return error($e->getMessage()); } event("PlatCat",["type"=>"add",'info'=>'cat',"data"=>$data]); return success("添加成功"); } /** * @param id 分类id * @param cat_name 分类名称 * @param pid 父级分类id * @param cat_desc 分类描述 * @param fund_code 财务代码 * @param specs_id 规格id * @param weight 权重 * @param status 状态 0正常 1禁用 * @return */ public function save(){ $param=$this->request->param(['id','cat_name', 'pid', 'cat_desc' => '', 'fund_code' => '', 'is_del' => '0','specs_id'=>[], 'weight' => 0, 'status' => 0],"post","trim"); $valid=Validate::rule([ "id|分类id"=>"require|integer|egt:1", "cat_name|分类名称"=>"require|max:255|unique:app\admin\model\Cat,cat_name^is_del", "pid|父级分类"=>"require|integer|egt:0", "cat_desc|分类描述"=>"max:255", "fund_code|财务代码"=>"max:255", "specs_id|规格id"=>"require|array", "weight|权重"=>"integer", "status|状态"=>"integer|in:0,1", ]); if(!$valid->check($param))return error($valid->getError()); if($param['pid']!=0){ $parent = $this->model->where("id",$param["pid"])->findOrEmpty(); if($parent->isEmpty())return error("父级分类不存在"); } $this->model->startTrans(); try{ $data=$this->model->with(["specs"])->findOrEmpty($param["id"]); if($data->isEmpty())return error("分类不存在"); $data->cat_name=$param["cat_name"]; $data->pid=$param["pid"]; $data->cat_desc=$param["cat_desc"]; $data->fund_code=$param["fund_code"]; $data->weight=$param["weight"]; $data->status=$param["status"]; $save=$data->save(); if(!$save)throw new \Exception("修改失败"); $add=array_diff($param["specs_id"],array_column($data->specs->toArray(),"specs_id")); $delte=CatSpecs::where([["cat_id","=",$data->id],["specs_id","not in",$param['specs_id']],['is_del','=',0]])->delete(); if($delte===false)throw new \Exception("删除失败"); if(!empty($add)){ $spec=$data->specs()->saveAll(array_map(function($item)use($data){return ["specs_id"=>$item,"cat_id"=>$data->id];},$add)); if(!$spec)throw new \Exception("添加失败"); } $this->model->commit(); }catch (\Exception $e){ $this->model->rollback(); return error($e->getMessage()); } event('PlatCat',['type'=>'add','info'=>'cat','data'=>$data]); return success("修改成功"); } /** * @param id 分类id * @return */ public function delete(){ $param=$this->request->param(["id"=>0],"post","trim"); $valid=Validate::rule([ "id|分类id"=>"require|integer|egt:1", ]); if(!$valid->check($param))return error($valid->getError()); $data=$this->model->with(["specs"])->findOrEmpty($param["id"]); if($data->isEmpty())return error("分类不存在"); $data->is_del=1; if(!$data->save())return error("删除失败"); event("PlatCat",["type"=>"delete",'info'=>'cat',"data"=>$data]); return success("删除成功"); } /** * @param id 分类id * @param status 状态 0正常 1禁用 * @return */ public function status(){ $param=$this->request->param(["id"=>0,"status"=>0],"post","trim"); $valid=Validate::rule([ "id|分类id"=>"require|integer|egt:1", "status|状态"=>"require|integer|in:0,1", ]); if(!$valid->check($param))return error($valid->getError()); $data=$this->model->findOrEmpty($param["id"]); if($data->isEmpty())return error("分类不存在"); if($data->status==$param["status"])return error("状态未改变"); $catIds=[$param['id']]; if($data->level==3){ if($param['status']==1){ $catInfo = Cat::GetCatListByChildId($param['id']); $catIds=array_map(function($v){ return $v->status==0?$v->id:0; },$catInfo); } }else{ if($param['status']==0){ $catInfo = Cat::where(["pid"=>$param['id'],"status"=>1,"is_del"=>0])->findOrEmpty(); if(!$catInfo->isEmpty())return error("该分类下有启用的子分类"); }else return error('所在等级不能启用'); } $update=$data->whereIn("id",$catIds)->update(["status"=>$param["status"]]); if(!$update)return error("修改失败"); event("PlatCat",["type"=>"status",'info'=>'cat',"data"=>["id"=>$catIds,"staus"=>$param['status']]]); return success(Cat::$statusCn[$param['status']]."成功"); } /** 获取分类列表 * @param cat_name 分类名称 * @param pid 父级分类id * @param status 状态 * @return \think\Response|\think\response\Json|void */ public function query(){ $param = $this->request->only(['cat_name' => '', 'pid' => '', 'status' => '',], 'post', 'trim'); $valid = Validate::rule([ "cat_name|分类名称" => "max:255", "pid|父级分类" => "integer|egt:0", "status|状态" => "integer|in:0,1", ]); if (!$valid->check($param)) return error($valid->getError()); try { $data = $this->model->with(["specs"])->where(function ($query) use ($param) { if ($param['cat_name']!=='') $query->where("cat_name", "like", "%{$param['cat_name']}%"); if ($param['pid']!=='') $query->where("pid", $param['pid']); if ($param['status'] !== '') $query->where("status", $param['status']); })->order("pid,weight desc,id")->select(); if (!$data->isEmpty()) { foreach ($data as &$item) { $item['child'] = Cat::GetCatListByPid($item['id']); } } }catch (\Exception $e) { return error($e->getMessage()); } return success("查询成功", $data); } }