package com.fdkankan.openApi.service.system.impl; import cn.hutool.core.util.ObjectUtil; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.fdkankan.openApi.constant.RedisKey; import com.fdkankan.openApi.entity.system.UserAuthInfo; import com.fdkankan.openApi.mapper.system.IUserAuthMapper; import com.fdkankan.openApi.service.system.IUserAuthService; import com.fdkankan.redis.util.RedisUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @DS("system") @Service public class UserAuthServiceImpl extends ServiceImpl implements IUserAuthService { @Autowired RedisUtil redisUtil; @Override public UserAuthInfo findByUserId(Integer userId) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(UserAuthInfo::getUserId, userId); return getOne((wrapper)); } @Override public UserAuthInfo findByAppKey(String appKey) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(UserAuthInfo::getAppKey, appKey); return getOne((wrapper)); } @Override public boolean findExistByAppKey(String appKey) { String key = String.format(RedisKey.USER_APP_KEY_INFO, appKey); if (redisUtil.hasKey(key)) { return true; } else { UserAuthInfo userAuthInfo = findByAppKey(appKey); if (ObjectUtil.isNotNull(userAuthInfo)) { redisUtil.set(key, userAuthInfo.getAppKey(), 3600); return true; } else { return false; } } } @Override @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED) public int updateCallCounts(String appKey) { return getBaseMapper().updateCallCounts(appKey); } @Override public boolean updateCallCounts(String appKey, Integer count) { int i = getBaseMapper().updateCallCountsByParam(appKey, count); if (i > 0) { redisUtil.set(String.format(RedisKey.API_METHOD_COUNT, appKey), String.valueOf(count)); return true; } return false; } //1:累加,2:累减 @Override public boolean updateCallCountsByType(String appKey, Integer count, Integer type) { int i = getBaseMapper().updateCallCountsByParamAndType(appKey, count, type); if (i > 0) { switch (type) { case 1: redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, appKey), count); break; case 2: redisUtil.decr(String.format(RedisKey.API_METHOD_COUNT, appKey), count); break; } return true; } return false; } }