package com.ibeetl.admin.console.service;
|
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.ibeetl.admin.console.dao.XzxElectronicFenceDao;
|
import com.ibeetl.admin.console.dao.XzxPartnerFenceDao;
|
import com.ibeetl.admin.console.model.AreaModel;
|
import com.ibeetl.admin.console.model.XzxElectronicFenceModel;
|
import com.ibeetl.admin.console.model.XzxPartnerFenceModel;
|
import com.ibeetl.admin.console.util.HttpClientUtils;
|
import com.ibeetl.admin.core.entity.XzxCityPartner;
|
import com.ibeetl.admin.core.entity.XzxElectronicFence;
|
import com.ibeetl.admin.core.service.CoreBaseService;
|
import com.ibeetl.admin.core.util.PlatformException;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.entity.ContentType;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.util.EntityUtils;
|
import org.beetl.sql.core.engine.PageQuery;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.io.UnsupportedEncodingException;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
|
/**
|
* XzxElectronicFence Service
|
*/
|
|
@Service
|
@Transactional
|
public class XzxElectronicFenceService extends CoreBaseService<XzxElectronicFence>{
|
|
@Autowired private XzxElectronicFenceDao xzxElectronicFenceDao;
|
@Autowired private XzxPartnerFenceDao partnerFenceDao;
|
@Autowired
|
XzxCityPartnerService service;
|
public PageQuery<XzxElectronicFence>queryByCondition(PageQuery query){
|
PageQuery ret = xzxElectronicFenceDao.queryByCondition(query);
|
queryListAfter(ret.getList());
|
return ret;
|
}
|
|
public List<String> queryTownListByArea(String townCode,String eleId){
|
return xzxElectronicFenceDao.queryTownListByArea(townCode,eleId);
|
}
|
public boolean queryPartnerAreaIds(String partnerId,String townId){
|
boolean flag = false;
|
int num = xzxElectronicFenceDao.queryPartnerAreaIds(partnerId,townId);
|
if(num>0){
|
flag = true;
|
}
|
return flag;
|
}
|
|
public String queryAreaTownId(String keyword){
|
return xzxElectronicFenceDao.queryAreaTownId(keyword);
|
}
|
public List<XzxElectronicFenceModel> queryFenceList(XzxElectronicFenceModel model){
|
List<String> partnerIdList = service.queryPartnerByCurrent();
|
model.setPartnerIdList(partnerIdList);
|
List<XzxElectronicFenceModel> list = xzxElectronicFenceDao.queryFenceList(model);
|
for (XzxElectronicFenceModel model1:list) {
|
String keyword = model1.getKeyword();
|
if(null!=keyword&&!"".equals(keyword)) {
|
AreaModel m = new AreaModel();
|
m.setName(keyword.trim());
|
AreaModel map = service.queryAreaLongiLati(m);
|
if(map!=null){
|
model1.setLongitude(map.getLongitude());
|
model1.setLatitude(map.getLatitude());
|
}
|
|
}else{
|
String longLatiArr = model1.getLongLatiArr();
|
String longi="";
|
String lati="";
|
List<Map<String,Object>> longlaL = new ArrayList<>();
|
if(null!=longLatiArr&&!"".equals(longLatiArr)){
|
String[] liststr = longLatiArr.split(";");
|
//List<Object> = JSON.parseArray(longLatiArr);
|
for (String str : liststr) {
|
//Map<String, Object> ret = (Map<String, Object>) object;//取出list里面的值转为map
|
Map<String, Object> ageMap = new HashMap<String, Object>();
|
//String[] objStrs = object.split(",");
|
String[] stt = str.split(",");
|
longi = stt[0];
|
lati = stt[1];
|
ageMap.put("longitude",longi);
|
ageMap.put("latitude",lati);
|
longlaL.add(ageMap);
|
model1.setLongitude(longi);
|
model1.setLatitude(lati);
|
//break;
|
}
|
String jsonString = JSON.toJSONString(longlaL);
|
model1.setLongLatiArr(jsonString);
|
}
|
}
|
|
}
|
return list;
|
}
|
public XzxElectronicFence queryElectronic(XzxElectronicFenceModel info){
|
return xzxElectronicFenceDao.queryElectronic(info);
|
}
|
public void batchDelXzxElectronicFence(List<Long> ids){
|
try {
|
xzxElectronicFenceDao.batchDelXzxElectronicFenceByIds(ids);
|
} catch (Exception e) {
|
throw new PlatformException("批量删除XzxElectronicFence失败", e);
|
}
|
}
|
|
public String queryLongiLatiForTownName(String key,String location,String longiLatiStr){
|
Map<String,String> map = new HashMap<>();
|
String district="";
|
if(null!=longiLatiStr){
|
map.put("location",longiLatiStr);
|
map.put("batch","true");
|
}else{
|
map.put("location",location);
|
}
|
if(null!=key&&!"".equals(key)){
|
map.put("key",key);
|
}else{
|
map.put("key","a64d28b2e036f4688b05916ef321b17d");
|
}
|
|
String url ="https://restapi.amap.com/v3/geocode/regeo";
|
String result = HttpClientUtils.doGet(url,map);
|
Map maps = (Map) JSON.parse(result);
|
String infocode = maps.get("infocode").toString();
|
if(infocode.equals("10000")){
|
System.out.println(maps);
|
String regeocode="";
|
if(null!=maps.get("regeocodes")){
|
List<Object> regeocodes = (List)maps.get("regeocodes");
|
//regeocode=checkAddressIp(regeocodes);
|
district = checkAddressIp(regeocodes);
|
}else{
|
regeocode = maps.get("regeocode").toString();
|
Map maps1 = (Map) JSON.parse(regeocode);
|
String addressComponent = maps1.get("addressComponent").toString();
|
Map maps2 = (Map) JSON.parse(addressComponent);
|
district = maps2.get("district").toString();
|
}
|
}
|
return district;
|
}
|
|
private String checkAddressIp(List<Object> regeocodes){
|
StringBuffer location=new StringBuffer();
|
String address="";
|
for (int i=0; i<regeocodes.size();i++) {
|
Map maps1 = (Map) JSON.parse(regeocodes.get(i).toString());
|
if(null!=address&&!"".equals(address)){
|
String component = maps1.get("addressComponent").toString();
|
Map mapsx = (Map) JSON.parse(component);
|
String addressNew= mapsx.get("district").toString();
|
if(address.equals(addressNew)){
|
continue;
|
}else{
|
location.append(",").append(i+1).append("_").append(address);
|
}
|
}else{
|
String addressComponent = maps1.get("addressComponent").toString();
|
Map maps2 = (Map) JSON.parse(addressComponent);
|
address= maps2.get("district").toString();
|
location.append(i+1).append("_").append(address);
|
}
|
|
}
|
return location.toString();
|
}
|
public String deleteGDFence(XzxElectronicFence info,String url,XzxCityPartner partner){
|
String uri = url+"?key="+partner.getPartnerKey()+"&gid="+info.getGid()+"&method=delete";
|
String result = HttpClientUtils.doPost(uri,new HashMap<>());
|
return result;
|
}
|
public String updateGDFence(XzxElectronicFence info, XzxPartnerFenceModel pfInfo,String longiLatiStr,String gid){
|
XzxCityPartner partner = service.queryById(pfInfo.getPartnerId());
|
String key = partner.getPartnerKey();
|
|
String uri = "https://restapi.amap.com/v4/geofence/meta?key="+key+"&gid="+gid+"&method=patch";
|
System.out.println("uri===="+uri);
|
HttpPost httpPost = new HttpPost(uri);
|
Map<String,String> map = new HashMap<>();;
|
map.put("name",info.getFenceName());
|
if(null!=longiLatiStr&&!"".equals(longiLatiStr)){
|
map.put("points",longiLatiStr);
|
}else{
|
map.put("center",info.getLongitude()+","+info.getLatitude());
|
map.put("radius",String.valueOf(info.getRadius()));
|
}
|
//map.put("enable","true");
|
map.put("valid_time","2054-12-30");
|
map.put("repeat","Mon,Tues,Wed,Thur,Fri,Sat,Sun");
|
//map.put("time","00:00,11:59;13:00,20:59");
|
map.put("desc",info.getFenceName());
|
map.put("alert_condition","enter");
|
String param= JSON.toJSONString(map);
|
System.out.println(param);
|
//String result = HttpClientUtils.doPost(uri,map);
|
String result = HttpClientUtils.doPost(uri,param, ContentType.APPLICATION_JSON);
|
//String result = HttpClientUtils.doPost(uri,map);
|
return result;
|
}
|
public List<String> queryODByArea(String townCode){
|
return xzxElectronicFenceDao.queryODByArea(townCode);
|
}
|
public String createGDFence(XzxElectronicFence info, XzxPartnerFenceModel pfInfo,String url,String longiLatiStr){
|
|
|
XzxCityPartner partner = service.queryById(pfInfo.getPartnerId());
|
String key = partner.getPartnerKey();
|
url =url+key;
|
HttpPost httpPost = new HttpPost(url);
|
Map<String,String> map = new HashMap<>();;
|
map.put("name",info.getFenceName());
|
if(null!=longiLatiStr&&!"".equals(longiLatiStr)){
|
map.put("points",longiLatiStr);
|
}else{
|
map.put("center",info.getLongitude()+","+info.getLatitude());
|
map.put("radius",String.valueOf(info.getRadius()));
|
}
|
|
|
map.put("enable","true");
|
map.put("valid_time","2054-12-30");
|
map.put("repeat","Mon,Tues,Wed,Thur,Fri,Sat,Sun");
|
//map.put("time","00:00,11:59;13:00,20:59");
|
map.put("desc",info.getFenceName());
|
map.put("alert_condition","enter;leave");
|
System.out.println(map);
|
String param= JSON.toJSONString(map);
|
String result = HttpClientUtils.doPost(url,param, ContentType.APPLICATION_JSON);
|
|
return result;
|
}
|
|
}
|