Subscribe.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace app\bug\controller;
  3. use app\bug\model\WorkAction;
  4. use app\bug\model\WorkSubinfo;
  5. use app\bug\model\WorkSubscr;
  6. use app\bug\model\WorkUser;use think\App;
  7. use think\Exception;
  8. use think\facade\Validate;
  9. class Subscribe extends Base{
  10. public function __construct(App $app) {
  11. parent::__construct($app);
  12. $this->model=new WorkSubscr();
  13. }
  14. //岗位功能申请新建
  15. public function create(){
  16. $param=$this->request->param(['action'=>'','role_id'=>'','belong'=>'',"change_action"=>[]],'post','trim');
  17. $valid =Validate::rule([
  18. 'action|岗位角色功能'=>'require|array',
  19. 'role_id|岗位角色Id'=>'require|number',
  20. 'belong|系统归属'=>'require|number|in:1,2,3',
  21. 'change_action|岗位角色功能修改记录'=>'require|array'
  22. ]);
  23. if($valid->check($param)==false)return error($valid->getError());
  24. $action= \app\bug\model\WorkRole::where('id','=',$param['role_id'])->findOrEmpty();
  25. $data = [
  26. 'belong'=>$param['belong'],
  27. 'action'=>$param['action'],
  28. 'role_action'=>$action->action??[],
  29. 'role_id'=>$param['role_id'],
  30. 'apply_id'=>$this->uid,
  31. 'apply_name'=>$this->uname,
  32. ];
  33. $info=[];
  34. if(!empty($param['change_action'])){
  35. $valid_item = Validate::rule([
  36. 'action_id|岗位功能'=>'require|number',
  37. 'type|类型'=>'require|number|in:1,2',
  38. 'remark|备注'=>'max:255'
  39. ]);
  40. $workAction =new WorkAction();
  41. foreach ($param['change_action'] as $item){
  42. if($valid_item->check($item)==false)return error($valid_item->getError());
  43. $temp=[];
  44. $temp['sub_id']="";
  45. $temp['content']=$workAction->GetInfoById($item['action_id'],$param['belong']);
  46. $temp['action_id']=$item['action_id'];
  47. $temp['type']=$item['type'];
  48. $temp['remark']=$item['remark']??"";
  49. $info[]=$temp;
  50. }
  51. }
  52. $this->model->startTrans();
  53. try{
  54. $sub = WorkSubscr::create($data);
  55. if($sub->isEmpty())throw new Exception("功能申请创建失败");
  56. if(empty($info)==false){
  57. foreach ($info as &$item) {
  58. $item['sub_id'] = $sub->id;
  59. }
  60. (new WorkSubinfo)->saveAll($info);
  61. }
  62. $this->model->commit();
  63. return success("功能申请创建成功");
  64. }catch (\Exception $e){
  65. $this->model->rollback();
  66. return error($e->getMessage());
  67. }
  68. }
  69. ////岗位功能申请编辑
  70. public function save(){
  71. $param=$this->request->param(['id'=>'','action'=>'','belong'=>'','change_action'=>[]],'post','trim');
  72. $valid =Validate::rule([
  73. 'id|岗位角色功能申请id'=>'require|number|gt:0',
  74. 'action|岗位角色功能'=>'require|array',
  75. 'belong|系统归属'=>'require|number|in:1,2,3',
  76. 'change_action|岗位角色功能修改记录'=>'require|array'
  77. ]);
  78. if($valid->check($param)==false)return error($valid->getError());
  79. $workInfo= $this->model->findOrEmpty($param['id']);
  80. if($workInfo->isEmpty())return error("功能申请未找到数据");
  81. if($workInfo->status!=1)return error("功能申请当前状态不可修改");
  82. $workInfo->belong= $param['belong'];
  83. $workInfo->action= $param['action'];
  84. $workInfo->apply_id= $this->uid;
  85. $workInfo->apply_name= $this->uid;
  86. $info=[];
  87. $keepId=[];
  88. if(!empty($param['change_action'])){
  89. $valid_item = Validate::rule([
  90. 'action_id|岗位功能'=>'require|array',
  91. 'type|类型'=>'require|number|in:1,2',
  92. 'remark|备注'=>'max:255'
  93. ]);
  94. $workAction =new WorkAction();
  95. foreach ($param['change_action'] as $item){
  96. if($valid_item->check($item)==false)return error($valid_item->getError());
  97. if(isset($item['id']) &&$item['id']>0){
  98. $keepId[]=$item['id'];
  99. continue;
  100. }
  101. $temp=[];
  102. $temp['sub_id']=$workInfo->id;
  103. $temp['content']=$workAction->GetInfoById($item['action_id'],$param['belong']);
  104. $temp['action_id']=$item['action_id'];
  105. $temp['type']=$item['type'];
  106. $temp['remark']=$item['remark']??'';
  107. $info[]=$temp;
  108. }
  109. }
  110. $this->model->startTrans();
  111. try{
  112. $sub = $workInfo->save();
  113. if($sub)throw new Exception('功能申请修改失败');
  114. if(empty($keepId)==false){
  115. $delete =WorkSubinfo::where([["id","not in",$keepId],["sub_id","=",$workInfo->id]])->select();
  116. if($delete->isEmpty()==false){
  117. $del=$delete->delete();
  118. if($del==false)throw new Exception('功能申请修改失败');
  119. }
  120. }
  121. if(empty($info)==false)(new WorkSubinfo)->saveAll($info);
  122. $this->model->commit();
  123. return success('功能申请修改成功');
  124. }catch (\Exception $e){
  125. $this->model->rollback();
  126. return error($e->getMessage());
  127. }
  128. }
  129. //岗位功能申请删除
  130. public function delete(){
  131. $id=$this->request->post('id/d');
  132. $info = $this->model->findOrEmpty($id);
  133. if($info->isEmpty()) error('未找到数据');
  134. if($info->status!=1) error('当前状态不可删除');
  135. $add = $info->delete();
  136. return $add ? success('删除成功'):error('删除失败');
  137. }
  138. //岗位功能申请详情
  139. public function info(){
  140. $id=$this->request->post('id/d');
  141. $info = $this->model->with(["roleInfo"=>['companyInfo'],"subInfo"])->findOrEmpty($id);
  142. if($info->isEmpty()) error('未找到数据');
  143. return success('获取成功',$info);
  144. }
  145. //岗位模板新建
  146. public function list(){
  147. $param=$this->request->param(['status'=>'','role_id'=>'','apply_id'=>'','belong'=>'','start'=>'','end'=>'','size'=>1,
  148. 'page'=>15],'post','trim');
  149. $where=[];
  150. if($param['status']!='')$where[]=['status','=',$param['status']];
  151. if($param['belong']!='')$where[]=['belong','=',$param['belong']];
  152. if($param['role_id']!='')$where[]=['role_id','=',$param['role_id']];
  153. if($param['apply_id']!='')$where[]=['apply_id','=',$param['apply_id']];
  154. if($param['start']!='' && $param['end']!=''){
  155. $where[]=['createTime','between',[startTime($param['start']),endTime($param['end'])]];
  156. }elseif($param['start']!='' && $param['end']==''){
  157. $where[]=['createTime','>',startTime($param['start'])];
  158. }elseif($param['start']=='' && $param['end']!=''){
  159. $where[]=['createTime','<',endTime($param['end'])];
  160. }
  161. $list =$this->model->with(["roleInfo"=>['companyInfo'],'subInfo'])->where($where)->order('id desc')->paginate
  162. (['list_rows'=>$param['size'],
  163. 'page'=>$param['page']]);
  164. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  165. }
  166. //岗位模板新建
  167. public function status(){
  168. $param=$this->request->param(['id'=>'','status'=>''],'post','trim');
  169. $valid =Validate::rule([
  170. 'id|功能申请ID'=>'require|number|gt:0',
  171. 'status|状态'=>'require|number|in:1,2,3,4,5'
  172. ]);
  173. if($valid->check($param)==false)return error($valid->getError());
  174. $info = $this->model->findOrEmpty($param['id']);
  175. if($info->isEmpty())return error('未找到数据');
  176. $info->status= $param['status'];
  177. $info->check_id= $this->uid;
  178. $info->check_name= $this->uname;
  179. $status=WorkSubscr::$statusCn[$param['status']];
  180. $add = $info->save();
  181. return $add ? success("{$status}成功"):error("{$status}失败");
  182. }
  183. public function infoStatus(){
  184. $param=$this->request->param(['id'=>'','status'=>'','remark'=>''],'post','trim');
  185. $valid =Validate::rule([
  186. 'id|功能申请ID'=>'require|number|gt:0',
  187. 'status|状态'=>'require|number|in:1,2',
  188. 'remark|备注'=>'max:255'
  189. ]);
  190. if($valid->check($param)==false)return error($valid->getError());
  191. $info = WorkSubinfo::with(['workSub'])->where("id",$param['id'])->findOrEmpty();
  192. if($info->isEmpty())return error('未找到数据');
  193. if($info->status!=0)return error('数据已审核');
  194. if(in_array($info->workSub->status,[3,4,5]))return error('功能申请状态有误不可审批');
  195. $info->status= $param['status'];
  196. $info->remark= $param['remark'];
  197. $status=WorkSubscr::$statusCn[$param['status']];
  198. $this->model->startTrans();
  199. try{
  200. $add = $info->save();
  201. if($add==false)throw new Exception("{$status}失败");
  202. $statusArr= WorkSubinfo::where('sub_id',$info->sub_id)->column('status');
  203. $statusSub = $info->workSub->status;
  204. if(count(array_unique($statusArr))==1){
  205. if(in_array(1,$statusArr)){
  206. $statusSub = 3;
  207. $info->workSub->is_check=2;
  208. }
  209. if(in_array(2,$statusArr)){
  210. $statusSub =4;
  211. $info->workSub->is_check=2;
  212. }
  213. }else{
  214. if(in_array(1,$statusArr)){
  215. $statusSub = 2;
  216. }
  217. }
  218. $info->workSub->status =$statusSub;
  219. $info->workSub->check_id =$this->uid;
  220. $info->workSub->check_name =$this->uname;
  221. $subup=$info->workSub->save();
  222. if($subup==false)throw new Exception("{$status}失败");
  223. if($info->workSub->is_check==2){
  224. $role=\app\bug\model\WorkRole::where("id",$info->workSub->role_id)->findOrEmpty();
  225. if($role->isEmpty()) throw new Exception("角色不存在");
  226. $role->action = WorkSubscr::GetActionById($info->sub_id);
  227. $roleUp=$role->save();
  228. if($roleUp==false)throw new Exception("角色同步权限失败");
  229. // WorkUser::where(["role_id"=>$info->workSub->role_id,"status"=>[1,2]])->save(["status"=>0]);
  230. }
  231. $this->model->commit();
  232. return success("{$status}成功");
  233. }catch (\Exception $e){
  234. $this->model->rollback();
  235. return error($e->getMessage());
  236. }
  237. }
  238. }