Subscribe.php 7.9 KB

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