|
|
@ -1,9 +1,11 @@ |
|
|
package com.bw.opai.app.service.impl; |
|
|
package com.bw.opai.app.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
import java.util.HashMap; |
|
|
import java.util.HashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
import org.apache.http.HttpResponse; |
|
|
import org.apache.http.HttpResponse; |
|
|
import org.apache.http.auth.AuthScope; |
|
|
import org.apache.http.auth.AuthScope; |
|
|
@ -22,11 +24,14 @@ import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
import com.bw.opai.app.dto.App; |
|
|
import com.bw.opai.app.dto.App; |
|
|
|
|
|
import com.bw.opai.app.dto.Task; |
|
|
import com.bw.opai.app.entity.AppResultDoc; |
|
|
import com.bw.opai.app.entity.AppResultDoc; |
|
|
import com.bw.opai.app.mapper.AppMapper; |
|
|
import com.bw.opai.app.mapper.AppMapper; |
|
|
|
|
|
import com.bw.opai.app.mapper.TaskMapper; |
|
|
import com.bw.opai.app.service.AppService; |
|
|
import com.bw.opai.app.service.AppService; |
|
|
import com.bw.opai.common.Res; |
|
|
import com.bw.opai.common.Res; |
|
|
import com.bw.opai.utils.Constants; |
|
|
import com.bw.opai.utils.Constants; |
|
|
@ -43,6 +48,8 @@ public class AppServiceImpl implements AppService { |
|
|
|
|
|
|
|
|
private final AppMapper appMapper; |
|
|
private final AppMapper appMapper; |
|
|
|
|
|
|
|
|
|
|
|
private final TaskMapper taskMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${elasticsearch.url}") |
|
|
@Value("${elasticsearch.url}") |
|
|
private String esUrl; |
|
|
private String esUrl; |
|
|
@ -111,16 +118,13 @@ public class AppServiceImpl implements AppService { |
|
|
if (appId == null) { |
|
|
if (appId == null) { |
|
|
return Res.fail("应用ID不能为空"); |
|
|
return Res.fail("应用ID不能为空"); |
|
|
} |
|
|
} |
|
|
//任务id校验 |
|
|
|
|
|
String taskId = param.get("taskId") == null||param.get("taskId").toString().equals("")? null : param.get("taskId").toString(); |
|
|
|
|
|
if (taskId == null) { |
|
|
|
|
|
return Res.fail("任务ID不能为空"); |
|
|
|
|
|
} |
|
|
|
|
|
// 伪示例:根据 ID 查询应用并启动 |
|
|
// 伪示例:根据 ID 查询应用并启动 |
|
|
App app = appMapper.selectById(appId); |
|
|
App app = appMapper.selectById(appId); |
|
|
if (app == null || app.getDel() != 0) { |
|
|
if (app == null || app.getDel() != 0) { |
|
|
return Res.fail("未找到对应应用"); |
|
|
return Res.fail("未找到对应应用"); |
|
|
} |
|
|
} |
|
|
|
|
|
String taskId = UUID.randomUUID().toString().replace("-", Constants.EMPTY); |
|
|
|
|
|
param.put(Constants.TASKID, taskId); |
|
|
//应用调用 |
|
|
//应用调用 |
|
|
String appUrl = app.getApi(); |
|
|
String appUrl = app.getApi(); |
|
|
String downloadRes = DownLoadUtil.doPost(appUrl, JSONObject.toJSONString(param)); |
|
|
String downloadRes = DownLoadUtil.doPost(appUrl, JSONObject.toJSONString(param)); |
|
|
@ -129,40 +133,196 @@ public class AppServiceImpl implements AppService { |
|
|
log.error("启动应用请求异常, param={},download error:{}", param, downloadRes); |
|
|
log.error("启动应用请求异常, param={},download error:{}", param, downloadRes); |
|
|
return Res.fail("启动应用请求异常"); |
|
|
return Res.fail("启动应用请求异常"); |
|
|
} |
|
|
} |
|
|
//任务记录写入es |
|
|
|
|
|
long now = System.currentTimeMillis(); |
|
|
|
|
|
|
|
|
|
|
|
AppResultDoc esEntity = new AppResultDoc(); |
|
|
|
|
|
esEntity.setAppId(app.getId()); |
|
|
|
|
|
esEntity.setAppName(app.getName()); |
|
|
|
|
|
//用户token -- 先给默认值 |
|
|
|
|
|
esEntity.setUserId("1"); |
|
|
|
|
|
|
|
|
|
|
|
esEntity.setTask(param); |
|
|
|
|
|
esEntity.setResult(null); |
|
|
|
|
|
|
|
|
|
|
|
esEntity.setCreateTime(now); |
|
|
|
|
|
esEntity.setFinishTime(null); |
|
|
|
|
|
// 运行中 0 是运行中,1是完成,2是失败 |
|
|
|
|
|
esEntity.setStatus(0); |
|
|
|
|
|
esEntity.setDel(0); |
|
|
|
|
|
esEntity.setLastEdit(now); |
|
|
|
|
|
|
|
|
|
|
|
// ---------- 写入 ES ---------- |
|
|
|
|
|
boolean isSuccess = save(esEntity, taskId); |
|
|
|
|
|
if(!isSuccess) { |
|
|
|
|
|
//记录写入失败,直接返回错误信息 |
|
|
|
|
|
log.error("启动应用记录写入异常, param={}", param); |
|
|
|
|
|
return Res.fail("启动应用记录写入异常"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//任务录入表中 |
|
|
|
|
|
Task task = new Task(); |
|
|
|
|
|
task.setId(taskId); |
|
|
|
|
|
task.setCreateTime(LocalDateTime.now()); |
|
|
|
|
|
task.setAppId(appId); |
|
|
|
|
|
task.setDel(0); |
|
|
|
|
|
task.setStatus(0); |
|
|
|
|
|
task.setUserId("1"); |
|
|
|
|
|
taskMapper.insert(task); |
|
|
// ---------- 返回 ---------- |
|
|
// ---------- 返回 ---------- |
|
|
return Res.ok(esEntity); |
|
|
|
|
|
|
|
|
return Res.ok(task); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
log.error("启动应用未知异常, param={}", param, e); |
|
|
log.error("启动应用未知异常, param={}", param, e); |
|
|
return Res.fail("启动应用未知异常"); |
|
|
return Res.fail("启动应用未知异常"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public Res<?> getMyTasks(Integer page, Integer size,Boolean isData) { |
|
|
|
|
|
|
|
|
|
|
|
if (page == null || page < 1) { |
|
|
|
|
|
page = 1; |
|
|
|
|
|
} |
|
|
|
|
|
if (size == null || size < 1 || size > 100) { |
|
|
|
|
|
size = 10; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 从登录上下文获取 userId(你项目里已有) 先写死 |
|
|
|
|
|
String userId = "1"; |
|
|
|
|
|
if (userId == null || userId.trim().equals("")) { |
|
|
|
|
|
return Res.fail("未获取到用户信息"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
Page<Task> pageParam = new Page<>(page, size); |
|
|
|
|
|
|
|
|
|
|
|
LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>(); |
|
|
|
|
|
wrapper.eq(Task::getDel, 0) |
|
|
|
|
|
.eq(userId != null, Task::getUserId, userId) |
|
|
|
|
|
.orderByDesc(Task::getCreateTime); |
|
|
|
|
|
|
|
|
|
|
|
Page<Task> result = taskMapper.selectPage(pageParam, wrapper); |
|
|
|
|
|
if(isData) { |
|
|
|
|
|
log.info("任务关联数据开始~"); |
|
|
|
|
|
List<Task> tasks = result.getRecords(); |
|
|
|
|
|
for (Task task : tasks) { |
|
|
|
|
|
String taskId = task.getId(); |
|
|
|
|
|
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
|
|
|
|
|
credentialsProvider.setCredentials( |
|
|
|
|
|
AuthScope.ANY, |
|
|
|
|
|
new UsernamePasswordCredentials(esUser, esPassword) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
CloseableHttpClient httpClient = null; |
|
|
|
|
|
try { |
|
|
|
|
|
if (esUser != null && !esUser.trim().equals("")) { |
|
|
|
|
|
httpClient = HttpClients.custom() |
|
|
|
|
|
.setDefaultCredentialsProvider(credentialsProvider) |
|
|
|
|
|
.build(); |
|
|
|
|
|
} else { |
|
|
|
|
|
httpClient = HttpClients.custom().build(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ================== 构建查询 DSL ================== |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> query = new HashMap<String, Object>(); |
|
|
|
|
|
// must 条件 |
|
|
|
|
|
List<Map<String, Object>> mustList = new ArrayList<Map<String, Object>>(); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> termDel = new HashMap<String, Object>(); |
|
|
|
|
|
termDel.put("del", 0); |
|
|
|
|
|
Map<String, Object> termDelWrap = new HashMap<String, Object>(); |
|
|
|
|
|
termDelWrap.put("term", termDel); |
|
|
|
|
|
mustList.add(termDelWrap); |
|
|
|
|
|
//任务id查询条件 |
|
|
|
|
|
Map<String, Object> termTaskId = new HashMap<String, Object>(); |
|
|
|
|
|
termTaskId.put("taskId", taskId); |
|
|
|
|
|
Map<String, Object> termTaskIdWrap = new HashMap<String, Object>(); |
|
|
|
|
|
termTaskIdWrap.put("term", termTaskId); |
|
|
|
|
|
mustList.add(termTaskIdWrap); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> bool = new HashMap<String, Object>(); |
|
|
|
|
|
bool.put("must", mustList); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> queryBody = new HashMap<String, Object>(); |
|
|
|
|
|
queryBody.put("bool", bool); |
|
|
|
|
|
|
|
|
|
|
|
query.put("query", queryBody); |
|
|
|
|
|
|
|
|
|
|
|
// sort |
|
|
|
|
|
List<Map<String, Object>> sortList = new ArrayList<Map<String, Object>>(); |
|
|
|
|
|
Map<String, Object> order = new HashMap<String, Object>(); |
|
|
|
|
|
order.put("order", "desc"); |
|
|
|
|
|
Map<String, Object> sortField = new HashMap<String, Object>(); |
|
|
|
|
|
sortField.put("createTime", order); |
|
|
|
|
|
sortList.add(sortField); |
|
|
|
|
|
query.put("sort", sortList); |
|
|
|
|
|
log.info("任务数据查询条件:{}",JSONObject.toJSONString(query)); |
|
|
|
|
|
|
|
|
|
|
|
// ================== 发起 HTTP 请求 ================== |
|
|
|
|
|
StringBuffer host = new StringBuffer(); |
|
|
|
|
|
host.append(esUrl) |
|
|
|
|
|
.append("/") |
|
|
|
|
|
.append(indexName) |
|
|
|
|
|
.append("/_search"); |
|
|
|
|
|
|
|
|
|
|
|
HttpPost httpPost = new HttpPost(host.toString()); |
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/json"); |
|
|
|
|
|
|
|
|
|
|
|
StringEntity entity = new StringEntity( |
|
|
|
|
|
JSONObject.toJSONString(query), |
|
|
|
|
|
ContentType.APPLICATION_JSON |
|
|
|
|
|
); |
|
|
|
|
|
httpPost.setEntity(entity); |
|
|
|
|
|
|
|
|
|
|
|
HttpResponse response = httpClient.execute(httpPost); |
|
|
|
|
|
int statusCode = response.getStatusLine().getStatusCode(); |
|
|
|
|
|
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); |
|
|
|
|
|
|
|
|
|
|
|
if (statusCode != 200) { |
|
|
|
|
|
log.error("ES 查询失败 status={}, body={}", statusCode, responseBody); |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ================== 解析返回 ================== |
|
|
|
|
|
JSONObject json = JSONObject.parseObject(responseBody); |
|
|
|
|
|
JSONObject hits = json.getJSONObject("hits"); |
|
|
|
|
|
|
|
|
|
|
|
List<AppResultDoc> list = new ArrayList<AppResultDoc>(); |
|
|
|
|
|
JSONArray hitList = hits.getJSONArray("hits"); |
|
|
|
|
|
for (int i = 0; i < hitList.size(); i++) { |
|
|
|
|
|
JSONObject source = hitList.getJSONObject(i).getJSONObject("_source"); |
|
|
|
|
|
AppResultDoc item = source.toJavaObject(AppResultDoc.class); |
|
|
|
|
|
list.add(item); |
|
|
|
|
|
} |
|
|
|
|
|
task.setData(list); |
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("查询用户任务失败 userId={}", userId, e); |
|
|
|
|
|
continue; |
|
|
|
|
|
} finally { |
|
|
|
|
|
if (httpClient != null) { |
|
|
|
|
|
try { |
|
|
|
|
|
httpClient.close(); |
|
|
|
|
|
} catch (Exception ignored) {} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
log.info("任务查询成功"); |
|
|
|
|
|
return Res.page(result); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("查询任务列表失败", e); |
|
|
|
|
|
return Res.fail("查询任务列表失败"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
@Override |
|
|
|
|
|
public Res<?> dataSave(AppResultDoc appData) { |
|
|
|
|
|
String docId = UUID.randomUUID().toString().replace("-", Constants.EMPTY); |
|
|
|
|
|
// ---------- 写入 ES ---------- |
|
|
|
|
|
boolean isSuccess = save(appData,docId); |
|
|
|
|
|
if(!isSuccess) { |
|
|
|
|
|
//记录写入失败,直接返回错误信息 |
|
|
|
|
|
log.error("启动应用记录写入异常, appData={}", appData); |
|
|
|
|
|
Task task = new Task(); |
|
|
|
|
|
task.setId(appData.getTaskId()); |
|
|
|
|
|
task.setStatus(appData.getStatus()); |
|
|
|
|
|
task.setFinishTime(LocalDateTime.now()); |
|
|
|
|
|
task.setStatus(2); |
|
|
|
|
|
task.setLastEdit(LocalDateTime.now()); |
|
|
|
|
|
taskMapper.updateById(task); |
|
|
|
|
|
return Res.fail("应用数据写入异常"); |
|
|
|
|
|
}else { |
|
|
|
|
|
log.info("任务状态更新"); |
|
|
|
|
|
Task task = new Task(); |
|
|
|
|
|
task.setId(appData.getTaskId()); |
|
|
|
|
|
task.setStatus(appData.getStatus()); |
|
|
|
|
|
task.setFinishTime(LocalDateTime.now()); |
|
|
|
|
|
task.setStatus(appData.getStatus()); |
|
|
|
|
|
task.setLastEdit(LocalDateTime.now()); |
|
|
|
|
|
taskMapper.updateById(task); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Res.ok(appData); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 节点数据持久化 |
|
|
* 节点数据持久化 |
|
|
* @param esEntity |
|
|
* @param esEntity |
|
|
@ -204,10 +364,10 @@ public class AppServiceImpl implements AppService { |
|
|
int code = 201; |
|
|
int code = 201; |
|
|
int updateCode = 200; |
|
|
int updateCode = 200; |
|
|
if (statusCode == code) { |
|
|
if (statusCode == code) { |
|
|
log.info("数据成功写入到索引:{},文档ID:{},appid:{},userId:{}",indexName,docId,esEntity.getAppId(),esEntity.getUserId()); |
|
|
|
|
|
|
|
|
log.info("数据成功写入到索引:{},文档ID:{},appid:{}",indexName,docId,esEntity.getAppId()); |
|
|
return true; |
|
|
return true; |
|
|
}else if(statusCode == updateCode){ |
|
|
}else if(statusCode == updateCode){ |
|
|
log.info("数据成功更新到索引:{},文档ID:{},appid:{},userId:{}",indexName,docId,esEntity.getAppId(),esEntity.getUserId()); |
|
|
|
|
|
|
|
|
log.info("数据成功更新到索引:{},文档ID:{},appid:{}",indexName,docId,esEntity.getAppId()); |
|
|
return true; |
|
|
return true; |
|
|
} else { |
|
|
} else { |
|
|
log.error("数据写入失败:{},文档ID:{},appid:{},es响应内容:{}",indexName,docId,esEntity.getAppId(),responseBody); |
|
|
log.error("数据写入失败:{},文档ID:{},appid:{},es响应内容:{}",indexName,docId,esEntity.getAppId(),responseBody); |
|
|
@ -225,134 +385,4 @@ public class AppServiceImpl implements AppService { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public Res<?> getMyTasks(Integer page, Integer size) { |
|
|
|
|
|
|
|
|
|
|
|
if (page == null || page < 1) { |
|
|
|
|
|
page = 1; |
|
|
|
|
|
} |
|
|
|
|
|
if (size == null || size < 1 || size > 100) { |
|
|
|
|
|
size = 10; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 从登录上下文获取 userId(你项目里已有) 先写死 |
|
|
|
|
|
String userId = "1"; |
|
|
|
|
|
if (userId == null || userId.trim().equals("")) { |
|
|
|
|
|
return Res.fail("未获取到用户信息"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
|
|
|
|
|
credentialsProvider.setCredentials( |
|
|
|
|
|
AuthScope.ANY, |
|
|
|
|
|
new UsernamePasswordCredentials(esUser, esPassword) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
CloseableHttpClient httpClient = null; |
|
|
|
|
|
try { |
|
|
|
|
|
if (esUser != null && !esUser.trim().equals("")) { |
|
|
|
|
|
httpClient = HttpClients.custom() |
|
|
|
|
|
.setDefaultCredentialsProvider(credentialsProvider) |
|
|
|
|
|
.build(); |
|
|
|
|
|
} else { |
|
|
|
|
|
httpClient = HttpClients.custom().build(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ================== 构建查询 DSL ================== |
|
|
|
|
|
int from = (page - 1) * size; |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> query = new HashMap<String, Object>(); |
|
|
|
|
|
query.put("from", from); |
|
|
|
|
|
query.put("size", size); |
|
|
|
|
|
|
|
|
|
|
|
// must 条件 |
|
|
|
|
|
List<Map<String, Object>> mustList = new ArrayList<Map<String, Object>>(); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> termUser = new HashMap<String, Object>(); |
|
|
|
|
|
termUser.put("userId", userId); |
|
|
|
|
|
Map<String, Object> termUserWrap = new HashMap<String, Object>(); |
|
|
|
|
|
termUserWrap.put("term", termUser); |
|
|
|
|
|
mustList.add(termUserWrap); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> termDel = new HashMap<String, Object>(); |
|
|
|
|
|
termDel.put("del", 0); |
|
|
|
|
|
Map<String, Object> termDelWrap = new HashMap<String, Object>(); |
|
|
|
|
|
termDelWrap.put("term", termDel); |
|
|
|
|
|
mustList.add(termDelWrap); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> bool = new HashMap<String, Object>(); |
|
|
|
|
|
bool.put("must", mustList); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> queryBody = new HashMap<String, Object>(); |
|
|
|
|
|
queryBody.put("bool", bool); |
|
|
|
|
|
|
|
|
|
|
|
query.put("query", queryBody); |
|
|
|
|
|
|
|
|
|
|
|
// sort |
|
|
|
|
|
List<Map<String, Object>> sortList = new ArrayList<Map<String, Object>>(); |
|
|
|
|
|
Map<String, Object> order = new HashMap<String, Object>(); |
|
|
|
|
|
order.put("order", "desc"); |
|
|
|
|
|
Map<String, Object> sortField = new HashMap<String, Object>(); |
|
|
|
|
|
sortField.put("createTime", order); |
|
|
|
|
|
sortList.add(sortField); |
|
|
|
|
|
query.put("sort", sortList); |
|
|
|
|
|
|
|
|
|
|
|
// ================== 发起 HTTP 请求 ================== |
|
|
|
|
|
StringBuffer host = new StringBuffer(); |
|
|
|
|
|
host.append(esUrl) |
|
|
|
|
|
.append("/") |
|
|
|
|
|
.append(indexName) |
|
|
|
|
|
.append("/_search"); |
|
|
|
|
|
|
|
|
|
|
|
HttpPost httpPost = new HttpPost(host.toString()); |
|
|
|
|
|
httpPost.setHeader("Content-Type", "application/json"); |
|
|
|
|
|
|
|
|
|
|
|
StringEntity entity = new StringEntity( |
|
|
|
|
|
JSONObject.toJSONString(query), |
|
|
|
|
|
ContentType.APPLICATION_JSON |
|
|
|
|
|
); |
|
|
|
|
|
httpPost.setEntity(entity); |
|
|
|
|
|
|
|
|
|
|
|
HttpResponse response = httpClient.execute(httpPost); |
|
|
|
|
|
int statusCode = response.getStatusLine().getStatusCode(); |
|
|
|
|
|
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); |
|
|
|
|
|
|
|
|
|
|
|
if (statusCode != 200) { |
|
|
|
|
|
log.error("ES 查询失败 status={}, body={}", statusCode, responseBody); |
|
|
|
|
|
return Res.fail("ES 查询失败"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ================== 解析返回 ================== |
|
|
|
|
|
JSONObject json = JSONObject.parseObject(responseBody); |
|
|
|
|
|
JSONObject hits = json.getJSONObject("hits"); |
|
|
|
|
|
|
|
|
|
|
|
Long total = hits.getJSONObject("total").getLong("value"); |
|
|
|
|
|
|
|
|
|
|
|
List<AppResultDoc> list = new ArrayList<AppResultDoc>(); |
|
|
|
|
|
JSONArray hitList = hits.getJSONArray("hits"); |
|
|
|
|
|
for (int i = 0; i < hitList.size(); i++) { |
|
|
|
|
|
JSONObject source = hitList.getJSONObject(i).getJSONObject("_source"); |
|
|
|
|
|
AppResultDoc item = source.toJavaObject(AppResultDoc.class); |
|
|
|
|
|
list.add(item); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> result = new HashMap<String, Object>(); |
|
|
|
|
|
result.put("page", page); |
|
|
|
|
|
result.put("size", size); |
|
|
|
|
|
result.put("total", total); |
|
|
|
|
|
result.put("list", list); |
|
|
|
|
|
|
|
|
|
|
|
return Res.ok(result); |
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("查询用户任务失败 userId={}", userId, e); |
|
|
|
|
|
return Res.fail("查询任务失败"); |
|
|
|
|
|
} finally { |
|
|
|
|
|
if (httpClient != null) { |
|
|
|
|
|
try { |
|
|
|
|
|
httpClient.close(); |
|
|
|
|
|
} catch (Exception ignored) {} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |