sunty
2019-08-02 c08fbcd91ea8cb75654d09f04ae4a645f321d7a0
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package service
 
import (
    "basic.com/dbapi.git"
    "encoding/json"
    "fmt"
    "strconv"
    "strings"
    "time"
    "webserver/extend/config"
    "webserver/extend/esutil"
    "webserver/extend/logger"
)
 
func GetVerificationData() []map[string]interface{} {
    url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
        "/" + config.EsInfo.EsIndex.DbTables.IndexName + "/_search"
    var setApi dbapi.SysSetApi
    _, sysconf := setApi.GetServerInfo()
    jsonDSL := `{
    "query": {
        "bool": {
            "filter": [{
                    "term": {
                        "isDelete": "0"
                    }
                },
                {
                    "terms": {
                        "analyServerId": ["` + sysconf.ServerId + `",""]
                    }
                }
            ]
        }
    },
    "size": 10000
}
`
    buf, err := esutil.EsReq("POST", url, []byte(jsonDSL))
    if err != nil {
        logger.Debug(err)
    }
 
    sources, err := esutil.Sourcelist(buf)
    if err != nil {
        logger.Debug(err)
    }
    return sources
}
 
func VerificationTime(source []map[string]interface{}) map[string][]string {
    resultInfo := make(map[string][]string, 0)
    currentTime := time.Now()
    for _, v := range source {
        id := v["id"].(string)
        startTime := v["startTime"].(string)
        endTime := ""
        if v["endTime"] != nil {
            endTime = v["endTime"].(string)
        }
        st, _ := time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
        if endTime != "" {
            et, _ := time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
            if et.After(currentTime) && st.Before(currentTime) {
                resultInfo["effective"] = append(resultInfo["effective"], id)
            } else {
                resultInfo["invalid"] = append(resultInfo["invalid"], id)
            }
        } else if st.Before(currentTime) && endTime == "" {
            resultInfo["effective"] = append(resultInfo["effective"], id)
        } else {
            resultInfo["invalid"] = append(resultInfo["invalid"], id)
        }
    }
    //fmt.Println(resultInfo)
    return resultInfo
}
 
func UpdateEnableStatus(id []string, flag bool) (message string) {
    ids := strings.Replace(strings.Trim(fmt.Sprint(id), "[]"), " ", "\",\"", -1)
    status := 0
    if flag == true {
        status = 1
    }
    enable := strconv.Itoa(status)
    url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
        "/" + config.EsInfo.EsIndex.DbTables.IndexName + "/_update_by_query?refresh"
    jsonDsl := `
            {
    "script": {
        "lang": "painless",
        "inline": "ctx._source.enable = ` + enable + `"
    },
    "query": {
        "terms": {
            "id": ["` + ids + `"]
        }
    }
}
`
    buf, err := esutil.EsReq("POST", url, []byte(jsonDsl))
    if err != nil {
        logger.Debug("http request info is err!")
        message = "修改失败"
    }
    var info interface{}
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        logger.Debug("http response interface can not change map[string]interface{}")
        message = "修改失败"
    }
    middle, ok := out["updated"].(float64)
    if !ok {
        logger.Debug("first result change error!")
        message = "修改失败"
    }
    if middle > 0 {
        logger.Debug("修改成功")
        message = "修改成功"
    }
    return message
}
 
func InitEnableStatus() (messageInfo string) {
    verificationData := GetVerificationData()
    verificationTime := VerificationTime(verificationData)
    idOfEffective := verificationTime["effective"]
    idOfinvalid := verificationTime["invalid"]
    mesInfo := false
    effectiveMesInfo := ""
    invalidMesInfo := ""
    if len(idOfEffective) > 0 {
        effectiveMesInfo = UpdateEnableStatus(idOfEffective, true)
    } else {
        effectiveMesInfo = "修改成功"
    }
    if len(idOfinvalid) > 0 {
        invalidMesInfo = UpdateEnableStatus(idOfinvalid, false)
    } else {
        invalidMesInfo = "修改成功"
    }
    if effectiveMesInfo == "修改成功" && invalidMesInfo == "修改成功" {
        mesInfo = true
    }
    if mesInfo == true {
        messageInfo = "修改成功"
    }
    return messageInfo
}