实体类:
users用户表:
package com.app.project.mode;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import www.appfast.cc.base.BaseEntity;
import java.sql.Timestamp;
@Table(name = "t_users")
public class UserModel extends BaseEntity {
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
Integer id;
String account;
String showname;
String password;
Timestamp crt_time;
String crt_user;
String it_serv_zone;
String phone;
String status;
String serviceproviderid;
String token;
Timestamp login_time;
Integer current_roleid;
Integer vendor;
Integer assettype_power;
@Transient
String roletype;
public String getRoletype() {
return roletype;
}
public void setRoletype(String roletype) {
this.roletype = roletype;
}
/*String rolename;
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}*/
public Integer getCurrent_roleid() {
return current_roleid;
}
public void setCurrent_roleid(Integer current_roleid) {
this.current_roleid = current_roleid;
}
public Timestamp getLogin_time() {
return login_time;
}
public void setLogin_time(Timestamp login_time) {
this.login_time = login_time;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getShowname() {
return showname;
}
public void setShowname(String showname) {
this.showname = showname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Timestamp getCrt_time() {
return crt_time;
}
public void setCrt_time(Timestamp crt_time) {
this.crt_time = crt_time;
}
public String getCrt_user() {
return crt_user;
}
public void setCrt_user(String crt_user) {
this.crt_user = crt_user;
}
public String getIt_serv_zone() {
return it_serv_zone;
}
public void setIt_serv_zone(String it_serv_zone) {
this.it_serv_zone = it_serv_zone;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getServiceproviderid() {
return serviceproviderid;
}
public void setServiceproviderid(String serviceproviderid) {
this.serviceproviderid = serviceproviderid;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public Integer getVendor() {
return vendor;
}
public void setVendor(Integer vendor) {
this.vendor = vendor;
}
public Integer getAssettype_power() {
return assettype_power;
}
public void setAssettype_power(Integer assettype_power) {
this.assettype_power = assettype_power;
}
}
用户角色关联表实体类:
package com.app.project.mode;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import www.appfast.cc.base.BaseEntity;
@Table(name = "t_roleusers")
public class UserRoleModule extends BaseEntity {
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
String id;
Integer it_serv_zone;
Integer user_id;
Integer role_id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getIt_serv_zone() {
return it_serv_zone;
}
public void setIt_serv_zone(Integer it_serv_zone) {
this.it_serv_zone = it_serv_zone;
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public Integer getRole_id() {
return role_id;
}
public void setRole_id(Integer role_id) {
this.role_id = role_id;
}
}
角色表实体类:
package com.app.project.mode;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import www.appfast.cc.base.BaseEntity;
@Table(name = "t_role")
public class RoleModule extends BaseEntity {
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
String id;
String powertype;
String rolename;
String viewstatus;
Integer rejectstatus;
Integer approvestatus;
String fees_viewstatus;
Integer fees_rejectstatus;
Integer fees_approvestatus;
String examine;
String viewquestionsstatus;
String willappointstatus;
String willtracestatus;
String donestatus;
public String getDonestatus() {
return donestatus;
}
public void setDonestatus(String donestatus) {
this.donestatus = donestatus;
}
public String getWilltracestatus() {
return willtracestatus;
}
public void setWilltracestatus(String willtracestatus) {
this.willtracestatus = willtracestatus;
}
public String getWillappointstatus() {
return willappointstatus;
}
public void setWillappointstatus(String willappointstatus) {
this.willappointstatus = willappointstatus;
}
public String getViewquestionsstatus() {
return viewquestionsstatus;
}
public void setViewquestionsstatus(String viewquestionsstatus) {
this.viewquestionsstatus = viewquestionsstatus;
}
public Integer getFees_rejectstatus() {
return fees_rejectstatus;
}
public void setFees_rejectstatus(Integer fees_rejectstatus) {
this.fees_rejectstatus = fees_rejectstatus;
}
public Integer getFees_approvestatus() {
return fees_approvestatus;
}
public void setFees_approvestatus(Integer fees_approvestatus) {
this.fees_approvestatus = fees_approvestatus;
}
public String getFees_viewstatus() {
return fees_viewstatus;
}
public void setFees_viewstatus(String fees_viewstatus) {
this.fees_viewstatus = fees_viewstatus;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPowertype() {
return powertype;
}
public void setPowertype(String powertype) {
this.powertype = powertype;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
public String getViewstatus() {
return viewstatus;
}
public void setViewstatus(String viewstatus) {
this.viewstatus = viewstatus;
}
public Integer getRejectstatus() {
return rejectstatus;
}
public void setRejectstatus(Integer rejectstatus) {
this.rejectstatus = rejectstatus;
}
public Integer getApprovestatus() {
return approvestatus;
}
public void setApprovestatus(Integer approvestatus) {
this.approvestatus = approvestatus;
}
public String getExamine() {
return examine;
}
public void setExamine(String examine) {
this.examine = examine;
}
}
角色权限表实体类:
package com.app.project.mode;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import www.appfast.cc.base.BaseEntity;
@Table(name = "t_rolepermission")
public class RolePermissionModule extends BaseEntity {
@Id
@Column(name = "id")
Integer id;
String role_id;
String power_id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRole_id() {
return role_id;
}
public void setRole_id(String role_id) {
this.role_id = role_id;
}
public String getPower_id() {
return power_id;
}
public void setPower_id(String power_id) {
this.power_id = power_id;
}
}
权限表实体类:
package com.app.project.mode;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import www.appfast.cc.base.BaseEntity;
@Table(name = "t_permission")
public class Permission extends BaseEntity{
@Id
@Column(name = "id")
Integer id;
String name;
String url;
String text;
String parent_id;
//二级菜单
@Transient
private List<Permission> data;
//三级菜单
@Transient
private List<Permission> children;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getParent_id() {
return parent_id;
}
public void setParent_id(String parent_id) {
this.parent_id = parent_id;
}
public List<Permission> getData() {
return data;
}
public void setData(List<Permission> data) {
this.data = data;
}
public List<Permission> getChildren() {
return children;
}
public void setChildren(List<Permission> children) {
this.children = children;
}
}
seriver层
package com.app.project.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import www.appfast.cc.base.BaseService;
import www.appfast.cc.util.BaseJunitTest;
import www.appfast.cc.util.CommonUtil;
import www.appfast.cc.util.DateUtil;
import www.appfast.cc.util.Page;
import com.app.project.mode.DataDicModule;
import com.app.project.mode.Permission;
import com.app.project.mode.RoleModule;
import com.app.project.mode.RolePermissionModule;
import com.app.project.mode.UserAreaModule;
import com.app.project.mode.UserModel;
import com.app.project.mode.UserRoleModule;
@Service
public class PermissionService extends BaseJunitTest {
@Resource
BaseService baseService;
public UserModel findUserModel(String token) {
// TODO Auto-generated method stub
String sql = "select * from t_users where token = " + "'" + token + "'";
UserModel user = baseService.getModel(sql, UserModel.class);
return user;
}
public List<RolePermissionModule> findpermissionid(Integer roleid) {
// TODO Auto-generated method stub
String sql = "select * from t_rolepermission where role_id = '"+roleid+"'";
System.out.println(sql);
List<RolePermissionModule> rolepermission = baseService.getListModels(sql, RolePermissionModule.class);
return rolepermission;
}
public List<Permission> findPermission(String firstsql) {
List<Permission> firstPermission = baseService.getListModels(firstsql, Permission.class);
return firstPermission;
}
public List<UserModel> findAllUsers(String name,Page pg) {
List<UserModel> userLists = null;
try {
StringBuffer sql = new StringBuffer("select * from t_users tu");
if(CommonUtil.isNotNull(name)){
sql.append(" where tu.account like '%"+name+"%' or showname like '%"+name+"%'");
}
String pageSql = baseService.getSqlWithPage(sql.toString(), pg);
userLists = baseService.getListModels(pageSql, UserModel.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return userLists;
}
public List<UserRoleModule> findUserRoleByUser(String roleSql) {
// TODO Auto-generated method stub
return baseService.getListModels(roleSql, UserRoleModule.class);
}
public List<RoleModule> findAllRoles(String roleName,Page pg){
List<RoleModule> roleList = null;
try {
StringBuffer sql = new StringBuffer("select * from t_role");
if(CommonUtil.isNotNull(roleName)){
sql.append(" where rolename like '%"+roleName+"%'");
}
String pageSql = baseService.getSqlWithPage(sql.toString(), pg);
roleList = baseService.getListModels(pageSql, RoleModule.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return roleList;
}
public List<Permission> findAllPermissions(String text,Page pg){
List<Permission> permissionList = null;
try {
StringBuffer sql = new StringBuffer("select a.id," +
"(select b.text as name from t_permission b where b.id=a.parent_id)," +
"a.url,a.text,a.parent_id from t_permission a");
if(CommonUtil.isNotNull(text)){
sql.append(" where a.text like '%"+text+"%'");
}
String pageSql = baseService.getSqlWithPage(sql.toString(), pg);
permissionList = baseService.getListModels(pageSql, Permission.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return permissionList;
}
public List<DataDicModule> findAllAreas(String data,Page pg){
List<DataDicModule> areaList = null;
try {
StringBuffer sql = new StringBuffer("select * from t_datadic where data_type = 'area'");
if(CommonUtil.isNotNull(data)){
sql.append(" and data like '%"+data+"%'");
}
String pageSql = baseService.getSqlWithPage(sql.toString(), pg);
areaList = baseService.getListModels(pageSql, DataDicModule.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return areaList;
}
public boolean addUser(UserModel user){
boolean result = false;
try {
if(user!=null){
String name = user.getShowname();
if(CommonUtil.isNotNull(name)){
user.setShowname(name);
}
user.setCrt_time(DateUtil.getTimestampByDate(new Date()));
if(baseService.save(user)==1){
result = true;
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public boolean updUser(UserModel user){
boolean result = false;
try {
if(user!=null){
UserModel userModel = null;
if(user.getId()!=null){
String sql = "select * from t_users tu where tu.id = " + user.getId();
userModel = baseService.getModel(sql, UserModel.class);
}
if(userModel!=null){
//userModel.setAccount(user.getAccount());
String name = user.getShowname();
if(name!=null && !("").equals(name)){
userModel.setShowname(name);
}
userModel.setPhone(user.getPhone());
if(baseService.update(userModel)==1){
result = true;
}
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public boolean delUser(String[] idsArray){
boolean result = false;
try {
if(CommonUtil.isNotNull(idsArray)){
String ids = CommonUtil.arrayToStr(idsArray);
//先判断中间表是否存在关联关系
String fsql = "select * from t_roleusers where user_id in ("+ids+")";
List<UserRoleModule> roleUserList = baseService.getListModels(fsql, UserRoleModule.class);
if(CommonUtil.isNotNull(roleUserList)){
result = false;
}else{
String sql = "delete from t_users u where u.id in ("+ids+")";
if(baseService.update(sql)>0){
result = true;
}
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public boolean addPermission(Permission permission){
boolean result = false;
try {
if(permission!=null){
String text = permission.getText();
if(CommonUtil.isNotNull(text)){
permission.setText(text);
}
String url = permission.getUrl();
if(CommonUtil.isNotNull(url)){
permission.setUrl(url);
}
permission.setParent_id(permission.getParent_id());
String sql = "select max(id) from t_permission";
String id = baseService.getSingleResult(sql);
permission.setId(Integer.parseInt(id)+1);
if(baseService.save(permission)==1){
result = true;
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public boolean updPermission(Permission permission){
boolean result = false;
try {
if(permission!=null){
Permission per = null;
if(permission.getId()!=null){
String sql = "select * from t_permission tp where tp.id = '" + permission.getId()+"'";
per = baseService.getModel(sql, Permission.class);
}
if(per!=null){
String text = permission.getText();
if(CommonUtil.isNotNull(text)){
per.setText(text);
}
per.setUrl(permission.getUrl());
String parentId = permission.getParent_id();
if(CommonUtil.isNotNull(parentId)){
per.setParent_id(parentId);
}else{
per.setParent_id("0");
}
String sql = "update t_permission set text='"+ per.getText()+"',url='"+ per.getUrl()+"'," +
" parent_id = '"+ per.getParent_id()+"' where id = '"+ per.getId()+"'";
if(baseService.update(sql)==1){
result = true;
}
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public boolean delPermission(String[] idsArray){
boolean result = false;
try {
if(idsArray!=null && idsArray.length>0){
String ids = CommonUtil.arrayToStr(idsArray);
//先判断中间表是否存在关联关系
String fsql = "select * from t_rolepermission where power_id in ("+ids+")";
List<RolePermissionModule> rolePerList = baseService.getListModels(fsql, RolePermissionModule.class);
if(CommonUtil.isNotNull(rolePerList)){
result = false;
}else{
String sql = "delete from t_permission tp where tp.id in ("+ids+")";
if(baseService.update(sql)>0){
result = true;
}
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public List<Permission> getParentPermission(){
List<Permission> perList = null;
try {
String sql = "select * from t_permission where parent_id = '0' " +
"or parent_id in (select id from t_permission where parent_id = '0')";
perList = baseService.getListModels(sql, Permission.class);
} catch (Exception e) {
e.printStackTrace();
}
return perList;
}
public boolean addRole(RoleModule role){
boolean result = false;
try {
if(role!=null){
String powerType = role.getPowertype();
if(CommonUtil.isNotNull(powerType)){
role.setPowertype(powerType);
}
String roleName = role.getRolename();
if(CommonUtil.isNotNull(roleName)){
role.setRolename(roleName);
}
role.setExamine("无");
if(baseService.save(role)==1){
result = true;
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public boolean updRole(RoleModule role){
boolean result = false;
try {
if(role!=null){
RoleModule roleMoule = null;
if(role.getId()!=null){
String sql = "select * from t_role tp where tp.id = '" + role.getId()+"'";
roleMoule = baseService.getModel(sql, RoleModule.class);
}
if(roleMoule!=null){
String powerType = role.getPowertype();
if(CommonUtil.isNotNull(powerType)){
roleMoule.setPowertype(powerType);
}
String roleName = role.getRolename();
if(CommonUtil.isNotNull(roleName)){
roleMoule.setRolename(roleName);
}
String sql = "update t_role set rolename='"+ roleMoule.getRolename()+"'," +
"powertype='"+ roleMoule.getPowertype()+"'where id = '"+ roleMoule.getId()+"'";;
if(baseService.update(sql)==1){
result = true;
}
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public boolean delRole(String[] idsArray){
boolean result = false;
try {
if(idsArray!=null && idsArray.length>0){
String ids = CommonUtil.arrayToStr(idsArray);
//先判断中间表是否存在关联关系
String fsql = "select * from t_rolepermission where role_id in ("+ids+")";
List<RolePermissionModule> rolePerList = baseService.getListModels(fsql, RolePermissionModule.class);
String ssql = "select * from t_roleusers where role_id in ("+ids+")";
List<UserRoleModule> userRoleList = baseService.getListModels(ssql, UserRoleModule.class);
if(CommonUtil.isNotNull(rolePerList) && CommonUtil.isNotNull(userRoleList)){
result = false;
}else{
String sql = "delete from t_role tr where tr.id in ("+ids+")";
if(baseService.update(sql)>0){
result = true;
}
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public boolean delRolePerByRoleId(String roleId){
boolean result = false;
try {
String sql = "delete from t_rolepermission rp where rp.role_id = '"+roleId+"'";
baseService.update(sql);
result = true;
} catch (Exception e) {
// TODO Auto-generated catch block
result = false;
e.printStackTrace();
}
return result;
}
public boolean delUserRoleByUserId(String userId){
boolean result = false;
try {
String sql = "delete from t_roleusers ru where ru.user_id = '"+userId+"'";
baseService.update(sql);
result = true;
} catch (Exception e) {
// TODO Auto-generated catch block
result = false;
e.printStackTrace();
}
return result;
}
public boolean delUserAreaByUserId(String userId){
boolean result = false;
try {
String sql = "delete from t_userarea where user_id = '"+userId+"'";
baseService.update(sql);
result = true;
} catch (Exception e) {
// TODO Auto-generated catch block
result = false;
e.printStackTrace();
}
return result;
}
public boolean editUserRole(String[] idsArray,String userId){
boolean result = false;
try {
if(delUserRoleByUserId(userId)){
for (String roleId : idsArray) {
UserRoleModule userRole = new UserRoleModule();
userRole.setUser_id(Integer.parseInt(userId));
userRole.setRole_id(Integer.parseInt(roleId));
if(baseService.save(userRole)==1){
result = true;
}
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public boolean editRolePermission(String[] idsArray,String roleId){
boolean result = false;
try {
if(delRolePerByRoleId(roleId)){
for (String powerId : idsArray) {
RolePermissionModule rolePer = new RolePermissionModule();
rolePer.setRole_id(roleId);
rolePer.setPower_id(powerId);
String sql = "select max(id) from t_rolepermission";
String id = baseService.getSingleResult(sql);
rolePer.setId((Integer.parseInt(id)+1));
if(baseService.save(rolePer)==1){
result = true;
}
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
public List<String> getPermissionByRoleId(String roleId){
List<String> strArr = new ArrayList<String>();
try {
String sql = "select power_id from t_rolepermission where role_id = '"+roleId+"'";
List<RolePermissionModule> rolePerList = baseService.getListModels(sql, RolePermissionModule.class);
if(CommonUtil.isNotNull(rolePerList)){
for (RolePermissionModule rolePer : rolePerList) {
strArr.add(rolePer.getPower_id());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return strArr;
}
public List<Integer> getRoleByUserId(String userId){
List<Integer> strArr = new ArrayList<Integer>();
try {
String sql = "select role_id from t_roleusers where user_id = '"+userId+"'";
List<UserRoleModule> userRoleList = baseService.getListModels(sql, UserRoleModule.class);
if(CommonUtil.isNotNull(userRoleList)){
for (UserRoleModule userRole : userRoleList) {
strArr.add(userRole.getRole_id());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return strArr;
}
public boolean isSuperManager(String userId){
boolean result = false;
try {
String sql = "select * from t_roleusers where user_id = '"+userId+"' and role_id = '1'";
if(baseService.getModel(sql, UserRoleModule.class)!=null){
result = true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public boolean isExistAccount(String account){
boolean result = false;
try {
String sql = "select * from t_users where account = '"+account+"'";
if(baseService.getModel(sql, UserRoleModule.class)!=null){
result = true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public List<String> getAreaByUserId(String userId){
List<String> strArr = new ArrayList<String>();
try {
String fsql = "select area_id from t_userarea where user_id = '"+userId+"'";
List<UserAreaModule> userAreaList = baseService.getListModels(fsql, UserAreaModule.class);
String areaIds = "";
if(CommonUtil.isNotNull(userAreaList)){
for (UserAreaModule userAreaModule : userAreaList) {
areaIds += "'"+userAreaModule.getArea_id()+"',";
}
areaIds = areaIds.substring(0, areaIds.length()-1);
}
if(CommonUtil.isNotNull(areaIds)){
String sql = "select * from t_datadic where val in ("+areaIds+") and data_type = 'area'";
List<DataDicModule> dataList = baseService.getListModels(sql, DataDicModule.class);
if(CommonUtil.isNotNull(dataList)){
for (DataDicModule data : dataList) {
strArr.add(data.getId());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return strArr;
}
public boolean editUserArea(String[] idsArray,String userId){
boolean result = false;
try {
if(delUserAreaByUserId(userId)){
for (String areaVal : idsArray) {
UserAreaModule userArea = new UserAreaModule();
userArea.setUser_id(userId);
userArea.setArea_id(areaVal);
if(baseService.save(userArea)==1){
result = true;
}
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
}
然后是action层:
package com.app.project.action;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import www.appfast.cc.util.Datagrid;
import www.appfast.cc.util.ExcelUtil;
import www.appfast.cc.util.Page;
import www.appfast.cc.util.UploadUtil;
import com.app.project.mode.DataDicModule;
import com.app.project.mode.Permission;
import com.app.project.mode.RoleModule;
import com.app.project.mode.UserModel;
import com.app.project.mode.UserRoleModule;
import com.app.project.service.PermissionService;
@Controller
@RequestMapping("/permissionService")
public class PermissionAction {
@Resource
PermissionService permissionService;
@RequestMapping(value = "/getPermission")
@ResponseBody
public List<Permission> getPermission(HttpServletRequest request){
List<Permission> allMenu = new ArrayList<Permission>();
String token = request.getParameter("token");
UserModel user = permissionService.findUserModel(token);
String roleSql = "select * from t_roleusers ru where ru.user_id = "+user.getId();
List<UserRoleModule> userRoleList = permissionService.findUserRoleByUser(roleSql);
String roleIds = "";
List<Permission> allPermission = null;
if(userRoleList!=null && userRoleList.size()>0){
for(UserRoleModule userRole : userRoleList){
roleIds += "'"+userRole.getRole_id()+"',";
}
roleIds = roleIds.substring(0, roleIds.length()-1);
//获取用户角色下所有权限
StringBuffer sql = new StringBuffer("select p.* from t_permission p where p.id in " +
"(select rp.power_id from t_rolepermission rp where rp.role_id in ("+roleIds+"))");
//获取一级菜单sql
String firstsql = sql.append(" ORDER BY cast(id as integer ) ASC").toString();
//查询所有菜单
allPermission = permissionService.findPermission(firstsql);
for(Permission fp : allPermission){
//获取二级菜单
List<Permission> secondMenu = new ArrayList<Permission>();
String fpPId = fp.getParent_id();
String fpId = fp.getId().toString();
if("0".equals(fpPId)){
for (Permission sp : allPermission) {
//获取三级菜单
List<Permission> thirdMenu = new ArrayList<Permission>();
String spId = sp.getId().toString();
String spPId = sp.getParent_id();
if(spPId.equals(fpId)){
for (Permission tp : allPermission) {
String tpPId = tp.getParent_id();
if(tpPId.equals(spId)){
thirdMenu.add(tp);
}
}
sp.setChildren(thirdMenu);
secondMenu.add(sp);
}
}
fp.setChildren(secondMenu);
allMenu.add(fp);
}
}
}
return allMenu;
}
@RequestMapping(value = "/returnUsers")
@ResponseBody
public Datagrid<UserModel> getUserList(String name,Page pg){
List<UserModel> rows = permissionService.findAllUsers(name,pg);
pg.setFlag(false);
int total = permissionService.findAllUsers(name,pg).size();
Datagrid<UserModel> datagrid = new Datagrid<UserModel>(total,rows);
return datagrid;
}
/**
* 得到所有角色
* @return roleList
*/
@RequestMapping(value = "/returnRoles")
@ResponseBody
public Datagrid<RoleModule> getRoleList(String roleName,Page pg){
List<RoleModule> rows = permissionService.findAllRoles(roleName,pg);
pg.setFlag(false);
int total = permissionService.findAllRoles(roleName,pg).size();
Datagrid<RoleModule> datagrid = new Datagrid<RoleModule>(total,rows);
return datagrid;
}
/**
* 得到所有权限
* @return list
*/
@RequestMapping(value = "/returnPermissions")
@ResponseBody
public Datagrid<Permission> getPermissionList(String text,Page pg){
List<Permission> rows = permissionService.findAllPermissions(text,pg);
pg.setFlag(false);
int total = permissionService.findAllPermissions(text,pg).size();
Datagrid<Permission> datagrid = new Datagrid<Permission>(total,rows);
return datagrid;
}
/**
* 得到所有区域
* @return list
*/
@RequestMapping(value = "/returnAreas")
@ResponseBody
public List<DataDicModule> getAreaList(String data,Page pg){
List<DataDicModule> list = permissionService.findAllAreas(data,pg);
return list;
}
/**
* 新增用户
* @param user
* @return boolean
*/
@RequestMapping(value = "/addUser")
@ResponseBody
public boolean addUser(HttpServletRequest request,UserModel user){
String token = request.getParameter("token");
UserModel userModel = permissionService.findUserModel(token);
if(userModel!=null){
user.setCrt_user(userModel.getAccount());
}
return permissionService.addUser(user);
}
/**
* 修改用户
* @param user
* @return boolean
*/
@RequestMapping(value = "/updateUser")
@ResponseBody
public boolean updUser(UserModel user){
return permissionService.updUser(user);
}
/**
* 删除用户
* @param ids
* @return boolean
*/
@RequestMapping(value = "/deleteUser")
@ResponseBody
public boolean delUser(String[] ids){
return permissionService.delUser(ids);
}
/**
* 新增权限
* @param permission
* @return
*/
@RequestMapping(value = "/addPermission")
@ResponseBody
public boolean addPermission(Permission permission){
return permissionService.addPermission(permission);
}
/**
* 修改权限
* @param permission
* @return
*/
@RequestMapping(value = "/updatePermission")
@ResponseBody
public boolean updatePermission(Permission permission){
return permissionService.updPermission(permission);
}
/**
* 删除权限
* @param idsArray
* @return
*/
@RequestMapping(value = "/deletePermission")
@ResponseBody
public boolean deletePermission(String[] ids){
return permissionService.delPermission(ids);
}
/**
* 获取一级二级权限菜单
* @return
*/
@RequestMapping(value = "/getParentPermission")
@ResponseBody
public List<Permission> getParentPermission(){
return permissionService.getParentPermission();
}
/**
* 新增角色
* @param role
* @return
*/
@RequestMapping(value = "/addRole")
@ResponseBody
public boolean addRole(RoleModule role){
return permissionService.addRole(role);
}
/**
* 修改角色
* @param role
* @return
*/
@RequestMapping(value = "/updateRole")
@ResponseBody
public boolean updateRole(RoleModule role){
return permissionService.updRole(role);
}
/**
* 删除角色
* @param idsArray
* @return
*/
@RequestMapping(value = "/deleteRole")
@ResponseBody
public boolean deleteRole(String[] ids){
return permissionService.delRole(ids);
}
/**
* 设置角色
* @param idsArray userId
* @return
*/
@RequestMapping(value = "/addOrdelUserRole")
@ResponseBody
public boolean addOrdelUserRole(String[] ids,String userId){
return permissionService.editUserRole(ids,userId);
}
/**
* 设置权限
* @param idsArray roleId
* @return
*/
@RequestMapping(value = "/addOrdelRolePermission")
@ResponseBody
public boolean addOrdelRolePermission(String[] ids,String roleId){
return permissionService.editRolePermission(ids,roleId);
}
/**
* 通过id查询Permission
* @param roleId
* @return
*/
@RequestMapping(value = "/getPermissionByRoleId")
@ResponseBody
public List<String> getPermissionByRoleId(String roleId){
return permissionService.getPermissionByRoleId(roleId);
}
/**
* 通过id查询role
* @param userId
* @return
*/
@RequestMapping(value = "/getRoleByUserId")
@ResponseBody
public List<Integer> getRoleByUserId(String userId){
return permissionService.getRoleByUserId(userId);
}
/**
* 通过userId判断是否为管理员
* @param boolean
* @return
*/
@RequestMapping(value = "/isSuperManager")
@ResponseBody
public boolean isSuperManager(String userId){
return permissionService.isSuperManager(userId);
}
/**
* 判断登录名是否存在
* @param account
* @return boolean
*/
@RequestMapping(value = "/isExistAccount")
@ResponseBody
public boolean isExistAccount(String account){
return permissionService.isExistAccount(account);
}
/**
* 通过id查询area
* @param userId
* @return
*/
@RequestMapping(value = "/getAreaByUserId")
@ResponseBody
public List<String> getAreaByUserId(String userId){
return permissionService.getAreaByUserId(userId);
}
/**
* 新增用户区域关系
* @param boolean
* @return
*/
@RequestMapping(value = "/addOrdelUserArea")
@ResponseBody
public boolean addOrdelUserArea(String[] ids,String userId){
return permissionService.editUserArea(ids,userId);
}
/*
*采用spring提供的上传文件的方法
*/
@RequestMapping("importUser")
@ResponseBody
public boolean importUser(HttpServletRequest request) throws IllegalStateException, IOException{
boolean result = false;
try {
String file = UploadUtil.springUpload(request);
ExcelUtil eu = new ExcelUtil();
eu.setExcelPath(file);
eu = eu.RestoreSettings();//还原设定
eu.setStartReadPos(3);
List<Row> rows = eu.readExcel();
for (Row row : rows) {
UserModel user = new UserModel();
user.setAccount(row.getCell(0).getStringCellValue());
user.setPassword(row.getCell(1).getStringCellValue());
user.setShowname(row.getCell(2).getStringCellValue());
if(permissionService.addUser(user)){
result = true;
}
}
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
}