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
95 lines
2.9 KiB
package com.dreamworks.boditech.service;
|
|
import com.dreamworks.boditech.entity.ParamUserLogin;
|
|
import com.dreamworks.boditech.entity.User;
|
|
import com.dreamworks.boditech.mapper.UserMapper;
|
|
import jakarta.annotation.Resource;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.DigestUtils;
|
|
@Service
|
|
public class UserService {
|
|
private static final Logger LOG = LoggerFactory.getLogger(UserService.class);
|
|
@Resource
|
|
private UserMapper userMapper;
|
|
@Resource
|
|
private ActionLogService actionLog;
|
|
// current user
|
|
private User curUser = null;
|
|
|
|
// login
|
|
public User login(ParamUserLogin param) {
|
|
User user = userMapper.findByAccount(param.account);
|
|
String hashPin = DigestUtils.md5DigestAsHex(param.pin.getBytes());
|
|
if ( null == user || !user.pin.equals(hashPin) ) {
|
|
throw new RuntimeException("USER_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE");
|
|
}
|
|
|
|
this.curUser = user;
|
|
this.curUser.pin = "****";
|
|
this.actionLog.setUserId(user.id);
|
|
this.actionLog.log("user.login", param.account);
|
|
return user;
|
|
}
|
|
|
|
// logout
|
|
public void logout() {
|
|
this.actionLog.log("user.logout");
|
|
this.actionLog.setUserId(0);
|
|
this.curUser = null;
|
|
}
|
|
|
|
// create
|
|
public User create(String account, String pin) {
|
|
if ( null == this.curUser || 0 == this.curUser.isAdmin) {
|
|
throw new RuntimeException("无权限创建用户");
|
|
}
|
|
|
|
User user = userMapper.findByAccount(account);
|
|
if ( null != user ) {
|
|
throw new RuntimeException("用户名已存在");
|
|
}
|
|
|
|
user = new User();
|
|
user.account = account;
|
|
user.pin = pin;
|
|
user.isAdmin = 0;
|
|
user.createdAt = System.currentTimeMillis();
|
|
user.createdBy = this.curUser.id;
|
|
userMapper.insert(user);
|
|
|
|
user = this.userMapper.findByAccount(account);
|
|
LOG.info("user create success, user id : {}", user.id);
|
|
return user;
|
|
}
|
|
|
|
// pin-update
|
|
public void pinUpdate(String pin) {
|
|
if ( null == this.curUser ) {
|
|
throw new RuntimeException("请先登录");
|
|
}
|
|
|
|
this.curUser.pin = pin;
|
|
int changeCount = userMapper.update(this.curUser);
|
|
if ( 1 != changeCount ) {
|
|
throw new RuntimeException("数据更新异常");
|
|
}
|
|
}
|
|
|
|
// delete
|
|
public void delete(int id) {
|
|
if ( null == this.curUser || 0 == this.curUser.isAdmin) {
|
|
throw new RuntimeException("无权限删除用户");
|
|
}
|
|
|
|
User user = this.userMapper.findById(id);
|
|
if ( null == user ) {
|
|
throw new RuntimeException("无效的用户id : " + id);
|
|
}
|
|
|
|
int deleteCount = this.userMapper.delete(user);
|
|
if ( 1 != deleteCount ) {
|
|
throw new RuntimeException("数据删除异常");
|
|
}
|
|
}
|
|
}
|