| | |
| | | import com.ks.goldcorn.pojo.DO.*; |
| | | import com.ks.goldcorn.service.GoldCornBalanceManager; |
| | | import com.ks.goldcorn.service.GoldCornRecordManager; |
| | | import com.ks.goldcorn.util.JedisUtil; |
| | | import org.apache.dubbo.config.annotation.Service; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import redis.clients.jedis.Jedis; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.concurrent.TimeUnit; |
| | |
| | | |
| | | @Resource |
| | | private RedisManager redisManager; |
| | | |
| | | @Resource |
| | | private JedisUtil jedisUtil; |
| | | |
| | | /** |
| | | * 验证系统与用户 |
| | |
| | | throw new GoldUserException(GoldTradeException.CODE_NOT_EXIST, "source is not exist"); |
| | | } |
| | | |
| | | |
| | | String key = appCode + "-" + uid + "-" + sourceCode; |
| | | if (!redisTemplate.opsForValue().setIfAbsent(key, 1, 60 * 2, TimeUnit.SECONDS)) { |
| | | throw new GoldTradeException(GoldTradeException.CODE_FREQUENCY_LIMIT, "请求频率过高,请稍后再试"); |
| | | } |
| | | try { |
| | | int count = redisManager.getGoldCornAddRecordCount(appCode, uid, sourceCode); |
| | | if (count >= source.getMaxDayCount()) { |
| | | throw new GoldTradeException(GoldTradeException.CODE_COUNT_LIMIT, "添加次数超限,请明日再试"); |
| | | } |
| | | jedisUtil.excute(new JedisUtil.JedisExcuter() { |
| | | @Override |
| | | public void execute(Jedis jedis) throws Exception { |
| | | if (jedis.setnx(key, "1") <= 0) { |
| | | throw new GoldTradeException(GoldTradeException.CODE_FREQUENCY_LIMIT, "请求频率过高,请稍后再试"); |
| | | } |
| | | jedis.expire(key, 60 * 2); |
| | | try { |
| | | int count = redisManager.getGoldCornAddRecordCount(appCode, uid, sourceCode); |
| | | if (count >= source.getMaxDayCount()) { |
| | | throw new GoldTradeException(GoldTradeException.CODE_COUNT_LIMIT, "添加次数超限,请明日再试"); |
| | | } |
| | | |
| | | if (!redisManager.canAddCornWithTime(appCode, uid, sourceCode)) { |
| | | throw new GoldTradeException(GoldTradeException.CODE_FREQUENCY_LIMIT, "添加时间间隔过小"); |
| | | } |
| | | //加记录 |
| | | GoldCornRecord record = new GoldCornRecord(); |
| | | record.setAppId(appId); |
| | | record.setGoldCorn(goldCornCount == null ? source.getGoldCorn() : goldCornCount); |
| | | record.setSourceCode(source.getSourceCode()); |
| | | record.setType(GoldCornRecord.TYPE_GET); |
| | | record.setUid(uid); |
| | | if (title != null) { |
| | | record.setTitle(title); |
| | | } else { |
| | | record.setTitle(source.getSourceName()); |
| | | } |
| | | if (desc != null) { |
| | | record.setDesc(desc); |
| | | } else { |
| | | record.setDesc(source.getSourceDesc()); |
| | | } |
| | | try { |
| | | goldCornRecordManager.addRecord(record); |
| | | } catch (GoldRecordException e) { |
| | | throw new GoldTradeException(GoldTradeException.CODE_ADD_RECORD_FAIL, "添加记录失败"); |
| | | } |
| | | //加余额 |
| | | goldCornBalanceManager.addMoney(appId, uid, record.getGoldCorn()); |
| | | if (!redisManager.canAddCornWithTime(appCode, uid, sourceCode)) { |
| | | throw new GoldTradeException(GoldTradeException.CODE_FREQUENCY_LIMIT, "添加时间间隔过小"); |
| | | } |
| | | //加记录 |
| | | GoldCornRecord record = new GoldCornRecord(); |
| | | record.setAppId(appId); |
| | | record.setGoldCorn(goldCornCount == null ? source.getGoldCorn() : goldCornCount); |
| | | record.setSourceCode(source.getSourceCode()); |
| | | record.setType(GoldCornRecord.TYPE_GET); |
| | | record.setUid(uid); |
| | | if (title != null) { |
| | | record.setTitle(title); |
| | | } else { |
| | | record.setTitle(source.getSourceName()); |
| | | } |
| | | if (desc != null) { |
| | | record.setDesc(desc); |
| | | } else { |
| | | record.setDesc(source.getSourceDesc()); |
| | | } |
| | | try { |
| | | goldCornRecordManager.addRecord(record); |
| | | } catch (GoldRecordException e) { |
| | | throw new GoldTradeException(GoldTradeException.CODE_ADD_RECORD_FAIL, "添加记录失败"); |
| | | } |
| | | //加余额 |
| | | goldCornBalanceManager.addMoney(appId, uid, record.getGoldCorn()); |
| | | |
| | | redisManager.addCornSuccess(appCode, uid, sourceCode, source.getMinTimeSpan()); |
| | | } catch (GoldUserException e) { |
| | | throw e; |
| | | redisManager.addCornSuccess(appCode, uid, sourceCode, source.getMinTimeSpan()); |
| | | } catch (GoldUserException e) { |
| | | throw e; |
| | | } catch (GoldTradeException e) { |
| | | throw e; |
| | | } catch (Exception e) { |
| | | |
| | | } finally { |
| | | jedis.del(key); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | } catch (GoldTradeException e) { |
| | | throw e; |
| | | } catch (GoldUserException e) { |
| | | throw e; |
| | | } catch (Exception e) { |
| | | logger.error("添加金币未知异常:{}", e); |
| | | } finally { |
| | | redisTemplate.delete(key); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |