关于java:用Java制作疫情防控管理系统

13次阅读

共计 19257 个字符,预计需要花费 49 分钟才能阅读完成。

我的项目介绍:通过对依社区为单位进行人群的治理,以及疫苗的状况,包含小区情况,通过 RBAC 进行角色与用户之间的权限治理。

我的项目:环境 -IDEA、Mysql 数据库,Tomcat 服务器,SpringMVC,SpringBoot,AOP,拦截器,过滤器,全局异样,RBAC 权限管制等。

1、登录模块 (注册)

外围代码:service 层

@Service
public class UserService extends BaseService<User,Integer> {

@Resource
// 引入 dao 层
private UserMapper userMapper;

@Resource
private UserRoleMapper userRoleMapper;

@Resource
private CommunityMapper communityMapper;


// 用户登录
public UserModel userLogin(String userName,String userPwd){
    // 对输出的账号密码进行判断,是否合乎格局
    checkUserLoginParam(userName,userPwd);

    // 通过对数据库的查问,查看用户是否存在
    User temp =  userMapper.queryUserByUserName(userName);
    AssertUtil.isTrue(temp == null,"用户不存在");

    // 判断用户的明码是否正确,拿数据库查问到的用户明码和用户输出的用户明码进行 equest 比拟
    checkUserPwd(userPwd,temp.getUserPwd());

    // 返回指标对象  对明码进行加密
    return builderUserInfo(temp);
}


/**
 * // 对输出的账号密码进行判断  是否合乎格局
 * @param userName   账号
 * @param userPwd    明码
 */
// 对输出的账号密码进行判断,是否合乎格局
private void checkUserLoginParam(String userName, String userPwd) {
    // 用户非空
    AssertUtil.isTrue(StringUtils.isBlank(userName),"用户名不能为空");

    // 明码非空
    AssertUtil.isTrue(StringUtils.isBlank(userPwd),"明码不能为空");
}



/**
 * // 判断明码是否正确
 * @param userPwd   用户输出的明码
 * @param userPwd1  数据库查出来的明码
 */
// 判断用户的明码是否正确,拿数据库查问到的用户明码和用户输出的用户明码进行 equest 比拟
private void checkUserPwd(String userPwd, String userPwd1) {
    // 对用户输出的明码进行加密
    userPwd = Md5Util.encode(userPwd);

    AssertUtil.isTrue(!(userPwd.equals(userPwd1)),"明码不正确");



}

/**
 *
 * @param temp 以后登录对象
 * @return
 */
// 对明码进行加密  返回指标对象
private UserModel builderUserInfo(User temp) {UserModel userModel = new UserModel();
    // 为用户明码进行加密
    userModel.setUserIdStr(UserIDBase64.encoderUserID(temp.getId()));
    userModel.setUserName(temp.getUserName());
    userModel.setTrueName(temp.getTrueName());
    return userModel;

}


/**
 *
 * @param userId                 以后 Cookie 存储的用户 dId
 * @param oldPassword           旧明码
 * @param newPassword           新密码
 * @param confirmPassword            确认明码
 */
// 批改明码
@Transactional(propagation = Propagation.REQUIRED)
public void updateUserPassword(Integer userId, String oldPassword, String newPassword, String confirmPassword) {
    // 通过 Id 获取 user 对象
    User user = userMapper.selectByPrimaryKey(userId);
    // 参数校验 (用户,旧明码,新密码,确认明码)
    checkPasswordParams(user,oldPassword,newPassword,confirmPassword);

    // 默认参数设置,把用户输出的新密码  加密 增加进去
    user.setUserPwd(Md5Util.encode(newPassword));

    // 执行更新操作
    AssertUtil.isTrue(userMapper.updateByPrimaryKeySelective(user)<1,"批改明码失败");
}


// 批改明码的参数校验
private void checkPasswordParams(User user, String oldPassword, String newPassword, String confirmPwd) {
    // 用户不能为空(不存在)AssertUtil.isTrue(null == user,"用户不存在");
    // 原始明码  非空
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    AssertUtil.isTrue(StringUtils.isBlank(oldPassword),"原始明码不能为空");
    // 原始明码是否和数据库查问到的明码统一
    AssertUtil.isTrue(!(Md5Util.encode(oldPassword).equals(user.getUserPwd())),"原始明码不正确");
    // 新密码不能为空
    AssertUtil.isTrue(StringUtils.isBlank(newPassword),"新密码不能为空");
    // 新密码和原始明码不能雷同
    AssertUtil.isTrue(oldPassword.equals(newPassword),"新密码不能和原始明码雷同");
    // 确认明码非空
    AssertUtil.isTrue(StringUtils.isBlank(confirmPwd),"确认明码不能为空");
    // 确认明码须要和新密码统一
    AssertUtil.isTrue(!(newPassword.equals(confirmPwd)),"新密码和确认明码不统一");
}

/**
 * 多条件分页查问用户数据
 * @param query
 * @return
 */
public Map<String, Object> queryUserByParams (UserQuery query) {Map<String, Object> map = new HashMap<>();
    PageHelper.startPage(query.getPage(), query.getLimit());
    PageInfo<User> pageInfo = new PageInfo<>(userMapper.selectByParams(query));
    map.put("code",0);
    map.put("msg", "");
    map.put("count", pageInfo.getTotal());
    map.put("data", pageInfo.getList());
    System.out.println("执行结束");
    return map;
}

/**
 * 增加用户
 * @param user
 */
@Transactional(propagation = Propagation.REQUIRED)
public void saveUser(User user){
    // 参数校验
    checkParams(user.getUserName(),user.getComId(),user.getVc());
    // 设置默认参数
    user.setCreateDate(new Date());
    user.setUpdateDate(new Date());
    user.setUserPwd(Md5Util.encode("123456"));
    // 执行增加,判断后果
    AssertUtil.isTrue(userMapper.insertSelective(user)==null,"用户增加失败!");
    relaionUserRole(user.getId(),user.getRoleIds());
    AssertUtil.isTrue(communityMapper.addNumByComId(user.getComId())<1, "社区用户增加失败");
}

/**
 * 用户更新,批改
 * @param user
 */
@Transactional(propagation = Propagation.REQUIRED)
public void updateUser(User user){
    //1. 参数校验
    // 通过用户 id 获取用户对象
    User temp=userMapper.selectByPrimaryKey(user.getId());
    // 判断对象是否存在
    AssertUtil.isTrue(temp==null,"待更新记录不存在");
    // 验证参数
    checkParams1(user.getUserName(),user.getComId(),user.getVc());
    //2. 设置默认参数
    user.setUpdateDate(new Date());
    //3. 执行更新,返回后果
    AssertUtil.isTrue(userMapper.updateByPrimaryKeySelective(user)<1,"用户更新失败!");
    relaionUserRole(user.getId(),user.getRoleIds());
}

private void checkParams(String userName, Integer comId, Integer vc) {AssertUtil.isTrue(StringUtils.isBlank(userName),"用户名不能为空");
    // 验证用户是否存在
    User temp=userMapper.queryUserByUserName(userName);
    AssertUtil.isTrue(temp!=null,"用户名已存在");
    AssertUtil.isTrue(comId==null,"请输出所在社区");
    AssertUtil.isTrue(vc==null,"请抉择疫苗接种情况");
}

private void checkParams1(String userName, Integer comId, Integer vc) {AssertUtil.isTrue(StringUtils.isBlank(userName),"用户名不能为空");
    // 验证用户是否存在
    AssertUtil.isTrue(comId==null,"请输出所在社区");
    AssertUtil.isTrue(vc==null,"请抉择疫苗接种情况");
}

@Transactional(propagation = Propagation.REQUIRED)
public void deleteUser(Integer[] ids){AssertUtil.isTrue(ids==null||ids.length==0,"请抉择您要删除的记录");
    for (int id:ids){User user=userMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(communityMapper.subNumByComId(user.getComId())!=ids.length, "社区用户删除失败");
    }
    AssertUtil.isTrue(deleteBatch(ids) != ids.length,"用户角色删除失败");
}


/*SZC*/
/**
 * 用户注册
 * @param userName
 * @param password1
 * @param password2
 * @param icon
 */
public void registerUser(String userName, String password1, String password2, String icon) {
    // 参数校验
    checkRegister(userName, password1, password2, icon);
    // 实例化 user
    User user = new User();
    // 设置默认参数
    user.setUserName(userName);
    user.setUserPwd(Md5Util.encode(password1));
    user.setUserPhone(icon);
    user.setCreateDate(new Date());
    user.setUpdateDate(new Date());
    // 执行办法
    AssertUtil.isTrue(userMapper.insertSelective(user)<1, "用户增加失败");
}

/**
 * 用户注册的参数校验
 * @param userName
 * @param password1
 * @param password2
 * @param icon
 */
private void checkRegister(String userName, String password1, String password2, String icon) {
    // 用户名不为空
    AssertUtil.isTrue(StringUtils.isBlank(userName), "请输出用户名");
    // 判断用户名是否存在
    User user1 = userMapper.selectByName(userName);
    AssertUtil.isTrue(user1!=null, "该用户已存在");
    // 判断手机号是否存在
    User user2 = userMapper.selectByPhone(icon);
    AssertUtil.isTrue(user2!=null, "该手机号已注册过账号");
    // 明码不为空
    AssertUtil.isTrue(StringUtils.isBlank(password1), "请输出明码");
    // 确认明码不为空
    AssertUtil.isTrue(StringUtils.isBlank(password2), "请输出确认明码");
    // 明码长度校验
    AssertUtil.isTrue(password1.length()<6 || password1.length()>12, "明码长度为 6 -12 位");
    // 明码和确认明码相等
    AssertUtil.isTrue(!password1.equals(password2), "确认明码与明码不统一");
    // 手机号非法
    AssertUtil.isTrue(!PhoneUtil.isMobile(icon), "请输出正确的手机号");
}

/**
 * 删除用户原先的角色,并从新赋予新的角色
 * @param userId
 * @param roleIds
 */
private void relaionUserRole(int userId, String roleIds) {
    // 通过 id 获取用户的角色数量
    int count = userRoleMapper.countUserRoleByUserId(userId);
    // count>0  阐明用户原先有角色  先删除所有的角色
    if (count>0) {AssertUtil.isTrue(userRoleMapper.deleteUserRoleByUserId(userId)!=count, "用户角色删除失败");
    }
    // 传入的角色信息不为空 增加新的角色
    if (StringUtils.isNoneBlank(roleIds)) {
        // 将传入的 roleIds 转成字符串数组
        String[] roleStrIds = roleIds.split(",");
        // 用来寄存用户的角色信息
        List<UserRole> roleList = new ArrayList<>();
        // 遍历 roleIds
        for (String rid : roleStrIds) {
            // 筹备对象
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(Integer.parseInt(rid));
            userRole.setCreateDate(new Date());
            userRole.setUpdateDate(new Date());
            roleList.add(userRole);
        }
        AssertUtil.isTrue(userRoleMapper.insertBatch(roleList) != roleList.size(), "用户角色调配失败");
    }

}

}

2、今日疫情模块


外围代码 Service

@Service
public class ConfirmedService extends BaseService<Confirmed,Integer> {

@Resource
// 引入 ConfirmedMapper
private ConfirmedMapper confirmedMapper;

@Resource
// 引入 user 表
private UserMapper userMapper;

@Resource
// 引入 user 表
private CommunityMapper  communityMapper;

// 角色的条件查问以及 分页
public Map<String,Object> findRoleByParam(ConfirmedQuery confirmedQuery){
    // 实例化对象
    Map<String,Object> map = new HashMap<>();
    // 实例化分页单位
    PageHelper.startPage(confirmedQuery.getPage(), confirmedQuery.getLimit());
    // 开始分页
    PageInfo<Confirmed> rlist = new PageInfo<>(selectByParams(confirmedQuery));
    map.put("code",0);
    map.put("msg","success");
    map.put("count",rlist.getTotal());
    map.put("data",rlist.getList());
    // 返回 Map
    return map;
}


@Transactional(propagation = Propagation.REQUIRED)           // 波及到事务   就须要此注解
// 用户模块的增加
public void addUser(Confirmed user) {
    //1、参数校验
    checkConfirmed(user.getTrueName(),user.getState());

    if (user.getComId().equals("浦东区")){user.setComId(1);
    }
    if (user.getComId().equals("黄浦区")){user.setComId(2);
    }
    if (user.getComId().equals("松江区")){user.setComId(3);
    }
    if (user.getComId().equals("徐汇区")){user.setComId(4);
    }
    if (user.getComId().equals("虹口区")){user.setComId(5);
    }

    // 查问 user 表中是否存在此人  不存在   增加下来  设置默认值
    User temp =  userMapper.selectByPhone(user.getTcPhone());
   //   手机号查问用户
    if (temp != null){
        // 衰弱状态改成 2    如果 user 表外面曾经有了的状况下
        userMapper.updateUserHealthById(temp.getUserPhone());
        // 默认值  确诊表中的 userId 字段
        user.setUserId(temp.getId());

    }else {          // 表里没有这个人的时候  增加 这个用户  新建一个 user 对象
        User u = new User();
        // 实在姓名
        u.setTrueName(user.getTrueName());
        // 名字
        u.setUserName(user.getTrueName());
        // 设置明码  默认值:123456
        u.setUserPwd(Md5Util.encode("123456"));

        // 设置社区 ID
        u.setComId(user.getComId());

        // 手机号  惟一
        u.setUserPhone(user.getTcPhone());
        u.setEcPhone(user.getTcPhone());
        u.setHealth("2");
        // 创立工夫
        u.setCreateDate(new Date());
        // 批改工夫
        u.setUpdateDate(new Date());

        // 增加用户是否胜利
        AssertUtil.isTrue(userMapper.insertSelective(u)<1,"插入用户失败");

        // 给确诊人员增加其  userId
        Integer userId =  userMapper.selectById(user.getTcPhone());
        user.setUserId(userId);
    }

    //2、默认值设置
    // 确诊日期
    user.setCreateDate(new Date());

    // 增加是否胜利
    AssertUtil.isTrue(insertSelective(user)<1,"增加失败");

    //relaionUserRole(user.getId(),user.getComId());


}


@Transactional(propagation = Propagation.REQUIRED)           // 波及到事务   就须要此注解
// 用户模块的批改
public void changeUser(Confirmed user) {
    // 通过 id 获取用户信息
    Confirmed temp = confirmedMapper.selectByPrimaryKey(user.getId());
    // 判断用户信息是否存在
    AssertUtil.isTrue(temp == null,"以后用户不存在");

    // 校验参数
    changeConfirmed(user.getTrueName(),user.getTcPhone(),user.getState());

    // 批改是否胜利  完整版
    //AssertUtil.isTrue(updateByPrimaryKeySelective(user)<1,"批改失败了");


    // 批改是否胜利  完整版
    AssertUtil.isTrue(confirmedMapper.uBPKS(user)<1,"批改失败了");

}

// 批改的参数校验
private void changeConfirmed(String trueName, String tcPhone, Integer state) {
    //1、用户名不能为空
    AssertUtil.isTrue(StringUtils.isBlank(trueName),"姓名不能为空");
    //2、以后状态不能为空
    AssertUtil.isTrue(StringUtils.isBlank(tcPhone),"请输出手机号");
    //3、以后状态不能为空
    AssertUtil.isTrue(state<1 || state>4,"请抉择正确的状态码");
}

// 用户模块的增加的参数校验
private void checkConfirmed(String trueName, Integer state) {
    //1、用户名不能为空
    AssertUtil.isTrue(StringUtils.isBlank(trueName),"姓名不能为空");
    //2、以后状态不能为空
    AssertUtil.isTrue(state<1 || state>3,"请抉择正确的状态码");
}

// 增加社区时的校验
private void relaionUserRole(Integer id, Integer comId) {
    // 筹备汇合  存储对象
    List<Community> urlist = new ArrayList<>();

    //userId,roleId
    // 判断是否抉择了角色信息

    // 只能抉择一个社区
    AssertUtil.isTrue(comId>1 || comId<1,"只能抉择一个社区");

    // 通过社区表的 com_id    查问到社区表的对应社区名
    communityMapper.selectaddresComId(comId);
    // 增加

}

@Transactional(propagation = Propagation.REQUIRED)           // 波及到事务   就须要此注解
// 确诊人员的批量删除
public void deleteUserByIds(Integer[] ids) {
    // 要删除记录不能为空
    AssertUtil.isTrue(ids == null || ids.length==0,"请抉择要删除的记录");

    // 批改 user 表的状态码
    for(Integer id: ids){Confirmed confirmed = confirmedMapper.selectId(id);
        System.out.println(id+ "-----------------");
        System.out.println(confirmed.getTrueName());
        AssertUtil.isTrue(userMapper.updateById(confirmed.getUserId())<1,"批改失败");
    }

    // 删除确诊表的个人信息记录
    AssertUtil.isTrue(deleteBatch(ids)!=ids.length,"删除失败");
}

// 查问所有社区
public List<Map<String, Object>> queryComs() {return confirmedMapper.selectComs();
}

}

3、防疫治理模块

外围代码 Service:

@Service
public class CommunityService extends BaseService<Community,Integer> {

@Resource
private CommunityMapper communityMapper;

/**
 * 多条件分页查问
 * @param query
 * @return
 */
public Map<String,Object> queryComByParams(CommunityQuery query){Map<String,Object> map=new HashMap<>();
    // 初始化分页
    PageHelper.startPage(query.getPage(), query.getLimit());
    // 开始分页
    PageInfo<Community> pageInfo=new PageInfo<>(communityMapper.selectByParams(query));
    // 筹备数据
    map.put("code",0);
    map.put("msg","");
    map.put("count",pageInfo.getTotal());
    map.put("data",pageInfo.getList());
    return map;
}

// 查问所有角色信息
public List<Map<String, Object>> findRoles(Integer userId) {return communityMapper.selectRoles(userId);
}

}

//============================================================
@Service
public class VaccinationService {

@Resource
VaccinationMapper vaccinationMapper;

/* 多条件查问 */
public Map<String,Object> selectAll(VaccinationQuery vaccinationQuery) {
    // 创立 map
    Map<String,Object> map =new HashMap<String,Object>();
    // 查数据并分页
    PageHelper.startPage(vaccinationQuery.getPage(),vaccinationQuery.getLimit());
    PageInfo<Vaccination> pageInfo=new PageInfo<>(vaccinationMapper.selectByParams(vaccinationQuery));
    map.put("code",0);
    map.put("msg","success");
    map.put("data",pageInfo.getList());
    map.put("count",pageInfo.getTotal());

    return map;
}
/* 通过 ID 获取对象 */
public Vaccination selectId(Integer id) {return vaccinationMapper.selectById(id);
}

/* 增加 */
@Transactional(propagation = Propagation.REQUIRED)
public void insertVaccination(Vaccination vaccination) {
    // 审核
    checkOK(vaccination);
    vaccination.setFirstDate(new Date());
    vaccination.setSecondDate(new Date());
    // 插入
    AssertUtil.isTrue(vaccinationMapper.insertSelective(vaccination)<1,"插入失败");
}

private void checkOK(Vaccination vaccinatio){AssertUtil.isTrue(vaccinatio==null,"请输出增加的角色");
    AssertUtil.isTrue(StringUtils.isBlank(vaccinatio.getTrueName()),"用户名不能为空");
    AssertUtil.isTrue(StringUtils.isBlank(vaccinatio.getFirst()),"请填写 (是 / 否)");
    AssertUtil.isTrue(StringUtils.isBlank(vaccinatio.getSecond()),"请填写 (是 / 否)");
}

/* 删除 */
public void delete(Integer[] ids) {AssertUtil.isTrue(ids==null||ids.length==0,"请抉择要删除的用户");
    AssertUtil.isTrue(vaccinationMapper.deleteVa(ids)!=ids.length,"删除失败~~~");
}

/* 编辑 */
public void updateVa(Vaccination vaccination) {checkOK(vaccination);
    if(vaccination.getFirst()==null||"否".equals(vaccination.getFirst())){vaccination.setFirstDate(null);
    }
    if(vaccination.getSecond()==null||"否".equals(vaccination.getSecond())){vaccination.setSecondDate(null);
    }
    if("是".equals(vaccination.getFirst())){vaccination.setFirstDate(new Date());
    }
    if("是".equals(vaccination.getSecond())){vaccination.setSecondDate(new Date());
    }
    AssertUtil.isTrue(vaccinationMapper.updateByPrimaryKeySelective(vaccination)<1,"批改失败~");

}

}

4、系统管理模块

外围代码:Service:

@Service
public class RoleService extends BaseService<Role,Integer> {

@Autowired(required = false)
RoleMapper roleMapper;

@Autowired(required = false)
RoleQuery roleQuery;

@Resource
private ModuleMapper moduleMapper;

@Resource
private PermissionMapper permissionMapper;

/* 多条件查问 */
public Map<String,Object> selectRole(RoleQuery roleQuery){
    // 创立 map
    Map<String,Object> map =new HashMap<String,Object>();
    // 查数据并分页
    PageHelper.startPage(roleQuery.getPage(),roleQuery.getLimit());
    PageInfo<Role> pageInfo=new PageInfo<>(roleMapper.selectByParams(roleQuery));
    map.put("code",0);
    map.put("msg","success");
    map.put("data",pageInfo.getList());
    map.put("count",pageInfo.getTotal());
    return map;
}

/* 增加角色 */
@Transactional(propagation = Propagation.REQUIRED)
public void insertRole(Role role) {
    // 审核
    checkRole(role);
    // 增加
    role.setCreateDate(new Date());
    role.setUpdateDate(new Date());
    System.out.println("就差一点!!!!");
    AssertUtil.isTrue(insertSelective(role)<1,"增加失败了呢~");
}
private void checkRole(Role role) {
    // 是否为空
    AssertUtil.isTrue(role==null,"请输出角色信息~");
    // 判断是否曾经反复
    System.out.println("判断");
    Role role1= roleMapper.selectByName(role.getRoleName());
    System.out.println("判断完结");
    System.out.println(role1!=null);
    AssertUtil.isTrue(role1!=null,"已增加过啦~");
    System.out.println("退出 @");
}
/* 编辑角色 */
@Transactional(propagation = Propagation.REQUIRED)
public void updateRole(Role role) {role.setUpdateDate(new Date());
    AssertUtil.isTrue(updateByPrimaryKeySelective(role)<1,"编辑失败啦~");
}

/**
 * 删除角色信息
 * @param role
 */
@Transactional(propagation = Propagation.REQUIRED)
public void deleteRole(Role role) {
    // 验证
    AssertUtil.isTrue(role.getId()==null || selectByPrimaryKey(role.getId())==null, "待删除角色不存在");
    // 设定默认值
    role.setUpdateDate(new Date());
    // 删除角色绑定的权限资源
    int count = roleMapper.countPermissionByRoleId(role.getId());
    if (count>0) {int i = roleMapper.deletePermissionsByRoleId(role.getId());
        AssertUtil.isTrue(i!=count, "角色绑定的权限资源删除失败");
    }
    // 判断是否胜利
    AssertUtil.isTrue(roleMapper.updateRoleById(role.getId())<1, "角色删除失败");
}

/**
 * 查问所有角色
 * @return
 */
public List<Map<String, Object>> seleceAllRole(Integer userId) {return roleMapper.seleceAllRole(userId);
}

/**
 * 给角色增加权限
 * @param mids
 * @param roleId
 */
@Transactional(propagation = Propagation.REQUIRED)
public void addGrant(Integer[] mids, Integer roleId) {
    // 判断 roleId 是否存在
    AssertUtil.isTrue(roleId==null || roleMapper.selectByPrimaryKey(roleId)==null, "待受权的角色不存在");
    // 统计以后角色的权限资源数量
    int count = roleMapper.countPermissionByRoleId(roleId);
    if (count>0) {
        // 如果角色存在权限资源,就全副删除
        int num = roleMapper.deletePermissionsByRoleId(roleId);
        AssertUtil.isTrue(count!=num, "资源删除失败");
    }
    List<Permission> plist = new ArrayList<>();
    if (mids!=null && mids.length!=0) {
        // 遍历 mids
        for (Integer mid : mids) {
            // 实例化对象
            Permission permission = new Permission();
            // 设置数据
            permission.setRoleId(roleId);
            permission.setModuleId(mid);
            // 权限码
            permission.setAclValue(moduleMapper.selectByPrimaryKey(mid).getOptValue());
            permission.setCreateDate(new Date());
            permission.setUpdateDate(new Date());
            // 增加到 list
            plist.add(permission);
        }
    }
    AssertUtil.isTrue(permissionMapper.insertBatch(plist)!=plist.size(), "角色权限更新失败");
}

}

5、用户模块

外围代码 Service:

@Service
public class RoleService extends BaseService<Role,Integer> {

@Autowired(required = false)
RoleMapper roleMapper;

@Autowired(required = false)
RoleQuery roleQuery;

@Resource
private ModuleMapper moduleMapper;

@Resource
private PermissionMapper permissionMapper;

/* 多条件查问 */
public Map<String,Object> selectRole(RoleQuery roleQuery){
    // 创立 map
    Map<String,Object> map =new HashMap<String,Object>();
    // 查数据并分页
    PageHelper.startPage(roleQuery.getPage(),roleQuery.getLimit());
    PageInfo<Role> pageInfo=new PageInfo<>(roleMapper.selectByParams(roleQuery));
    map.put("code",0);
    map.put("msg","success");
    map.put("data",pageInfo.getList());
    map.put("count",pageInfo.getTotal());
    return map;
}

/* 增加角色 */
@Transactional(propagation = Propagation.REQUIRED)
public void insertRole(Role role) {
    // 审核
    checkRole(role);
    // 增加
    role.setCreateDate(new Date());
    role.setUpdateDate(new Date());
    System.out.println("就差一点!!!!");
    AssertUtil.isTrue(insertSelective(role)<1,"增加失败了呢~");
}
private void checkRole(Role role) {
    // 是否为空
    AssertUtil.isTrue(role==null,"请输出角色信息~");
    // 判断是否曾经反复
    System.out.println("判断");
    Role role1= roleMapper.selectByName(role.getRoleName());
    System.out.println("判断完结");
    System.out.println(role1!=null);
    AssertUtil.isTrue(role1!=null,"已增加过啦~");
    System.out.println("退出 @");
}
/* 编辑角色 */
@Transactional(propagation = Propagation.REQUIRED)
public void updateRole(Role role) {role.setUpdateDate(new Date());
    AssertUtil.isTrue(updateByPrimaryKeySelective(role)<1,"编辑失败啦~");
}

/**
 * 删除角色信息
 * @param role
 */
@Transactional(propagation = Propagation.REQUIRED)
public void deleteRole(Role role) {
    // 验证
    AssertUtil.isTrue(role.getId()==null || selectByPrimaryKey(role.getId())==null, "待删除角色不存在");
    // 设定默认值
    role.setUpdateDate(new Date());
    // 删除角色绑定的权限资源
    int count = roleMapper.countPermissionByRoleId(role.getId());
    if (count>0) {int i = roleMapper.deletePermissionsByRoleId(role.getId());
        AssertUtil.isTrue(i!=count, "角色绑定的权限资源删除失败");
    }
    // 判断是否胜利
    AssertUtil.isTrue(roleMapper.updateRoleById(role.getId())<1, "角色删除失败");
}

/**
 * 查问所有角色
 * @return
 */
public List<Map<String, Object>> seleceAllRole(Integer userId) {return roleMapper.seleceAllRole(userId);
}

/**
 * 给角色增加权限
 * @param mids
 * @param roleId
 */
@Transactional(propagation = Propagation.REQUIRED)
public void addGrant(Integer[] mids, Integer roleId) {
    // 判断 roleId 是否存在
    AssertUtil.isTrue(roleId==null || roleMapper.selectByPrimaryKey(roleId)==null, "待受权的角色不存在");
    // 统计以后角色的权限资源数量
    int count = roleMapper.countPermissionByRoleId(roleId);
    if (count>0) {
        // 如果角色存在权限资源,就全副删除
        int num = roleMapper.deletePermissionsByRoleId(roleId);
        AssertUtil.isTrue(count!=num, "资源删除失败");
    }
    List<Permission> plist = new ArrayList<>();
    if (mids!=null && mids.length!=0) {
        // 遍历 mids
        for (Integer mid : mids) {
            // 实例化对象
            Permission permission = new Permission();
            // 设置数据
            permission.setRoleId(roleId);
            permission.setModuleId(mid);
            // 权限码
            permission.setAclValue(moduleMapper.selectByPrimaryKey(mid).getOptValue());
            permission.setCreateDate(new Date());
            permission.setUpdateDate(new Date());
            // 增加到 list
            plist.add(permission);
        }
    }
    AssertUtil.isTrue(permissionMapper.insertBatch(plist)!=plist.size(), "角色权限更新失败");
}

}

正文完
 0