From 0e9b6603d4ae9d11c1fbc90257ce816c5807b8ff Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期五, 28 十月 2022 16:55:02 +0800
Subject: [PATCH] 批量添加结算消息

---
 app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornSettleServiceImpl.java |  107 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 88 insertions(+), 19 deletions(-)

diff --git a/app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornSettleServiceImpl.java b/app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornSettleServiceImpl.java
index bfa1940..b226e36 100644
--- a/app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornSettleServiceImpl.java
+++ b/app/src/main/java/com/yeshi/makemoney/app/service/impl/goldcorn/GoldCornSettleServiceImpl.java
@@ -1,35 +1,45 @@
 package com.yeshi.makemoney.app.service.impl.goldcorn;
 
+import com.google.gson.Gson;
 import com.ks.lib.common.exception.ParamsException;
+import com.ks.push.exception.BPushTaskException;
 import com.yeshi.makemoney.app.dto.mq.GoldCornSettleMQMsg;
+import com.yeshi.makemoney.app.entity.SystemEnum;
 import com.yeshi.makemoney.app.entity.goldcorn.GoldCornConsumeRecord;
 import com.yeshi.makemoney.app.entity.goldcorn.GoldCornConsumeType;
+import com.yeshi.makemoney.app.entity.goldcorn.GoldCornSettleRecord;
 import com.yeshi.makemoney.app.entity.money.UserMoneyRecord;
 import com.yeshi.makemoney.app.entity.user.UserInfo;
 import com.yeshi.makemoney.app.exception.goldcorn.GoldCornConsumeRecordException;
 import com.yeshi.makemoney.app.exception.goldcorn.GoldCornMoneyExchangeRateRecordException;
+import com.yeshi.makemoney.app.exception.goldcorn.GoldCornSettleRecordException;
 import com.yeshi.makemoney.app.exception.money.UserMoneyRecordException;
 import com.yeshi.makemoney.app.exception.user.UserInfoException;
-import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornConsumeRecordService;
-import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornGetRecordService;
-import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornMoneyExchangeRateRecordService;
-import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornSettleService;
+import com.yeshi.makemoney.app.service.inter.goldcorn.*;
 import com.yeshi.makemoney.app.service.inter.money.UserMoneyService;
 import com.yeshi.makemoney.app.service.inter.msg.UserMsgNotifyService;
 import com.yeshi.makemoney.app.service.inter.user.UserInfoService;
+import com.yeshi.makemoney.app.service.manager.PushManager;
+import com.yeshi.makemoney.app.service.query.goldcorn.GoldCornGetRecordQuery;
 import com.yeshi.makemoney.app.utils.factory.UserMoneyRecordFactory;
 import com.yeshi.makemoney.app.utils.factory.goldcorn.GoldCornConsumeRecordFactory;
 import com.yeshi.makemoney.app.utils.factory.msg.UserMsgFactory;
 import com.yeshi.makemoney.app.utils.goldcorn.GoldCornUtil;
 import com.yeshi.makemoney.app.utils.mq.CMQManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.yeshi.utils.JsonUtil;
+import org.yeshi.utils.ThreadUtil;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author hxh
@@ -39,6 +49,7 @@
  */
 @Service
 public class GoldCornSettleServiceImpl implements GoldCornSettleService {
+    Logger settleLogger = LoggerFactory.getLogger("settleLog");
 
     @Resource
     private GoldCornGetRecordService goldCornGetRecordService;
@@ -58,27 +69,74 @@
     @Resource
     private UserMsgNotifyService userMsgNotifyService;
 
+    @Resource
+    private PushManager pushManager;
+
+    @Resource
+    private GoldCornSettleRecordService goldCornSettleRecordService;
+
     @Override
-    public void startSettle(Date date) {
+    public void startSettle(Date date, String remarks, SystemEnum system) throws Exception {
         String day = GoldCornUtil.getFormatDay(date);
+
+        if (GoldCornUtil.convertFormatDay(day).getTime() >= GoldCornUtil.convertFormatDay(GoldCornUtil.getFormatDay(new Date())).getTime()) {
+            throw new ParamsException(ParamsException.CODE_PARAMS_NOT_ENOUGH, "鍙兘缁撶畻浠婂ぉ浠ュ墠鐨勬棩鏈�");
+        }
+
+        BigDecimal rate = goldCornMoneyExchangeRateRecordService.getRate(day, system, new Date());
+        if (rate == null) {
+            throw new ParamsException(ParamsException.CODE_PARAMS_NOT_ENOUGH, "姹囩巼涓虹┖");
+        }
+
+        //缁熻閲戝竵鏁伴噺
+        GoldCornGetRecordQuery query = new GoldCornGetRecordQuery();
+        query.setDay(day);
+        Map<String, Long> numMap = goldCornGetRecordService.sumGoldCornNum(query);
+        if (numMap.get(day) == null) {
+            throw new ParamsException(ParamsException.CODE_PARAMS_NOT_ENOUGH, "閲戝竵鏁伴噺缁熻鍑洪敊");
+        }
+
+
+        GoldCornSettleRecord record = new GoldCornSettleRecord();
+        record.setSystem(system);
+        record.setDay(day);
+        record.setRate(rate);
+        record.setCornNum(numMap.get(day));
+        record.setTotalMoney(new BigDecimal(0));
+
+
         long count = goldCornGetRecordService.countUidsByDay(day);
-        int pageSize = 1000;
+        record.setUserCount(count);
+        record.setSettledUserCount(0L);
+        goldCornSettleRecordService.add(record);
+
+        int pageSize = 16;
         int page = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
         for (int i = 0; i < page; i++) {
-            List<Long> list = goldCornGetRecordService.getUidsByDay(day, i + 1, pageSize);
+            List<Long> list = goldCornGetRecordService.getUidsByDay(record.getDay(), i + 1, pageSize);
+            List<GoldCornSettleMQMsg> msgList = new ArrayList<>();
             for (Long uid : list) {
-                //鍔犲叆鍒癈MQ
-                CMQManager.getInstance().addGoldCornSettleMsg(new GoldCornSettleMQMsg(uid, day, System.currentTimeMillis()));
+                GoldCornSettleMQMsg msg = new GoldCornSettleMQMsg(record.getId(), uid, day, System.currentTimeMillis());
+                msgList.add(msg);
+                settleLogger.info("鍔犲叆缁撶畻闃熷垪锛歿}", new Gson().toJson(msg));
             }
+            //鍔犲叆鍒癈MQ
+            CMQManager.getInstance().addGoldCornSettleMsg(msgList);
         }
+
     }
 
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public void settle(Long uid, String day, BigDecimal rate) throws GoldCornConsumeRecordException, UserMoneyRecordException {
+    public void settle(Long uid, String day, BigDecimal rate, String settleId) throws GoldCornConsumeRecordException, UserMoneyRecordException {
         long cornNum = goldCornGetRecordService.getGoldCornByDay(uid, GoldCornUtil.convertFormatDay(day));
         //閲戝竵鏁伴噺
-        GoldCornConsumeRecord record = GoldCornConsumeRecordFactory.createExchange(uid, day, (int) cornNum, rate);
+        GoldCornConsumeRecord record = GoldCornConsumeRecordFactory.createExchange(uid, day, (int) cornNum, rate, settleId);
+        if (goldCornConsumeRecordService.get(record.getId()) != null) {
+            //宸茬粡缁撶畻杩囦簡
+            return;
+        }
+
         try {
             UserMoneyRecord moneyRecord = UserMoneyRecordFactory.createGoldCornExchange(record);
             userMoneyService.addUserMoney(moneyRecord);
@@ -86,16 +144,27 @@
             throw new UserMoneyRecordException(e.getCode(), e.getMsg());
         }
         goldCornConsumeRecordService.add(record);
-        //娣诲姞娑堟伅閫氱煡
-        try {
-            userMsgNotifyService.notify(UserMsgFactory.createGoldCornExchange(record, userMoneyService.getBalance(uid)));
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+        //娑堟伅閫氱煡涓嶉渶瑕佸湪涓荤嚎绋�
+        ThreadUtil.run(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    userMsgNotifyService.notify(UserMsgFactory.createGoldCornExchange(record, userMoneyService.getBalance(uid)));
+                    //娣诲姞绔欏鎺ㄩ��
+                    UserInfo user = userInfoService.get(uid);
+                    if (user == null) {
+                        return;
+                    }
+                    pushManager.pushGoldCornSettle(user.getSystem(), record.getUid(), Math.abs(record.getCornNum()), new BigDecimal(record.getMoney()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR));
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        });
     }
 
     @Override
-    public void settle(Long uid, String day) throws UserInfoException, GoldCornMoneyExchangeRateRecordException, UserMoneyRecordException, GoldCornConsumeRecordException {
+    public void settle(Long uid, String day, String settleId) throws UserInfoException, GoldCornMoneyExchangeRateRecordException, UserMoneyRecordException, GoldCornConsumeRecordException {
         UserInfo user = userInfoService.get(uid);
         if (user == null) {
             throw new UserInfoException(UserInfoException.CODE_NOT_EXIST, "鐢ㄦ埛涓嶅瓨鍦�");
@@ -112,6 +181,6 @@
             throw new GoldCornMoneyExchangeRateRecordException(GoldCornMoneyExchangeRateRecordException.CODE_NOT_EXIST, "鏈幏鍙栧埌姹囩巼");
         }
 
-        settle(uid, day, rate);
+        settle(uid, day, rate, settleId);
     }
 }

--
Gitblit v1.8.0