WorkRole.php 12 KB

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