Work.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace app\bug\controller;
  3. use app\bug\model\WorkAction;
  4. use app\bug\model\WorkTemplate;
  5. use think\App;
  6. use think\Exception;
  7. use think\facade\Validate;
  8. class Work extends Base{
  9. public function __construct(App $app) {
  10. parent::__construct($app);
  11. $this->model=new \app\bug\model\Work();
  12. }
  13. //岗位模板新建
  14. public function create(){
  15. $param=$this->request->param([
  16. "work_name"=>"",
  17. "belong"=>"",
  18. "level"=>"",
  19. "actionInfo"=>[]
  20. ],"post","trim");
  21. $valid =Validate::rule([
  22. "work_name|岗位名称"=>"require|max:200|unique:\app\bug\model\Work,work_name^belong",
  23. "belong|系统归属"=>"require|number|in:1,2,3",
  24. "level|账户等级"=>"require|number|in:1,2,3",
  25. "action|功能权限"=>"array"
  26. ]);
  27. if($valid->check($param)==false)return error($valid->getError());
  28. $data = [
  29. 'work_name'=>$param['work_name'],
  30. 'belong'=>$param['belong'],
  31. 'level'=>$param['level'],
  32. 'apply_id'=>$this->uid,
  33. 'apply_name'=>$this->uname,
  34. ];
  35. $action=[];
  36. if(!empty($param['actionInfo'])){
  37. $valid_action =Validate::rule([
  38. 'belong_action|系统功能集合'=>'require|array',
  39. 'belong_process|系统流程集合'=>'array',
  40. 'belong_private|系统私有功能集合'=>'array',
  41. 'menu_id|菜单页面Id'=>'require|number',
  42. 'action_name|岗位功能名称'=>'require|max:255',
  43. ]);
  44. foreach ($param['actionInfo'] as $item){
  45. if($valid_action->check($item)==false)return error($valid_action->getError());
  46. $item['belong']=$param['belong'];
  47. $item['apply_id']=$this->uid;
  48. $item['apply_name']=$this->uname;
  49. $action[]=$item;
  50. }
  51. }
  52. $this->model->startTrans();
  53. try {
  54. $add = \app\bug\model\Work::create($data);
  55. if($add->isEmpty()) throw new Exception("岗位创建失败");
  56. if(!empty($action)){
  57. $tempAction = (new WorkAction())->CreateByArray($action);
  58. $Template = [
  59. "work_id"=>$add->id,
  60. "action"=>$tempAction,
  61. 'apply_id'=>$this->uid,
  62. 'apply_name'=>$this->uname,
  63. ];
  64. $temp=WorkTemplate::create($Template);
  65. if($temp->isEmpty()) throw new Exception('岗位创建失败');
  66. }
  67. $this->model->commit();
  68. return success('创建成功');
  69. }catch (\Exception $exception){
  70. $this->model->rollback();
  71. return error($exception->getMessage());
  72. }
  73. }
  74. //岗位模板新建
  75. public function save(){
  76. $param=$this->request->param(["id"=>"",'work_name'=>'','belong'=>'','action'=>[],'level'=>''],'post','trim');
  77. $valid =Validate::rule([
  78. 'id|岗位ID'=>'require|number|gt:0',
  79. 'work_name|岗位名称'=>'require|max:200|unique:\app\bug\model\Work,work_name^belong',
  80. 'belong|系统归属'=>'require|number|in:1,2,3',
  81. 'action|功能权限'=>'array',
  82. 'level|账户等级'=>'require|number|in:1,2,3'
  83. ]);
  84. if($valid->check($param)==false)return error($valid->getError());
  85. $info = $this->model->with(['workTemplate'])->findOrEmpty($param['id']);
  86. if($info->isEmpty()) error("未找到数据");
  87. $info->work_name= $param['work_name'];
  88. $info->belong= $param['belong'];
  89. $info->level= $param['level'];
  90. $info->work_name= $param['work_name'];
  91. $this->model->startTrans();
  92. try {
  93. $add = $info->save();
  94. if($add==false) throw new Exception('岗位编辑失败');
  95. if(is_null($info->workTemplate)){
  96. $up=$info->workTemplate()
  97. ->save([
  98. 'action'=>$param['action'],
  99. 'work_id'=>$info->id,
  100. 'apply_id'=>$this->uid,
  101. 'apply_name'=>$this->uname,
  102. ]);
  103. }else{
  104. $info->workTemplate->action=$param['action'];
  105. $info->workTemplate->apply_id=$this->uid;
  106. $info->workTemplate->apply_name=$this->uname;
  107. $up=$info->workTemplate->save();
  108. }
  109. if($up==false) throw new Exception('岗位编辑失败');
  110. $this->model->commit();
  111. return success('岗位编辑成功');
  112. }catch (\Exception $exception){
  113. $this->model->rollback();
  114. return error($exception->getMessage());
  115. }
  116. }
  117. public function info(){
  118. $id=$this->request->post("id/d");
  119. $info = $this->model->with(['workTemplate'])->findOrEmpty($id);
  120. if($info->isEmpty()) error("未找到数据");
  121. if(!is_null($info->workTemplate))$info->workTemplate->actionInfo= (new WorkAction())->GetTreeActionByIdArr
  122. ($info->workTemplate->action,$info->belong);
  123. return success('查询成功',$info);
  124. }
  125. //岗位模板新建
  126. public function delete(){
  127. $id=$this->request->post("id/d");
  128. $info = $this->model->findOrEmpty($id);
  129. if($info->isEmpty())return error('未找到数据');
  130. $used = \app\bug\model\WorkRole::where('work_id','=',$id)->findOrEmpty();
  131. if(!$used->isEmpty())return error('该岗位已使用,不能删除');
  132. $add = $info->delete();
  133. return $add ? success('删除成功'):error('删除失败');
  134. }
  135. //岗位模板新建
  136. public function list(){
  137. $param=$this->request->param(['work_name'=>'','belong'=>'','status'=>'','level'=>'','size'=>1,'page'=>15],'post','trim');
  138. $where=[];
  139. if($param['level']!='')$where[]=['level','=',$param['level']];
  140. if($param['belong']!='')$where[]=['belong','=',$param['belong']];
  141. if($param['status']!='')$where[]=['status','=',$param['status']];
  142. if($param['work_name']!='')$where[]=['work_name','like',"%{$param['work_name']}%"];
  143. $list =$this->model->where($where)->order('id desc')->paginate(['list_rows'=>$param['size'],'page'=>$param['page']]);
  144. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  145. }
  146. //岗位模板新建
  147. public function status(){
  148. $param=$this->request->param(['id'=>'','status'=>''],'post','trim');
  149. $valid =Validate::rule([
  150. 'id|岗位ID'=>'require|number|gt:0',
  151. 'status|状态'=>'require|number|in:0,1'
  152. ]);
  153. if($valid->check($param)==false)return error($valid->getError());
  154. $info = $this->model->findOrEmpty($param['id']);
  155. if($info->isEmpty())return error('未找到数据');
  156. $info->status= $param['status'];
  157. $status=\app\bug\model\Work::$statusCn[$param['status']];
  158. $add = $info->save();
  159. return $add ? success("{$status}成功"):error("{$status}失败");
  160. }
  161. public function WorkActionCreate(){
  162. $param=$this->request->param([
  163. 'belong_action'=>[],
  164. 'belong_process'=>[],
  165. 'belong_private'=>[],
  166. 'menu_id'=>'',
  167. 'work_id'=>'',
  168. 'belong'=>0,
  169. 'action_name'=>''],'post','trim');
  170. $valid =Validate::rule([
  171. 'belong_action|系统功能集合'=>'require|array',
  172. 'belong_process|系统流程集合'=>'array',
  173. 'belong_private|系统流程集合'=>'array',
  174. 'menu_id|菜单页面Id'=>'require|number',
  175. 'work_id|岗位Id'=>'require|number',
  176. 'belong|系统归属'=>'number|in:0,1,2,3',
  177. 'action_name|功能名称'=>'require|max:255',
  178. ]);
  179. if($valid->check($param)==false)return error($valid->getError());
  180. $work = $this->model->with(["workTemplate"])->findOrEmpty($param['work_id']);
  181. if($work->isEmpty())return error("岗位信息不存在");
  182. $this->model->startTrans();
  183. try{
  184. $data = [
  185. 'action_name'=>$param['action_name'],
  186. 'belong'=>$param['belong'],
  187. 'menu_id'=>$param['menu_id'],
  188. 'belong_action'=>$param['belong_action'],
  189. 'belong_process'=>$param['belong_process'],
  190. 'belong_private'=>$param['belong_private'],
  191. 'apply_id'=>$this->uid,
  192. 'apply_name'=>$this->uname,
  193. ];
  194. $add = WorkAction::create($data);
  195. if($add->isEmpty()) return error("功能新建失败");
  196. if(is_null($work->workTemplate)){
  197. $up=$work->workTemplate()
  198. ->save([
  199. "action"=>[$add->id],
  200. "work_id"=>$param['work_id'],
  201. "apply_id"=>$this->uid,
  202. "apply_name"=>$this->uname,
  203. ]);
  204. }else{
  205. $action = $work->workTemplate->action;
  206. array_push($action,$add->id);
  207. $work->workTemplate->action=$action;
  208. $work->workTemplate->apply_id=$this->uid;
  209. $work->workTemplate->apply_name=$this->uname;
  210. $up=$work->workTemplate->save();
  211. }
  212. if($up==false) return error('功能新建失败');
  213. $this->model->commit();
  214. return success("创建成功");
  215. }catch (\Exception $e){
  216. $this->model->rollback();
  217. return error($e->getMessage());
  218. }
  219. }
  220. public function WorkActionSave(){
  221. $param=$this->request->param([
  222. "id"=>"",
  223. 'belong_action'=>[],
  224. 'belong_process'=>[],
  225. 'belong_private'=>[],
  226. 'menu_id'=>'',
  227. 'belong'=>0,
  228. 'action_name'=>''],'post','trim');
  229. $valid =Validate::rule([
  230. 'id|岗位功能ID'=>'require|number',
  231. 'belong_action|系统功能集合'=>'require|array',
  232. 'belong_process|系统流程集合'=>'array',
  233. 'belong_private|系统私有功能集合'=>'array',
  234. 'menu_id|菜单页面Id'=>'require|number',
  235. 'belong|系统归属'=>'number|in:0,1,2,3',
  236. 'action_name|岗位功能名称'=>'require|max:255',
  237. ]);
  238. if($valid->check($param)==false)return error($valid->getError());
  239. $info= WorkAction::where(["id"=>$param['id']])->findOrEmpty();
  240. if($info->isEmpty())return error("数据为找到");
  241. $info->action_name=$param['action_name'];
  242. $info->belong=$param['belong'];
  243. $info->menu_id=$param['menu_id'];
  244. $info->belong_action=$param['belong_action'];
  245. $info->belong_process=$param['belong_process'];
  246. $info->belong_private=$param['belong_private'];
  247. $add = $info->save();
  248. return $add ? success('岗位功能修改成功'):error('岗位功能修改失败');
  249. }
  250. public function WorkActionDelete(){
  251. $id=$this->request->post('id/d');
  252. $info = WorkAction::where(['id'=>$id])->findOrEmpty();
  253. if($info->isEmpty()) error('未找到数据');
  254. $add = $info->delete();
  255. return $add ? success('删除成功'):error('删除失败');
  256. }
  257. public function WorkActionInfo(){
  258. $id=$this->request->post('id/d');
  259. $info = WorkAction::where(['id'=>$id])->findOrEmpty();
  260. if($info->isEmpty()) error('未找到数据');
  261. $meun = $info->GetMenuList($info->menu_id,$info->belong);
  262. $info->belong_action_info = $info->GetActionList($info->belong_action,$info->belong);
  263. $info->belong_private_info = $info->GetActionList($info->belong_private,$info->belong);
  264. $info->belong_process_info = $info->GetProcessList($info->belong_process,$info->belong);
  265. return success('获取成功',array_merge($info->toArray(),$meun));
  266. }
  267. public function WorkActionList(){
  268. $param=$this->request->param(['menu_id'=>'','belong'=>0,'action_name'=>'',"page"=>1,"size"=>15],'post','trim');
  269. $where=[];
  270. if($param['menu_id']!="")$where[]=["menu_id","=",$param['menu_id']];
  271. if($param['belong']!="")$where[]=["belong","=",$param['belong']];
  272. if($param['action_name']!="")$where[]=["action_name","like","%{$param['action_name']}%"];
  273. $list = WorkAction::where($where)->order("id desc")->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  274. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  275. }
  276. public function WorkTemplateSave(){
  277. $param=$this->request->param(["work_id"=>"","action"=>[]],"post","trim");
  278. $valid =Validate::rule([
  279. 'work_id|岗位ID'=>'require|number',
  280. 'action|岗位功能集合'=>'require|array'
  281. ]);
  282. if($valid->check($param)==false)return error($valid->getError());
  283. $info= WorkTemplate::where(['work_id'=>$param['work_id']])->findOrEmpty();
  284. $info->work_id=$param['work_id'];
  285. $info->action=$param['action'];
  286. $add = $info->save();
  287. return $add ? success('岗位模板修改成功'):error('岗位模板修改失败');
  288. }
  289. public function WorkTemplateInfo(){
  290. $work_id=$this->request->post('work_id/d');
  291. $info= WorkTemplate::with(["workInfo"])->where(['work_id'=>$work_id])->findOrEmpty();
  292. if(!$info->isEmpty()) {
  293. $info->actionInfo= (new WorkAction())->GetTreeActionByIdArr($info->action,$info->workInfo->belong);
  294. }
  295. return success('获取成功',$info);
  296. }
  297. }