5 changed files with 127 additions and 36 deletions
-
51src/main/java/com/dreamworks/boditech/controller/UserController.java
-
2src/main/java/com/dreamworks/boditech/entity/User.java
-
4src/main/java/com/dreamworks/boditech/mapper/UserMapper.java
-
22src/main/java/com/dreamworks/boditech/service/DeviceService.java
-
82src/main/java/com/dreamworks/boditech/service/UserService.java
@ -0,0 +1,82 @@ |
|||
package com.dreamworks.boditech.service; |
|||
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; |
|||
@Service |
|||
public class UserService { |
|||
private static final Logger LOG = LoggerFactory.getLogger(UserService.class); |
|||
|
|||
@Resource |
|||
private UserMapper userMapper; |
|||
|
|||
private User curUser = null; |
|||
|
|||
// login |
|||
public User login(String name, String pin) { |
|||
User user = userMapper.findByName(name); |
|||
if ( null == user || !user.pin.equals(pin) ) { |
|||
throw new RuntimeException("无效的用户名或PIN"); |
|||
} |
|||
|
|||
this.curUser = user; |
|||
LOG.info("user login success, user id : {}", this.curUser.id); |
|||
return user; |
|||
} |
|||
|
|||
// create |
|||
public User create(String name, String pin) { |
|||
if ( null == this.curUser || 0 == this.curUser.isAdmin) { |
|||
throw new RuntimeException("无权限创建用户"); |
|||
} |
|||
|
|||
User user = userMapper.findByName(name); |
|||
if ( null != user ) { |
|||
throw new RuntimeException("用户名已存在"); |
|||
} |
|||
|
|||
user = new User(); |
|||
user.name = name; |
|||
user.pin = pin; |
|||
user.isAdmin = 0; |
|||
user.createdAt = System.currentTimeMillis(); |
|||
user.createdBy = this.curUser.id; |
|||
userMapper.insert(user); |
|||
|
|||
user = this.userMapper.findByName(name); |
|||
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("数据删除异常"); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue