model=new \app\user\model\AccountItem(); } /** * @param int $account_id 账户id * @param int $itemid 部门id * @param int $position 1:部门 2:主管部门 * @return \think\response\Json */ public function setItem(){ $param=$this->request->param(["account_id"=>"","itemid"=>"",'position'=>'1'],"post","trim"); $valid = Validate::rule([ "account_id|账户id"=>"require|number|gt:0", "itemid|部门id"=>"require|number|gt:0", ]); if($valid->check($param)==false) return error($valid->getError()); $info = $this->model->where(["account_id"=>$param['account_id']])->findOrEmpty(); $this->model->startTrans(); try{ if($param['position']==2){ $position=$this->model->where([['itemid','=',$param['itemid']],['position','=',2],['account_id','<>',$param['account_id']],]) ->findOrEmpty(); if($position->isEmpty()==false){ $position->position=1; $position->save(); } } $info->account_id = $param['account_id']; $info->itemid = $param['itemid']; $info->position = $param['position']; $result= $info->save(); if($result==false) return error("部门设置失败"); $this->model->commit(); }catch(\Exception $e){ $this->model->rollback(); return error($e->getMessage()); } return success("部门设置成功"); } /** * @param int $account_id 账户id * @return \think\Response|\think\response\Json|void */ public function resetItem(){ $param=$this->request->param(['account_id'=>''],'post','trim'); $valid = Validate::rule([ 'account_id|账户id'=>'require|number|gt:0', ]); if($valid->check($param)==false) return error($valid->getError()); $info = $this->model->where(['account_id'=>$param['account_id']])->findOrEmpty(); if($info->position==2)return error('请先解除用户部门主管'); $result= $info->delete(); return $result ? success('部门解除成功'): error('部门解除失败'); } /** * @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' => '', 'name' => '', 'account_id' => '', 'companyNo' => ''], 'post', 'trim'); $condition = [["is_del","=",0]]; if($param['pid']!=='')$condition[]=['pid','=',$param['pid']]; if($param['name']!=='')$condition[]=['name','like','%'.$param['name'].'%']; if($param['account_id']!=='')$condition[]=['account_id','=',$param['account_id']]; if($param['companyNo']!=='')$condition[]=['companyNo','like','%'.$param['companyNo'].'%']; $depart = new CompanyItem(); $list = $depart->with(["companyInfo"])->where($condition)->order('id asc')->select(); $userwhere=[['is_del','=',0]]; if(!$list->isEmpty()){ $userwhere[]=['itemid', 'in', array_unique(array_column($list->toArray(),"pid"))]; }else{ $userwhere[]=['itemid', '=', $param['pid']??0]; } $item= $this->model->with(['accountInfo'=>['userInfo']])->where($userwhere)->select(); return success("获取成功",['depart' => $list, 'item' => $item]); } /** * @return \think\Response|\think\response\Json|void */ public function getItem(){ $param = $this->request->param(['account_id' => ''], 'post', 'trim'); $valid = Validate::rule([ 'account_id|账户id' => 'require|number|gt:0', ]); if($valid->check($param)==false) return error($valid->getError()); $info = $this->model->with(['ItemName'])->where(['account_id'=>$param['account_id']])->findOrEmpty(); if($info->isEmpty())return error('未找到部门信息'); return success('获取成功',$info); } }