微人事——操作员管理

1、Dao层

public interface HrRepository extends JpaRepository<Hr,Integer> {
    public Hr findByUsername(String username);
    public List<Role> findAllRolesById(Integer id);
    public Hr findByName(String name);
    boolean existsById(Integer id);
    List<Hr> findAllById(Integer id);

    @Override
    void deleteById(Integer integer);

    
    @Modifying
    @Transactional
    @Query(value = "insert into menu_role(rid,mid) values(?1,?2)",nativeQuery = true)
    void addHr(Integer rid,Integer mids);
}
public interface HrRoleRepository extends JpaRepository<hrRole,Integer> {
    public List<hrRole> findAllByHrid(Integer id);

    @Modifying
    @Transactional
    @Query("delete from hrRole where hrid=?1")
    public void deleteByHrid(Integer hrid);

    @Modifying
    @Transactional
    @Query(value = "insert into hr_role(hrid,rid) values(?1,?2)",nativeQuery = true)
    void addHrRole(Integer hrid,Integer rid);
}

2、Service层

之前的方法我就省略没写

  • HrUtile是个工具类
  • 可以通过这个方法获得当前登陆状态的用户信息
public class HrUtils {
    public static Hr getCurrentHr() {
        return ((Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
    }
@Service
public class HrService implements UserDetailsService {

    @Autowired
    HrRepository hrRepository;
    @Autowired
    HrRoleRepository hrRoleRepository;
    @Autowired
    RoleRepository roleRepository;
	
	//获得当前的登陆的用户信息
    public List<Hr> getAllHrs(){
        return hrRepository.findAllById(HrUtils.getCurrentHr().getId());
    }
    
  • 这是个更新方法
  • 这个更新方法能够添加用户以及更新用户
  • 根据判别name来查看是更新用户还是创建新用户
    public Boolean updateHr(Hr hr){
        hr.setEnabled(true);
        if (hrRepository.findByName(hr.getName()) != null){
            Hr hr1 = hrRepository.findByName(hr.getName());
            if (hr.getPhone() != null){
                hr1.setPhone(hr.getPhone());
            }
            if (hr.getAddress() != null){
                hr1.setAddress(hr.getAddress());
            }
            if (hr.getTelephone() != null){
                hr1.setTelephone(hr.getTelephone());
            }
            if (hr.getUsername() != null){
                hr1.setUsername(hr.getUsername());
            }
            if (hr.getPassword() != null){
                hr1.setPassword(hr.getPassword());
            }
            if (hr.getUserface() != null){
                hr1.setUserface(hr.getUserface());
            }
            hrRepository.save(hr1);
            return hrRepository.existsById(hr1.getId());
        }else{
            hrRepository.save(hr);
            return hrRepository.existsById(hr.getId());
        }
    }

删除方法

    public Boolean deleteHrById(Integer id){
        hrRepository.deleteById(id);
        return hrRepository.existsById(id);
    }
}

3、Controller层

@RestController
@RequestMapping("/system/hr")
public class HrController {
    @Autowired
    HrService hrService;
    @Autowired
    RoleService roleService;
    @Autowired
    HrRoleService hrRoleService;

	//获得当前登陆信息
    @GetMapping("/")
    public List<Hr> getAllHrs(){
        return hrService.getAllHrs();
    }

	//更新或者添加账户
    @PutMapping("/")
    public RespBean updateHr(@RequestBody Hr hr){
        if (hrService.updateHr(hr) == true){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }

	//获得所有角色
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        return roleService.getAllRoles();
    }

	//设置用户角色
    @PutMapping("/roles")
    public RespBean updateHrRole(@RequestBody String str){
        Integer hrid = Integer.parseInt(JSON.parseObject(str).get("hrid").toString());
        List<Integer> rids = JSON.parseArray(JSON.parseObject(str).getString("rids"),Integer.class);
        if (hrRoleService.updateHrRole(hrid,rids) == rids.size()){
            return RespBean.ok("更新角色成功");
        }
        return RespBean.error("更新角色失败");
    }

	//删除用户
    @DeleteMapping("/{id}")
    public RespBean deleteHrById(@PathVariable Integer id){
        if (hrService.deleteHrById(id)){
            return RespBean.ok("删除成功");
        }
        return  RespBean.error("删除失败");
    }

}

方法全部可以在postman上测试成功 和之前一样我就不测试了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值