package com.cloud.retrieve.service.impl;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.cloud.common.utils.RestTemplateUtil;
|
import com.cloud.model.common.Result;
|
import com.cloud.retrieve.dao.CulOrDevDao;
|
import com.cloud.retrieve.model.EsDataQueryParam;
|
import com.cloud.retrieve.service.DeviceService;
|
import com.cloud.retrieve.service.EsDataService;
|
import com.cloud.retrieve.service.SearchPhotoService;
|
import com.cloud.retrieve.service.UserService;
|
import com.cloud.retrieve.utils.C_Es_Map;
|
import com.cloud.retrieve.utils.EnumStr;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.core.ListOperations;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.http.MediaType;
|
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.client.HttpClientErrorException;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
import java.util.stream.Stream;
|
|
@Slf4j
|
@Service
|
public class EsDataServiceImpl implements EsDataService {
|
|
@Autowired
|
private EnumStr enumStr;
|
@Autowired
|
private CulOrDevDao culOrDevDao;
|
|
@Autowired
|
private DeviceService deviceService;
|
@Autowired
|
private UserService userService;
|
|
private SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
private SimpleDateFormat sdfMouth = new SimpleDateFormat("yyyy-MM");
|
|
private SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
|
@Autowired
|
private SearchPhotoService searchPhotoService;
|
|
/**
|
* 首页 全部条件查询数据
|
* @param esSearch
|
* @return
|
*/
|
public JSONObject getAllDataByMoreParams(EsDataQueryParam esSearch){
|
String bsUrl = "http://"+enumStr.getEsHOSTNAME()+":"+enumStr.getEsHTTP_PORT();
|
String esBaseUrl = enumStr.getEsIndexUrl()+enumStr.getEsSearch();
|
JSONObject respView = new JSONObject();
|
String reqJson = changeObjectToJson(esSearch);
|
log.info("es 查询json 参数:==>"+reqJson);
|
JSONObject dataBaseParams = JSONObject.parseObject(reqJson);
|
|
JSONObject respData = null;
|
String resp = RestTemplateUtil.post(bsUrl+esBaseUrl,dataBaseParams,MediaType.APPLICATION_JSON_UTF8, false);
|
if(StringUtils.isNotEmpty(resp))
|
respData = JSONObject.parseObject(resp);
|
JSONObject hits = respData.getJSONObject("hits");
|
Long total = hits.getLong("total");
|
respView.put("total",total);
|
JSONArray hits1 = hits.getJSONArray("hits");
|
JSONArray jsonArray = new JSONArray();
|
if (hits1!=null&& !hits1.isEmpty()){
|
for (Object obj : hits1) {
|
JSONObject hit = (JSONObject)obj;
|
String id = hit.getString("_id");
|
JSONObject source = hit.getJSONObject("_source");
|
source.put("Id",id);
|
// 返回数据处理
|
respSource( source);
|
jsonArray.add(source);
|
}
|
}
|
// 添加 数据判断 是否添加数据待确认报警
|
if (esSearch.getIsAlermSum()!=null && esSearch.getIsAlermSum()){
|
if ("1".equals(esSearch.getAck_alarm())){
|
respView.put("alarmSum",total);
|
esSearch.setAck_alarm("0");
|
Long alarmSum = getIsAckAlarmSum(esSearch);
|
respView.put("noAlarmSum",alarmSum);
|
respView.put("allAlarmSum",alarmSum+total);
|
}else if ("0".equals(esSearch.getAck_alarm())){
|
esSearch.setAck_alarm("1");
|
Long alarmSum = getIsAckAlarmSum(esSearch);
|
respView.put("alarmSum",alarmSum);
|
respView.put("noAlarmSum",total);
|
respView.put("allAlarmSum",alarmSum+total);
|
}else{
|
esSearch.setAck_alarm("1");
|
Long alarmSum = getIsAckAlarmSum(esSearch);
|
respView.put("alarmSum",alarmSum);
|
respView.put("noAlarmSum",total-alarmSum);
|
respView.put("allAlarmSum",total);
|
}
|
}
|
respView.put("esList",jsonArray);
|
return respView;
|
}
|
|
/**
|
* 分割 底库 数据条件
|
* @param tCluAndBaseNameAndHub
|
* @return cluId baseName hubStatus
|
*/
|
public String[] getCluAndBaseNameAndHub(String tCluAndBaseNameAndHub){
|
return tCluAndBaseNameAndHub.split("\\$");
|
}
|
|
// 获取 待确认报警总数
|
private Long getIsAckAlarmSum(EsDataQueryParam esSearch){
|
|
String bsUrl = "http://"+enumStr.getEsHOSTNAME()+":"+enumStr.getEsHTTP_PORT();
|
String esBaseUrl = enumStr.getEsIndexUrl()+enumStr.getEsSearch();
|
|
String reqJson = changeObjectToJson(esSearch);
|
log.info("es 查询json 参数:==>"+reqJson);
|
JSONObject dataBaseParams = JSONObject.parseObject(reqJson);
|
JSONObject respData = null;
|
String resp = RestTemplateUtil.post(bsUrl+esBaseUrl,dataBaseParams,MediaType.APPLICATION_JSON_UTF8, false);
|
if(StringUtils.isNotEmpty(resp))
|
respData = JSONObject.parseObject(resp);
|
JSONObject hits = respData.getJSONObject("hits");
|
Long total = hits.getLong("total");
|
return total;
|
}
|
|
// 修改确认报警
|
@Override
|
public JSONObject updateEsDataByEsId(String dataId, String ack_alarm) {
|
String bsUrl = "http://"+enumStr.getEsHOSTNAME()+":"+enumStr.getEsHTTP_PORT();
|
String esIndexUrl = enumStr.getEsIndexUrl();
|
String[] nidexes = esIndexUrl.substring(1).split(",");
|
JSONObject respView = new JSONObject();boolean isUpdate = false;
|
int in = 0;
|
for (String nidex : nidexes){
|
String esType = in==0?enumStr.getEsPersonType():enumStr.getEsActionType();
|
String esBaseUrl = "/"+nidex+esType+"/"+dataId+enumStr.getEsUpdate();
|
String isRefresh = "?refresh=wait_for"; // wait_for 等候默认参数刷新 true 立即刷新
|
String reqJson = "{\"doc\":{\"ack_alarm\":\"1\"},\"detect_noop\":false}";
|
JSONObject dataBaseParams = JSONObject.parseObject(reqJson);
|
log.info("修改url:"+bsUrl+esBaseUrl+isRefresh);
|
log.info("修改Params:"+dataBaseParams.toJSONString());
|
JSONObject respData = null;
|
try {
|
String resp = RestTemplateUtil.post(bsUrl+esBaseUrl+isRefresh,dataBaseParams,MediaType.APPLICATION_JSON_UTF8, false);
|
if(StringUtils.isNotEmpty(resp))
|
respData = JSONObject.parseObject(resp);
|
} catch (HttpClientErrorException e) {
|
log.info("es 修改确认报警信息:"+e.getLocalizedMessage());
|
// e.printStackTrace();
|
}
|
if (respData != null && respData.getInteger("status") == null ){
|
respView.put("update",1);
|
respView.put("success",true);
|
isUpdate = true;
|
break;
|
}
|
in++;
|
}
|
if (!isUpdate){
|
respView.put("update",0);
|
respView.put("success",false);
|
}
|
return respView;
|
}
|
// 查询摄像机报警记录===
|
@Override
|
public JSONObject findEsPersonAlarmDataByEquPage(Integer page, Integer size,String equId,String personIsHub) {
|
String bsUrl = "http://"+enumStr.getEsHOSTNAME()+":"+enumStr.getEsHTTP_PORT();
|
String esBaseUrl = enumStr.getEsIndexUrl()+enumStr.getEsSearch();
|
JSONObject respView = new JSONObject();
|
int from = (page-1)*size;
|
String frimJson = allQueryJson.replace("fromNum", "" + from);
|
String sizeJson = frimJson.replace("sizeNum", "" + size);
|
String queryParams = ""; boolean isAddParams = false; // 查询 term 拼接 // 是否 term 有内容
|
if (personIsHub != null && "1".equals(personIsHub)){
|
String hubTerm = term.replace("eqiId","1");
|
String isHubTerm = hubTerm.replace("videoReqNum","personIsHub");
|
if (isAddParams){queryParams+=",";}else {isAddParams = true; } // 查询条件分隔符
|
queryParams+=isHubTerm;
|
}
|
if(equId!=null &&!equId.isEmpty()){
|
String equTerm = term.replace("eqiId",equId);
|
if (isAddParams){queryParams+=",";}else {isAddParams = true; } // 查询条件分隔符
|
queryParams+=equTerm;
|
}else {
|
log.info("es 查询条件中equId未知:"+equId);
|
}
|
if (sizeJson.contains("shouldParams")) sizeJson = sizeJson .replace(",shouldParams","");
|
// log.info("es 查询json 待拼接参数:==>"+queryParams);
|
String reqJson = sizeJson.replace("addSearchParams", queryParams);
|
|
|
reqJson = reqJson.replace("sdkTypes","");
|
reqJson = reqJson.replace("scoreSort","");
|
// log.info("es 查询json 参数:==>"+reqJson);
|
JSONObject dataBaseParams = JSONObject.parseObject(reqJson);
|
JSONObject respData = null;
|
String resp = RestTemplateUtil.post(bsUrl+esBaseUrl,dataBaseParams,MediaType.APPLICATION_JSON_UTF8,false);
|
if(StringUtils.isNotEmpty(resp))
|
respData = JSONObject.parseObject(resp);
|
JSONObject hits = respData.getJSONObject("hits");
|
String total = hits.getString("total");
|
respView.put("total",total);
|
JSONArray hits1 = hits.getJSONArray("hits");
|
JSONArray jsonArray = new JSONArray();
|
if (hits1!=null&& !hits1.isEmpty()){
|
for (Object obj : hits1) {
|
JSONObject hit = (JSONObject)obj;
|
String id = hit.getString("_id");
|
JSONObject source = hit.getJSONObject("_source");
|
source.put("Id",id);
|
// 返回数据处理
|
respSource( source);
|
jsonArray.add(source);
|
}
|
}
|
respView.put("equList",jsonArray);
|
return respView;
|
}
|
|
private String allAlarmJson = "{\"from\":page,\"size\":number,\"query\":{\"bool\":{\"must\":[" +
|
"{\"terms\":{\"ack_alarm\":[\"0\",\"\"]}},{\"term\":{\"personIsHub\":\"1\"}}daterange]}}," +
|
"\"_source\":[\"BaseName\",\"Gender\",\"Id\",\"Race\",\"indeviceid\",\"indevicename\",\"likeDate\"," +
|
"\"likePer\",\"personId\",\"personIsHub\",\"personPicUrl\",\"picAddress\",\"picDate\",\"picName\"," +
|
"\"picSmUrl\",\"sdkType\",\"videoIp\",\"videoNum\",\"videoReqNum\",\"ack_alarm\",\"imgKey\"],\"sort\":[{\"picDate\":{\"order\":\"desc\"}}]}";
|
|
// 地图页 全部报警记录
|
@Override
|
public JSONObject findEsAllAlarmDataByPage(Integer page, Integer size,Date startDate,Date endDate) {
|
String bsUrl = "http://"+enumStr.getEsHOSTNAME()+":"+enumStr.getEsHTTP_PORT();
|
String esBaseUrl = enumStr.getEsIndexUrl()+enumStr.getEsSearch();
|
JSONObject respView = new JSONObject();
|
int from = (page-1)*size;
|
String frimJson = allAlarmJson.replace("page", "" + from);
|
String sizeJson = frimJson.replace("number", "" + size);
|
// 时间需要处理
|
if (startDate == null && endDate == null){
|
sizeJson = sizeJson.replace("daterange","");
|
}else{
|
String range = ",{\"range\":{\"picDate\": {\"gt\": \"startDate\"," +
|
"\"lte\": \"now/d\"}}}";
|
if (startDate != null){
|
range = range.replace("startDate", sdfTime.format(startDate));
|
}else {
|
range = range.replace("\"gt\": \"startDate\",","");
|
}
|
if (endDate != null){
|
range = range.replace("now/d",sdfTime.format(endDate));
|
}
|
sizeJson = sizeJson.replace("daterange",range);
|
}
|
JSONObject dataBaseParams = JSONObject.parseObject(sizeJson);
|
// log.info("sizeJson 查询全部报警记录参数:"+sizeJson);
|
// log.info("esurl:"+bsUrl+esBaseUrl);
|
JSONObject respData = null;
|
String resp = RestTemplateUtil.post(bsUrl+esBaseUrl,dataBaseParams,MediaType.APPLICATION_JSON_UTF8,false);
|
if(StringUtils.isNotEmpty(resp))
|
respData = JSONObject.parseObject(resp);
|
|
JSONObject hits = respData.getJSONObject("hits");
|
String total = hits.getString("total");
|
respView.put("total",total);
|
JSONArray hits1 = hits.getJSONArray("hits");
|
JSONArray jsonArray = new JSONArray();
|
if (hits1!=null&& !hits1.isEmpty()){
|
for (Object obj : hits1) {
|
JSONObject hit = (JSONObject)obj;
|
String id = hit.getString("_id");
|
JSONObject source = hit.getJSONObject("_source");
|
source.put("Id",id);
|
// 返回数据处理
|
respSource( source);
|
jsonArray.add(source);
|
}
|
}
|
respView.put("equList",jsonArray);
|
return respView;
|
}
|
private String getOneById = "{\"from\":0,\"size\":1,\"query\":{\"bool\":{\"must\":" +
|
"[{\"term\":{\"_id\":\"personId\"}}]}}}";
|
@Override
|
public JSONObject getPersonDetail(String id, String personId) {
|
String bsUrl = "http://"+enumStr.getEsHOSTNAME()+":"+enumStr.getEsHTTP_PORT();
|
String esBaseUrl = enumStr.getEsIndexUrl()+enumStr.getEsSearch();
|
String searchParam = getOneById.replace("personId",id);
|
JSONObject dataBaseParams = JSONObject.parseObject(searchParam);
|
// log.info("sizeJson 查询单人记录参数:"+searchParam);
|
JSONObject respData = null;
|
String resp = RestTemplateUtil.post(bsUrl+esBaseUrl,
|
dataBaseParams, MediaType.APPLICATION_JSON_UTF8, false);
|
if(StringUtils.isNotEmpty(resp))
|
respData = JSONObject.parseObject(resp);
|
|
JSONObject hits = respData.getJSONObject("hits");
|
// String total = hits.getString("total");
|
JSONArray hits1 = hits.getJSONArray("hits");
|
JSONObject respView = new JSONObject();
|
if (hits1!=null&& !hits1.isEmpty()){
|
JSONObject hit = (JSONObject)hits1.get(0);
|
JSONObject source = hit.getJSONObject("_source");
|
source.put("Id",id);
|
// 返回数据处理
|
respSource( source);
|
respView = source;
|
log.info(id+"数据id查询返回:"+respView);
|
}
|
// name, gender,cardId, phone, numCard, orgName
|
// Map<String, Object> personMap = culOrDevDao.selectPersonDetailById(personId);
|
Map personMap = userService.queryUserInfoById(personId);
|
respView.put("name",personMap.get("name"));
|
respView.put("gender",personMap.get("gender"));
|
respView.put("cardId",personMap.get("cardId"));
|
respView.put("phone",personMap.get("phone"));
|
respView.put("no",personMap.get("no"));
|
respView.put("orgName",personMap.get("orgName"));
|
return respView;
|
}
|
|
|
private String personQueryJson = "{" +
|
"\"from\": 0," +
|
"\"size\": 100," +
|
"\"query\": {" +
|
"\"bool\": {" +
|
"\"must\": [addSearchParams]," +
|
"\"must_not\": [{\"term\": {\"personId\":\"perId\"}}]," +
|
"\"adjust_pure_negative\": true," +
|
"\"boost\": 1" +
|
"}" +
|
"}," +
|
"\"sort\": [{" +
|
"\"picDate\": {" +
|
"\"order\": \"desc\"" +
|
"}" +
|
"}]" +
|
"}";
|
/**
|
* 随行人员查询
|
* @param videoReqNum
|
* @param picDate
|
* @return
|
*/
|
@Override
|
public JSONObject getAccompanyPerson(String videoReqNum,String personId,String picDate,String Id) throws ParseException {
|
|
String bsUrl = "http://"+enumStr.getEsHOSTNAME()+":"+enumStr.getEsHTTP_PORT();
|
String esBaseUrl = enumStr.getEsIndexUrl()+enumStr.getEsSearch();
|
// 设备 id
|
String searchParam1 = term.replace("eqiId",videoReqNum);
|
// sdkType = 1
|
String idTerm2 = term.replace("videoReqNum", "sdkType");
|
String searchParam2 = idTerm2.replace("eqiId","1");
|
// 时间 调整
|
String rangeParam3 = "";
|
if(picDate!=null && !picDate.isEmpty()){
|
Date nowDate = sdfTime.parse(picDate);
|
String startDate = range.replace("startDate", sdfTime.format(nowDate.getTime()-2000));
|
rangeParam3 = startDate.replace("endDate", sdfTime.format(nowDate.getTime()+2000));
|
}else {
|
log.info("es 随行人员查询条件中时间未知:"+picDate);
|
}
|
String queryJson = personQueryJson.replace("addSearchParams", searchParam1+","
|
+searchParam2+","+rangeParam3);
|
String reqJson = "";
|
if (personId !=null && !personId.isEmpty() && !"wait todo".equalsIgnoreCase(personId)){
|
reqJson = queryJson.replace("perId", personId);
|
}else{
|
String personJson = queryJson.replace("personId", "_id");
|
reqJson = personJson.replace("perId", Id);
|
}
|
JSONObject dataBaseParams = JSONObject.parseObject(reqJson);
|
// log.info("sizeJson 查询全部报警记录参数:"+reqJson);
|
JSONObject respData = null;
|
String resp = RestTemplateUtil.post(bsUrl+esBaseUrl,
|
dataBaseParams, MediaType.APPLICATION_JSON_UTF8, false);
|
if(StringUtils.isNotEmpty(resp))
|
respData = JSONObject.parseObject(resp);
|
JSONObject hits = respData.getJSONObject("hits");
|
String total = hits.getString("total");
|
JSONArray hits1 = hits.getJSONArray("hits");
|
JSONObject respView = new JSONObject();
|
JSONArray respResult = new JSONArray();
|
// 数据 比对结果存储 ------------- AccompanyPersonEsSaveData
|
// String path = saveAccompanyEsData(Id, picDate);
|
if (hits1!=null&& !hits1.isEmpty()){
|
JSONObject hit = (JSONObject)hits1.get(0);
|
String id = hit.getString("_id");
|
// boolean isContainId = getEsDataById(id,path);
|
boolean isContainId = false;
|
if (!isContainId){ // 人员去重 true 为存在
|
JSONObject source = hit.getJSONObject("_source");
|
source.put("Id",id);
|
//随行人员去重 -------------------- AccompanyPersonEsGetData
|
// 返回数据处理
|
respSource(source);
|
respResult.add(source);
|
}
|
}
|
respView.put("total",respResult.size());
|
respView.put("personList",respResult);
|
return respView;
|
}
|
// 从 redis 中查询数据 让后 确认过滤是否存在此数据
|
private boolean getEsDataById(String id,String path) {
|
String eskey = path+enumStr.getEsListkey();
|
log.info("redis中取数据");
|
Long esDataTotle = (Long) redisTemplate.opsForList().size(eskey);
|
log.info("es数据总数为"+esDataTotle);
|
if (esDataTotle == null || esDataTotle < 1){
|
return false;
|
}
|
List<Map<String,Object>> esList = redisTemplate.opsForList().range(eskey, 0,-1);
|
long id1 = esList.parallelStream().filter(map -> (id.equals(map.get("Id")))).count();
|
if (id1 > 0){
|
return true;
|
}else {
|
return false;
|
}
|
}
|
|
// 存储 随行人员 比对数据 未完待续
|
private String saveAccompanyEsData(String id, String picDate) throws ParseException {
|
Map<String,Object> params = new HashMap<>();
|
params.put("page",1);
|
params.put("liker",70);
|
params.put("length",1000);
|
String bsUrl = "http://"+enumStr.getEsHOSTNAME()+":"+enumStr.getEsHTTP_PORT();
|
String esBaseUrl = enumStr.getEsIndexUrl().split(",")[0]+enumStr.getEsPersonType();
|
// 调用 es 查询路径 path
|
// 192.168.1.203:9200/videopersons/perVideoPicture/4f510335-3376-45cb-a661-8266c0512e74
|
String resp = RestTemplateUtil.get(bsUrl+esBaseUrl+"/"+id,
|
new JSONObject(), false);
|
JSONObject jsonObject = JSONObject.parseObject(resp);
|
Boolean found = jsonObject.getBoolean("found");
|
if (found){
|
String path = jsonObject.getJSONObject("_source").getString("picSmUrl");
|
params.put("path",path);
|
Date parse = sdfTime.parse(picDate);
|
params.put("startDate",sdfTime.format(parse.getTime()-2000));
|
params.put("endDate",sdfTime.format(parse.getTime()+2000));
|
Map map = searchPhotoService.findLikerPicsRtnEsData(params);
|
return path;
|
}else {
|
return null;
|
}
|
}
|
|
private String personLocusJson = "{\"query\":{\"bool\":{\"must\":[" +
|
"{\"term\":{\"personId\":\"personIdValue\"}}," +
|
"{\"range\":{\"picDate\":{\"gt\":\"startDate\",\"lt\":\"endDate\"}}}]}}," +
|
"\"_source\":[\"videoReqNum\",\"picDate\",\"picAddress\"],\"sort\":[{\"picDate\":{\"order\":\"asc\"}}],\"size\":1000}";
|
|
/**
|
* 人员轨迹
|
* @param personId
|
* @param startDate
|
* @param endDate
|
* @return
|
*/
|
@Override
|
public JSONObject getPersonnLocus(String personId, String startDate, String endDate) {
|
String bsUrl = "http://"+enumStr.getEsHOSTNAME()+":"+enumStr.getEsHTTP_PORT();
|
String esBaseUrl = enumStr.getEsIndexUrl()+enumStr.getEsSearch();
|
// 设备 id
|
String personParam = personLocusJson.replace("personIdValue",personId);
|
// 时间 调整
|
String startDateParam = personParam.replace("startDate",startDate);
|
String reqJson = startDateParam.replace("endDate", endDate);
|
log.info("reqJson 人员轨迹查询参数:"+reqJson);
|
JSONObject dataBaseParams = JSONObject.parseObject(reqJson);
|
JSONObject respData = null;
|
String resp = RestTemplateUtil.post(bsUrl+esBaseUrl,
|
dataBaseParams, MediaType.APPLICATION_JSON_UTF8, false);
|
if(StringUtils.isNotEmpty(resp))
|
respData = JSONObject.parseObject(resp);
|
JSONObject hits = respData.getJSONObject("hits");
|
String total = hits.getString("total");
|
JSONArray hits1 = hits.getJSONArray("hits");
|
JSONObject respView = new JSONObject();
|
JSONArray respResult = new JSONArray(); List<String> devIds = new ArrayList<>();
|
JSONArray devpath = new JSONArray();
|
respView.put("total",total);
|
if (hits1!=null&& !hits1.isEmpty()){
|
String firstDate = null;
|
for (Object obj : hits1){
|
JSONObject hit = (JSONObject)obj;
|
String id = hit.getString("_id");
|
JSONObject source = hit.getJSONObject("_source");
|
source.put("Id",id);
|
// monthfirst 每月标记 true 是首天 false
|
Date picDate = source.getSqlDate("picDate");
|
if (picDate!=null&&!sdfMouth.format(picDate).equals(firstDate)){
|
source.put("monthfirst",true);
|
firstDate = sdfMouth.format(picDate);
|
}else {
|
source.put("monthfirst",false);
|
}
|
String videoReqNum = source.getString("videoReqNum");
|
if (videoReqNum != null){
|
devpath.add(new String[]{videoReqNum+"_lon",videoReqNum+"_lat"});
|
}else {
|
devpath.add(new String[2]);
|
}
|
devIds.add(source.getString("videoReqNum"));
|
respResult.add(source);
|
}
|
// 处理 路径 轨迹 数据
|
changPathByDevId( respView, devpath);
|
}
|
respView.put("locusList",respResult);
|
// 集群信息处理
|
// log.info("数据汇总:"+devIds.size());
|
JSONArray sumNum = groupByGroupSum(devIds);
|
respView.put("sumNum",sumNum);
|
return respView;
|
}
|
|
/**
|
* 分组
|
* @param demos
|
*/
|
public JSONArray groupByGroupSum(List<String> demos){
|
|
Map<String, Long> collect = demos.parallelStream().collect(Collectors.groupingBy(String::toString,Collectors.counting()));
|
JSONArray respDevSum = new JSONArray();
|
collect.forEach((key,value)->{
|
// System.out.println("分组:"+key+":"+value);
|
JSONObject devInfo = new JSONObject();
|
devInfo.put("devId",key);
|
devInfo.put("decCount",value);
|
respDevSum.add(devInfo);
|
});
|
return respDevSum;
|
}
|
// 转换 摄像机坐标 path
|
public void changPathByDevId(JSONObject respView,JSONArray devpath){
|
// 设备id 处理
|
Result result = deviceService.findAllDevices();
|
if (result.isSuccess() && devpath.size() > 0){
|
JSONObject data = (JSONObject) result.getData();
|
JSONArray posList = data.getJSONArray("posList");
|
String devPathBeforeJson = devpath.toJSONString();
|
// log.info("转换前 path 内容:devPathBeforeJson:"+devPathBeforeJson);
|
for (Object obj : posList) {
|
JSONObject dev = (JSONObject)obj;
|
String devId = dev.getString("id");
|
String devlat = dev.getString("latitude");
|
String devlon = dev.getString("longitude");
|
devPathBeforeJson = devPathBeforeJson.replaceAll("\""+devId + "_lon\"", devlon);
|
devPathBeforeJson = devPathBeforeJson.replaceAll("\""+devId + "_lat\"", devlat);
|
}
|
//log.info("设备路径变换后json:"+devPathBeforeJson);
|
JSONArray afterPath = JSONArray.parseArray(devPathBeforeJson);
|
for (int ap = 0,size = afterPath.size();ap<size; ap++) {
|
JSONArray devPath= (JSONArray)afterPath.get(ap);
|
if (devPath.size() != 2){
|
afterPath.remove(ap);ap--;size--;
|
}else if (devPath.getString(0).contains("_lon") || devPath.getString(1).contains("_lat")){
|
afterPath.remove(ap);ap--;size--;
|
}
|
}
|
respView.put("path",afterPath); // 转换无误
|
}else {
|
devpath = new JSONArray();
|
log.info("设备坐标查询异常。result:"+result);
|
respView.put("path",devpath);
|
}
|
}
|
|
@Autowired
|
private RedisTemplate redisTemplate;
|
|
// 获取es 比对后数据轨迹
|
@Override
|
public JSONObject getPersonnLocusByRedis(String picId) {
|
// 下方 测试 轨迹
|
/* if (true){
|
return getPersonnLocus("PSVAD010120181215175357774","2018-12-01","2018-12-31");
|
}*/
|
// 待 redis 有数据的
|
ListOperations opsForList = redisTemplate.opsForList();
|
List<Map<String,Object>> range = opsForList.range(picId + enumStr.getEsListkey(), 0, -1);
|
//
|
JSONObject jsonView = new JSONObject();
|
JSONArray devpath = new JSONArray();
|
JSONArray devList = new JSONArray(); List<String> devIds = new ArrayList<>();
|
|
Collections.sort(range, new Comparator<Map<String,Object>>(){
|
public int compare(Map<String,Object> map1, Map<String,Object> map2) {
|
int i = (int) (map2.get("picDate")+"").compareTo( map1.get("picDate")+"");
|
if(i == 0){
|
return 0;
|
}else if(i<0){
|
return -1;
|
}else {
|
return 1;
|
}
|
}
|
});
|
if (range != null){
|
jsonView.put("total",""+range.size());
|
SimpleDateFormat sdfDT = new SimpleDateFormat("yyyy年MM月");
|
for (Map<String,Object> esMap : range){
|
JSONObject source = new JSONObject();
|
source.put("picDate",esMap.get("picDate"));
|
source.put("videoReqNum",esMap.get("videoReqNum"));
|
source.put("picAddress",esMap.get("picAddress"));
|
source.put("Id",esMap.get("Id"));
|
// monthfirst 每月标记 true 是首天 false
|
Date picDate = source.getSqlDate("picDate");
|
source.put("timeDay", sdfDay.format(picDate));
|
String videoReqNum = source.getString("videoReqNum");//摄像机id
|
|
if (videoReqNum != null){
|
devpath.add(0,new String[]{videoReqNum+"_lon",videoReqNum+"_lat"});
|
}else {
|
devpath.add(0,new String[2]);
|
}
|
devList.add(source);
|
devIds.add(source.getString("videoReqNum"));
|
}
|
// 获取所有 摄像机 信息
|
JSONArray posList = new JSONArray();
|
Map<String, Object> reqInfosMap = new HashMap<String,Object>();
|
Result result = deviceService.findAllDevices();
|
if (result.isSuccess() && devpath.size() > 0){
|
JSONObject data = (JSONObject) result.getData();
|
posList = data.getJSONArray("posList");
|
reqInfosMap = posList.parallelStream().collect(Collectors.toMap(jsonObj->((JSONObject)jsonObj).getString("id"),jsonObj->(jsonObj)));
|
}
|
// 依据 设备id 分组聚合 数据 devList 数据为空
|
Map<String, List<Object>> videoReqNum = devList.parallelStream().collect(Collectors.groupingBy(source -> ((JSONObject) source).getString("videoReqNum")));
|
JSONArray devGroupList = new JSONArray();
|
JSONArray devInfos = new JSONArray();
|
Map<String, Object> finalReqInfosMap = reqInfosMap;
|
videoReqNum.forEach((k, v) ->{
|
JSONObject reqSource = new JSONObject();
|
reqSource.put("videoReqNum",k);
|
// 每个 摄像机的数据进行处理
|
List soues = (List)v;
|
String firstDate = null;
|
String currDay = "";// timeDay
|
for (int i = 0; i < soues.size(); i++) {
|
JSONObject sou = (JSONObject)soues.get(i);
|
Date picDate = sou.getSqlDate("picDate");
|
String timeDay = sou.getString("timeDay");
|
if (picDate!=null&&!sdfMouth.format(picDate).equals(firstDate)){
|
JSONObject mouthSource = new JSONObject();
|
mouthSource .put("monthfirst",true);
|
mouthSource.put("Id","jingersong");
|
mouthSource.put("showMonth",sdfDT.format(picDate));
|
mouthSource.put("videoReqNum",k);
|
soues.add(i++,mouthSource);
|
firstDate = sdfMouth.format(picDate);
|
sou.put("monthfirst",false);currDay = timeDay;
|
} else if (timeDay != null&& currDay.equals(timeDay)){
|
soues.remove(i--);
|
}else{
|
currDay = timeDay;
|
sou.put("monthfirst",false);
|
}
|
}
|
reqSource.put("dateList",soues);
|
devGroupList.add(reqSource);
|
JSONObject devInfo = new JSONObject();
|
devInfo.put("id",k);
|
JSONObject dev = (JSONObject) finalReqInfosMap.get(k);
|
if (dev != null){
|
devInfo.put("name",dev.getString("name"));
|
devInfo.put("address",dev.getString("address"));
|
devInfos.add(devInfo);
|
}
|
});
|
jsonView.put("locusList",devGroupList);
|
jsonView.put("devInfos",devInfos);
|
}else{
|
jsonView.put("total",0);
|
jsonView.put("locusList",new JSONArray());
|
jsonView.put("path",new JSONArray());
|
}
|
// 处理 路径 轨迹 数据
|
changPathByDevId(jsonView, devpath);
|
// 集群信息处理
|
// log.info("数据汇总:"+devIds.size());
|
JSONArray sumNum = groupByGroupSum(devIds);
|
jsonView.put("sumNum",sumNum);
|
return jsonView;
|
}
|
// 返回数据 source 特殊处理
|
private void respSource(JSONObject source){
|
String sdkType = source.getString("sdkType");
|
// sdkType 类型 进行处理
|
String[] sdkTypes = C_Es_Map.esSdkType;
|
String[] sdkValues = C_Es_Map.sdkValue;
|
if(sdkType!=null&& sdkTypes.length==sdkValues.length){
|
for (int i = 0; i < sdkTypes.length; i++){
|
if(sdkType.equalsIgnoreCase(sdkTypes[i])){
|
sdkType = sdkValues[i];break;
|
}
|
}
|
if (sdkType.equals("人脸")){
|
sdkType = "1";
|
}
|
source.put("sdkType",sdkType);
|
}
|
// personId 'waittodo' 处理
|
String personId = (String) source.get("personId");
|
if (personId!=null && !personId.isEmpty() ){
|
if ( personId.contains("wait todo")){
|
source.put("personId","");
|
}else {
|
// 查询数据库 name, gender, cardId, phone,b.`no` numCard,o.name orgName
|
// Map<String, Object> respMap = culOrDevDao.selectPersonDetailById(personId);
|
Map respMap = userService.queryUserInfoById(personId);
|
if (respMap != null){
|
String name = (String) respMap.get("name");
|
source.put("name",name);
|
source.put("gender",respMap.get("gender")); // 修改 覆盖es 性别属性
|
source.put("cardId",respMap.get("cardId"));
|
source.put("phone",respMap.get("phone"));
|
source.put("no",respMap.get("no"));
|
source.put("orgName",respMap.get("orgName"));
|
}
|
}
|
}
|
|
// 底库名称处理
|
String baseName = (String) source.get("BaseName");
|
if(baseName!=null && baseName.startsWith("lt_")){
|
source.put("BaseName", baseName.substring(3));
|
}else if (baseName.contains("wait todo")){
|
source.put("BaseName","");
|
}
|
// 相似值处理 likePer
|
Object likePer =null;
|
try {
|
likePer = source.get("likePer");
|
if(likePer!=null){
|
// log.info("float likePer:"+likePer);
|
// source.put("likePer",Math.round((Double)likePer*100));
|
source.put("likePer",Math.round(Double.parseDouble(likePer.toString())*100));
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
log.info("Id:"+source.get("Id")+"本次查询likePer:"+likePer);
|
}
|
// Age 处理
|
Object age = source.get("Age");
|
if(age!=null){
|
int agei = -1;
|
try {
|
agei = (Integer)age;
|
} catch (ClassCastException e){
|
agei = Integer.valueOf((String)age);
|
log.info(e.getLocalizedMessage());
|
}
|
if(agei>0 && agei < 7){
|
age ="童年";
|
}else if(agei>=7 && agei < 18){
|
age ="少年";
|
}else if(agei>=18 && agei < 40){
|
age ="青年";
|
}else if(agei>=40 && agei < 65){
|
age ="中年";
|
}else if(agei>= 65){
|
age ="老年";
|
}
|
}else {age = "";}
|
source.put("Age",age);
|
}
|
|
private String term = "{" +
|
"\"term\": {\"videoReqNum\":\"eqiId\"}" +
|
"}";
|
|
private String matchJson = "{\"multi_match\":{" +
|
"\"query\":\"matchauery\"," +
|
"\"fields\": [" +
|
"\"Gender^2.0\"," +
|
"\"Race^2.0\"," +
|
"\"BaseName^1.5\"," +
|
"\"content^1.0\"," +
|
"\"picAddress^1.0\"" +
|
"]," +
|
"\"type\":\"cross_fields\"" +
|
"}}";
|
private String terms = "{" +
|
"\"terms\": {" +
|
"\"videoReqNum\": termlist" +
|
"}" +
|
"}";
|
private String range = "{" +
|
"\"range\": {" +
|
"\"picDate\": {" +
|
"\"from\": \"startDate\"," +
|
"\"to\": \"endDate\"," +
|
"\"include_lower\": true," +
|
"\"include_upper\": false," +
|
"\"boost\": 1" +
|
"}}}";
|
/**
|
* 查询json 转换
|
* @param esSearch
|
* @return
|
*/
|
public String changeObjectToJson(EsDataQueryParam esSearch){
|
String contentes = esSearch.getContent();
|
Integer startAge = null,endAge = null;
|
log.info("esSearch-->"+esSearch);
|
Integer page = esSearch.getPage();
|
Integer size = esSearch.getSize();
|
int from = (page-1)*size;
|
String frimJson = allQueryJson.replace("fromNum", "" + from);
|
String sizeJson = frimJson.replace("sizeNum", "" + size);
|
String[] split = null;
|
// content 输入框内容
|
boolean isCont = false;
|
if (contentes!= null && !contentes.isEmpty()){
|
if(contentes.contains("童")){
|
startAge = 0; endAge = 7;isCont =true;
|
}
|
if(contentes.contains("少")){
|
startAge = 7; endAge = 18;isCont =true;
|
}
|
if(contentes.contains("青")){
|
startAge= 18;endAge = 40;isCont =true;
|
}
|
if(contentes.contains("中")){
|
startAge =40;endAge = 65;isCont =true;
|
}
|
if(contentes.contains("老")){
|
startAge =65;endAge = 200;isCont =true;
|
}
|
if (isCont){
|
contentes = contentes.replaceAll("(童年)|(少年)|(青年)|(中年)|(老年)|(童)|(少)|(青)|(中)|(老)","").trim();
|
}
|
split = contentes.trim().split("\\s+");
|
esSearch.setContents(split);
|
esSearch.setContent(contentes); // wp 后加的
|
}else {
|
esSearch.setContents(null);
|
}
|
String queryParams = "";boolean isAddParams = false;
|
// 时间截取 条件查询
|
if(esSearch.getStartDate()!=null && esSearch.getEndDate()!=null){
|
String startDate = range.replace("startDate", sdfTime.format(esSearch.getStartDate()));
|
String endDate = startDate.replace("endDate", sdfTime.format(esSearch.getEndDate()));
|
if (isAddParams){queryParams+=",";} isAddParams = true; // 查询条件分隔符
|
queryParams+=endDate;
|
}else {
|
log.info("es 查询条件中时间未知:"+esSearch.getStartDate()+"<====>"+esSearch.getEndDate());
|
}
|
|
// 是否确认报警
|
if(esSearch.getAck_alarm()!=null && !esSearch.getAck_alarm().isEmpty()){
|
String ack_alarm = esSearch.getAck_alarm();
|
String alarmValue = null;
|
if ("1".equalsIgnoreCase(ack_alarm)){
|
String personIsHubParam = term.replace("videoReqNum","ack_alarm");
|
String endDate = personIsHubParam.replace("eqiId","1");
|
if(isAddParams){queryParams+=",";}else { isAddParams = true; } // 查询条件分隔符
|
queryParams+=endDate;
|
}else if ("0".equalsIgnoreCase(ack_alarm)){
|
alarmValue = "0";
|
String personIsHubParam = term.replace("videoReqNum","ack_alarm");
|
String endDate = personIsHubParam.replace("eqiId",alarmValue);
|
if(isAddParams){queryParams+=",";}else { isAddParams = true; } // 查询条件分隔符
|
queryParams+=endDate;
|
}
|
}
|
// likePer 相似值的处理
|
if (esSearch.getLikePer() != null && esSearch.getLikePer()> 0 && esSearch.getLikePer() <= 100){
|
String startDate = range.replace("startDate",(esSearch.getLikePer()-0.5)*0.01+"");
|
String endDate = startDate.replace("endDate", "1.01");
|
endDate = endDate.replace("picDate","likePer");
|
if (isAddParams){queryParams+=",";} isAddParams = true; // 查询条件分隔符
|
queryParams+=endDate;
|
}
|
List<String> analyType = esSearch.getSdkType();
|
// sdkType {"terms":{"sdkType":["1","6","4","7","8"]}},
|
if (analyType == null || analyType.isEmpty()){
|
// sizeJson = sizeJson.replace("sdkTypes","");
|
}else { // {"term":{"sdkType":"1"}},
|
String analyJson = "";
|
if (isAddParams){queryParams+=",";} isAddParams = true;// 查询条件分隔符
|
if (analyType.size()>0){
|
for (int s=0;s <analyType.size();s++) {
|
for (int i =0;i<C_Es_Map.sdkValue.length;i++){
|
if (analyType.get(s).equalsIgnoreCase(C_Es_Map.sdkValue[i])){
|
analyType.set(s,C_Es_Map.esSdkType[i]);
|
}
|
}
|
}
|
}
|
String terms = C_Es_Map.terms;
|
String sdkValueJson = terms.replace("sdkValues", JSONArray.toJSONString(analyType));
|
queryParams += sdkValueJson;
|
}
|
|
String shouldJson = ""; // 多集群整合
|
// 是否报警库查询
|
if(esSearch.getRecordType()!=null && !esSearch.getRecordType().isEmpty()){
|
String recordType = esSearch.getRecordType();
|
String personIsHubValue = null;
|
if ("alarm".equalsIgnoreCase(recordType)){
|
personIsHubValue = "1";
|
}else if ("other".equalsIgnoreCase(recordType)){
|
personIsHubValue = "0";
|
}
|
if (personIsHubValue != null){
|
String personIsHubParam = term.replace("videoReqNum","personIsHub");
|
String endDate = personIsHubParam.replace("eqiId",personIsHubValue);
|
if(isAddParams){queryParams+=",";}else { isAddParams = true; } // 查询条件分隔符
|
queryParams+=endDate;
|
}
|
|
}else {
|
// 依据 底库查询
|
// 底库 数据记录
|
if(esSearch.getDataBaseList()!=null && !esSearch.getDataBaseList().isEmpty()){
|
List<String> dataBaseList = esSearch.getDataBaseList();
|
// 旧版本 单集群 执行 暂废 wp 19-02-15
|
/* String dbName = terms.replace("indeviceid","BaseName"+C_Es_Map.fieldRaw);
|
// List<String> lists = Arrays.asList(esSearch.getDataBaseList());
|
String termlist = dbName.replace("termlist",JSONObject.toJSONString(dataBaseList)); // String.join(",", esSearch.getDataBaseList())
|
if (isAddParams){queryParams+=",";}else{ isAddParams = true;} // 查询条件分隔符
|
queryParams+=termlist;*/
|
// 新版本 支持 多集群 多底库类型执行 启用 wp 19-02-15
|
for (int i = 0; i < dataBaseList.size(); i++) {
|
String dbNameMerge = dataBaseList.get(i);
|
String[] dbNameMerges = getCluAndBaseNameAndHub(dbNameMerge);
|
if (dbNameMerges.length == 3){
|
if (i != 0){shouldJson += ",";}
|
String boolJson = boolQueryJson.replace("SSS",dbNameMerges[0]);
|
boolJson = boolJson.replace("baseNameValue",dbNameMerges[1]);
|
boolJson = boolJson.replace("hubValue",dbNameMerges[2]);
|
shouldJson += boolJson;
|
}
|
}
|
}else{
|
log.info("es 查询条件中dataBaseList无:"+esSearch.getStartDate()+"<====>"+esSearch.getEndDate());
|
}
|
log.info("es 查询条件中时间未知:"+esSearch.getStartDate()+"<====>"+esSearch.getEndDate());
|
}
|
|
// 青年 截取 条件 查询
|
if(startAge!=null && endAge!=null){
|
String ageField = range.replace("picDate","Age");
|
String startage = ageField.replace("startDate",startAge.toString());
|
String endage = startage.replace("endDate",endAge.toString());
|
if (isAddParams){queryParams+=",";} isAddParams = true;// 查询条件分隔符
|
queryParams+=endage;
|
}else{
|
log.info("es 查询条件中age无:"+esSearch.getStartDate()+"<====>"+esSearch.getEndDate());
|
}
|
|
// 设备id 集合查询
|
if(esSearch.getIndeviceid()!=null && !esSearch.getIndeviceid().isEmpty() && (esSearch.getCluId()==null || esSearch.getCluId().isEmpty())){
|
String termlist = terms.replace("termlist",JSONObject.toJSONString(esSearch.getIndeviceid()) ); // String.join(",", esSearch.getIndeviceid())
|
if (isAddParams){queryParams+=",";} isAddParams = true; // 查询条件分隔符
|
queryParams+=termlist;
|
}else if ("monitorTree".equalsIgnoreCase(esSearch.getCluId())){
|
String termlist = terms.replace("termlist","[\"monitorTree\"]"); // String.join(",", esSearch.getIndeviceid())
|
if (isAddParams){queryParams+=",";} isAddParams = true; // 查询条件分隔符
|
queryParams+=termlist;
|
}else{
|
log.info("es 查询条件中IndeviceidList无:"+esSearch.getStartDate()+"<====>"+esSearch.getEndDate()+"esSearch.getCluId()"+esSearch.getCluId());
|
}
|
// contents 数据查询
|
if (esSearch.getContents()!=null&& esSearch.getContents().length>0
|
&&esSearch.getContent() != null && !esSearch.getContent().isEmpty() ){
|
List<Object> employerList = userService.getEmployerList(esSearch.getContent());
|
List<String> ids = null;
|
if (employerList != null){
|
ids = employerList.stream()
|
.map(p -> (((JSONObject)p).getString("id")))
|
.collect(Collectors.toList());
|
}
|
if (!StringUtils.isBlank(shouldJson)){shouldJson+=",";}
|
String matchauery = matchJson.replace("matchauery", esSearch.getContent()); // cont
|
shouldJson += matchauery;
|
if (ids != null && !ids.isEmpty()){
|
String personIds = terms.replace("videoReqNum", "personId");
|
String personJson = personIds.replace("termlist", JSONObject.toJSONString(ids));
|
shouldJson += (","+personJson);
|
}
|
}
|
shouldJson = shouldQueryJson.replace("boolQuerys", shouldJson);// 构建 should
|
sizeJson = sizeJson .replace("shouldParams",shouldJson); // 真正 添加 数据库的判断
|
|
if (esSearch.getContent()!= null && !esSearch.getContent().isEmpty()){
|
String scoreSort = "{\"_score\":{\"order\":\"desc\"}},";
|
sizeJson = sizeJson.replace("scoreSort",scoreSort);
|
}else {
|
sizeJson = sizeJson.replace("scoreSort","");
|
}
|
// sizeJson = sizeJson.replace("picDate","_score"); // 用来干啥的?
|
String queryJson = sizeJson.replace("addSearchParams", queryParams);
|
return queryJson;
|
}
|
// 设备记录 和 全部查询用到了
|
private String allQueryJson = "{" +
|
"\"from\": fromNum," +
|
"\"size\": sizeNum," +
|
"\"query\": {" +
|
"\"bool\": {" +
|
"\"must\": [addSearchParams,shouldParams]," +
|
"\"adjust_pure_negative\": true," +
|
"\"boost\": 1" +
|
"}" +
|
"}," +
|
"\"sort\": [" +
|
"scoreSort"+
|
"{\"picDate\": {\"order\": \"desc\"}}" +
|
"]" +
|
"}";
|
|
private String shouldQueryJson = "{\"bool\": {\"should\": [boolQuerys]}}";
|
|
private final String boolQueryJson = "{\"bool\":{\"must\":[" +
|
"{\"term\":{\"cluster_id\":{\"value\":\"SSS\"}}}," +
|
"{\"term\":{\"BaseName.raw\":{\"value\":\"baseNameValue\"}}}," +
|
"{\"term\":{\"personIsHub\":{\"value\":\"hubValue\"}}}]}}";
|
|
}
|