wzp
2021-07-30 afda40ef498cca59e58b2b6869deae90f3c13de5
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace AsiaINFO.SMS.BusinessFactory
{
    public class CheckRepeat
    {
        private static CheckRepeat _instance = null;
        public static CheckRepeat Instance
        {
            get
            {
                if (CheckRepeat._instance == null)
                {
                    CheckRepeat._instance = new CheckRepeat();
                }
                return _instance;
            }
        }
 
        private CheckRepeat() { }
 
        private Dictionary<int, Dictionary<string, bool>> _hasSubmit = new Dictionary<int, Dictionary<string, bool>>();
 
        public bool checkMessageId(string msgId)
        {
            int curKey = this.getCurKey();
            int[] checkKeys = getCheckKeys(curKey);
            this.removeDict(curKey);
            foreach (int key in checkKeys)
            {
                if (this._hasSubmit.ContainsKey(key) && this._hasSubmit[key] != null)
                {
                    if (this._hasSubmit[key].ContainsKey(msgId)) return true;
                }
            }
            return false;
        }
        private int getCurKey()
        {
            DateTime now = DateTime.Now;
            int min = (int.Parse(now.ToString("mm")) / 10);//0,1,2,3,4,5
            return min;
        }
        public void addSubmitMessageToCache(string msgId)
        {
            int curKey = this.getCurKey();
            if (this._hasSubmit.ContainsKey(curKey) && this._hasSubmit[curKey] != null)
            {
                Dictionary<string, bool> keys = this._hasSubmit[curKey];
                keys.Add(msgId, true);
            }
            else if (this._hasSubmit.ContainsKey(curKey))
            {
                Dictionary<string, bool> keys = new Dictionary<string, bool>();
                keys.Add(msgId, true);
                this._hasSubmit[curKey] = keys;
            }
            else
            {
                Dictionary<string, bool> keys = new Dictionary<string, bool>();
                keys.Add(msgId, true);
                this._hasSubmit.Add(curKey, keys);
            }
 
            DateTime now = DateTime.Now;
            int min = (int.Parse(now.ToString("mm")) / 10);
            //log4netService.Debug("addSubmitMessageToCache成功,总数:" + _hasSubmitMobile[min].Count);
        }
        private int[] getCheckKeys(int curKey)
        {
            if (curKey == 0)
            {
                return new int[] { 0, curKey + 5 };
            }
 
            return new int[] { curKey, curKey - 1 };
 
        }
        private bool isDelKey(int curKey, int delKey)
        {
            if (curKey == delKey) return false;
 
            if (curKey == 0 && delKey == 5)
            {
                return false;
            }
            if (delKey == curKey - 1) return false;
 
            //log4netService.Debug("移除成功,curKey=" + curKey);
            return true;
        }
 
 
        private void removeDict(int curKey)
        {
            List<int> keys = new List<int>();
            foreach (int key in this._hasSubmit.Keys)
            {
                if (key == curKey)
                {
                    continue;
                }
                if (this.isDelKey(curKey, key))
                {
                    keys.Add(key);
                    //keys[keys.Count] = key;
                    //this._hasSubmitMobile[key] = null;
                }
 
            }
 
            for (int i = 0; i < keys.Count; i++)
            {
                int key = keys[i];
                this._hasSubmit[key] = null;
            }
            //log4netService.Debug("removeDict正常:" + keys.Count);
        }
 
    }
}