554325746@qq.com
2019-07-15 6118eda0305661482318209b55f2a66c14704a6d
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package esutil
 
import (
    "bytes"
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "net/http"
    "time"
 
    "basic.com/pubsub/protomsg.git"
)
 
 
func Parsesources(sources []map[string]interface{}) (esinfos []*protomsg.Esinfo) {
    var ok bool
    for _, source := range sources {
       var tmpinfo protomsg.Esinfo
        tmpinfo.FaceFeature, ok =   source["faceFeature"].(string)
        if !ok {
            continue
        }
 
        tmpinfo.Id, ok    =   source["id"].(string)  
        if !ok {
            continue 
        }
        tmpinfo.Tableid, ok     =   source["tableId"].(string)
        if !ok {
            continue
        }
 
        esinfos = append(esinfos, &tmpinfo)
 
//        tmpinfo.PersonName,ok  =   source["personName"].(string)
//            Isnil("personName", ok)
//        tmpinfo.PersonPicUrl,ok =  source["personPicUrl"].(string)
//            Isnil("personPicUrl", ok)
//        tmpinfo.PhoneNum,ok     =  source["phoneNum"].(string)
//            Isnil("phoneNum", ok)
//        tmpinfo.Sex,ok          =  source["sex"].(string)
//            Isnil("sex", ok)
//        tmpinfo.Idcard,ok       =  source["idCard"].(string)
//            Isnil("idCard", ok)
//        tmpinfo.MonitorLevel,ok =  source["monitorLevel"].(string)
 
        // 根据 tableid 获取 tablename
//        name, _:= Dbtablename(tmpinfo.TableId) 
//        tmpinfo.TableName= name 
//        binfos.Infos = append(binfos.Infos, &tmpinfo)
 
    }
    return 
}
//解析底库人员结构
func Dbpersonbyid(sources []map[string]interface{}) (tmpinfos []protomsg.Dbperson) {
        var ok bool
 
        var tmpinfo protomsg.Dbperson
        for _, source := range  sources{
            tmpinfo.FaceFeature, ok =   source["faceFeature"].(string)
            if !ok {
                continue
            }
 
            tmpinfo.Id, ok    =   source["id"].(string)
            if !ok {
                continue
            }
 
            tmpinfo.TableId, ok     =   source["tableId"].(string)
            if !ok {
                continue
            }
 
            tmpinfo.PersonName,ok  =   source["personName"].(string)
                Isnil("personName", ok)
            tmpinfo.PersonPicUrl,ok =  source["personPicUrl"].(string)
                Isnil("personPicUrl", ok)
            tmpinfo.PhoneNum,ok     =  source["phoneNum"].(string)
                Isnil("phoneNum", ok)
            tmpinfo.Sex,ok          =  source["sex"].(string)
                Isnil("sex", ok)
            tmpinfo.IdCard,ok       =  source["idCard"].(string)
                Isnil("idCard", ok)
            tmpinfo.MonitorLevel,ok =  source["monitorLevel"].(string)
                Isnil("monitorLevel",ok)
            tmpinfo.UpdateTime,ok = source["updateTime"].(string)
                Isnil("updateTime",ok)
            tmpinfo.Age,ok          =  source["age"].(string)
                Isnil("age", ok)
 
            isDelete,ok  :=   source["isDelete"].(float64)
            if ok {
                tmpinfo.IsDelete= int32(isDelete)
            }
            Isnil("isDelete", ok)
 
            enable,ok  :=   source["enable"].(float64)
            if ok {
                tmpinfo.Enable= int32(enable)
            }
            Isnil("endTime", ok)
 
            tmpinfo.Reserved,ok = source["reserved"].(string)
                Isnil("reserved",ok)
            tmpinfo.PicDesc,ok = source["picDesc"].(string)
                Isnil("picDesc",ok)
            tmpinfo.CreateTime,ok = source["createTime"].(string)
                Isnil("createTime",ok)
            tmpinfo.CreateBy,ok = source["createBy"].(string)
                Isnil("createBy",ok)
 
            //根据 tableid 获取 tablename
            //name, _:= Dbtablename(tmpinfo.TableId)
            //tmpinfo.TableName= name
            tmpinfos = append(tmpinfos, tmpinfo)
        }
 
        return tmpinfos
}
 
//解析底库结构
func Dbtablebyid(sources []map[string]interface{}) (tmpinfos []protomsg.Dbtable) {
    var ok bool
 
    var tmpinfo protomsg.Dbtable
    for _, source := range  sources{
 
        tmpinfo.Id, ok    =   source["id"].(string)
        if !ok {
            continue
        }
        tmpinfo.TableName,ok  =   source["tableName"].(string)
        Isnil("tableName", ok)
        tmpinfo.TableType,ok =  source["tableType"].(string)
        Isnil("tableType", ok)
        tmpinfo.CreateBy,ok     =  source["createBy"].(string)
        Isnil("createBy", ok)
        tmpinfo.TableDesc,ok  =   source["tableDesc"].(string)
        Isnil("tableDesc", ok)
        tmpinfo.BwType,ok =  source["bwType"].(string)
        Isnil("bwType", ok)
        tmpinfo.StartTime,ok     =  source["startTime"].(string)
        Isnil("startTime", ok)
        tmpinfo.EndTime,ok  =   source["endTime"].(string)
        Isnil("endTime", ok)
 
        isDelete,ok  :=   source["isDelete"].(float64)
        if ok {
            tmpinfo.IsDelete= int32(isDelete)
        }
        Isnil("isDelete", ok)
 
        enable,ok  :=   source["enable"].(float64)
        if ok {
            tmpinfo.Enable= int32(enable)
        }
        Isnil("endTime", ok)
 
        tmpinfo.CreateTime,ok     =  source["createTime"].(string)
        Isnil("createTime", ok)
        tmpinfo.UpdateTime,ok  =   source["updateTime"].(string)
        Isnil("updateTime", ok)
        tmpinfo.IsForever,ok =  source["isForever"].(string)
        Isnil("isForever", ok)
        tmpinfo.IsSync,ok =  source["isSync"].(string)
        Isnil("isSync", ok)
        tmpinfo.AnalyServerId,ok     =  source["analyServerId"].(string)
        Isnil("analyServerId", ok)
 
        tmpinfos = append(tmpinfos, tmpinfo)
    }
 
    return tmpinfos
}
 
func Sourcelist(buf []byte)(sources []map[string]interface{}, err error){
    var info interface{}
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        return nil, errors.New("http response interface can not change map[string]interface{}")
    }
 
    middle, ok := out["hits"].(map[string]interface{})
    if !ok {
        return nil, errors.New("first hits change error!")
    }
    for _, in := range middle["hits"].([]interface{}){
        tmpbuf, ok := in.(map[string]interface{})
        if !ok {
                fmt.Println("change to source error!") 
                continue
        }
        source, ok := tmpbuf["_source"].(map[string]interface{})
        if !ok {
            fmt.Println("change _source error!") 
            continue
        }
        sources  = append(sources, source )
    }
    return sources,nil
}
 
func EsReq(method string, url string, parama []byte) (buf []byte, err error) {
    defer elapsed("page")()
    timeout := time.Duration(10 * time.Second) 
    client := http.Client{
        Timeout: timeout,
    }
    request, err := http.NewRequest(method, url, bytes.NewBuffer(parama)) 
    request.Header.Set("Content-type", "application/json")
 
    if err != nil {
        fmt.Println("build request fail !")
        return nil, err 
    }
 
    resp, err := client.Do(request)
    if err != nil{
        fmt.Println("request error: ", err)
        return nil, err 
    }
 
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err) 
        return nil , err
    }
    return body, nil
}
 
// 计算时间
func elapsed(what string) func() {
    start := time.Now()
    return func() {
        fmt.Printf("%s took %v\n", what, time.Since(start))
    }
}
 
// 赋值时检测是否能够赋值
func Isnil(key string, ok bool){
    if !ok {
            fmt.Println(key, "is nil can not asign") 
    }
}