UserAuthServiceImpl.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.fdkankan.openApi.service.system.impl;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.baomidou.dynamic.datasource.annotation.DS;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.fdkankan.openApi.constant.RedisKey;
  7. import com.fdkankan.openApi.entity.system.UserAuthInfo;
  8. import com.fdkankan.openApi.mapper.system.IUserAuthMapper;
  9. import com.fdkankan.openApi.service.system.IUserAuthService;
  10. import com.fdkankan.redis.util.RedisUtil;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Isolation;
  14. import org.springframework.transaction.annotation.Propagation;
  15. import org.springframework.transaction.annotation.Transactional;
  16. @DS("system")
  17. @Service
  18. public class UserAuthServiceImpl extends ServiceImpl<IUserAuthMapper, UserAuthInfo> implements IUserAuthService {
  19. @Autowired
  20. RedisUtil redisUtil;
  21. @Override
  22. public UserAuthInfo findByUserId(Integer userId) {
  23. LambdaQueryWrapper<UserAuthInfo> wrapper = new LambdaQueryWrapper<>();
  24. wrapper.eq(UserAuthInfo::getUserId, userId);
  25. return getOne((wrapper));
  26. }
  27. @Override
  28. public UserAuthInfo findByAppKey(String appKey) {
  29. LambdaQueryWrapper<UserAuthInfo> wrapper = new LambdaQueryWrapper<>();
  30. wrapper.eq(UserAuthInfo::getAppKey, appKey);
  31. return getOne((wrapper));
  32. }
  33. @Override
  34. public boolean findExistByAppKey(String appKey) {
  35. String key = String.format(RedisKey.USER_APP_KEY_INFO, appKey);
  36. if (redisUtil.hasKey(key)) {
  37. return true;
  38. } else {
  39. UserAuthInfo userAuthInfo = findByAppKey(appKey);
  40. if (ObjectUtil.isNotNull(userAuthInfo)) {
  41. redisUtil.set(key, userAuthInfo.getAppKey(), 3600);
  42. return true;
  43. } else {
  44. return false;
  45. }
  46. }
  47. }
  48. @Override
  49. @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
  50. public int updateCallCounts(String appKey) {
  51. return getBaseMapper().updateCallCounts(appKey);
  52. }
  53. @Override
  54. public boolean updateCallCounts(String appKey, Integer count) {
  55. int i = getBaseMapper().updateCallCountsByParam(appKey, count);
  56. if (i > 0) {
  57. redisUtil.set(String.format(RedisKey.API_METHOD_COUNT, appKey), String.valueOf(count));
  58. return true;
  59. }
  60. return false;
  61. }
  62. //1:累加,2:累减
  63. @Override
  64. public boolean updateCallCountsByType(String appKey, Integer count, Integer type) {
  65. int i = getBaseMapper().updateCallCountsByParamAndType(appKey, count, type);
  66. if (i > 0) {
  67. switch (type) {
  68. case 1:
  69. redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, appKey), count);
  70. break;
  71. case 2:
  72. redisUtil.decr(String.format(RedisKey.API_METHOD_COUNT, appKey), count);
  73. break;
  74. }
  75. return true;
  76. }
  77. return false;
  78. }
  79. }