Depart.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\user\controller;
  3. use app\user\model\CompanyItem;
  4. use think\App;
  5. use think\facade\Validate;
  6. class Depart extends Base{
  7. protected $noLogin=[];
  8. public function __construct(App $app) {
  9. parent::__construct($app);
  10. $this->model=new CompanyItem();
  11. }
  12. //部门列表
  13. public function list(){
  14. $param=$this->request->param(['pid'=>0,'level'=>1,'status'=>'','companyNo'=>'','name'=>'','page'=>1,
  15. 'size'=>15],'post','trim');
  16. $where=[['is_del','=',0]];
  17. if($param['pid']!=='')$where[]=['pid','=',$param['pid']];
  18. if($param['level']!=='')$where[]=['level','=',$param['level']];
  19. if($param['status']!=='')$where[]=['status','=',$param['status']];
  20. if($param['companyNo']!=='')$where[]=['companyNo','=',$param['companyNo']];
  21. if($param['name']!=='')$where[]=['name','like',"%{$param['name']}%"];
  22. $list=$this->model->with(["companyInfo"])->where($where)->order('weight desc,id desc')->paginate(['list_rows'=>$param['size'],'page'=>$param['page']]);
  23. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  24. }
  25. //部门数据获取
  26. public function query(){
  27. $param=$this->request->param(['pid'=>0,'level'=>1,'status'=>'','companyNo'=>'','name'=>'',],'post','trim');
  28. $where=[['is_del','=',0]];
  29. if($param['pid']!=='')$where[]=['pid','=',$param['pid']];
  30. if($param['level']!=='')$where[]=['level','=',$param['level']];
  31. if($param['status']!=='')$where[]=['status','=',$param['status']];
  32. if($param['companyNo']!=='')$where[]=['companyNo','=',$param['companyNo']];
  33. if($param['name']!=='')$where[]=['name','like',"%{$param['name']}%"];
  34. $list=$this->model->with(['companyInfo'])->where($where)->order('weight desc,id desc')->select();
  35. return success('获取成功',$list);
  36. }
  37. //部门数据创建
  38. public function create(){
  39. $param=$this->request->param(['pid'=>0,'status'=>1,'is_del'=>0,'companyNo'=>'','name'=>'',
  40. 'weight'=>'',],'post','trim');
  41. $val = Validate::rule([
  42. 'companyNo|所属企业' => 'require|max:255',
  43. 'is_del|删除状态' => 'number',
  44. 'name|部门名称' => 'require|max:255|unique:app\user\model\CompanyItem,name^is_del^companyNo',
  45. 'pid|父级id' => 'require|number|egt:0',
  46. 'weight|排序权重' => 'number|gt:0',
  47. ]);
  48. if ($val->check($param) == false) return error( $val->getError());
  49. $depart_link="";
  50. if($param['pid']!=0){
  51. $parent = $this->model->findOrEmpty($param['pid']);
  52. if($parent->isEmpty())return error("未找到父级部门信息");
  53. $depart_link=array_filter(array_column($parent->depart_link,'id'));
  54. }
  55. $info = [
  56. "companyNo"=>$param['companyNo'],
  57. "name"=>$param['name'],
  58. "pid"=>$param['pid'],
  59. "depart_link"=>$depart_link,
  60. "weight"=>$param['weight'],
  61. "status"=>$param['status'],
  62. ];
  63. $model=CompanyItem::create($info);
  64. return$model->isEmpty()?error("创建失败") :success('创建成功');
  65. }
  66. //部门数据编辑save
  67. public function save(){
  68. $param=$this->request->param(['id'=>0,'pid'=>0,'status'=>1,'is_del'=>0,'companyNo'=>'','name'=>'',
  69. 'weight'=>'',],'post','trim');
  70. $val = Validate::rule([
  71. 'id|部门主键' => 'require|number|gt:0',
  72. 'companyNo|所属企业' => 'require|max:255',
  73. 'is_del|删除状态' => 'number',
  74. 'name|部门名称' => 'require|max:255|unique:app\user\model\CompanyItem,name^is_del^companyNo',
  75. 'pid|父级id' => 'require|number|egt:0',
  76. 'weight|排序权重' => 'number|gt:0',
  77. 'status|部门状态' => 'number|in:0,1',
  78. ]);
  79. if ($val->check($param) == false) return error( $val->getError());
  80. $info = $this->model->findOrEmpty($param['id']);
  81. if($info->isEmpty())return error('未找到部门信息');
  82. if($param['pid']!=0 && $param['pid']!=$info->pid){
  83. $parent = $this->model->findOrEmpty($param['pid']);
  84. if($parent->isEmpty())return error('未找到父级部门信息');
  85. $info->depart_link=array_filter(array_column($parent->depart_link,'id'));
  86. }
  87. $info->companyNo=$param['companyNo'];
  88. $info->name=$param['name'];
  89. $info->pid=$param['pid'];
  90. $info->weight=$param['weight'];
  91. $info->status=$param['status'];
  92. $model=$info->save();
  93. return$model==false?error('修改失败') :success('修改成功');
  94. }
  95. public function delete(){
  96. $id = $this->request->post('id/d');
  97. $items =$this->model->findOrEmpty($id);
  98. if($items->isEmpty())return error('部门信息不存在');
  99. $isT = \app\user\model\AccountItem::where("itemid",$id)->select();
  100. if($isT->isEmpty()==false)return error("部门存在关联人员无法删除");
  101. $items->is_del =1;
  102. $result= $items->save();
  103. return $result ? success('删除成功'): error('删除失败');
  104. }
  105. public function deleteAccount(){
  106. $id = $this->request->post('id/d');
  107. $items =$this->model->findOrEmpty($id);
  108. if($items->isEmpty())return error('部门信息不存在');
  109. $isT = \app\user\model\AccountItem::where('itemid',$id)->select();
  110. if($isT->isEmpty())return error('部门关联人员已解除');
  111. $del= \app\user\model\AccountItem::where('itemid',$id)->delete();
  112. return $del ? success('部门关联人员解除成功'): error('部门关联人员解除失败');
  113. }
  114. }