123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace app\user\controller;
- use app\user\model\CompanyItem;
- use think\App;
- use think\facade\Validate;
- class Depart extends Base{
- protected $noLogin=[];
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model=new CompanyItem();
- }
- //部门列表
- public function list(){
- $param=$this->request->param(['pid'=>0,'level'=>1,'status'=>'','companyNo'=>'','name'=>'','page'=>1,
- 'size'=>15],'post','trim');
- $where=[['is_del','=',0]];
- if($param['pid']!=='')$where[]=['pid','=',$param['pid']];
- if($param['level']!=='')$where[]=['level','=',$param['level']];
- if($param['status']!=='')$where[]=['status','=',$param['status']];
- if($param['companyNo']!=='')$where[]=['companyNo','=',$param['companyNo']];
- if($param['name']!=='')$where[]=['name','like',"%{$param['name']}%"];
- $list=$this->model->with(["companyInfo"])->where($where)->order('weight desc,id desc')->paginate(['list_rows'=>$param['size'],'page'=>$param['page']]);
- return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
- }
- //部门数据获取
- public function query(){
- $param=$this->request->param(['pid'=>0,'level'=>1,'status'=>'','companyNo'=>'','name'=>'',],'post','trim');
- $where=[['is_del','=',0]];
- if($param['pid']!=='')$where[]=['pid','=',$param['pid']];
- if($param['level']!=='')$where[]=['level','=',$param['level']];
- if($param['status']!=='')$where[]=['status','=',$param['status']];
- if($param['companyNo']!=='')$where[]=['companyNo','=',$param['companyNo']];
- if($param['name']!=='')$where[]=['name','like',"%{$param['name']}%"];
- $list=$this->model->with(['companyInfo'])->where($where)->order('weight desc,id desc')->select();
- return success('获取成功',$list);
- }
-
- //部门数据创建
- public function create(){
- $param=$this->request->param(['pid'=>0,'status'=>1,'is_del'=>0,'companyNo'=>'','name'=>'',
- 'weight'=>'',],'post','trim');
- $val = Validate::rule([
- 'companyNo|所属企业' => 'require|max:255',
- 'is_del|删除状态' => 'number',
- 'name|部门名称' => 'require|max:255|unique:app\user\model\CompanyItem,name^is_del^companyNo',
- 'pid|父级id' => 'require|number|egt:0',
- 'weight|排序权重' => 'number|gt:0',
- ]);
-
- if ($val->check($param) == false) return error( $val->getError());
- $depart_link="";
- if($param['pid']!=0){
- $parent = $this->model->findOrEmpty($param['pid']);
- if($parent->isEmpty())return error("未找到父级部门信息");
- $depart_link=array_filter(array_column($parent->depart_link,'id'));
- }
- $info = [
- "companyNo"=>$param['companyNo'],
- "name"=>$param['name'],
- "pid"=>$param['pid'],
- "depart_link"=>$depart_link,
- "weight"=>$param['weight'],
- "status"=>$param['status'],
- ];
- $model=CompanyItem::create($info);
- return$model->isEmpty()?error("创建失败") :success('创建成功');
- }
-
- //部门数据编辑save
- public function save(){
- $param=$this->request->param(['id'=>0,'pid'=>0,'status'=>1,'is_del'=>0,'companyNo'=>'','name'=>'',
- 'weight'=>'',],'post','trim');
- $val = Validate::rule([
- 'id|部门主键' => 'require|number|gt:0',
- 'companyNo|所属企业' => 'require|max:255',
- 'is_del|删除状态' => 'number',
- 'name|部门名称' => 'require|max:255|unique:app\user\model\CompanyItem,name^is_del^companyNo',
- 'pid|父级id' => 'require|number|egt:0',
- 'weight|排序权重' => 'number|gt:0',
- 'status|部门状态' => 'number|in:0,1',
- ]);
- if ($val->check($param) == false) return error( $val->getError());
- $info = $this->model->findOrEmpty($param['id']);
- if($info->isEmpty())return error('未找到部门信息');
- if($param['pid']!=0 && $param['pid']!=$info->pid){
- $parent = $this->model->findOrEmpty($param['pid']);
- if($parent->isEmpty())return error('未找到父级部门信息');
- $info->depart_link=array_filter(array_column($parent->depart_link,'id'));
- }
- $info->companyNo=$param['companyNo'];
- $info->name=$param['name'];
- $info->pid=$param['pid'];
- $info->weight=$param['weight'];
- $info->status=$param['status'];
- $model=$info->save();
- return$model==false?error('修改失败') :success('修改成功');
- }
- public function delete(){
- $id = $this->request->post('id/d');
- $items =$this->model->findOrEmpty($id);
- if($items->isEmpty())return error('部门信息不存在');
- $isT = \app\user\model\AccountItem::where("itemid",$id)->select();
- if($isT->isEmpty()==false)return error("部门存在关联人员无法删除");
- $items->is_del =1;
- $result= $items->save();
- return $result ? success('删除成功'): error('删除失败');
- }
-
- public function deleteAccount(){
- $id = $this->request->post('id/d');
- $items =$this->model->findOrEmpty($id);
- if($items->isEmpty())return error('部门信息不存在');
- $isT = \app\user\model\AccountItem::where('itemid',$id)->select();
- if($isT->isEmpty())return error('部门关联人员已解除');
- $del= \app\user\model\AccountItem::where('itemid',$id)->delete();
- return $del ? success('部门关联人员解除成功'): error('部门关联人员解除失败');
- }
- }
|