liuxiaolong
2019-05-06 a7bed6b4cfecd61ec153818945f982c5bb796b98
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package com.cloud.attendance.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloud.attendance.dao.AttDayDao;
import com.cloud.attendance.dao.AttRuleDao;
import com.cloud.attendance.model.AttRule;
import com.cloud.attendance.model.PersonForEs;
import com.cloud.attendance.service.QueryEsService;
import com.cloud.attendance.service.TokenService;
import com.cloud.attendance.service.UserService;
import com.cloud.attendance.utils.EnumStr;
import com.cloud.attendance.utils.HttpClient;
import com.cloud.model.sys.LoginAppUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.text.SimpleDateFormat;
import java.util.*;
@Service
@Slf4j
public class QueryEsServiceImpl implements QueryEsService {
 
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EnumStr enumStr;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AttRuleDao attRuleDao;
    @Autowired
    private UserService userService;
 
 
    @Autowired
    private AttDayDao attDayDao;
    @Override
    public List<JSONObject> queryAttRecordBySecond(){
        List<JSONObject> lists = new ArrayList<JSONObject>();
        HttpHeaders headers = new HttpHeaders();
        String startTime = sdf.format(System.currentTimeMillis()-3000);
        String endTime = sdf.format(System.currentTimeMillis()-2000);
        String querySecondRecord = "{\"query\":{\"range\":{\""+EnumStr.picDateField+"\":{\"gte\":\""+startTime+"\",\"lt\":\""+endTime+"\"}}}}";
        MediaType type = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(querySecondRecord, headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity("http://" + HttpClient.getHOSTNAME() + ":" + HttpClient.getHTTP_PORT() + "/cananaserman/_search",
                formEntity, JSONObject.class);
        if (!responseEntity.getStatusCode().is2xxSuccessful()){
            // 报错 非200状态
            throw new RuntimeException("es 查询秒级数据 返回失败!~");
        }else {
            JSONObject resp = responseEntity.getBody();
            JSONArray attRecords = resp.getJSONObject("hits").getJSONArray("hits");
            for (Object attRecord : attRecords) {
                JSONObject source = ((JSONObject) attRecord).getJSONObject("_source");
                // 获取 personId 组合数据
                String pid = source.getString("pid");
                // 直接查询数据库
//                Map<String, Object> person= attDayDao.getPersonInfoById(pid);
                // 调用人员接口
                Map<String, Object> person = userService.getPersonInfoById(pid);
                if (person!=null) {
                    String showType = "其他";
                    if (EnumStr.TEACHER_SQL.equals(person.get("type"))){
                        showType = "教师";
                    }else if (EnumStr.STUDENT_SQL.equals(person.get("type"))){
                        showType = "学生";
                    }
                    source.put("name",person.get("name"));
                    source.put("identity",showType);
                    source.put("photo",person.get("photos"));
                }else {
                    source.put("identity",null);
                    source.put("photo",null);
                    source.put("name",null);
                }
                source.put(EnumStr.picDateShow,source.getString(EnumStr.picDateField));  // 考勤日期
                source.put(EnumStr.snapPicShow,source.getString(EnumStr.perPicField));    // 抓拍图片
                source.put(EnumStr.devIdShow,source.getString(EnumStr.devIdField));  // 设备IDID
                source.put(EnumStr.devNameShow,source.getString(EnumStr.devNameField));   // 设备名称
                removeSource( source);  // 伤处多余字段
                lists.add(source);
            }
        }
        log.info(lists.size()+"条数据 es 定时发送"+System.currentTimeMillis());
        return  lists;
    }
    /**
     *    "_source": {
     *                 "pname": "李姝宁",
     *                         "pid": "1552",
     *                         "onlinetime": "2018-10-31 02:00:04",
     *                         "indeviceid": "ZJKQJ-VIDEO",
     *                         "indevicename": "主教学楼考勤机"
     *             }
     */
 
        // 搜索条件查询 全部记录
    public Map<String, Object> queryAttRecord(String contentValue, String location, String identity, String gradeValue,
                                              String classValue, Boolean delFlag, List<String> orgIds, String orderName, String sortDate,
                                              Date startTime, Date endTime, Integer page, Integer size){
        Map<String, Object> retMap = new HashMap<String, Object>();
        String starttime = sdf.format(startTime);
        String endtime = sdf.format(endTime);
        int from =0;
        if (page!=null && size !=null ){
            from = (page - 1) * size ;
        }else{
            from = 0 ; size = 20;
        }
        retMap.put("total",0);retMap.put("size",size);  // 设置 初始值
        String personids = ""; // 查询条件满足的人员//     (2018080601) or (2018080603) or (2018080604)
         String contentQuery = "";
 
        String persinIdQuery = "";
        List<JSONObject> lists = new ArrayList<JSONObject>();   // 存储返回数据
       /* if (EnumStr.STUDENT.equalsIgnoreCase(identity)){
                identity = EnumStr.STUDENT_SQL;
            }else if (EnumStr.TEACHER.equalsIgnoreCase(identity)){
                identity = EnumStr.TEACHER_SQL;
            }else */
            if (StringUtils.isBlank(identity)){
                identity = null;
            }
            //  添加权限
        LoginAppUser appUser = tokenService.currentUser();
        Long orgId = appUser.getOrgId();
        if (StringUtils.isBlank(gradeValue)){
            Integer officeId = appUser.getOfficeId();
            gradeValue = officeId.toString();
        }
            // 摄像机范围
        AttRule attRule = attRuleDao.selectAttRule();
        String deviceIds = null;
           if (attRule != null) deviceIds =  attRule.getDeviceIds();
        String reqIds = null;
        if (!StringUtils.isBlank(deviceIds)){
            reqIds = JSONArray.toJSONString(deviceIds.split(","));
        }else {
            retMap.put("esAttList",new ArrayList<>());
            return retMap;
        }
        // 调用 sql 直接查询人员数据
//        List<String> idList = attDayDao.queryPersonByContent(contentValue, gradeValue, classValue, identity, null);
//        // 调用 人员接口查询人员数据
        List<String> idList = userService.queryPersonByContent(contentValue,gradeValue,classValue,identity,null,delFlag,orgIds);
 
        String join = StringUtils.join(idList, ")or(");
        if (idList.size()>0){
            personids = "("+ join +")";
            persinIdQuery = ",{\"query_string\":{\"default_field\":\""+EnumStr.personIdField+"\",\"query\":\""+personids+"\"}}";
        }else{
            retMap.put("esAttList",lists);
            return  retMap;
        }
        if (location !=null ){
            // 处理  indevicename 字段为keyword则不能模糊查询  设为 text 则可以执行下列
//            String location ="("+ String.join(")and(", location.split(""))+")";
            // 2018-12-04 改为 位置
            contentQuery = ",{\"query_string\":{\"default_field\":\""+EnumStr.devAddressField+"\",\"query\":\""+location+"\"}}";
        }
        // 摄像机 范围条件添加 wp 19-03-20
        if (!StringUtils.isBlank(reqIds) ){
            contentQuery += ",{\"terms\":{\""+EnumStr.VideoReqNum+"\":"+reqIds+"}}";
        }
        if ("desc".equalsIgnoreCase(sortDate)){
            sortDate = "desc";
        }else {
            sortDate = "asc";
        }
        //  排序 字段 处理
        String ordername = EnumStr.picDateField;
        if(EnumStr.devNameShow.equalsIgnoreCase(orderName)){
            ordername = EnumStr.devAddressField;
        }
        String queryAttRecordByPerson = "{\"query\":{\"bool\":{\"must\":[{\"range\":{\""+EnumStr.picDateField+"\":{\"gte\":" +
                "\""+starttime+"\",\"lte\":\""+endtime+"\"}}}"+contentQuery+persinIdQuery+"]," +
                "\"must_not\":[],\"should\":[]}},\"from\":"+from+",\"size\":"+size+",\"sort\":{\""+ordername
                +"\":{\"order\":\""+sortDate+"\"}},\"aggs\":{}}";  // 增加排序类型
        log.info(queryAttRecordByPerson+"查询 json 语句");
 
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(queryAttRecordByPerson, headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity("http://" + enumStr.getEsHOSTNAME()
                        + ":" + enumStr.getEsHTTP_PORT() +
                        "/"+EnumStr.esIndexField+"/_search",
                formEntity, JSONObject.class);
        if (!responseEntity.getStatusCode().is2xxSuccessful()){
            // 报错 非200状态
            throw new RuntimeException("es 查询秒级数据 返回失败!~");
        }else{
            JSONObject resp = responseEntity.getBody();
            JSONObject hits = resp.getJSONObject("hits");
            Integer total = hits.getInteger("total");
            JSONArray attRecords = hits.getJSONArray("hits");
            Map<String, String> personType = userService.queryPersonType();
            for (Object attRecord : attRecords){
                JSONObject source = ((JSONObject) attRecord).getJSONObject("_source");
//                source.put("identity",identity);
                // 获取 personId 组合数据
                String pid = source.getString(EnumStr.personIdField);
                //直接查询数据库
//                Map<String, Object> person= attDayDao.getPersonInfoById(pid);
                // 调用人员接口
                Map<String, Object> person = userService.getPersonInfoById(pid);
                if (person!=null){
                    String showType = "其他";
                    String pertype = (String) person.get("type");
                if (EnumStr.TEACHER_SQL.equals(pertype)){
                    showType = "教师";
                }else if(EnumStr.STUDENT_SQL.equals(pertype)){
                    showType = "学生";
                }else{
                    showType = personType.get(pertype);
                    if (showType == null) showType = "其他"+pertype;
                }
                    source.put(EnumStr.idCardField,person.get("cardId"));
                    source.put(EnumStr.perIdentityShow,showType);
                    source.put(EnumStr.photoAddShow,person.get("photos"));
 
                    // 人员 是否 已删除  wp 19-03-15
                    String name = (String) person.get("name");
                    String perDelFlag = (String) person.get("delFlag");
                    if ("1".equals(perDelFlag)){
                        name = name != null?(name+"(已删除)"):"(已删除)";
                    }
                    source.put(EnumStr.pnameShow,name != null?name:"");
                }else {
                    source.put(EnumStr.idCardField,"");
                    source.put(EnumStr.pnameShow,"");
                    source.put(EnumStr.perIdentityShow,null);
                    source.put(EnumStr.photoAddShow,null);
                }
                source.put(EnumStr.personIdShow,source.getString(EnumStr.personIdField));  // pid
                source.put(EnumStr.picDateShow,source.getString(EnumStr.picDateField).substring(0,19));  // 考勤日期
                source.put(EnumStr.snapPicShow,source.getString(EnumStr.perPicField));    // 抓拍图片
                source.put(EnumStr.devIdShow,source.getString(EnumStr.devIdField));  // 设备IDID
                String devName = source.getString(EnumStr.devNameShow);
                source.put(EnumStr.devNameShow,source.getString(EnumStr.devAddressField));   //devNameField设备名称  devAddressField
                source.put(EnumStr.devAddressField,devName);
                removeSource( source);  // 伤处多余字段
                lists.add(source);
            }
            retMap.put("total",total);
        }
        retMap.put("esAttList",lists);
        return  retMap;
    }
// 单人 单天 记录  List<JSONObject>
    public Map<String,Object> queryPersonById(String pid,Date startTime, Date endTime){
        String starttime = sdfDate.format(startTime);
        String endtime = sdfDate.format(endTime);
        String persinIdQuery = ",{\"query_string\":{\"default_field\":\""+EnumStr.personIdField+"\",\"query\":\""+pid+"\"}}";
 
        String queryAttRecordByPerson = "{\"query\":{\"bool\":{\"must\":[{\"range\":{\""+EnumStr.picDateField+"\":{\"gte\":" +
                "\""+starttime+"\",\"lte\":\""+endtime+"\"}}}"+persinIdQuery+"]," +
                "\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":1000,\"sort\":[\""+EnumStr.picDateField+"\"],\"aggs\":{}}";
        log.info(queryAttRecordByPerson+"查询 json 语句");
        List<JSONObject> lists = new ArrayList<JSONObject>();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(queryAttRecordByPerson, headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity("http://" +enumStr.getEsHOSTNAME()
                        + ":" +enumStr.getEsHTTP_PORT() +
                        "/"+EnumStr.esIndexField+"/_search",
                formEntity, JSONObject.class);
        Map<String,Object> personMap = new HashMap<String,Object>();
        if (!responseEntity.getStatusCode().is2xxSuccessful()){
            // 报错 非200状态
            throw new RuntimeException("es 查询秒级数据 返回失败!~");
        }else {
            JSONObject resp = responseEntity.getBody();
            JSONArray attRecords = resp.getJSONObject("hits").getJSONArray("hits");
            //直接查询数据库
//            Map<String, Object> person= attDayDao.getPersonInfoById(pid);
            // 调用人员接口
            Map<String, Object> person = userService.getPersonInfoById(pid);
           if (person != null){
               String showType = "其他";
               if (EnumStr.TEACHER_SQL.equals(person.get("type"))){
                   showType = "教师";
               }else if (EnumStr.STUDENT_SQL.equals(person.get("type"))){
                   showType = "学生";
               }
               personMap.put(EnumStr.perIdentityShow,showType);
               personMap.put(EnumStr.idCardField,person.get("cardId"));
               personMap.put(EnumStr.photoAddShow,person.get("photos"));
               personMap.put(EnumStr.pnameShow,person.get("name"));
           }else {
               personMap.put(EnumStr.perIdentityShow,"");
               personMap.put(EnumStr.photoAddShow,null);
               personMap.put(EnumStr.pnameShow,null);
           }
            for (Object attRecord : attRecords){
                JSONObject source = ((JSONObject) attRecord).getJSONObject("_source");
//                source.put("Snapshotpath",source.getString("Snapshotpath"));
                source.put(EnumStr.personIdShow,source.getString(EnumStr.personIdField));  // pid
                source.put(EnumStr.picDateShow,source.getString(EnumStr.picDateField).substring(0,19));  // 考勤日期
                source.put(EnumStr.snapPicShow,source.getString(EnumStr.perPicField));    // 抓拍图片
                source.put(EnumStr.devIdShow,source.getString(EnumStr.devIdField));  // 设备IDID
                source.put(EnumStr.devNameShow,source.getString(EnumStr.devAddressField));   //  devNameField设备名称  devAddressField
                removeSource( source);  // 伤处多余字段
                lists.add(source);
            }
            personMap.put("dayList",lists);
        }
        return  personMap;
    }
    private void removeSource(JSONObject source){
//        "Age""BaseName""BeautyLevel""ChannlId":"FaceFeature""Gender":
        source.remove("Age");  source.remove("BaseName");  source.remove("BeautyLevel");
        source.remove("ChannlId");  source.remove("FaceFeature");source.remove("Gender");
// "Id":"Race":"SimleLevel":"content":"idcard":"indeviceid":"indevicename":
        source.remove("Id");  source.remove("Race");  source.remove("SimleLevel");
        source.remove("content");  //source.remove("idcard");
// "isDelete":"likeDate":"likePer":"personId":"personIsHub":"personPicUrl"
        source.remove("isDelete");  source.remove("likeDate");  source.remove("likePer");
        source.remove("personIsHub");
// :"picAddress":"picDate":"picLocalUrl":"picMaxUrl":"picName":
       // source.remove("picAddress");
        source.remove("picDate");  source.remove("picLocalUrl");
        source.remove("picMaxUrl");  source.remove("picName");
// "picSmUrl":"viType":"videoIp":"videoNum":"videoReqNum":
          source.remove("videoIp");  source.remove("videoNum");
      /*  source.remove("videoReqNum");*/
    }
 
    // 从es 查询所有人员  EnumStr.picDateField
    public List<PersonForEs> queryPersonIdFromEs(){
        String queryAttRecordByPerson = "{\"size\":0,\"aggs\":{\""+EnumStr.personIdShow+"\":{\"terms\":{\"field\":\""+EnumStr.personIdField+"\"}},"+
                "\"aggs\":{\"terms\":{\"size\":10000,\"field\":\""+EnumStr.personIdField+"\"},"+
                "\"aggs\":{\"top_sales_hits\":{\"top_hits\":{\"_source\":{\"includes\":[\""+EnumStr.personIdField+"\",\""
                +EnumStr.personUrlField+"\",\""+EnumStr.idCardField+"\"]},\"size\":1}}}}}}";
        log.info(queryAttRecordByPerson+"查询 json 语句");
        List<PersonForEs> listPer = new ArrayList<PersonForEs>();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(queryAttRecordByPerson, headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity("http://" +enumStr.getEsHOSTNAME()
                        + ":" +enumStr.getEsHTTP_PORT() +
                        "/"+EnumStr.esIndexField+"/_search",
                formEntity, JSONObject.class);
        Map<String,Object> personMap = new HashMap<String,Object>();
        if (!responseEntity.getStatusCode().is2xxSuccessful()){
            // 报错 非200状态
            throw new RuntimeException("es 查询 人员数据 返回失败!~");
        }else {
            JSONObject resp = responseEntity.getBody();
            JSONArray perList = resp.getJSONObject("aggregations")
                                .getJSONObject("aggs")
                                 .getJSONArray("buckets");
           // 遍历加人
            int times = 0;
            Integer[] is = {133,134,135,136,137,138,139,147,148,152};  // 年级班级列表
            int il = is.length;
            for (Object perSource : perList){
                PersonForEs perEs = new PersonForEs();
                perEs.setCreateBy("es-add");
                JSONArray hits = ((JSONObject) perSource).getJSONObject("top_sales_hits").getJSONObject("hits").getJSONArray("hits");
                if (hits.size()>0){
                    JSONObject source = ((JSONObject)hits.get(0)).getJSONObject("_source");
                    log.info(source+"es 中人员情况");
                   if(source != null &&!"wait todo".equalsIgnoreCase(source.getString(EnumStr.personIdField))){
                    perEs.setPid(source.getString(EnumStr.personIdField));
                    perEs.setOrgId(1);  // 11 北京林业大学附属小学
                    perEs.setOfficeId(is[times++%il]);  // 12 五年级 13 三班  30 31  52 53 73 74
                    perEs.setName(source.getString(EnumStr.idCardField));
                    perEs.setGender((int)(Math.random() * 2 + 1)==1?"男":"女");
                    perEs.setPhotos(source.getString(EnumStr.personUrlField));
                    perEs.setType((int)(Math.random() * 2 + 1)==1?EnumStr.STUDENT_SQL:EnumStr.TEACHER_SQL);
                    listPer.add(perEs);
                    if (times>100)times=0;  // 重置
                   }
 
                }
            }
        }
        return  listPer;
    }
}