From 90425b8303972690bac952786abbbe50ae3abba3 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 06 五月 2021 18:46:51 +0800
Subject: [PATCH] 推送服务完善

---
 service-goldcorn/src/main/java/com/ks/goldcorn/service/remote/GoldCornTradeServiceImpl.java |  127 +++++++++++++++++++++++++++++++++---------
 1 files changed, 100 insertions(+), 27 deletions(-)

diff --git a/service-goldcorn/src/main/java/com/ks/goldcorn/service/remote/GoldCornTradeServiceImpl.java b/service-goldcorn/src/main/java/com/ks/goldcorn/service/remote/GoldCornTradeServiceImpl.java
index b8a2a08..5d3a3c4 100644
--- a/service-goldcorn/src/main/java/com/ks/goldcorn/service/remote/GoldCornTradeServiceImpl.java
+++ b/service-goldcorn/src/main/java/com/ks/goldcorn/service/remote/GoldCornTradeServiceImpl.java
@@ -4,19 +4,27 @@
 import com.ks.goldcorn.exception.GoldRecordException;
 import com.ks.goldcorn.exception.GoldTradeException;
 import com.ks.goldcorn.exception.GoldUserException;
+import com.ks.goldcorn.manager.RedisManager;
 import com.ks.goldcorn.mapper.GoldCornAppInfoMapper;
 import com.ks.goldcorn.mapper.GoldCornBalanceMapper;
 import com.ks.goldcorn.mapper.GoldCornConsumeSourceMapper;
 import com.ks.goldcorn.mapper.GoldCornGetSourceMapper;
 import com.ks.goldcorn.pojo.DO.*;
+import com.ks.goldcorn.service.GoldCornBalanceManager;
 import com.ks.goldcorn.service.GoldCornRecordManager;
-import org.springframework.stereotype.Service;
+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 javax.annotation.Resource;
+import java.util.concurrent.TimeUnit;
 
-@Service
+@Service(version = "1.0")
 public class GoldCornTradeServiceImpl implements GoldCornTradeService {
+
+    Logger logger = LoggerFactory.getLogger(GoldCornTradeService.class);
 
     @Resource
     private GoldCornAppInfoMapper goldCornAppInfoMapper;
@@ -36,6 +44,12 @@
     @Resource
     private GoldCornRecordManager goldCornRecordManager;
 
+    @Resource
+    private RedisTemplate<String, Object> redisTemplate;
+
+    @Resource
+    private RedisManager redisManager;
+
     /**
      * 楠岃瘉绯荤粺涓庣敤鎴�
      *
@@ -45,7 +59,7 @@
      * @throws GoldAppException
      * @throws GoldUserException
      */
-    private GoldCornAppInfo validateAppAndUser(String appCode, String uid) throws GoldAppException, GoldUserException {
+    private Long validateAppAndUser(String appCode, String uid) throws GoldAppException, GoldUserException {
         GoldCornAppInfo appInfo = goldCornAppInfoMapper.selectByAppCode(appCode);
         if (appInfo == null) {
             throw new GoldAppException(GoldAppException.CODE_NOT_EXIST, "app is not exist");
@@ -56,55 +70,114 @@
             throw new GoldUserException(GoldUserException.CODE_NOT_EXIST, "uid is not exist");
         }
 
-        return appInfo;
+        return appInfo.getId();
+    }
+
+    @Override
+    public long getNextDoTaskTime(String appCode, String uid, String sourceCode) {
+        long time = redisManager.getCanAddCornExpireTime(appCode, uid, sourceCode);
+        if (time <= 0) {
+            return -1;
+        }
+
+        return System.currentTimeMillis() + 1000 * time;
     }
 
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public void addGoldCorn(String appCode, String uid, GoldCornGetSource source) throws GoldAppException, GoldUserException, GoldTradeException {
-        GoldCornAppInfo app = validateAppAndUser(appCode, uid);
-        source = goldCornGetSourceMapper.selectByPrimaryKey(source.getId());
+    public void addGoldCorn(String appCode, String uid, String sourceCode, Integer goldCornCount, String title, String desc) throws GoldAppException, GoldUserException, GoldTradeException {
+        Long appId = validateAppAndUser(appCode, uid);
+        GoldCornGetSource source = goldCornGetSourceMapper.selectByAppIdAndSourceCode(appId, sourceCode);
         if (source == null) {
             throw new GoldUserException(GoldTradeException.CODE_NOT_EXIST, "source is not exist");
         }
 
-        //鍔犺褰�
-        GoldCornRecord record = new GoldCornRecord();
-        record.setAppId(app.getId());
-        record.setGoldCorn(source.getGoldCorn());
-        record.setSourceId(source.getId());
-        record.setType(GoldCornRecord.TYPE_GET);
-        record.setUid(uid);
+
+        String key = appCode + "-" + uid + "-" + sourceCode;
+        if (!redisTemplate.opsForValue().setIfAbsent(key, 1, 60 * 2, TimeUnit.SECONDS)) {
+            throw new GoldTradeException(GoldTradeException.CODE_FREQUENCY_LIMIT, "璇锋眰棰戠巼杩囬珮锛岃绋嶅悗鍐嶈瘯");
+        }
         try {
-            goldCornRecordManager.addRecord(record);
-        } catch (GoldRecordException e) {
-            throw new GoldTradeException(GoldTradeException.CODE_ADD_RECORD_FAIL, "娣诲姞璁板綍澶辫触");
+            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());
+
+            redisManager.addCornSuccess(appCode, uid, sourceCode, source.getMinTimeSpan());
+        } catch (GoldUserException e) {
+            throw e;
+        } catch (GoldTradeException e) {
+            throw e;
+        } catch (Exception e) {
+            logger.error("娣诲姞閲戝竵鏈煡寮傚父锛歿}", e);
+        } finally {
+            redisTemplate.delete(key);
         }
-        //鍔犱綑棰�
-        goldCornBalanceManager.addMoney(app.getId(), uid, source.getGoldCorn());
     }
 
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public void consumeGoldCorn(String appCode, String uid, GoldCornConsumeSource source) throws GoldAppException, GoldUserException, GoldTradeException {
-        GoldCornAppInfo app = validateAppAndUser(appCode, uid);
-        source = goldCornConsumeSourceMapper.selectByPrimaryKey(source.getId());
+    public void consumeGoldCorn(String appCode, String uid, String sourceCode, Integer goldCornCount, String title, String desc) throws GoldAppException, GoldUserException, GoldTradeException {
+        Long appId = validateAppAndUser(appCode, uid);
+        GoldCornConsumeSource source = goldCornConsumeSourceMapper.selectByAppIdAndSourceCode(appId, sourceCode);
         if (source == null) {
             throw new GoldUserException(GoldTradeException.CODE_NOT_EXIST, "source is not exist");
         }
+
+        int goldCorn = goldCornCount == null ? source.getGoldCorn() : goldCornCount;
+
+        //鍑忎綑棰�
+        goldCornBalanceManager.subMoney(appId, uid, goldCorn);
+
         //鍔犺褰�
         GoldCornRecord record = new GoldCornRecord();
-        record.setAppId(app.getId());
-        record.setGoldCorn(source.getGoldCorn());
-        record.setSourceId(source.getId());
+        record.setAppId(appId);
+        record.setGoldCorn(goldCorn);
+        record.setSourceCode(source.getSourceCode());
         record.setType(GoldCornRecord.TYPE_CONSUME);
         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.subMoney(app.getId(), uid, source.getGoldCorn());
     }
 }

--
Gitblit v1.8.0