123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace app\bug\controller;
- use app\bug\model\WorkAction;
- use app\bug\model\WorkUser;
- use app\user\model\Account;
- use app\user\model\AccountCompany;use think\App;
- use think\Collection;use think\facade\Validate;
- class WorkRole extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model= new \app\bug\model\WorkRole();
- }
-
- public function create(){
- $param = $this->request->param(["role_name"=>"","companyNo"=>"","level"=>"","work_id"=>"","belong"=>"","action"=>[]],"post","trim");
- $valid=Validate::rule([
- "role_name|角色名称"=>"require|max:255|unique:\app\bug\model\WorkRole,role_name^companyNo^belong",
- "companyNo|业务公司"=>"max:255",
- "level|等级"=>"require|number|in:1,2,3,4",
- "work_id|岗位模板id"=>"require|number",
- "action|角色权限集合"=>"require|array"
- ]);
- if($valid->check($param)==false) return error($valid->getError());
- if($param['level']!=3&& $param['companyNo']=="")return error('业务公司不能为空');
- $template = \app\bug\model\Work::with(["workTemplate"])->findOrEmpty($param['work_id']);
- if($template->isEmpty()) return error("岗位模板不存在");
- if($template->status==0)return error('岗位模板已禁用');
- if(empty($template->action??[]))return error('岗位模板权限不能为空');
- if(empty(array_diff($param['action'],$template->action))==false)return error('选择权限超出模板权限范围');
- $data=[
- "role_name"=>$param['role_name'],
- "companyNo"=>$param['companyNo'],
- "work_id"=>$param['work_id'],
- "level"=>$param['level'],
- "belong"=>$template->belong,
- "action"=>$param["action"],
- "process"=>[],
- "apply_id"=>$this->uid,
- "apply_name"=>$this->uname,
- ];
- $add= $this->model->save($data);
- return $add ? success('岗位角色创建成功'):error('岗位角色创建失败');
- }
-
- public function save(){
- $param = $this->request->param(["id"=>"",'role_name'=>'',"level"=>"",'companyNo'=>'',"belong"=>"",'work_id'=>'','action'=>[]],'post','trim');
- $valid=Validate::rule([
- 'id|岗位角色id'=>'require|number|gt:0',
- 'role_name|角色名称'=>'require|max:255|unique:\app\bug\model\WorkRole,role_name^companyNo^belong',
- 'companyNo|业务公司'=>'max:255',
- 'level|等级'=>'require|number|in:1,2,3,4',
- 'work_id|岗位模板id'=>'require|number',
- 'action|角色权限集合'=>'require|array'
- ]);
- if($valid->check($param)==false) return error($valid->getError());
- $info = $this->model->findOrEmpty($param['id']);
- if($param['level']!=3&& $param['companyNo']=='')return error('业务公司不能为空');
- if($info->isEmpty()) return error('岗位角色不存在');
- $template = \app\bug\model\Work::with(['workTemplate'])->findOrEmpty($param['work_id']);
- if($template->isEmpty()) return error('岗位模板不存在');
- if($template->status==0)return error('岗位模板已禁用');
- if(empty($template->action??[]))return error('岗位模板权限不能为空');
- if(empty(array_diff($param['action'],$template->action))==false)return error('选择权限超出模板权限范围');
- // $user = WorkUser::where(['role_id'=>$param['id'],"status"=>[1,2]])->findOrEmpty();
- // if($user->isEmpty()==false)return error('岗位角色有账户正在使用中无法修改');
- $info->role_name= $param['role_name'];
- $info->companyNo= $param['companyNo'];
- $info->level= $param['level'];
- $info->work_id= $param['work_id'];
- $info->belong= $template->belong;
- $info->action= $param['action'];
- $info->status= 0;
- $add= $info->save();
- if($add==false)return error('岗位角色修改失败');
- event('role_sync',['info_id'=>$info->id,'type'=>'off']);
- return success('岗位角色修改成功');
- }
-
- public function info(){
- $id = $this->request->post("id/d");
- $info = $this->model->findOrEmpty($id);
- if($info->isEmpty()) return error('岗位角色不存在');
- $info->actionInfo= (new WorkAction())->GetTreeActionByIdArr($info->action,$info->belong);
- return success('获取成功',$info);
- }
- public function status(){
- $param = $this->request->param(['id'=>'','status'=>''],'post','trim');
- $valid = Validate::rule([
- 'id|账户角色ID'=>'require|number|gt:0',
- 'status|状态'=>'require|number|in:0,1'
- ]);
- if($valid->check($param)==false) return error($valid->getError());
- $info = $this->model->with(['companyInfo','workInfo'])->findOrEmpty($param['id']);
- if($info->isEmpty())return error('岗位角色信息不存在');
- $info->status=$param['status'];
- $add = $info->save();
- $messg= \app\bug\model\WorkRole::$statusCn[$param['status']];
- if($add==false)return error("岗位角色{$messg}失败");
- event('role_sync',['info_id'=>$info->id,'type'=>$param['status']==1?"on":'off']);
- return success("岗位角色{$messg}成功");
- }
- public function delete(){
- $id = $this->request->post('id/d');
- $info = $this->model->findOrEmpty($id);
- if($info->isEmpty()) return error('岗位角色不存在');
- $user = WorkUser::where(["role_id"=>$id])->findOrEmpty();
- if($user->isEmpty()==false)return error('岗位角色有账户正在使用无法删除');
- $delete = $info->delete();
- if($delete==false)return error('删除失败');
- event('role_sync',["info_id"=>$id,"type"=>"delete"]);
- return success('删除成功');
- }
-
- public function list(){
- $param = $this->request->param(['role_name'=>'','companyNo'=>'','belong'=>'','level'=>'','work_id'=>'',
- 'status'=>'','page'=>1,"size"=>15],'post','trim');
- $where=[];
- if($param['companyNo']!='')$where[]=['companyNo','=',$param['companyNo']];
- if($param['belong']!='')$where[]=['belong','=',$param['belong']];
- if($param['level']!='')$where[]=['level','=',$param['level']];
- if($param['status']!='')$where[]=['status','=',$param['status']];
- if($param['role_name']!='')$where[]=['role_name','like',"%{$param['role_name']}%"];
- if($param['work_id']!='')$where[]=['work_id','=',$param['work_id']];
- $list =$this->model->with(['companyInfo','workInfo'])->where($where)->order('id desc')->paginate(['list_rows'=>$param['size'],
- 'page'=>$param['page']]);
- foreach ($list->items() as &$item){
- $item["usedNum"] = WorkUser::where(['role_id'=>$item->id])->count();
- }
- return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
- }
-
- public function WorkRoleCreate(){
- $param=$this->request->param([
- "account_id"=>"",
- "role_id"=>"",
- "companyNo"=>"",
- ],"post","trim");
- $valid = Validate::rule([
- "account_id|账户ID"=>"require|number|gt:0",
- "role_id|岗位角色Id"=>"require|number|gt:0",
- "companyNo|业务公司"=>"require|max:255",
- ]);
- if($valid->check($param)==false) return error($valid->getError());
- $userinfo = Account::with(["userInfo"])->where(["is_del"=>0])->findOrEmpty($param['account_id']);
- if($userinfo->isEmpty())return error("用户信息不存在");
- $roleInfo = $this->model->findOrEmpty($param['role_id']);
- if($roleInfo->isEmpty())return error('角色信息不存在');
- $data=[
- "account_id"=>$param['account_id'],
- "role_id" => $param['role_id'],
- "companyNo" => $param['companyNo'],
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- ];
- $add =WorkUser::create($data);
- return $add ? success('账户角色设置成功'):error('账户角色设置失败');
- }
-
- public function WorkRoleSave(){
- $param=$this->request->param(['id'=>'',"account_id"=>"","companyNo"=>"",'role_id'=>''],'post','trim');
- $valid = Validate::rule([
- 'id|账户角色ID'=>'require|number|gt:0',
- 'account_id|账户ID'=>'require|number|gt:0',
- 'role_id|岗位角色Id'=>'require|number|gt:0',
- 'companyNo|业务公司'=>'require|max:255',
- ]);
- if($valid->check($param)==false) return error($valid->getError());
- $info = WorkUser::where(["id"=>$param['id']])->findOrEmpty();
- if($info->isEmpty())return error('账户角色信息不存在');
- if($info->status!=0)return error('账户角色状态不可修改');
- $userinfo = Account::with(['userInfo'])->where(['is_del'=>0])->findOrEmpty($param['account_id']);
- if($userinfo->isEmpty())return error('用户信息不存在');
- $roleInfo = $this->model->findOrEmpty($param['role_id']);
- if($roleInfo->isEmpty())return error('角色信息不存在');
- $info->account_id=$param['account_id'];
- $info->role_id=$param['role_id'];
- $info->companyNo=$param['companyNo'];
- $add = $info->save();
- return $add ? success('账户角色设置成功'):error('账户角色设置失败');
- }
-
- public function WorkRoleStatus(){
- $param=$this->request->param(['id'=>'','status'=>''],'post','trim');
- $valid = Validate::rule([
- 'id|账户角色ID'=>'require|number|gt:0',
- 'status|状态'=>'require|number|in:0,1,4'
- ]);
- if($valid->check($param)==false) return error($valid->getError());
- $info = WorkUser::where(['id'=>$param['id']])->findOrEmpty();
- if($info->isEmpty())return error('账户角色信息不存在');
- $info->status=$param['status'];
- $add = $info->save();
- $messg= WorkUser::$statusCn[$param['status']];
- if($add==false)return error("账户角色{$messg}失败");
- event("user_sync",["info_id"=>$info->id,"type"=>$param['status']==1?"on":'off']);
- return success("账户角色{$messg}成功");
- }
-
- public function WorkRoleInfo(){
- $id=$this->request->post("id/d");
- $info = WorkUser::with(['account',"workRole"])->where(['id'=>$id])->findOrEmpty();
- if($info->isEmpty())return error('账户角色信息不存在');
- $info->statusCn= WorkUser::$statusCn[$info->status];
- return success("获取成功",$info);
- }
-
- public function WorkRoleInfoByCondition(){
- $param=$this->request->param(["companyNo"=>"","belong"=>"","account_id"=>""],"post","trim");
- $valid=Validate::rule([
- "companyNo|业务公司"=>"require|max:255",
- "belong|系统归属"=>"require|number|in:1,2,3",
- "account_id|账户id"=>"require|number|gt:0",
- ]);
- if($valid->check($param)==false)return error($valid->getError());
-
- $info = WorkUser::withJoin(['workRole'],"left")
- ->where([["work_user.companyNo","=",$param['companyNo']],["belong","=",$param["belong"]],['account_id','=',
- $param['account_id']]])
- ->findOrEmpty();
- if($info->isEmpty())return error('账户角色信息不存在');
- $info->statusCn= WorkUser::$statusCn[$info->status];
- return success('获取成功',$info);
- }
- public function WorkRoleDelete(){
- $id=$this->request->post('id/d');
- $info = WorkUser::where(['id'=>$id])->findOrEmpty();
- if($info->isEmpty())return error('账户角色信息不存在');
- if($info->status==1 || $info->status==2 )return error('账户角色状态不可删除');
- $del = $info->delete();
- if($del==false)return error('账户角色删除失败');
- event("user_sync",["info_id"=>$id,"type"=>'del']);
- return success('删除成功');
- }
-
- public function WorkRoleList(){
- $param = $this->request->param(['companyNo'=>'','status'=>'','level'=>'','apply_id'=>'',
- 'belong'=>1,'account_id'=>'','page'=>1,'size'=>15],'post','trim');
- $where=[["a.is_del","=",0]];
- $workWhere=[['work_user.status','in',[1,2]]];
- if($param['companyNo']!='')$where[]=['companyCode','=',$param['companyNo']];
- if($param['level']!='')$where[]=['level','=',$param['level']];
- if($param['account_id']!='')$where[]=['a.id','=',$param['account_id']];
- if($param['belong']!=''){
- $workWhere[]=["belong","=",$param['belong']];
- }
- if($param['apply_id']!=''){
- $workWhere[]=['workUser.apply_id','=',$param['apply_id']];
- }
- $uidArr = WorkUser::withJoin(['workRole'],"left")->where($workWhere)->column('account_id');
- if($param['status']!==''){
- if($param['status']==1){
- $where[]=['a.id','in',$uidArr];
- }else{
- $where[]=['a.id','not in',$uidArr];
- }
- }
- $list =Account::alias("a")
- ->leftJoin("account_company b","a.id=b.account_id and b.is_del=0")
- ->where($where)
- ->field("b.id,a.username,a.status,a.level,a.source,b.account_id,b.companyCode,b.companyName,b.company_type,b.is_main,a.addtime,a.updatetime")
- ->order('b.id desc')
- ->paginate(['list_rows'=>$param['size'],'page'=>$param['page']]);
- foreach ($list->items() as &$item){
- $uidArr = WorkUser::withJoin(['workRole'],'left')
- ->where(['account_id'=>$item['account_id'],'work_user.companyNo'=>$item['companyCode'],"belong"=>$param['belong']])
- ->findOrEmpty();
- $userinfo = Account::with(['userInfo',"accountItem"=>["itemName"]])->where(['is_del'=>0])
- ->findOrEmpty($item['account_id']);
- $item['nickname'] =$userinfo->nickname??'';
- $item['user_mobile'] =$userinfo->user_mobile??'';
- $item['openId'] =$userinfo->openId??'';
- $item['field_deny'] =$userinfo->field_deny??[];
- $item['depart_name']=$userinfo->depart_name??'';
- $item['itemid']=$userinfo->itemid??0;
- $item['depart_link']=$userinfo->depart_link??[];
- $item['position']=$userinfo->position??'';
- $item['role_name'] ='';
- $item['user_role_id'] =0;
- $item['role_status'] =0;
- $item['apply_name'] = '';
- $item['usedNum']=0;
- if($uidArr->isEmpty()==false){
- $item['role_name'] = $uidArr->role_name??'';
- $item['role_status'] = $uidArr->status??0;
- $item['user_role_id'] =$uidArr->id;
- $item['apply_name'] = $uidArr->apply_name??'';
- $item['usedNum'] = WorkUser::where(['role_id'=>$uidArr->role_id])->count();
- }
- }
- return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
- }
- }
|