yujian
2019-05-28 111c99cc49f7d466b93c287610abbd39fe690413
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.yeshi.fanli.service.impl.monitor;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.exception.SMSException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.monitor.BusinessEmergent110Service;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.TencentSMSUtil;
 
@Service
public class BusinessEmergent110ServiceImpl implements BusinessEmergent110Service {
 
    String[] phones = new String[] { "18581318252", "15025351808", "18696787365" };
 
    private static Map<String, List<String>> map = new HashMap<>();
    private static long shareTime = 0L;
 
    @Resource
    private ConfigService configService;
 
    @Override
    public void shareTaoBaoGoodsError(String identifyCode) {
        String key = "monitor-error-share-tb";
        int timeM = 5;
        int limitCount = 5;
        // 5分钟内错误 5次触发报警
 
        List<String> stList = map.get(key);
        if (stList == null) {
            stList = new ArrayList<>();
            map.put(key, stList);
        }
        if (System.currentTimeMillis() - shareTime > 1000 * 60 * timeM) {
            stList.clear();
            shareTime = System.currentTimeMillis();
        }
        boolean exist = false;
        for (String code : stList)
            if (identifyCode.equalsIgnoreCase(code)) {
                exist = true;
                break;
            }
        if (!exist)
            stList.add(identifyCode);
        if (stList.size() >= limitCount) {// 触发报警
            baoJin(key, "返利券【分享赚】");
        }
    }
 
    @Override
    public void buyTaoBaoGoodsError(String identifyCode) {
        // 5分钟内错误 3次触发报警
        String key = "monitor-error-buy-tb";
        int timeM = 5;
        int limitCount = 5;
        // 5分钟内错误 5次触发报警
        List<String> stList = map.get(key);
        if (stList == null) {
            stList = new ArrayList<>();
            map.put(key, stList);
        }
        if (System.currentTimeMillis() - shareTime > 1000 * 60 * timeM) {
            stList.clear();
            shareTime = System.currentTimeMillis();
        }
        boolean exist = false;
        for (String code : stList)
            if (identifyCode.equalsIgnoreCase(code)) {
                exist = true;
                break;
            }
        if (!exist)
            stList.add(identifyCode);
        if (stList.size() >= limitCount) {// 触发报警
            baoJin(key, "返利券【领券返利】");
        }
    }
 
    static Map<String, Long> emergentTimeMap = new HashMap<>();
 
    private void baoJin(String key, String name) {
        if (emergentTimeMap.get(key) == null || System.currentTimeMillis() - emergentTimeMap.get(key) > 1000 * 60 * 2)// 2分钟报警一次
        {
            emergentTimeMap.put(key, System.currentTimeMillis());
            // 发送短信
            if ("1".equalsIgnoreCase(configService.get("can_send_emergency_msg")))
                for (String phone : phones) {
                    try {
                        TencentSMSUtil.sendSingleMsg(phone, Constant.smsConfig.getServiceEmergency()
                                .replace("[签名]", Constant.smsConfig.getSmsSign()).replace("[服务名字]", name));
                    } catch (SMSException e) {
                        e.printStackTrace();
                    }
                }
        }
    }
 
}