UserServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package com.om.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.ListUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.om.constant.RedisConstant;
  8. import com.om.entity.dto.*;
  9. import com.om.entity.po.*;
  10. import com.om.entity.vo.*;
  11. import com.om.exception.BadReqException;
  12. import com.om.exception.BizException;
  13. import com.om.exception.CustomerAuthenticationException;
  14. import com.om.mapper.UserMapper;
  15. import com.om.service.*;
  16. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  17. import com.om.utils.*;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.data.redis.core.RedisTemplate;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.util.DigestUtils;
  23. import javax.annotation.Resource;
  24. import javax.servlet.http.HttpServletRequest;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.concurrent.TimeUnit;
  30. /**
  31. * <p>
  32. * 应用用户信息表 服务实现类
  33. * </p>
  34. *
  35. * @author bmmx
  36. * @since 2024-01-29
  37. */
  38. @Service
  39. @Slf4j
  40. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
  41. @Resource
  42. private JwtUtils jwtUtils;
  43. @Resource
  44. private RedisTemplate redisTemplate;
  45. @Resource
  46. IUserVciService userVciService;
  47. @Resource
  48. UserMapper userMapper;
  49. @Resource
  50. IVciInfoService vciInfoService;
  51. @Resource
  52. HttpServletRequest request;
  53. @Resource
  54. private IClientService clientService;
  55. @Resource
  56. private IDistributorService distributorService;
  57. @Override
  58. public Result login(UserLoginDTO dto) {
  59. //从dto中获取数据
  60. String username = dto.getUsername();
  61. String password = dto.getPassword();
  62. String clientNum = dto.getClientNum();
  63. String deviceSn = dto.getDeviceSn();
  64. Integer type = dto.getType();
  65. String appVersion = dto.getAppVersion();
  66. //判断数据是否为空
  67. if (StringUtils.isBlank(username) || StringUtils.isBlank(password) || StringUtils.isBlank(clientNum) ||
  68. StringUtils.isBlank(deviceSn) || type == null) {
  69. return Result.fail("未获取到数据").result(ResultCode.NO_DATA);
  70. }
  71. //根据用户名查询用户
  72. User user = this.lambdaQuery()
  73. .eq(User::getUsername, username)
  74. .one();
  75. //判断用户是否存在
  76. if (BeanUtil.isEmpty(user)) {
  77. return Result.fail("用户不存在").result(ResultCode.USER_NO_EXIST);
  78. }
  79. //密码加密
  80. try {
  81. password = AesUtil.Encrypt(password);
  82. } catch (Exception e) {
  83. throw new BizException("密码加密出错", ResultCode.PASSWORD_ENCRYPT_ERROR);
  84. }
  85. //判断密码是否正确
  86. if (!StringUtils.equals(password, user.getPassword())) {
  87. return Result.fail("密码错误").result(ResultCode.PASSWORD_ERROR);
  88. }
  89. if (user.getState() == 0) {
  90. return Result.fail("用户被禁用").result(ResultCode.USER_DISABLED);
  91. }
  92. /* // 绑定 客户端编号 和 设备 序列号 ( 判断type是登录(0)还是激活(1) )
  93. if (!user.getDeviceSn().equals(dto.getDeviceSn())){
  94. return Result.error().message("账号尚未激活").result(ResultCode.USER_NO_ACTIVATE);
  95. }*/
  96. if (type == 1) {
  97. user.setDeviceSn(deviceSn);
  98. }
  99. Client client = clientService.lambdaQuery()
  100. .eq(Client::getNumber, clientNum)
  101. .one();
  102. if (BeanUtil.isEmpty(client)) {
  103. throw new BadReqException("该客户端不存在");
  104. }
  105. // 生成token
  106. Map<String, Object> claims = new HashMap<>();
  107. claims.put("u_id", user.getId());
  108. claims.put("deviceSn", deviceSn);
  109. claims.put("appVersion", appVersion);
  110. claims.put("clientNum", clientNum);
  111. String guid = AesUtil.generateGuid(claims);
  112. //封装vo返回
  113. UserLoginVO userLoginVO = new UserLoginVO();
  114. // 查询 该用户所属的 维修厂
  115. Integer distributorId = user.getDistributorId();//用户所属的维修厂id
  116. Distributor distributor = distributorService.getById(distributorId);
  117. BeanUtil.copyProperties(distributor, userLoginVO);
  118. // 查询用户 所绑定的vci设备
  119. List<UserVci> userVciList = userVciService.lambdaQuery()
  120. .eq(UserVci::getUserId, user.getId())
  121. .list();
  122. if (!BeanUtil.isEmpty(userVciList)) {
  123. List<VciInfoVO> vciInfoVOList = new ArrayList<>();
  124. int defaultCount = 0;
  125. for (UserVci userVci : userVciList) {
  126. VciInfoVO vciInfoVO = new VciInfoVO();
  127. Integer vciInfoId = userVci.getVciInfoId();
  128. VciInfo vciInfo = vciInfoService.getById(vciInfoId);
  129. if (BeanUtil.isEmpty(vciInfo)) {
  130. throw new BadReqException("该VCI设备不存在");
  131. }
  132. if (userVci.getIsDefault() == 1) {
  133. userLoginVO.setVciSn(vciInfo.getVciNum());
  134. defaultCount++;
  135. }
  136. vciInfoVO.setVciNum(vciInfo.getVciNum());
  137. vciInfoVO.setState(String.valueOf(vciInfo.getState()));
  138. vciInfoVO.setPairingPwd(vciInfo.getPairingPwd());
  139. vciInfoVO.setBluetoothAddress(vciInfo.getBluetoothAddress());
  140. vciInfoVO.setIsDefault(String.valueOf(userVci.getIsDefault()));
  141. vciInfoVOList.add(vciInfoVO);
  142. }
  143. userLoginVO.setVciInfoList(vciInfoVOList);
  144. }
  145. //设置userLoginVO
  146. userLoginVO.setUsername(username);
  147. userLoginVO.setPassword(password);
  148. userLoginVO.setGuid(guid);
  149. return Result.ok(userLoginVO);
  150. }
  151. @Override
  152. public Result add(UserAddVO user) {
  153. User addUser = new User();
  154. BeanUtil.copyProperties(user, addUser);
  155. if (addUser.getUsername() == null || addUser.getPassword() == null) {
  156. return Result.error();
  157. }
  158. String password = addUser.getPassword();
  159. //密码加密
  160. try {
  161. password = AesUtil.Encrypt(password);
  162. } catch (Exception e) {
  163. throw new BizException("密码加密出错", ResultCode.PASSWORD_ENCRYPT_ERROR);
  164. }
  165. addUser.setPassword(password);
  166. Integer distributorId = user.getDistributorId();
  167. //根据id查询维修厂
  168. Distributor distributor = distributorService.getById(distributorId);
  169. if (BeanUtil.isEmpty(distributor)) {
  170. throw new BadReqException("该维修厂不存在");
  171. }
  172. addUser.setDistributorName(distributor.getCompany());
  173. this.save(addUser);
  174. return Result.ok();
  175. }
  176. @Override
  177. public Result delete(Integer id) {
  178. if (id == null) {
  179. return Result.error();
  180. }
  181. boolean deleteById = this.removeById(id);
  182. if (deleteById == false) {
  183. return Result.error();
  184. }
  185. return Result.ok();
  186. }
  187. @Override
  188. public Result getListByUserId(Integer userId) {
  189. User user = getById(userId);
  190. LambdaQueryWrapper<UserVci> vciLambdaQueryWrapper = new LambdaQueryWrapper<>();
  191. vciLambdaQueryWrapper.eq(UserVci::getUserId, userId);
  192. List<UserVci> list = userVciService.list(vciLambdaQueryWrapper);
  193. Integer[] vciId = new Integer[list.size()];
  194. String[] vciNum = new String[list.size()];
  195. for (int i = 0; i < list.size(); i++) {
  196. LambdaQueryWrapper<VciInfo> queryWrapper = new LambdaQueryWrapper<>();
  197. queryWrapper.eq(VciInfo::getVciNum, list.get(i).getVciInfoId());
  198. VciInfo vciInfo = vciInfoService.getOne(queryWrapper);
  199. vciId[i] = vciInfo.getId();
  200. vciNum[i] = vciInfo.getVciNum();
  201. }
  202. UserVciNumVO userVciNumVO = new UserVciNumVO();
  203. userVciNumVO.setVciId(vciId);
  204. userVciNumVO.setVciNum(vciNum);
  205. return Result.ok(userVciNumVO);
  206. }
  207. @Override
  208. public Result<UserQueryPageVO> getPageList(UserQueryPageDTO dto) {
  209. Integer pageIndex = dto.getPageIndex();
  210. Integer pageSize = dto.getPageSize();
  211. String searchUsername = dto.getSearchUsername();
  212. String searchPhone = dto.getSearchPhone();
  213. Page<User> page = this.lambdaQuery()
  214. .like(searchUsername != null, User::getUsername, searchUsername)
  215. .like(searchPhone != null, User::getTel, searchPhone)
  216. .orderByDesc(User::getCreateTime)
  217. .page(new Page<>(pageIndex, pageSize));
  218. UserQueryPageVO vo = new UserQueryPageVO();
  219. vo.setCurrent((int) page.getCurrent());
  220. vo.setSize((int) page.getSize());
  221. vo.setPages((int) page.getPages());
  222. vo.setTotal((int) page.getTotal());
  223. if (searchUsername != null || searchPhone != null) {
  224. vo.setSearchCount(true);
  225. }
  226. List<User> records = page.getRecords();
  227. vo.setRecords(records);
  228. return Result.ok(vo);
  229. }
  230. @Override
  231. public Result getByUserId(Integer userId) {
  232. User user = this.getById(userId);
  233. if (user == null) {
  234. return Result.error();
  235. }
  236. return Result.ok(user);
  237. }
  238. @Override
  239. public Result relieve() {
  240. return null;
  241. }
  242. @Override
  243. public Result updateState(Integer id, Integer state) {
  244. User selectUser = getById(id);
  245. if (BeanUtil.isEmpty(selectUser)) {
  246. throw new BadReqException("该用户不存在");
  247. }
  248. selectUser.setState(state);
  249. LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
  250. queryWrapper.eq(User::getId, id);
  251. userMapper.update(selectUser, queryWrapper);
  252. return Result.ok();
  253. }
  254. @Override
  255. public Result updateType(Integer id, Integer type) {
  256. User selectUser = getById(id);
  257. if (BeanUtil.isEmpty(selectUser)) {
  258. throw new BadReqException("该用户不存在");
  259. }
  260. selectUser.setUserType(type);
  261. LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
  262. queryWrapper.eq(User::getId, id);
  263. userMapper.update(selectUser, queryWrapper);
  264. return Result.ok();
  265. }
  266. @Override
  267. public Result updateUserInfo(UserAddVO user) {
  268. Integer userId = user.getId();
  269. User userById = getById(userId);
  270. String password = user.getPassword();
  271. BeanUtil.copyProperties(user, userById);
  272. //密码加密
  273. try {
  274. password = AesUtil.Encrypt(password);
  275. } catch (Exception e) {
  276. throw new BizException("密码加密出错", ResultCode.PASSWORD_ENCRYPT_ERROR);
  277. }
  278. if (!password.equals(userById.getPassword())) {
  279. userById.setPassword(password);
  280. }
  281. Integer distributorId = userById.getDistributorId();
  282. //根据id查询维修厂
  283. Distributor distributor = distributorService.getById(distributorId);
  284. if (BeanUtil.isEmpty(distributor)) {
  285. throw new BadReqException("该维修厂不存在");
  286. }
  287. userById.setDistributorName(distributor.getCompany());
  288. this.updateById(userById);
  289. return Result.ok();
  290. }
  291. @Override
  292. public Result password(AppUserUpdatePwdDTO dto) {
  293. if (BeanUtil.isEmpty(dto)) {
  294. return Result.fail("未获取到数据").result(ResultCode.NO_DATA);
  295. }
  296. String guid = dto.getGuid();
  297. GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid);
  298. Integer userId = guidDTO.getUserId();
  299. User user = getById(userId);
  300. if (user == null) {
  301. return Result.error().message("用户不存在").result(ResultCode.USER_NO_EXIST);
  302. }
  303. try {
  304. user.setPassword(AesUtil.Encrypt(dto.getNewPassword()));
  305. } catch (Exception e) {
  306. return Result.fail("密码加密出错").result(ResultCode.PASSWORD_ENCRYPT_ERROR);
  307. }
  308. this.updateById(user);
  309. return Result.ok();
  310. }
  311. @Override
  312. public Result register(UserLoginDTO userLoginDTO) {
  313. User user = new User();
  314. if (userLoginDTO.getUsername() == null || userLoginDTO.getPassword() == null) {
  315. return Result.error().message("用户名密码为空").result(ResultCode.NO_DATA);
  316. }
  317. if (userLoginDTO.getPassword().equals(userLoginDTO.getRePassword())) {
  318. return Result.error().message("两次密码不一样").result(ResultCode.PASSWORD_ERROR);
  319. }
  320. user.setUsername(userLoginDTO.getUsername());
  321. try {
  322. user.setPassword(AesUtil.Encrypt(userLoginDTO.getPassword()));
  323. } catch (Exception e) {
  324. return Result.error().message("密码加密错误").result(ResultCode.PASSWORD_ENCRYPT_ERROR);
  325. }
  326. boolean save = save(user);
  327. if (!save) {
  328. throw new BizException("数据库操作失败");
  329. }
  330. return Result.ok();
  331. }
  332. @Override
  333. public Result updateUserName(AppBaseDTO dto) {
  334. String guid = dto.getGuid();
  335. GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid);
  336. Integer userId = guidDTO.getUserId();
  337. User user = getById(userId);
  338. if (user == null) {
  339. return Result.error().message("用户不存在").result(ResultCode.USER_NO_EXIST);
  340. }
  341. user.setUsername(dto.getUsername());
  342. LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
  343. queryWrapper.eq(User::getId, userId);
  344. this.update(user, queryWrapper);
  345. return Result.ok();
  346. }
  347. @Override
  348. public Result logout() {
  349. String token = request.getHeader("token");
  350. token = request.getParameter("token");
  351. //从token中获取用户id
  352. Integer uId = jwtUtils.getUserIdFromToken(token);
  353. //判断 用户id 是否为空
  354. if (org.springframework.util.StringUtils.isEmpty(uId)) {
  355. throw new CustomerAuthenticationException("token解析失败");
  356. }
  357. //判断redis中是否存在token信息
  358. String tokenKey = RedisConstant.USER_TOKEN_PREFIX + uId;
  359. redisTemplate.delete(tokenKey);
  360. return Result.ok();
  361. }
  362. @Override
  363. public Result updateVci(AppBaseDTO dto) {
  364. if (BeanUtil.isEmpty(dto)) {
  365. return Result.error().message("参数为空").result(ResultCode.NO_DATA);
  366. }
  367. String guid = dto.getGuid();
  368. GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid);
  369. Integer userId = guidDTO.getUserId();
  370. String vciSn = dto.getVciSn();
  371. VciInfo vciInfo = vciInfoService.lambdaQuery()
  372. .eq(VciInfo::getVciNum, vciSn)
  373. .one();
  374. if (vciInfo == null) {
  375. return Result.error().message("VCI设备不存在").result(ResultCode.SERVICE_HANDLE_ERROR);
  376. }
  377. UserVci userVci = userVciService.lambdaQuery()
  378. .eq(UserVci::getUserId, userId)
  379. .eq(UserVci::getIsDefault, true)
  380. .one();
  381. userVci.setVciInfoId(vciInfo.getId());
  382. LambdaQueryWrapper<VciInfo> queryWrapper = new LambdaQueryWrapper<>();
  383. queryWrapper.eq(VciInfo::getId, vciInfo.getId());
  384. boolean update = vciInfoService.update(vciInfo, queryWrapper);
  385. if (!update) {
  386. throw new BizException("数据库操作失败");
  387. }
  388. return Result.ok();
  389. }
  390. @Override
  391. public Result bindingVci(AppBindVO bindVO) {
  392. if (BeanUtil.isEmpty(bindVO)) {
  393. return Result.error().message("参数为空").result(ResultCode.NO_DATA);
  394. }
  395. Integer userId = bindVO.getUserId();
  396. String vciNum = bindVO.getVciSn();
  397. LambdaQueryWrapper<VciInfo> vciInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
  398. vciInfoLambdaQueryWrapper.eq(VciInfo::getVciNum, vciNum);
  399. VciInfo vciInfo = vciInfoService.getOne(vciInfoLambdaQueryWrapper);
  400. if (vciInfo == null) {
  401. return Result.error().message("VCI设备不存在").result(ResultCode.SERVICE_HANDLE_ERROR);
  402. }
  403. UserVci userVci = new UserVci();
  404. userVci.setUserId(userId);
  405. userVci.setVciInfoId(vciInfo.getId());
  406. userVciService.save(userVci);
  407. return Result.ok();
  408. }
  409. @Override
  410. public Result<List<VciInfo>> getVciByUserId(Integer userId) {
  411. //根据id查询user
  412. User user = this.getById(userId);
  413. if (BeanUtil.isEmpty(user)) {
  414. throw new BadReqException("该用户不存在");
  415. }
  416. List<UserVci> list = userVciService.lambdaQuery()
  417. .eq(UserVci::getUserId, userId)
  418. .list();
  419. if (list.isEmpty()) {
  420. return Result.ok();
  421. }
  422. return null;
  423. }
  424. }