You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package com.dreamworks.boditech.service;
  2. import com.dreamworks.boditech.entity.ParamUserLogin;
  3. import com.dreamworks.boditech.entity.User;
  4. import com.dreamworks.boditech.mapper.UserMapper;
  5. import jakarta.annotation.Resource;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.util.DigestUtils;
  10. @Service
  11. public class UserService {
  12. private static final Logger LOG = LoggerFactory.getLogger(UserService.class);
  13. @Resource
  14. private UserMapper userMapper;
  15. @Resource
  16. private ActionLogService actionLog;
  17. // current user
  18. private User curUser = null;
  19. // login
  20. public User login(ParamUserLogin param) {
  21. User user = userMapper.findByAccount(param.account);
  22. String hashPin = DigestUtils.md5DigestAsHex(param.pin.getBytes());
  23. if ( null == user || !user.pin.equals(hashPin) ) {
  24. throw new RuntimeException("USER_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE");
  25. }
  26. this.curUser = user;
  27. this.curUser.pin = "****";
  28. this.actionLog.setUserId(user.id);
  29. this.actionLog.log("user.login", param.account);
  30. return user;
  31. }
  32. // logout
  33. public void logout() {
  34. this.actionLog.log("user.logout");
  35. this.actionLog.setUserId(0);
  36. this.curUser = null;
  37. }
  38. // create
  39. public User create(String account, String pin) {
  40. if ( null == this.curUser || 0 == this.curUser.isAdmin) {
  41. throw new RuntimeException("无权限创建用户");
  42. }
  43. User user = userMapper.findByAccount(account);
  44. if ( null != user ) {
  45. throw new RuntimeException("用户名已存在");
  46. }
  47. user = new User();
  48. user.account = account;
  49. user.pin = pin;
  50. user.isAdmin = 0;
  51. user.createdAt = System.currentTimeMillis();
  52. user.createdBy = this.curUser.id;
  53. userMapper.insert(user);
  54. user = this.userMapper.findByAccount(account);
  55. LOG.info("user create success, user id : {}", user.id);
  56. return user;
  57. }
  58. // pin-update
  59. public void pinUpdate(String pin) {
  60. if ( null == this.curUser ) {
  61. throw new RuntimeException("请先登录");
  62. }
  63. this.curUser.pin = pin;
  64. int changeCount = userMapper.update(this.curUser);
  65. if ( 1 != changeCount ) {
  66. throw new RuntimeException("数据更新异常");
  67. }
  68. }
  69. // delete
  70. public void delete(int id) {
  71. if ( null == this.curUser || 0 == this.curUser.isAdmin) {
  72. throw new RuntimeException("无权限删除用户");
  73. }
  74. User user = this.userMapper.findById(id);
  75. if ( null == user ) {
  76. throw new RuntimeException("无效的用户id : " + id);
  77. }
  78. int deleteCount = this.userMapper.delete(user);
  79. if ( 1 != deleteCount ) {
  80. throw new RuntimeException("数据删除异常");
  81. }
  82. }
  83. }