WorkRole.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace app\bug\controller;
  3. use app\bug\model\WorkAction;
  4. use app\bug\model\WorkUser;
  5. use app\user\model\Account;
  6. use app\user\model\AccountCompany;use think\App;
  7. use think\Collection;use think\facade\Validate;
  8. class WorkRole extends Base{
  9. public function __construct(App $app) {
  10. parent::__construct($app);
  11. $this->model= new \app\bug\model\WorkRole();
  12. }
  13. public function create(){
  14. $param = $this->request->param(["role_name"=>"","companyNo"=>"","level"=>"","work_id"=>"","belong"=>"","action"=>[]],"post","trim");
  15. $valid=Validate::rule([
  16. "role_name|角色名称"=>"require|max:255|unique:\app\bug\model\WorkRole,role_name^companyNo^belong",
  17. "companyNo|业务公司"=>"max:255",
  18. "level|等级"=>"require|number|in:1,2,3,4",
  19. "work_id|岗位模板id"=>"require|number",
  20. "action|角色权限集合"=>"require|array"
  21. ]);
  22. if($valid->check($param)==false) return error($valid->getError());
  23. if($param['level']!=3&& $param['companyNo']=="")return error('业务公司不能为空');
  24. $template = \app\bug\model\Work::with(["workTemplate"])->findOrEmpty($param['work_id']);
  25. if($template->isEmpty()) return error("岗位模板不存在");
  26. if($template->status==0)return error('岗位模板已禁用');
  27. if(empty($template->action??[]))return error('岗位模板权限不能为空');
  28. if(empty(array_diff($param['action'],$template->action))==false)return error('选择权限超出模板权限范围');
  29. $data=[
  30. "role_name"=>$param['role_name'],
  31. "companyNo"=>$param['companyNo'],
  32. "work_id"=>$param['work_id'],
  33. "level"=>$param['level'],
  34. "belong"=>$template->belong,
  35. "action"=>$param["action"],
  36. "process"=>[],
  37. "apply_id"=>$this->uid,
  38. "apply_name"=>$this->uname,
  39. ];
  40. $add= $this->model->save($data);
  41. return $add ? success('岗位角色创建成功'):error('岗位角色创建失败');
  42. }
  43. public function save(){
  44. $param = $this->request->param(["id"=>"",'role_name'=>'',"level"=>"",'companyNo'=>'',"belong"=>"",'work_id'=>'','action'=>[]],'post','trim');
  45. $valid=Validate::rule([
  46. 'id|岗位角色id'=>'require|number|gt:0',
  47. 'role_name|角色名称'=>'require|max:255|unique:\app\bug\model\WorkRole,role_name^companyNo^belong',
  48. 'companyNo|业务公司'=>'max:255',
  49. 'level|等级'=>'require|number|in:1,2,3,4',
  50. 'work_id|岗位模板id'=>'require|number',
  51. 'action|角色权限集合'=>'require|array'
  52. ]);
  53. if($valid->check($param)==false) return error($valid->getError());
  54. $info = $this->model->findOrEmpty($param['id']);
  55. if($param['level']!=3&& $param['companyNo']=='')return error('业务公司不能为空');
  56. if($info->isEmpty()) return error('岗位角色不存在');
  57. $template = \app\bug\model\Work::with(['workTemplate'])->findOrEmpty($param['work_id']);
  58. if($template->isEmpty()) return error('岗位模板不存在');
  59. if($template->status==0)return error('岗位模板已禁用');
  60. if(empty($template->action??[]))return error('岗位模板权限不能为空');
  61. if(empty(array_diff($param['action'],$template->action))==false)return error('选择权限超出模板权限范围');
  62. // $user = WorkUser::where(['role_id'=>$param['id'],"status"=>[1,2]])->findOrEmpty();
  63. // if($user->isEmpty()==false)return error('岗位角色有账户正在使用中无法修改');
  64. $info->role_name= $param['role_name'];
  65. $info->companyNo= $param['companyNo'];
  66. $info->level= $param['level'];
  67. $info->work_id= $param['work_id'];
  68. $info->belong= $template->belong;
  69. $info->action= $param['action'];
  70. $info->status= 0;
  71. $add= $info->save();
  72. if($add==false)return error('岗位角色修改失败');
  73. event('role_sync',['info_id'=>$info->id,'type'=>'off']);
  74. return success('岗位角色修改成功');
  75. }
  76. public function info(){
  77. $id = $this->request->post("id/d");
  78. $info = $this->model->findOrEmpty($id);
  79. if($info->isEmpty()) return error('岗位角色不存在');
  80. $info->actionInfo= (new WorkAction())->GetTreeActionByIdArr($info->action,$info->belong);
  81. return success('获取成功',$info);
  82. }
  83. public function status(){
  84. $param = $this->request->param(['id'=>'','status'=>''],'post','trim');
  85. $valid = Validate::rule([
  86. 'id|账户角色ID'=>'require|number|gt:0',
  87. 'status|状态'=>'require|number|in:0,1'
  88. ]);
  89. if($valid->check($param)==false) return error($valid->getError());
  90. $info = $this->model->with(['companyInfo','workInfo'])->findOrEmpty($param['id']);
  91. if($info->isEmpty())return error('岗位角色信息不存在');
  92. $info->status=$param['status'];
  93. $add = $info->save();
  94. $messg= \app\bug\model\WorkRole::$statusCn[$param['status']];
  95. if($add==false)return error("岗位角色{$messg}失败");
  96. event('role_sync',['info_id'=>$info->id,'type'=>$param['status']==1?"on":'off']);
  97. return success("岗位角色{$messg}成功");
  98. }
  99. public function delete(){
  100. $id = $this->request->post('id/d');
  101. $info = $this->model->findOrEmpty($id);
  102. if($info->isEmpty()) return error('岗位角色不存在');
  103. $user = WorkUser::where(["role_id"=>$id])->findOrEmpty();
  104. if($user->isEmpty()==false)return error('岗位角色有账户正在使用无法删除');
  105. $delete = $info->delete();
  106. if($delete==false)return error('删除失败');
  107. event('role_sync',["info_id"=>$id,"type"=>"delete"]);
  108. return success('删除成功');
  109. }
  110. public function list(){
  111. $param = $this->request->param(['role_name'=>'','companyNo'=>'','belong'=>'','level'=>'','work_id'=>'',
  112. 'status'=>'','page'=>1,"size"=>15],'post','trim');
  113. $where=[];
  114. if($param['companyNo']!='')$where[]=['companyNo','=',$param['companyNo']];
  115. if($param['belong']!='')$where[]=['belong','=',$param['belong']];
  116. if($param['level']!='')$where[]=['level','=',$param['level']];
  117. if($param['status']!='')$where[]=['status','=',$param['status']];
  118. if($param['role_name']!='')$where[]=['role_name','like',"%{$param['role_name']}%"];
  119. if($param['work_id']!='')$where[]=['work_id','=',$param['work_id']];
  120. $list =$this->model->with(['companyInfo','workInfo'])->where($where)->order('id desc')->paginate(['list_rows'=>$param['size'],
  121. 'page'=>$param['page']]);
  122. foreach ($list->items() as &$item){
  123. $item["usedNum"] = WorkUser::where(['role_id'=>$item->id])->count();
  124. }
  125. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  126. }
  127. public function WorkRoleCreate(){
  128. $param=$this->request->param([
  129. "account_id"=>"",
  130. "role_id"=>"",
  131. "companyNo"=>"",
  132. ],"post","trim");
  133. $valid = Validate::rule([
  134. "account_id|账户ID"=>"require|number|gt:0",
  135. "role_id|岗位角色Id"=>"require|number|gt:0",
  136. "companyNo|业务公司"=>"require|max:255",
  137. ]);
  138. if($valid->check($param)==false) return error($valid->getError());
  139. $userinfo = Account::with(["userInfo"])->where(["is_del"=>0])->findOrEmpty($param['account_id']);
  140. if($userinfo->isEmpty())return error("用户信息不存在");
  141. $roleInfo = $this->model->findOrEmpty($param['role_id']);
  142. if($roleInfo->isEmpty())return error('角色信息不存在');
  143. $data=[
  144. "account_id"=>$param['account_id'],
  145. "role_id" => $param['role_id'],
  146. "companyNo" => $param['companyNo'],
  147. 'apply_id'=>$this->uid,
  148. 'apply_name'=>$this->uname,
  149. ];
  150. $add =WorkUser::create($data);
  151. return $add ? success('账户角色设置成功'):error('账户角色设置失败');
  152. }
  153. public function WorkRoleSave(){
  154. $param=$this->request->param(['id'=>'',"account_id"=>"","companyNo"=>"",'role_id'=>''],'post','trim');
  155. $valid = Validate::rule([
  156. 'id|账户角色ID'=>'require|number|gt:0',
  157. 'account_id|账户ID'=>'require|number|gt:0',
  158. 'role_id|岗位角色Id'=>'require|number|gt:0',
  159. 'companyNo|业务公司'=>'require|max:255',
  160. ]);
  161. if($valid->check($param)==false) return error($valid->getError());
  162. $info = WorkUser::where(["id"=>$param['id']])->findOrEmpty();
  163. if($info->isEmpty())return error('账户角色信息不存在');
  164. if($info->status!=0)return error('账户角色状态不可修改');
  165. $userinfo = Account::with(['userInfo'])->where(['is_del'=>0])->findOrEmpty($param['account_id']);
  166. if($userinfo->isEmpty())return error('用户信息不存在');
  167. $roleInfo = $this->model->findOrEmpty($param['role_id']);
  168. if($roleInfo->isEmpty())return error('角色信息不存在');
  169. $info->account_id=$param['account_id'];
  170. $info->role_id=$param['role_id'];
  171. $info->companyNo=$param['companyNo'];
  172. $add = $info->save();
  173. return $add ? success('账户角色设置成功'):error('账户角色设置失败');
  174. }
  175. public function WorkRoleStatus(){
  176. $param=$this->request->param(['id'=>'','status'=>''],'post','trim');
  177. $valid = Validate::rule([
  178. 'id|账户角色ID'=>'require|number|gt:0',
  179. 'status|状态'=>'require|number|in:0,1,4'
  180. ]);
  181. if($valid->check($param)==false) return error($valid->getError());
  182. $info = WorkUser::where(['id'=>$param['id']])->findOrEmpty();
  183. if($info->isEmpty())return error('账户角色信息不存在');
  184. $info->status=$param['status'];
  185. $add = $info->save();
  186. $messg= WorkUser::$statusCn[$param['status']];
  187. if($add==false)return error("账户角色{$messg}失败");
  188. event("user_sync",["info_id"=>$info->id,"type"=>$param['status']==1?"on":'off']);
  189. return success("账户角色{$messg}成功");
  190. }
  191. public function WorkRoleInfo(){
  192. $id=$this->request->post("id/d");
  193. $info = WorkUser::with(['account',"workRole"])->where(['id'=>$id])->findOrEmpty();
  194. if($info->isEmpty())return error('账户角色信息不存在');
  195. $info->statusCn= WorkUser::$statusCn[$info->status];
  196. return success("获取成功",$info);
  197. }
  198. public function WorkRoleInfoByCondition(){
  199. $param=$this->request->param(["companyNo"=>"","belong"=>"","account_id"=>""],"post","trim");
  200. $valid=Validate::rule([
  201. "companyNo|业务公司"=>"require|max:255",
  202. "belong|系统归属"=>"require|number|in:1,2,3",
  203. "account_id|账户id"=>"require|number|gt:0",
  204. ]);
  205. if($valid->check($param)==false)return error($valid->getError());
  206. $info = WorkUser::withJoin(['workRole'],"left")
  207. ->where([["work_user.companyNo","=",$param['companyNo']],["belong","=",$param["belong"]],['account_id','=',
  208. $param['account_id']]])
  209. ->findOrEmpty();
  210. if($info->isEmpty())return error('账户角色信息不存在');
  211. $info->statusCn= WorkUser::$statusCn[$info->status];
  212. return success('获取成功',$info);
  213. }
  214. public function WorkRoleDelete(){
  215. $id=$this->request->post('id/d');
  216. $info = WorkUser::where(['id'=>$id])->findOrEmpty();
  217. if($info->isEmpty())return error('账户角色信息不存在');
  218. if($info->status==1 || $info->status==2 )return error('账户角色状态不可删除');
  219. $del = $info->delete();
  220. if($del==false)return error('账户角色删除失败');
  221. event("user_sync",["info_id"=>$id,"type"=>'del']);
  222. return success('删除成功');
  223. }
  224. public function WorkRoleList(){
  225. $param = $this->request->param(['companyNo'=>'','status'=>'','level'=>'','apply_id'=>'',
  226. 'belong'=>1,'account_id'=>'','page'=>1,'size'=>15],'post','trim');
  227. $where=[["a.is_del","=",0]];
  228. $workWhere=[['work_user.status','in',[1,2]]];
  229. if($param['companyNo']!='')$where[]=['companyCode','=',$param['companyNo']];
  230. if($param['level']!='')$where[]=['level','=',$param['level']];
  231. if($param['account_id']!='')$where[]=['a.id','=',$param['account_id']];
  232. if($param['belong']!=''){
  233. $workWhere[]=["belong","=",$param['belong']];
  234. }
  235. if($param['apply_id']!=''){
  236. $workWhere[]=['workUser.apply_id','=',$param['apply_id']];
  237. }
  238. $uidArr = WorkUser::withJoin(['workRole'],"left")->where($workWhere)->column('account_id');
  239. if($param['status']!==''){
  240. if($param['status']==1){
  241. $where[]=['a.id','in',$uidArr];
  242. }else{
  243. $where[]=['a.id','not in',$uidArr];
  244. }
  245. }
  246. $list =Account::alias("a")
  247. ->leftJoin("account_company b","a.id=b.account_id and b.is_del=0")
  248. ->where($where)
  249. ->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")
  250. ->order('b.id desc')
  251. ->paginate(['list_rows'=>$param['size'],'page'=>$param['page']]);
  252. foreach ($list->items() as &$item){
  253. $uidArr = WorkUser::withJoin(['workRole'],'left')
  254. ->where(['account_id'=>$item['account_id'],'work_user.companyNo'=>$item['companyCode'],"belong"=>$param['belong']])
  255. ->findOrEmpty();
  256. $userinfo = Account::with(['userInfo',"accountItem"=>["itemName"]])->where(['is_del'=>0])
  257. ->findOrEmpty($item['account_id']);
  258. $item['nickname'] =$userinfo->nickname??'';
  259. $item['user_mobile'] =$userinfo->user_mobile??'';
  260. $item['openId'] =$userinfo->openId??'';
  261. $item['field_deny'] =$userinfo->field_deny??[];
  262. $item['depart_name']=$userinfo->depart_name??'';
  263. $item['itemid']=$userinfo->itemid??0;
  264. $item['depart_link']=$userinfo->depart_link??[];
  265. $item['position']=$userinfo->position??'';
  266. $item['role_name'] ='';
  267. $item['user_role_id'] =0;
  268. $item['role_status'] =0;
  269. $item['apply_name'] = '';
  270. $item['usedNum']=0;
  271. if($uidArr->isEmpty()==false){
  272. $item['role_name'] = $uidArr->role_name??'';
  273. $item['role_status'] = $uidArr->status??0;
  274. $item['user_role_id'] =$uidArr->id;
  275. $item['apply_name'] = $uidArr->apply_name??'';
  276. $item['usedNum'] = WorkUser::where(['role_id'=>$uidArr->role_id])->count();
  277. }
  278. }
  279. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  280. }
  281. }