New file |
| | |
| | | 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 |
| | | } |