Kaynağa Gözat

:feat:游戏告警任务,类型为4的消息,拼成一条发送

zhangxianyu 10 ay önce
ebeveyn
işleme
e4a6e56fdd

+ 3 - 4
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/controller/GameMonitorAlarmController.java

@@ -39,13 +39,12 @@ public class GameMonitorAlarmController {
     }
 
 
+
+
     @ApiOperation(value = "测试")
     @PutMapping("/monitor")
     public ResultVO<Boolean> test() {
-        List<String> serverIdByGSId = gameServerService.getServerIdByGSId(120L);
-        serverIdByGSId.forEach(System.out::println);
-//        orderCostMonitorAlarmService.monitorTencentCostStatus();
-//        orderCostMonitorAlarmService.monitorDataStatus();
+        gameMonitorAlarmService.sendMsgToUserTest();
         return ResultVO.ok();
     }
 }

+ 4 - 0
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/pojo/vo/GameMonitorAlarmVO.java

@@ -93,6 +93,10 @@ public class GameMonitorAlarmVO {
      * 策略内容
      */
     private String configExplain;
+    /**
+     * 策略类型
+     */
+    private Integer configType;
 
     /**
      * 报警状态,0表示未报警,1标识已报警

+ 2 - 0
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/service/IGameMonitorAlarmService.java

@@ -10,4 +10,6 @@ public interface IGameMonitorAlarmService {
 
     boolean sendMsgToUser();
 
+    boolean sendMsgToUserTest();
+
 }

+ 124 - 23
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/service/impl/GameMonitorAlarmServiceImpl.java

@@ -8,6 +8,7 @@ import com.zanxiang.game.data.serve.service.IGameMonitorAlarmService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.dubbo.config.annotation.DubboReference;
+import org.jetbrains.annotations.NotNull;
 import org.nutz.dao.Chain;
 import org.nutz.dao.Cnd;
 import org.nutz.dao.Dao;
@@ -18,7 +19,11 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * @author tianhua
@@ -46,37 +51,64 @@ public class GameMonitorAlarmServiceImpl implements IGameMonitorAlarmService {
         dao.execute(sql);
         //结果
         log.info("查询告警信息结束");
-        try{
+        try {
             List<GameMonitorAlarmVO> list1 = sql.getList(GameMonitorAlarmVO.class);
             log.info("告警信息:{}", list1);
-            if (CollectionUtils.isEmpty(list1)){
+            if (CollectionUtils.isEmpty(list1)) {
                 log.info("没有告警信息");
                 return true;
             }
+            //用type==4进行分组
+            Map<Boolean, List<GameMonitorAlarmVO>> partitionedMap = list1.stream()
+                    .collect(Collectors.partitioningBy(vo -> vo.getConfigType() == 4));
+            List<GameMonitorAlarmVO> listWithType4 = partitionedMap.get(true);
+
+            //如果有type==4的,则每个投手的消息整合为1条告警
+            if (CollectionUtils.isNotEmpty(listWithType4)) {
+                Map<String, List<GameMonitorAlarmVO>> map = new HashMap<>();
+                for (GameMonitorAlarmVO vo : listWithType4) {
+                    String[] pitcherIds = vo.getPitcherId().split("-");
+                    for (String pitcherId : pitcherIds) {
+                        if (map.get(pitcherId) != null) {
+                            map.get(pitcherId).add(vo);
+                        }else {
+                            ArrayList<GameMonitorAlarmVO> newList = new ArrayList<>();
+                            newList.add(vo);
+                            map.put(pitcherId,newList);
+                        }
+                    }
+                }
+                for (String pitcherId : map.keySet()) {
+                    List<GameMonitorAlarmVO> gameMonitorAlarmVOS = map.get(pitcherId);
+                    StringBuilder msgStr = new StringBuilder(0);
+                    //把消息拼成一条
+                    for (int i = 0; i < gameMonitorAlarmVOS.size(); i++) {
+                        String s = alarmStr(gameMonitorAlarmVOS.get(i),i);
+                        msgStr.append(s);
+                    }
+
+                    log.info("type类型为4的告警信息,投手id:{}",pitcherId);
+                    dingTalkMsgRpc.sendByUserId(Long.valueOf(pitcherId), new String(msgStr));
+                    log.info("type类型为4的告警信息发送成功,投手id:{}",pitcherId);
+                }
+                for (GameMonitorAlarmVO vo : listWithType4) {
+                    Criteria cri = getCriteria(vo);
+                    dao.update(AdsMonitorAlarm.class, Chain.make("warn_status", 1), cri);
+                }
+            }
+
+            //如果没有type==4的,则每个投手的消息单独发送
+            List<GameMonitorAlarmVO> listWithoutType4 = partitionedMap.get(false);
             log.info("准备发送告警信息");
-            for (GameMonitorAlarmVO vo : list1) {
+            for (GameMonitorAlarmVO vo : listWithoutType4) {
                 String[] pitcherIds = vo.getPitcherId().split("-");
                 for (String pitcherId : pitcherIds) {
                     //发送给多个投手
                     log.info("发送告警信息给投手:{}", pitcherId);
-                    dingTalkMsgRpc.sendByUserId(Long.valueOf(pitcherId), alarmStr(vo));
+                    dingTalkMsgRpc.sendByUserId(Long.valueOf(pitcherId), alarmStr(vo,null));
                     log.info("发送告警信息给投手:{}成功", pitcherId);
                 }
-                //更新的条件
-                Criteria cri = Cnd.cri();
-                if (vo.getId() != null) {
-                    cri.where().andEquals("id", vo.getId());
-                }
-                if (vo.getDt() != null) {
-                    cri.where().andEquals("dt", vo.getDt());
-                }
-                if (vo.getGameId() != null) {
-                    cri.where().andEquals("gameId", vo.getGameId());
-                }
-                if (vo.getUserId() != null) {
-                    cri.where().andEquals("userId", vo.getUserId());
-                }
-                log.info("更新告警信息状态");
+                Criteria cri = getCriteria(vo);
                 dao.update(AdsMonitorAlarm.class, Chain.make("warn_status", 1), cri);
             }
 
@@ -86,11 +118,76 @@ public class GameMonitorAlarmServiceImpl implements IGameMonitorAlarmService {
         return true;
     }
 
+    @Override
+    public boolean sendMsgToUserTest() {
+        Sql sql = Sqls.create(getMonitorAlarmInfoSql());
+        sql.setCallback(Sqls.callback.entities());
+        sql.setEntity(dao.getEntity(GameMonitorAlarmVO.class));
+        dao.execute(sql);
+        List<GameMonitorAlarmVO> list1 = sql.getList(GameMonitorAlarmVO.class);
+        //用type==4进行分组
+        Map<Boolean, List<GameMonitorAlarmVO>> partitionedMap = list1.stream()
+                .collect(Collectors.partitioningBy(vo -> vo.getConfigType() == 4));
+        List<GameMonitorAlarmVO> listWithType4 = partitionedMap.get(true);
+        //如果有type==4的,则每个投手的消息整合为1条告警
+        if (CollectionUtils.isNotEmpty(listWithType4)) {
+            Map<String, List<GameMonitorAlarmVO>> map = new HashMap<>();
+            for (GameMonitorAlarmVO vo : listWithType4) {
+                String[] pitcherIds = vo.getPitcherId().split("-");
+                for (String pitcherId : pitcherIds) {
+                    if (map.get(pitcherId) != null) {
+                        map.get(pitcherId).add(vo);
+                    }else {
+                        ArrayList<GameMonitorAlarmVO> newList = new ArrayList<>();
+                        newList.add(vo);
+                        map.put(pitcherId,newList);
+                    }
+                }
+            }
+            for (String pitcherId : map.keySet()) {
+                List<GameMonitorAlarmVO> gameMonitorAlarmVOS = map.get(pitcherId);
+                StringBuilder msgStr = new StringBuilder(0);
+                //把消息拼成一条
+                for (int i = 0; i < gameMonitorAlarmVOS.size(); i++) {
+                    String s = alarmStr(gameMonitorAlarmVOS.get(i),i);
+                    msgStr.append(s);
+                }
+
+                log.info("type类型为4的告警信息,投手id:{}",pitcherId);
+                log.info("type类型为4的告警信息:{}",msgStr);
+                log.info("type类型为4的告警信息发送成功,投手id:{}",pitcherId);
+            }
+        }
+        return true;
+    }
+
+    private  Criteria getCriteria(GameMonitorAlarmVO vo) {
+        //更新的条件
+        Criteria cri = Cnd.cri();
+        if (vo.getId() != null) {
+            cri.where().andEquals("id", vo.getId());
+        }
+        if (vo.getDt() != null) {
+            cri.where().andEquals("dt", vo.getDt());
+        }
+        if (vo.getGameId() != null) {
+            cri.where().andEquals("gameId", vo.getGameId());
+        }
+        if (vo.getUserId() != null) {
+            cri.where().andEquals("userId", vo.getUserId());
+        }
+        log.info("更新告警信息状态");
+        return cri;
+    }
+
     /**
      * 生成告警信息字符串
      */
-    private String alarmStr(GameMonitorAlarmVO vo) {
+    private String alarmStr(GameMonitorAlarmVO vo,Integer index) {
         StringBuilder msgStr = new StringBuilder(0);
+        if(index!=null){
+            msgStr.append("【"+(index+1)+"】 \n");
+        }
         msgStr.append("游戏策略响应时间:")
                 .append(vo.getDt())
                 .append(" ;")
@@ -112,13 +209,16 @@ public class GameMonitorAlarmServiceImpl implements IGameMonitorAlarmService {
                 .append(" ;")
                 .append("\n")
                 .append("该用户响应的策略属于:")
-                .append(vo.getConfigExplain());
+                .append(vo.getConfigExplain())
+                .append(";")
+                .append("\n");
 
         return msgStr.toString();
     }
 
     /**
      * 游戏监控告警sql
+     *
      * @return String
      */
     private String getMonitorAlarmInfoSql() {
@@ -139,6 +239,7 @@ public class GameMonitorAlarmServiceImpl implements IGameMonitorAlarmService {
                 	a.pitcher_id ,
                 	a.message ,
                 	a.config_explain ,
+                	a.config_type,
                 	a.warn_status
                 FROM game_ads.ads_monitor_alarm a
                 LEFT JOIN dm_game_order.t_game_user b on a.source_system = b.source_system AND a.user_id = b.id
@@ -155,6 +256,6 @@ public class GameMonitorAlarmServiceImpl implements IGameMonitorAlarmService {
                 WHERE a.warn_status = 0
                 """;
     }
-    
-    
+
+
 }