Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
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
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;
    }
 
}