package com.om.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.ListUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.om.constant.RedisConstant; import com.om.entity.dto.*; import com.om.entity.po.*; import com.om.entity.vo.*; import com.om.exception.BadReqException; import com.om.exception.BizException; import com.om.exception.CustomerAuthenticationException; import com.om.mapper.UserMapper; import com.om.service.*; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.om.utils.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.DigestUtils; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /** *

* 应用用户信息表 服务实现类 *

* * @author bmmx * @since 2024-01-29 */ @Service @Slf4j public class UserServiceImpl extends ServiceImpl implements IUserService { @Resource private JwtUtils jwtUtils; @Resource private RedisTemplate redisTemplate; @Resource IUserVciService userVciService; @Resource UserMapper userMapper; @Resource IVciInfoService vciInfoService; @Resource HttpServletRequest request; @Resource private IClientService clientService; @Resource private IDistributorService distributorService; @Override public Result login(UserLoginDTO dto) { //从dto中获取数据 String username = dto.getUsername(); String password = dto.getPassword(); String clientNum = dto.getClientNum(); String deviceSn = dto.getDeviceSn(); Integer type = dto.getType(); String appVersion = dto.getAppVersion(); //判断数据是否为空 if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(clientNum) || StringUtils.isBlank(deviceSn) || type == null) { return Result.fail("未获取到数据").result(ResultCode.NO_DATA); } //根据用户名查询用户 User user = this.lambdaQuery() .eq(User::getUsername, username) .one(); //判断用户是否存在 if (BeanUtil.isEmpty(user)) { return Result.fail("用户不存在").result(ResultCode.USER_NO_EXIST); } //密码加密 try { password = AesUtil.Encrypt(password); } catch (Exception e) { throw new BizException("密码加密出错", ResultCode.PASSWORD_ENCRYPT_ERROR); } //判断密码是否正确 if (!StringUtils.equals(password, user.getPassword())) { return Result.fail("密码错误").result(ResultCode.PASSWORD_ERROR); } if (user.getState() == 0) { return Result.fail("用户被禁用").result(ResultCode.USER_DISABLED); } /* // 绑定 客户端编号 和 设备 序列号 ( 判断type是登录(0)还是激活(1) ) if (!user.getDeviceSn().equals(dto.getDeviceSn())){ return Result.error().message("账号尚未激活").result(ResultCode.USER_NO_ACTIVATE); }*/ if (type == 1) { user.setDeviceSn(deviceSn); } Client client = clientService.lambdaQuery() .eq(Client::getNumber, clientNum) .one(); if (BeanUtil.isEmpty(client)) { throw new BadReqException("该客户端不存在"); } // 生成token Map claims = new HashMap<>(); claims.put("u_id", user.getId()); claims.put("deviceSn", deviceSn); claims.put("appVersion", appVersion); claims.put("clientNum", clientNum); String guid = AesUtil.generateGuid(claims); //封装vo返回 UserLoginVO userLoginVO = new UserLoginVO(); // 查询 该用户所属的 维修厂 Integer distributorId = user.getDistributorId();//用户所属的维修厂id Distributor distributor = distributorService.getById(distributorId); BeanUtil.copyProperties(distributor, userLoginVO); // 查询用户 所绑定的vci设备 List userVciList = userVciService.lambdaQuery() .eq(UserVci::getUserId, user.getId()) .list(); if (!BeanUtil.isEmpty(userVciList)) { List vciInfoVOList = new ArrayList<>(); int defaultCount = 0; for (UserVci userVci : userVciList) { VciInfoVO vciInfoVO = new VciInfoVO(); Integer vciInfoId = userVci.getVciInfoId(); VciInfo vciInfo = vciInfoService.getById(vciInfoId); if (BeanUtil.isEmpty(vciInfo)) { throw new BadReqException("该VCI设备不存在"); } if (userVci.getIsDefault() == 1) { userLoginVO.setVciSn(vciInfo.getVciNum()); defaultCount++; } vciInfoVO.setVciNum(vciInfo.getVciNum()); vciInfoVO.setState(String.valueOf(vciInfo.getState())); vciInfoVO.setPairingPwd(vciInfo.getPairingPwd()); vciInfoVO.setBluetoothAddress(vciInfo.getBluetoothAddress()); vciInfoVO.setIsDefault(String.valueOf(userVci.getIsDefault())); vciInfoVOList.add(vciInfoVO); } userLoginVO.setVciInfoList(vciInfoVOList); } //设置userLoginVO userLoginVO.setUsername(username); userLoginVO.setPassword(password); userLoginVO.setGuid(guid); return Result.ok(userLoginVO); } @Override public Result add(UserAddVO user) { User addUser = new User(); BeanUtil.copyProperties(user, addUser); if (addUser.getUsername() == null || addUser.getPassword() == null) { return Result.error(); } String password = addUser.getPassword(); //密码加密 try { password = AesUtil.Encrypt(password); } catch (Exception e) { throw new BizException("密码加密出错", ResultCode.PASSWORD_ENCRYPT_ERROR); } addUser.setPassword(password); Integer distributorId = user.getDistributorId(); //根据id查询维修厂 Distributor distributor = distributorService.getById(distributorId); if (BeanUtil.isEmpty(distributor)) { throw new BadReqException("该维修厂不存在"); } addUser.setDistributorName(distributor.getCompany()); this.save(addUser); return Result.ok(); } @Override public Result delete(Integer id) { if (id == null) { return Result.error(); } boolean deleteById = this.removeById(id); if (deleteById == false) { return Result.error(); } return Result.ok(); } @Override public Result getListByUserId(Integer userId) { User user = getById(userId); LambdaQueryWrapper vciLambdaQueryWrapper = new LambdaQueryWrapper<>(); vciLambdaQueryWrapper.eq(UserVci::getUserId, userId); List list = userVciService.list(vciLambdaQueryWrapper); Integer[] vciId = new Integer[list.size()]; String[] vciNum = new String[list.size()]; for (int i = 0; i < list.size(); i++) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(VciInfo::getVciNum, list.get(i).getVciInfoId()); VciInfo vciInfo = vciInfoService.getOne(queryWrapper); vciId[i] = vciInfo.getId(); vciNum[i] = vciInfo.getVciNum(); } UserVciNumVO userVciNumVO = new UserVciNumVO(); userVciNumVO.setVciId(vciId); userVciNumVO.setVciNum(vciNum); return Result.ok(userVciNumVO); } @Override public Result getPageList(UserQueryPageDTO dto) { Integer pageIndex = dto.getPageIndex(); Integer pageSize = dto.getPageSize(); String searchUsername = dto.getSearchUsername(); String searchPhone = dto.getSearchPhone(); Page 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 records = page.getRecords(); vo.setRecords(records); return Result.ok(vo); } @Override public Result getByUserId(Integer userId) { User user = this.getById(userId); if (user == null) { return Result.error(); } return Result.ok(user); } @Override public Result relieve() { return null; } @Override public Result updateState(Integer id, Integer state) { User selectUser = getById(id); if (BeanUtil.isEmpty(selectUser)) { throw new BadReqException("该用户不存在"); } selectUser.setState(state); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(User::getId, id); userMapper.update(selectUser, queryWrapper); return Result.ok(); } @Override public Result updateType(Integer id, Integer type) { User selectUser = getById(id); if (BeanUtil.isEmpty(selectUser)) { throw new BadReqException("该用户不存在"); } selectUser.setUserType(type); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(User::getId, id); userMapper.update(selectUser, queryWrapper); return Result.ok(); } @Override public Result updateUserInfo(UserAddVO user) { Integer userId = user.getId(); User userById = getById(userId); String password = user.getPassword(); BeanUtil.copyProperties(user, userById); //密码加密 try { password = AesUtil.Encrypt(password); } catch (Exception e) { throw new BizException("密码加密出错", ResultCode.PASSWORD_ENCRYPT_ERROR); } if (!password.equals(userById.getPassword())) { userById.setPassword(password); } Integer distributorId = userById.getDistributorId(); //根据id查询维修厂 Distributor distributor = distributorService.getById(distributorId); if (BeanUtil.isEmpty(distributor)) { throw new BadReqException("该维修厂不存在"); } userById.setDistributorName(distributor.getCompany()); this.updateById(userById); return Result.ok(); } @Override public Result password(AppUserUpdatePwdDTO dto) { if (BeanUtil.isEmpty(dto)) { return Result.fail("未获取到数据").result(ResultCode.NO_DATA); } String guid = dto.getGuid(); GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid); Integer userId = guidDTO.getUserId(); User user = getById(userId); if (user == null) { return Result.error().message("用户不存在").result(ResultCode.USER_NO_EXIST); } try { user.setPassword(AesUtil.Encrypt(dto.getNewPassword())); } catch (Exception e) { return Result.fail("密码加密出错").result(ResultCode.PASSWORD_ENCRYPT_ERROR); } this.updateById(user); return Result.ok(); } @Override public Result register(UserLoginDTO userLoginDTO) { User user = new User(); if (userLoginDTO.getUsername() == null || userLoginDTO.getPassword() == null) { return Result.error().message("用户名密码为空").result(ResultCode.NO_DATA); } if (userLoginDTO.getPassword().equals(userLoginDTO.getRePassword())) { return Result.error().message("两次密码不一样").result(ResultCode.PASSWORD_ERROR); } user.setUsername(userLoginDTO.getUsername()); try { user.setPassword(AesUtil.Encrypt(userLoginDTO.getPassword())); } catch (Exception e) { return Result.error().message("密码加密错误").result(ResultCode.PASSWORD_ENCRYPT_ERROR); } boolean save = save(user); if (!save) { throw new BizException("数据库操作失败"); } return Result.ok(); } @Override public Result updateUserName(AppBaseDTO dto) { String guid = dto.getGuid(); GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid); Integer userId = guidDTO.getUserId(); User user = getById(userId); if (user == null) { return Result.error().message("用户不存在").result(ResultCode.USER_NO_EXIST); } user.setUsername(dto.getUsername()); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(User::getId, userId); this.update(user, queryWrapper); return Result.ok(); } @Override public Result logout() { String token = request.getHeader("token"); token = request.getParameter("token"); //从token中获取用户id Integer uId = jwtUtils.getUserIdFromToken(token); //判断 用户id 是否为空 if (org.springframework.util.StringUtils.isEmpty(uId)) { throw new CustomerAuthenticationException("token解析失败"); } //判断redis中是否存在token信息 String tokenKey = RedisConstant.USER_TOKEN_PREFIX + uId; redisTemplate.delete(tokenKey); return Result.ok(); } @Override public Result updateVci(AppBaseDTO dto) { if (BeanUtil.isEmpty(dto)) { return Result.error().message("参数为空").result(ResultCode.NO_DATA); } String guid = dto.getGuid(); GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid); Integer userId = guidDTO.getUserId(); String vciSn = dto.getVciSn(); VciInfo vciInfo = vciInfoService.lambdaQuery() .eq(VciInfo::getVciNum, vciSn) .one(); if (vciInfo == null) { return Result.error().message("VCI设备不存在").result(ResultCode.SERVICE_HANDLE_ERROR); } UserVci userVci = userVciService.lambdaQuery() .eq(UserVci::getUserId, userId) .eq(UserVci::getIsDefault, true) .one(); userVci.setVciInfoId(vciInfo.getId()); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(VciInfo::getId, vciInfo.getId()); boolean update = vciInfoService.update(vciInfo, queryWrapper); if (!update) { throw new BizException("数据库操作失败"); } return Result.ok(); } @Override public Result bindingVci(AppBindVO bindVO) { if (BeanUtil.isEmpty(bindVO)) { return Result.error().message("参数为空").result(ResultCode.NO_DATA); } Integer userId = bindVO.getUserId(); String vciNum = bindVO.getVciSn(); LambdaQueryWrapper vciInfoLambdaQueryWrapper = new LambdaQueryWrapper<>(); vciInfoLambdaQueryWrapper.eq(VciInfo::getVciNum, vciNum); VciInfo vciInfo = vciInfoService.getOne(vciInfoLambdaQueryWrapper); if (vciInfo == null) { return Result.error().message("VCI设备不存在").result(ResultCode.SERVICE_HANDLE_ERROR); } UserVci userVci = new UserVci(); userVci.setUserId(userId); userVci.setVciInfoId(vciInfo.getId()); userVciService.save(userVci); return Result.ok(); } @Override public Result> getVciByUserId(Integer userId) { //根据id查询user User user = this.getById(userId); if (BeanUtil.isEmpty(user)) { throw new BadReqException("该用户不存在"); } List list = userVciService.lambdaQuery() .eq(UserVci::getUserId, userId) .list(); if (list.isEmpty()) { return Result.ok(); } return null; } }