|
@@ -3,11 +3,14 @@ package com.om.service.impl;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.collection.ListUtil;
|
|
import cn.hutool.core.collection.ListUtil;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.om.constant.RedisConstant;
|
|
import com.om.constant.RedisConstant;
|
|
import com.om.entity.dto.UserLoginDTO;
|
|
import com.om.entity.dto.UserLoginDTO;
|
|
|
|
+import com.om.entity.dto.UserQueryPageDTO;
|
|
import com.om.entity.po.User;
|
|
import com.om.entity.po.User;
|
|
import com.om.entity.vo.UserAddVO;
|
|
import com.om.entity.vo.UserAddVO;
|
|
import com.om.entity.vo.UserLoginVO;
|
|
import com.om.entity.vo.UserLoginVO;
|
|
|
|
+import com.om.entity.vo.UserQueryPageVO;
|
|
import com.om.exception.BadReqException;
|
|
import com.om.exception.BadReqException;
|
|
import com.om.mapper.UserMapper;
|
|
import com.om.mapper.UserMapper;
|
|
import com.om.service.IUserService;
|
|
import com.om.service.IUserService;
|
|
@@ -15,6 +18,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.om.utils.JwtUtils;
|
|
import com.om.utils.JwtUtils;
|
|
import com.om.utils.Result;
|
|
import com.om.utils.Result;
|
|
import com.om.utils.UserContext;
|
|
import com.om.utils.UserContext;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -35,6 +39,7 @@ import java.util.concurrent.TimeUnit;
|
|
* @since 2024-01-29
|
|
* @since 2024-01-29
|
|
*/
|
|
*/
|
|
@Service
|
|
@Service
|
|
|
|
+@Slf4j
|
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
|
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
@@ -59,7 +64,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
|
|
|
|
//判断数据是否为空
|
|
//判断数据是否为空
|
|
if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(clientNum) ||
|
|
if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(clientNum) ||
|
|
- StringUtils.isBlank(deviceSn) || type == null){
|
|
|
|
|
|
+ StringUtils.isBlank(deviceSn) || type == null) {
|
|
throw new BadReqException("数据为空!!");
|
|
throw new BadReqException("数据为空!!");
|
|
}
|
|
}
|
|
|
|
|
|
@@ -69,7 +74,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
.one();
|
|
.one();
|
|
|
|
|
|
//判断用户是否存在
|
|
//判断用户是否存在
|
|
- if (BeanUtil.isEmpty(user)){
|
|
|
|
|
|
+ if (BeanUtil.isEmpty(user)) {
|
|
throw new BadReqException("该用户不存在!!");
|
|
throw new BadReqException("该用户不存在!!");
|
|
}
|
|
}
|
|
|
|
|
|
@@ -77,7 +82,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
//密码加密
|
|
//密码加密
|
|
password = DigestUtils.md5DigestAsHex(password.getBytes());
|
|
password = DigestUtils.md5DigestAsHex(password.getBytes());
|
|
//判断密码是否正确
|
|
//判断密码是否正确
|
|
- if (!password.equals(user.getPassword())){
|
|
|
|
|
|
+ if (!password.equals(user.getPassword())) {
|
|
throw new BadReqException("密码错误");
|
|
throw new BadReqException("密码错误");
|
|
}
|
|
}
|
|
// TODO 判断用户状态
|
|
// TODO 判断用户状态
|
|
@@ -86,11 +91,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
|
|
|
|
// 生成token
|
|
// 生成token
|
|
Map<String, Object> claims = new HashMap<>();
|
|
Map<String, Object> claims = new HashMap<>();
|
|
- claims.put("u_id",user.getId());
|
|
|
|
|
|
+ claims.put("u_id", user.getId());
|
|
String token = jwtUtils.generateToken(claims);
|
|
String token = jwtUtils.generateToken(claims);
|
|
//把token存入到redis中
|
|
//把token存入到redis中
|
|
String key = RedisConstant.USER_TOKEN_PREFIX + user.getId();
|
|
String key = RedisConstant.USER_TOKEN_PREFIX + user.getId();
|
|
- redisTemplate.opsForValue().set(key,token,RedisConstant.USER_TOKEN_TTL, TimeUnit.SECONDS);
|
|
|
|
|
|
+ redisTemplate.opsForValue().set(key, token, RedisConstant.USER_TOKEN_TTL, TimeUnit.SECONDS);
|
|
|
|
|
|
//封装vo返回
|
|
//封装vo返回
|
|
UserLoginVO userLoginVO = new UserLoginVO();
|
|
UserLoginVO userLoginVO = new UserLoginVO();
|
|
@@ -110,8 +115,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
@Override
|
|
@Override
|
|
public Result add(UserAddVO user) {
|
|
public Result add(UserAddVO user) {
|
|
User addUser = new User();
|
|
User addUser = new User();
|
|
- BeanUtil.copyProperties(user,addUser);
|
|
|
|
- if (addUser.getUsername()==null || addUser.getPassword() == null ){
|
|
|
|
|
|
+ BeanUtil.copyProperties(user, addUser);
|
|
|
|
+ if (addUser.getUsername() == null || addUser.getPassword() == null) {
|
|
return Result.error();
|
|
return Result.error();
|
|
}
|
|
}
|
|
this.save(addUser);
|
|
this.save(addUser);
|
|
@@ -123,11 +128,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public Result delete(Integer id) {
|
|
public Result delete(Integer id) {
|
|
- if (id == null){
|
|
|
|
|
|
+ if (id == null) {
|
|
return Result.error();
|
|
return Result.error();
|
|
}
|
|
}
|
|
boolean deleteById = this.removeById(id);
|
|
boolean deleteById = this.removeById(id);
|
|
- if (deleteById == false){
|
|
|
|
|
|
+ if (deleteById == false) {
|
|
return Result.error();
|
|
return Result.error();
|
|
}
|
|
}
|
|
return Result.ok();
|
|
return Result.ok();
|
|
@@ -141,20 +146,36 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public Result getPageList(UserAddVO user) {
|
|
|
|
-
|
|
|
|
- List<User> userList = userMapper.selectList(null);
|
|
|
|
- if (user.getPageIndex() <=0 || user.getPageSize()<=0 || userList == null){
|
|
|
|
- return Result.error();
|
|
|
|
|
|
+ public Result<UserQueryPageVO> getPageList(UserQueryPageDTO dto) {
|
|
|
|
+ Integer pageIndex = dto.getPageIndex();
|
|
|
|
+ Integer pageSize = dto.getPageSize();
|
|
|
|
+ String searchUsername = dto.getSearchUsername();
|
|
|
|
+ String searchPhone = dto.getSearchPhone();
|
|
|
|
+
|
|
|
|
+ Page<User> page = this.lambdaQuery()
|
|
|
|
+ .like(searchUsername != null, User::getUsername, searchUsername)
|
|
|
|
+ .like(searchPhone != null, User::getTel, searchPhone)
|
|
|
|
+ .orderByDesc(User::getCreateTime)
|
|
|
|
+ .page(new Page<>(pageIndex, pageSize));
|
|
|
|
+
|
|
|
|
+ UserQueryPageVO vo = new UserQueryPageVO();
|
|
|
|
+ vo.setCurrent((int) page.getCurrent());
|
|
|
|
+ vo.setSize((int) page.getSize());
|
|
|
|
+ vo.setPages((int) page.getPages());
|
|
|
|
+ vo.setTotal((int) page.getTotal());
|
|
|
|
+ if (searchUsername!=null || searchPhone !=null){
|
|
|
|
+ vo.setSearchCount(true);
|
|
}
|
|
}
|
|
- List<User> list = ListUtil.page(user.getPageIndex(), user.getPageSize(), userList);
|
|
|
|
- return Result.ok(list);
|
|
|
|
|
|
+ List<User> records = page.getRecords();
|
|
|
|
+ vo.setRecords(records);
|
|
|
|
+
|
|
|
|
+ return Result.ok(vo);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public Result getByUserId(Integer userId) {
|
|
public Result getByUserId(Integer userId) {
|
|
User user = this.getById(userId);
|
|
User user = this.getById(userId);
|
|
- if (user == null){
|
|
|
|
|
|
+ if (user == null) {
|
|
return Result.error();
|
|
return Result.error();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -167,25 +188,26 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public Result updateState(User user) {
|
|
|
|
- Integer userId = UserContext.getUserId();
|
|
|
|
|
|
+ public Result updateState(Integer id, Integer state) {
|
|
|
|
+ User selectUser = getById(id);
|
|
|
|
+ if (BeanUtil.isEmpty(selectUser)){
|
|
|
|
+ throw new BadReqException("该用户不存在");
|
|
|
|
+ }
|
|
|
|
|
|
- User selectUser = getById(userId);
|
|
|
|
- selectUser.setState(user.getState());
|
|
|
|
- userMapper.update(selectUser,null);
|
|
|
|
|
|
+ selectUser.setState(state);
|
|
|
|
+ userMapper.update(selectUser, null);
|
|
|
|
|
|
return Result.ok();
|
|
return Result.ok();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public Result updateType(User user) {
|
|
|
|
- if (user.getUserType()==null){
|
|
|
|
- return Result.error();
|
|
|
|
- }
|
|
|
|
- Integer userId = UserContext.getUserId();
|
|
|
|
- User selectUser = getById(userId);
|
|
|
|
- selectUser.setUserType(user.getUserType());
|
|
|
|
- userMapper.update(selectUser,null);
|
|
|
|
|
|
+ public Result updateType(Integer id, Integer type) {
|
|
|
|
+ User selectUser = getById(id);
|
|
|
|
+ if (BeanUtil.isEmpty(selectUser)){
|
|
|
|
+ throw new BadReqException("该用户不存在");
|
|
|
|
+ }
|
|
|
|
+ selectUser.setUserType(type);
|
|
|
|
+ userMapper.update(selectUser, null);
|
|
return Result.ok();
|
|
return Result.ok();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -193,7 +215,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|
public Result updateUserInfo(UserAddVO user) {
|
|
public Result updateUserInfo(UserAddVO user) {
|
|
Integer userId = UserContext.getUserId();
|
|
Integer userId = UserContext.getUserId();
|
|
User userById = getById(userId);
|
|
User userById = getById(userId);
|
|
- BeanUtil.copyProperties(user,userById);
|
|
|
|
|
|
+ BeanUtil.copyProperties(user, userById);
|
|
userById.setId(userId);
|
|
userById.setId(userId);
|
|
|
|
|
|
return Result.ok();
|
|
return Result.ok();
|