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
package com.yb.service;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.houbb.opencc4j.core.impl.ZhConvertBootstrap;
import com.yb.config.XzConfig;
import com.yb.helper.RsaHelper;
import com.yb.util.EntityHttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class IntentionService {
    @Autowired
    private XzConfig xzConfig;
    @Autowired
    private AgentService agentService;
    @Autowired
    private XzService xzService;
    @Autowired
    private EntityHttpUtil entityHttpUtil;
 
    public HashMap<String, Object> getFunctionMatch(String orderString, Map<String, String> allParams) throws Exception {
        HashMap<String, Object> mapObj = new HashMap<String, Object>();
        JSONObject orderObject = JSON.parseObject(orderString);
        String name = JSONObject.parseObject(orderObject.getString("args")).getString("place");
        String func = orderObject.getString("name");
        String history = orderObject.getString("history");
 
        if (func.equals(xzConfig.funcName[0])) {
            mapObj = getPoiMap(orderObject, func);
        } else if (func.equals(xzConfig.funcName[1])) {
            mapObj = getAroundPoi(orderObject, func);
        } else if (func.equals(xzConfig.funcName[2])) {
            mapObj = getQueryMeta(orderObject, func);
        } else if (func.equals(xzConfig.funcName[3])) {
            mapObj = getQueryRelationship(orderObject, func);
        } else if (func.equals(xzConfig.funcName[4])) {
            mapObj = getCombatSimulate(orderObject, func);
        } else if (func.equals(xzConfig.funcName[5])) {
            mapObj = getBatEnv(orderObject, func, allParams);
        }
        return mapObj;
 
    }
 
    private HashMap<String, Object> getCombatSimulate(JSONObject orderObject, String func) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("func", func);
        return hashMap;
    }
 
    private HashMap<String, Object> getBatEnv(JSONObject orderObject, String func, Map<String, String> allParams) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("func", func);
//        List list = orderObject.get("history");
        System.out.println("rec history1:" + allParams);
        String history = allParams.get("history");
        System.out.println("rec history1:" + history);
        String val = entityHttpUtil.postAgentMessage(xzConfig.battleReport, allParams);
        JSONObject js = JSONObject.parseObject(val);
        if (js == null) {
            hashMap.put("msg", "报告生成失败");
            return hashMap;
        }
        System.out.println("rec history1:" + val);
        hashMap.put("msg", js.getString("content"));
 
        return hashMap;
    }
 
    //查看某个区域内各个实体/对象之间的关系
    private HashMap<String, Object> getQueryRelationship(JSONObject orderObject, String func) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("func", func);
        JSONObject argsObject = JSON.parseObject(orderObject.getString("args"));
        String place = argsObject.getString("place");
        HashMap<String, Object> poiMap = agentService.getQueryRelationship(place, xzConfig.queryFiled);
        String key = "msg";
        String msg = (String) poiMap.get(key);
        hashMap.put("msg", msg);
        String center = (String) poiMap.get("center");
        if(center != null){
            hashMap.put("data", poiMap);
        }
 
        return hashMap;
    }
 
    //查询数据统计
    private HashMap<String, Object> getQueryMeta(JSONObject orderObject, String func) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("func", func);
        String filedString = xzService.getQueryMeta(xzConfig.layerId, xzConfig.dbid, xzConfig.typeFiled, xzConfig.queryEntity);
        JSONObject metaObject = JSON.parseObject(filedString);
        JSONArray dataArray = metaObject.getJSONArray("data");
 
        String markDown = "| 数据类型 | 数据量 |\n|------|------|\n";
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject dataObj = dataArray.getJSONObject(i);
            System.out.println("rec queryMeta:" + dataObj);
            HashMap<String, Object> listhash = new HashMap<>();
            String name = dataObj.getString(xzConfig.typeFiled);
            String count = dataObj.getString("count_" + xzConfig.typeFiled);
            ArrayList filedList = getQueryByFiledList(name);
            listhash.put("name", name);
            listhash.put("ids", filedList);
            list.add(listhash);
            markDown += "| " + name + " | " + count + " |\n";
        }
        hashMap.put("ids", list);
        hashMap.put("data", markDown);
        return hashMap;
 
    }
 
 
    //    关键字查询分类查询数据量
    private ArrayList getQueryByFiledList(String name) throws Exception {
        String obj = xzConfig.typeFiled + " = '" + name + "'" + "";
        String encrypt = RsaHelper.encrypt(xzService.getPublickey(), obj);
        HashMap hashMap = xzService.getHashMap(xzConfig.layerId, xzConfig.dbid, "1", "0", encrypt, null);
        String val = entityHttpUtil.getPostMessage(xzConfig.queryEntity, hashMap);
        ArrayList list = new ArrayList();
        String fObj = JSONObject.parseObject(val).getString("data");
        System.out.println("rec queryMeta:" + fObj);
        if (fObj != null) {
            JSONArray fArray = JSONArray.parseArray(JSONObject.parseObject(fObj).getString("items"));
            if (fArray.size() > 0) {
                for (int i = 0; i < fArray.size(); i++) {
                    String sid = JSONObject.parseObject(fArray.get(i).toString()).getString("seid");
                    list.add(sid);
                }
            }
        }
        return list;
    }
 
    //根据范围查询数据
    private HashMap<String, Object> getAroundPoi(JSONObject orderString, String func) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("func", func);
        JSONObject argsObject = JSON.parseObject(orderString.getString("args"));
        String place = argsObject.getString("place");
        String type = argsObject.getString("attris");
        String dis = argsObject.getString("redius");
        int radius = 5000;
        if (dis != null && dis.contains("km")) {
            dis = dis.replace("km", "");
            radius = Integer.parseInt(dis) * 1000;
        }
        if (dis != null && dis.contains("m")) {
            dis = dis.replace("m", "");
            radius = Integer.parseInt(dis);
        }
        if (dis != null && dis.contains("千米")) {
            dis = dis.replace("千米", "");
            radius = Integer.parseInt(dis) * 1000;
        }
        if (dis != null && dis.contains("米")) {
            dis = dis.replace("米", "");
            radius = Integer.parseInt(dis);
        }
 
        String aroundPoi = agentService.getAgentAroundPoi(place, type, radius, xzConfig.queryFiled, xzConfig.typeFiled);
        if (aroundPoi == "" || aroundPoi.isEmpty()) {
            aroundPoi = agentService.getAgentAroundPoi(getTraditional(place), type, radius, xzConfig.queryFiled, xzConfig.typeFiled);
            if (aroundPoi == "" || aroundPoi.isEmpty()) {
                String msg = "未查询到" + place + "相关数据。请提供更加详细数据。";
                hashMap.put("msg", msg);
                return hashMap;
            }
 
        }
        JSONArray dataArray = JSONArray.parseArray(aroundPoi);
        hashMap.put("data", dataArray);
        if (type.contains("全部目标")) {
            if (dataArray.size() == 0) {
                hashMap.put("data", dataArray);
                String msg = "已查询到" + place + "内的" + type + "相关数据。共查询到" + dataArray.size() + "条数据。";
                hashMap.put("msg", msg);
                return hashMap;
            }
            ArrayList<HashMap<String, Object>> listArray = new ArrayList<>();
            String markDown = "| 数据类型 | 数据量 |\n|------|------|\n";
            ArrayList<HashMap<String, Object>> list = new ArrayList<>();
            List<String> cName = new ArrayList<>();
            List<Integer> cNum = new ArrayList<>();
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject dataObj = dataArray.getJSONObject(i);
                String fclass1 = dataObj.getString("fclass_1");
                String ids = dataObj.getString("seid");
                if (cName.contains(fclass1)) {
                    for (int j = 0; j < cName.size(); j++) {
                        String name = cName.get(j);
                        if (name.equals(fclass1)) {
                            System.out.println("目标统计 = " + cName.get(j) + fclass1);
                            int num = cNum.get(j) + 1;
                            cNum.set(j, num);
 
                            HashMap<String, Object> itemObj = list.get(j);
 
                            for (String key : itemObj.keySet()) {
                                if (key.contains("ids")) {
                                    ArrayList list_ids = (ArrayList) itemObj.get(key);
                                    list_ids.add(ids);
                                    itemObj.put(key,list_ids);
                                }
 
                            }
 
                        }
                    }
 
 
                } else {
                    cName.add(fclass1);
                    cNum.add(1);
                    HashMap<String, Object> listhash = new HashMap<>();
                    ArrayList listId = new ArrayList();
                    listId.add(ids);
                    listhash.put("name", fclass1);
                    listhash.put("ids", listId);
                    list.add(listhash);
                }
            }
 
            for (int i = 0; i < cName.size(); i++) {
                markDown += "| " + cName.get(i) + " | " + cNum.get(i) + " |\n";
            }
            hashMap.put("ids", list);
            hashMap.put("data", markDown);
            hashMap.put("func", "queryMeta");
            return hashMap;
        }
 
 
        String msg = "已查询到" + place + "内的" + type + "相关数据。共查询到" + dataArray.size() + "条数据。";
        hashMap.put("msg", msg);
        return hashMap;
 
 
    }
 
    public String getTraditional(String place) {
        ZhConvertBootstrap zhConvertBootstrap = ZhConvertBootstrap.newInstance();
        return zhConvertBootstrap.toTraditional(place);
 
    }
 
    //查询点位
    public HashMap<String, Object> getPoiMap(JSONObject orderString, String func) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        JSONObject argsObject = JSON.parseObject(orderString.getString("args"));
        String place = argsObject.getString("place");
        String poiMap = agentService.getAgentPoiMap(place, xzConfig.queryFiled);
        JSONObject poiObj = JSONObject.parseObject(poiMap);
        if (poiMap == "" || poiMap.isEmpty()) {
            poiMap = agentService.getAgentPoiMap(getTraditional(place), xzConfig.queryFiled);
            if (poiMap == "" || poiMap.isEmpty()) {
                String msg = "未查询到" + place + "相关数据。请提供更加详细数据。";
                hashMap.put("msg", msg);
                return hashMap;
            }
        }
        System.out.println("poiMap: " + poiObj);
        hashMap.put("func", func);
        hashMap.put("data", poiObj);
        String msg = "已查询到" + place + "相关数据。" + place + "数据已在地图上显示";
        hashMap.put("msg", msg);
        return hashMap;
    }
 
 
}