package com.shlanbao.tzsc.pms.sys.role.service.impl; import java.util.ArrayList; import java.util.List; import com.shlanbao.tzsc.utils.tools.*; import net.sf.json.JSONArray; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.shlanbao.tzsc.base.dao.SysResourceDaoI; import com.shlanbao.tzsc.base.dao.SysRoleDaoI; import com.shlanbao.tzsc.base.dao.SysRoleResourceDaoI; import com.shlanbao.tzsc.base.dao.SysUserDaoI; import com.shlanbao.tzsc.base.dao.SysUserRoleDaoI; import com.shlanbao.tzsc.base.mapping.MdFixCode; import com.shlanbao.tzsc.base.mapping.SysResource; import com.shlanbao.tzsc.base.mapping.SysRole; import com.shlanbao.tzsc.base.mapping.SysRoleResource; import com.shlanbao.tzsc.base.mapping.SysUser; import com.shlanbao.tzsc.base.mapping.SysUserRole; import com.shlanbao.tzsc.base.service.BaseService; import com.shlanbao.tzsc.init.BaseParams; import com.shlanbao.tzsc.pms.md.fixCode.beans.FixCodeBean; import com.shlanbao.tzsc.pms.md.fixCode.service.FixCodeServiceI; import com.shlanbao.tzsc.pms.sys.resource.beans.ResourceBean; import com.shlanbao.tzsc.pms.sys.role.beans.RoleBean; import com.shlanbao.tzsc.pms.sys.role.service.RoleServiceI; import com.shlanbao.tzsc.pms.sys.user.beans.UserBean; import javax.servlet.http.HttpServletRequest; /** * 角色业务实现类 * @author Leejean * @create 2014年9月16日上午10:28:46 */ @Service public class RoleServiceImp extends BaseService implements RoleServiceI{ @Autowired private SysRoleDaoI sysRoleDao; @Autowired private SysResourceDaoI sysResourceDao; @Autowired private SysRoleResourceDaoI sysRoleResourceDao; @Autowired private SysUserDaoI sysUserDao; @Autowired private SysUserRoleDaoI sysUserRoleDao; @Autowired private FixCodeServiceI fixCodeService; @Autowired private HttpServletRequest request; @Override public List getAllRoles(RoleBean roleBean) throws Exception { List roleBeans = new ArrayList(); String name=""; if(StringUtil.notNull(roleBean.getName())){ name = roleBean.getName(); } List roles=sysRoleDao.query("from SysRole o where o.del=0 and o.name like '%"+name+"%' order by o.seq asc"); for (SysRole role : roles) { RoleBean bean=BeanConvertor.copyProperties(role, RoleBean.class); FillUserInfoUtil.fillCreateAndUpdateUserInfo(role,bean); roleBeans.add(bean); } return roleBeans; } @LogAnno(operateType = "新增角色") @Override public void addRole(RoleBean roleBean) throws Exception { //添加角色基础信息 SysRole role=BeanConvertor.copyProperties(roleBean, SysRole.class); FillUserInfoUtil.fillCreateUserInfo(role,request); SysRole roler=sysRoleDao.saveAndReturn(role); roleBean.setId(roler.getId()); savemdFixCodeInfo(roleBean); } //维护综合基础数据 private void savemdFixCodeInfo(RoleBean roleBean){ //轮保角色 String lbTypeName=roleBean.getLbTypeName(); //点检角色 String djTypeName=roleBean.getDjTypeName(); String rolerId=roleBean.getId(); //判断是否需要刷新缓存 if(StringUtil.notEmpty(lbTypeName)){ MdFixCode lbBean=new MdFixCode(); lbBean.setName(lbTypeName); lbBean.setCode(rolerId); lbBean.setUpcode("LBTYPE001"); lbBean.setDel("0"); lbBean.setDes("轮保类型项目"); FixCodeBean params = new FixCodeBean(); params.setUpcode(lbBean.getUpcode()); params.setCode(lbBean.getCode()); fixCodeService.saveFixCode(lbBean,params); } if(StringUtil.notEmpty(djTypeName)){ MdFixCode djbean=new MdFixCode(); djbean.setName(djTypeName); djbean.setCode(rolerId); djbean.setUpcode("DJTYPE001"); djbean.setDel("0"); djbean.setDes("点检类型项目"); FixCodeBean params = new FixCodeBean(); params.setUpcode(djbean.getUpcode()); params.setCode(djbean.getCode()); fixCodeService.saveFixCode(djbean,params); } } @LogAnno(operateType = "编辑角色") @Override public void editRole(RoleBean roleBean) throws Exception { SysRole sysRole=sysRoleDao.findById(SysRole.class, roleBean.getId()); BeanConvertor.copyProperties(roleBean, sysRole); FillUserInfoUtil.fillUpdateUserInfo(sysRole,request); roleBean.setId(sysRole.getId()); savemdFixCodeInfo(roleBean); } @LogAnno(operateType = "删除角色") @Override public void deleteRole(String id) throws Exception { sysRoleDao.findById(SysRole.class, id).setDel(1L); } @LogAnno(operateType = "批量删除角色") @Override public void batchDeleteRoles(String ids) throws Exception { for(String id: StringUtil.splitToStringList(ids, ",")){ this.deleteRole(id); } } @LogAnno(operateType = "给角色分配权限") @Override public void assignResourceToRole(String id, String resids) throws Exception { //清除用户所有角色 //批量添加 sysRoleResourceDao.deleteByParams("delete from SysRoleResource o where o.sysRole.id = ?",id); for (String resid : StringUtil.splitToStringList(resids, ",")) { SysRoleResource o = new SysRoleResource(new SysRole(id), new SysResource(resid)); try{ FillUserInfoUtil.fillCreateUserInfo(o,request); }catch (Exception e) { e.printStackTrace(); } sysRoleResourceDao.save(o); } } @Override public void batchAssignResourceToRoles(String rids, String resids) throws Exception { //清除用户所有角色 //批量添加 for (String rid : StringUtil.splitToStringList(rids, ",")) { sysRoleResourceDao.deleteByParams("delete from SysRoleResource o where o.sysRole.id = ?",rid); for (String resid : StringUtil.splitToStringList(resids, ",")) { sysRoleResourceDao.save(new SysRoleResource(new SysRole(rid), new SysResource(resid))); } } } @Override public RoleBean getSysRoleById(String id) throws Exception { RoleBean bean= BeanConvertor.copyProperties(sysRoleDao.findById(SysRole.class, id),RoleBean.class); //查询该角色在综合基础数据中维护的保养类型 //刷新md_fix_code 数据 //BaseParams.setListMdFixCode(fixCodeService.queryFixCode(null)); RedisUtil.addMap("baseParams","FIXCODES", JSONArray.fromObject(fixCodeService.queryFixCode(null)).toString()); List list=BaseParams.getListMdFixCode(); //查询点检权限 for (FixCodeBean fixCodeBean : list) { //点检 if("DJTYPE001".equals(fixCodeBean.getUpcode()) && id.equals(fixCodeBean.getCode())){ bean.setDjTypeName(fixCodeBean.getName()); } //轮保护 if("LBTYPE001".equals(fixCodeBean.getUpcode()) && id.equals(fixCodeBean.getCode())){ bean.setLbTypeName(fixCodeBean.getName()); } } return bean; } @Override public List getResourcesByRole(String id) throws Exception { //查询所有目录资源 List menuResources=sysResourceDao.query("from SysResource o where o.del=0 and o.enable=1 and o.typ=1 order by o.seq asc"); //查询所有功能资源 List funcResources=sysResourceDao.query("from SysResource o where o.del=0 and o.enable=1 and o.typ=2 order by o.seq asc"); //查询当前角色拥有目录资源 List roleOwnMenuResources = sysResourceDao.query( "select o.sysResource from SysRoleResource o where o.sysResource.typ=1 and o.sysRole.id = ?",id); //查询当前角色拥有目录资源 List roleOwnFuncResources = sysResourceDao.query( "select o.sysResource from SysRoleResource o where o.sysResource.typ=2 and o.sysRole.id = ?",id); List resourceBeans = new ArrayList(); for (SysResource sysResource : menuResources) { ResourceBean resourceBean = BeanConvertor.copyProperties(sysResource, ResourceBean.class); if(sysResource.getSysResource()!=null){ resourceBean.setPid(sysResource.getSysResource().getId()); resourceBean.setPname(sysResource.getSysResource().getText()); } //判断当前角色是否拥有这个目录权限,如果有checked=true resourceBean.setChecked(isAssignedResource(roleOwnMenuResources,sysResource.getId())); //设置该目录下面的功能,并判断是否拥有该权限 resourceBean.setFunctions(fmtFuncsToMenus(sysResource.getId(),funcResources,roleOwnFuncResources)); resourceBeans.add(resourceBean); } // return resourceBeans; } private List fmtFuncsToMenus(String menuId,List funcResources,List roleOwnFuncResources) throws Exception{ if(funcResources==null){ return null; } List functions =new ArrayList(); for(SysResource func:funcResources){ try { if(func.getSysResource().getId().equals(menuId)){//该功能属于这个模块 ResourceBean funcBean = BeanConvertor.copyProperties(func, ResourceBean.class); //判断当前角色是否拥有这个功能权限 funcBean.setChecked(isOwnFunc(roleOwnFuncResources,func.getId())); functions.add(funcBean); } } catch (Exception e) { e.printStackTrace(); } } return functions; } /** * 判断角色是否拥有该功能权限 * @author Leejean * @create 2014年8月26日上午9:44:25 * @param roleOwnFuncResources 用户拥有的功能列表 * @param id 功能id * @return */ private boolean isOwnFunc(List roleOwnFuncResources, String id) { for (SysResource sysResource : roleOwnFuncResources) { if(sysResource.getId().equals(id)){ return true; } } return false; } /** * 判断角色是否拥有此目录权限 * @author Leejean * @create 2014年8月26日上午9:40:16 * @param roleOwnMenuResources 目录s * @param id * @return */ private boolean isAssignedResource(List roleOwnMenuResources, String id) { for (SysResource sysResource : roleOwnMenuResources) { if(sysResource.getId().equals(id)){ return true; } } return false; } @LogAnno(operateType = "给角色分配用户") @Override public void assignUsersToRole(String id, String ids) throws Exception { //清除 sysUserRoleDao.deleteByParams("delete from SysUserRole o where o.sysRole.id = ?",id); for (String uid : StringUtil.splitToStringList(ids, ",")) { SysUserRole o = new SysUserRole(new SysRole(id), new SysUser(uid)); FillUserInfoUtil.fillCreateUserInfo(o,request); sysUserRoleDao.save(o); } } @Override public List getRoleUnAssignedUsers(String oid, String name) throws Exception { if(name==null)name=""; String hql= "from SysUser o where o.del=0 and o.enable=1 and (o.name like '%"+name+"%' or o.loginName like '%"+name+"%' or o.eno like '%"+name+"%' ) and o.id not in (select oo.sysUser.id from SysUserRole oo where oo.sysRole.id=?)"; //获得所有用户 List rows = sysUserDao.query(hql,oid); return BeanConvertor.copyList(rows,UserBean.class); } @Override public List getRoleAssignedUsers(String oid) throws Exception { String hql= "from SysUserRole o where o.sysRole.id=?"; //获得所有用户 List rows = sysUserDao.query("select o.sysUser ".concat(hql), oid); return BeanConvertor.copyList(rows,UserBean.class); } @Override public List getRolesByUser(String id) throws Exception { //查询所有角色 List sysRoles=sysRoleDao.query("from SysRole o where o.del=0 and o.enable=1"); //查询当前用户拥有的角色 List sysUserRoles = sysRoleDao.query( "select o.sysRole from SysUserRole o where o.sysUser.id = ?",id); List roleBeans = new ArrayList(); for (SysRole sysRole : sysRoles) { RoleBean roleBean = BeanConvertor.copyProperties(sysRole, RoleBean.class); roleBean.setChecked(isAssignedRole(sysUserRoles,sysRole.getId())); roleBeans.add(roleBean); } return roleBeans; } /** * 是否拥有此角色 * @author Leejean * @create 2014-8-19下午06:36:21 * @param sysRoles * @param id * @return */ private boolean isAssignedRole(List sysRoles, String id) { for (SysRole sysRole : sysRoles) { if(sysRole.getId().equals(id)){ return true; } } return false; } @LogAnno(operateType = "给用户分配角色") @Override public void assignRole(String id, String ids) throws Exception { //清除用户所有角色 sysUserRoleDao.deleteByParams("delete from SysUserRole o where o.sysUser.id = ?",id); //批量添加 for (String rid : StringUtil.splitToStringList(ids, ",")) { SysUserRole o = new SysUserRole(new SysRole(rid), new SysUser(id)); FillUserInfoUtil.fillCreateUserInfo(o,request); sysUserRoleDao.save(o); } } @LogAnno(operateType = "用户角色批量分配") @Override public void batchAssignRole(String uids, String rids) throws Exception { //清除用户所有角色 //批量添加 for (String uid : StringUtil.splitToStringList(uids, ",")) { sysUserRoleDao.deleteByParams("delete from SysUserRole o where o.sysUser.id = ?",uid); for (String rid : StringUtil.splitToStringList(rids, ",")) { SysUserRole o = new SysUserRole(new SysRole(rid), new SysUser(uid)); FillUserInfoUtil.fillCreateUserInfo(o,request); sysUserRoleDao.save(o); } } } }