123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- namespace app\bug\controller;
- use app\bug\model\WorkAction;
- use app\bug\model\WorkSubinfo;
- use app\bug\model\WorkSubscr;
- use app\bug\model\WorkUser;use think\App;
- use think\Exception;
- use think\facade\Validate;
- class Subscribe extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model=new WorkSubscr();
- }
-
- //岗位功能申请新建
- public function create(){
- $param=$this->request->param(['action'=>'','role_id'=>'','belong'=>'',"change_action"=>[]],'post','trim');
- $valid =Validate::rule([
- 'action|岗位角色功能'=>'require|array',
- 'role_id|岗位角色Id'=>'require|number',
- 'belong|系统归属'=>'require|number|in:1,2,3',
- 'change_action|岗位角色功能修改记录'=>'require|array'
- ]);
- if($valid->check($param)==false)return error($valid->getError());
- $action= \app\bug\model\WorkRole::where('id','=',$param['role_id'])->findOrEmpty();
- $data = [
- 'belong'=>$param['belong'],
- 'action'=>$param['action'],
- 'role_action'=>$action->action??[],
- 'role_id'=>$param['role_id'],
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- ];
- $info=[];
- if(!empty($param['change_action'])){
- $valid_item = Validate::rule([
- 'action_id|岗位功能'=>'require|number',
- 'type|类型'=>'require|number|in:1,2',
- 'remark|备注'=>'max:255'
- ]);
- $workAction =new WorkAction();
- foreach ($param['change_action'] as $item){
- if($valid_item->check($item)==false)return error($valid_item->getError());
- $temp=[];
- $temp['sub_id']="";
- $temp['content']=$workAction->GetInfoById($item['action_id'],$param['belong']);
- $temp['action_id']=$item['action_id'];
- $temp['type']=$item['type'];
- $temp['remark']=$item['remark']??"";
- $info[]=$temp;
- }
- }
- $this->model->startTrans();
- try{
- $sub = WorkSubscr::create($data);
- if($sub->isEmpty())throw new Exception("功能申请创建失败");
- if(empty($info)==false){
- foreach ($info as &$item) {
- $item['sub_id'] = $sub->id;
- }
- (new WorkSubinfo)->saveAll($info);
- }
- $this->model->commit();
- return success("功能申请创建成功");
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
-
- }
-
- ////岗位功能申请编辑
- public function save(){
- $param=$this->request->param(['id'=>'','action'=>'','belong'=>'','change_action'=>[]],'post','trim');
- $valid =Validate::rule([
- 'id|岗位角色功能申请id'=>'require|number|gt:0',
- 'action|岗位角色功能'=>'require|array',
- 'belong|系统归属'=>'require|number|in:1,2,3',
- 'change_action|岗位角色功能修改记录'=>'require|array'
- ]);
- if($valid->check($param)==false)return error($valid->getError());
- $workInfo= $this->model->findOrEmpty($param['id']);
- if($workInfo->isEmpty())return error("功能申请未找到数据");
- if($workInfo->status!=1)return error("功能申请当前状态不可修改");
- $workInfo->belong= $param['belong'];
- $workInfo->action= $param['action'];
- $workInfo->apply_id= $this->uid;
- $workInfo->apply_name= $this->uid;
- $info=[];
- $keepId=[];
- if(!empty($param['change_action'])){
- $valid_item = Validate::rule([
- 'action_id|岗位功能'=>'require|array',
- 'type|类型'=>'require|number|in:1,2',
- 'remark|备注'=>'max:255'
- ]);
- $workAction =new WorkAction();
- foreach ($param['change_action'] as $item){
- if($valid_item->check($item)==false)return error($valid_item->getError());
- if(isset($item['id']) &&$item['id']>0){
- $keepId[]=$item['id'];
- continue;
- }
- $temp=[];
- $temp['sub_id']=$workInfo->id;
- $temp['content']=$workAction->GetInfoById($item['action_id'],$param['belong']);
- $temp['action_id']=$item['action_id'];
- $temp['type']=$item['type'];
- $temp['remark']=$item['remark']??'';
- $info[]=$temp;
- }
- }
- $this->model->startTrans();
- try{
- $sub = $workInfo->save();
- if($sub)throw new Exception('功能申请修改失败');
- if(empty($keepId)==false){
- $delete =WorkSubinfo::where([["id","not in",$keepId],["sub_id","=",$workInfo->id]])->select();
- if($delete->isEmpty()==false){
- $del=$delete->delete();
- if($del==false)throw new Exception('功能申请修改失败');
- }
- }
- if(empty($info)==false)(new WorkSubinfo)->saveAll($info);
- $this->model->commit();
- return success('功能申请修改成功');
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
- }
-
- //岗位功能申请删除
- public function delete(){
- $id=$this->request->post('id/d');
- $info = $this->model->findOrEmpty($id);
- if($info->isEmpty()) error('未找到数据');
- if($info->status!=1) error('当前状态不可删除');
- $add = $info->delete();
- return $add ? success('删除成功'):error('删除失败');
- }
-
- //岗位功能申请详情
- public function info(){
- $id=$this->request->post('id/d');
- $info = $this->model->with(["roleInfo"=>['companyInfo'],"subInfo"])->findOrEmpty($id);
- if($info->isEmpty()) error('未找到数据');
- return success('获取成功',$info);
- }
- //岗位模板新建
- public function list(){
- $param=$this->request->param(['status'=>'','role_id'=>'','apply_id'=>'','belong'=>'','start'=>'','end'=>'','size'=>1,
- 'page'=>15],'post','trim');
- $where=[];
- if($param['status']!='')$where[]=['status','=',$param['status']];
- if($param['belong']!='')$where[]=['belong','=',$param['belong']];
- if($param['role_id']!='')$where[]=['role_id','=',$param['role_id']];
- if($param['apply_id']!='')$where[]=['apply_id','=',$param['apply_id']];
- if($param['start']!='' && $param['end']!=''){
- $where[]=['createTime','between',[startTime($param['start']),endTime($param['end'])]];
- }elseif($param['start']!='' && $param['end']==''){
- $where[]=['createTime','>',startTime($param['start'])];
- }elseif($param['start']=='' && $param['end']!=''){
- $where[]=['createTime','<',endTime($param['end'])];
- }
- $list =$this->model->with(["roleInfo"=>['companyInfo'],'subInfo'])->where($where)->order('id desc')->paginate
- (['list_rows'=>$param['size'],
- 'page'=>$param['page']]);
- return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
- }
- //岗位模板新建
- public function status(){
- $param=$this->request->param(['id'=>'','status'=>''],'post','trim');
- $valid =Validate::rule([
- 'id|功能申请ID'=>'require|number|gt:0',
- 'status|状态'=>'require|number|in:1,2,3,4,5'
- ]);
- if($valid->check($param)==false)return error($valid->getError());
- $info = $this->model->findOrEmpty($param['id']);
- if($info->isEmpty())return error('未找到数据');
- $info->status= $param['status'];
- $info->check_id= $this->uid;
- $info->check_name= $this->uname;
- $status=WorkSubscr::$statusCn[$param['status']];
- $add = $info->save();
- return $add ? success("{$status}成功"):error("{$status}失败");
- }
- public function infoStatus(){
- $param=$this->request->param(['id'=>'','status'=>'','remark'=>''],'post','trim');
- $valid =Validate::rule([
- 'id|功能申请ID'=>'require|number|gt:0',
- 'status|状态'=>'require|number|in:1,2',
- 'remark|备注'=>'max:255'
- ]);
- if($valid->check($param)==false)return error($valid->getError());
- $info = WorkSubinfo::with(['workSub'])->where("id",$param['id'])->findOrEmpty();
- if($info->isEmpty())return error('未找到数据');
- if($info->status!=0)return error('数据已审核');
- if(in_array($info->workSub->status,[3,4,5]))return error('功能申请状态有误不可审批');
- $info->status= $param['status'];
- $info->remark= $param['remark'];
- $status=WorkSubscr::$statusCn[$param['status']];
- $this->model->startTrans();
- try{
- $add = $info->save();
-
- if($add==false)throw new Exception("{$status}失败");
- $statusArr= WorkSubinfo::where('sub_id',$info->sub_id)->column('status');
- $statusSub = $info->workSub->status;
- if(count(array_unique($statusArr))==1){
- if(in_array(1,$statusArr)){
- $statusSub = 3;
- $info->workSub->is_check=2;
- }
- if(in_array(2,$statusArr)){
- $statusSub =4;
- $info->workSub->is_check=2;
- }
- }else{
- if(in_array(1,$statusArr)){
- $statusSub = 2;
- }
- }
-
- $info->workSub->status =$statusSub;
- $info->workSub->check_id =$this->uid;
- $info->workSub->check_name =$this->uname;
- $subup=$info->workSub->save();
- if($subup==false)throw new Exception("{$status}失败");
- if($info->workSub->is_check==2){
- $role=\app\bug\model\WorkRole::where("id",$info->workSub->role_id)->findOrEmpty();
- if($role->isEmpty()) throw new Exception("角色不存在");
- $role->action = WorkSubscr::GetActionById($info->sub_id);
- $roleUp=$role->save();
- if($roleUp==false)throw new Exception("角色同步权限失败");
- // WorkUser::where(["role_id"=>$info->workSub->role_id,"status"=>[1,2]])->save(["status"=>0]);
- }
- $this->model->commit();
- return success("{$status}成功");
- }catch (\Exception $e){
- $this->model->rollback();
- return error($e->getMessage());
- }
-
- }
- }
|