package com.lyms.talkonlineweb.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyms.talkonlineweb.domain.LymsRole;
import com.lyms.talkonlineweb.domain.LymsUser;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.service.LymsRoleService;
import com.lyms.talkonlineweb.service.LymsUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
/**
* 角色管理
*/
@RestController
@Log4j2
public class RoleController {
@Autowired
private LymsUserService lymsUserService;
@Autowired
private LymsRoleService lymsRoleService;
/**
* 保存角色
* @param role
* @return
*/
@PostMapping("saveRole")
public BaseResponse saveRole(LymsRole role){
BaseResponse baseResponse=new BaseResponse();
// LymsUser user=lymsUserService.getUserByToken(Authorization);
// role.setCreatedby(user.getUid());
if(role.getRid()==null){
role.setCreatedtime(new Date());
}else {
// role.setUpdatedby(user.getUid());
role.setUpdatedTime(new Date());
}
boolean f=false;
if(!StringUtils.isEmpty(role.getRname())){
f=lymsRoleService.saveOrUpdate(role);
}
System.out.println(role);
baseResponse.setErrorcode(f==true?0:1);
return baseResponse;
}
/**
* 查询角色列表
* @param role
* @param current
* @param size
* @return
*/
@GetMapping("sltRole")
public BaseResponse sltRole(LymsRole role,int current,int size){
BaseResponse baseResponse=new BaseResponse();
Page<LymsRole> page=new Page<>(current,size);
Page<LymsRole> rolePage=lymsRoleService.page(page, Wrappers.query(role).orderByDesc("updated_time","createdtime"));
baseResponse.setObject(rolePage);
return baseResponse;
}
/**
* 删除角色
* @param rid
* @return
*/
@GetMapping("delRole")
public BaseResponse delRole(int rid){
BaseResponse baseResponse=new BaseResponse();
boolean f=lymsRoleService.removeById(rid);
baseResponse.setErrorcode(f==true?0:1);
return baseResponse;
}
/**
* 根据用户获取角色
* @param uid
* @return
*/
@GetMapping("getRolesByUid")
public BaseResponse getRolesByUid(int uid){
BaseResponse baseResponse=new BaseResponse();
List<LymsRole> rLst=lymsRoleService.getRolesByUid(uid);
baseResponse.setObject(rLst);
return baseResponse;
}
/**
* 添加角色包含的权限
* @param rid
* @param perms 如果是多个权限需要用","分割
* @return
*/
@PostMapping("addRoleByPerms")
public BaseResponse addRoleByPerms(int rid,String perms){
BaseResponse baseResponse=new BaseResponse();
String[] pArr=perms.split(",");
int cnt=0;
cnt=lymsRoleService.delPersByRole(rid);//删除旧的关系
for (int i = 0; i < pArr.length; i++) {
cnt=lymsRoleService.addRoleByPerms(rid,Integer.parseInt(pArr[i]));
}
// baseResponse.setObject(rLst);
return baseResponse;
}
}