Commit d4c85a91 authored by chenzai's avatar chenzai

条件灵活的分页查询以及优化uploadmass方法

parent 34602b20
......@@ -30,6 +30,7 @@ import org.typroject.tyboot.core.foundation.context.SpringContextHelper;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
......@@ -69,6 +70,9 @@ public class ClientHandler<path> implements Runnable {
}
private String upload2Maas(InputStream inputStream, String hostAndPort) throws IOException {
InputStream intercept = intercept(inputStream);
AmosRequestContext robotAuthentication = SpringContextHelper.getBean(AmosRequestContext.class);
if (Objects.nonNull(robotAuthentication)) {
String token = robotAuthentication.getToken();
......@@ -93,16 +97,16 @@ public class ClientHandler<path> implements Runnable {
}
};
params.add("file", resource);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, getHeader(token,product,appKey,hostAndPort,true));
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, getHeader(token, product, appKey, hostAndPort, true));
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(uploadUrl, HttpMethod.POST, requestEntity, String.class);
String body = responseEntity.getBody();
JSONObject jsonObject = JSONObject.parseObject(body);
String result = jsonObject.getString("result");
String path = jsonObject.getString("path");
if (jsonObject.getString("status").equals("200")){
log.info("路径:"+path+"/"+result);
}else {
if (jsonObject.getString("status").equals("200")) {
log.info("路径:" + path + "/" + result);
} else {
throw new RuntimeException("返回状态码错误");
}
......@@ -152,14 +156,14 @@ public class ClientHandler<path> implements Runnable {
return null;
}
private HttpHeaders getHeader(String token,String product,String appKey,String hostAndPort,Boolean isUpload){
private HttpHeaders getHeader(String token, String product, String appKey, String hostAndPort, Boolean isUpload) {
HttpHeaders header = new HttpHeaders();
header.add(Constant.TOKEN, token);
header.add(Constant.PRODUCT, product);
header.add(Constant.APPKEY, appKey);
if (isUpload){
if (isUpload) {
header.setContentType(MediaType.MULTIPART_FORM_DATA);
}else {
} else {
header.setContentType(MediaType.APPLICATION_JSON);
}
return header;
......@@ -169,6 +173,58 @@ public class ClientHandler<path> implements Runnable {
requestInfoToSocketServer();
}
//截取文件流
public static InputStream intercept(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String string1 = sb.toString();
int startFilePath = string1.indexOf("##STATITLE##");
int endFilePath = string1.indexOf("##ENDTITLE##");
int startFile = string1.indexOf("##STAFILE##");
int endFile = string1.indexOf("##ENDFILE##");
if (startFilePath < 0) {
log.info("不存在文件名开始标志");
return null;
}
if (endFilePath < 0) {
log.info("不存在文件名结束标志");
return null;
}
if (startFile < 0) {
log.info("不存在文件开始标志");
return null;
}
if (endFile < 0) {
log.info("不存在文件结束标志");
return null;
}
String filePath = string1.substring(startFilePath, endFilePath).substring("##STATITLE##".length());
log.info("文件名:" + filePath);
String file = string1.substring(startFile, endFile).substring("##STAFILE##".length());
log.info("文件:" + file);
InputStream fileInputStream = new ByteArrayInputStream(file.getBytes());
return fileInputStream;
}
private static void requestInfoToSocketServer() {
try {
Socket socket = new Socket("127.0.0.1", 7777);
......
package com.yeejoin.amos.boot.module.cas.api.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import java.util.List;
import java.sql.*;
import java.util.*;
@Service
public class IdxBizSyncService {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String passwd;
// 多个查询(分页)
public List selectAllPageQuery(String tableName, Map params, int current, int size) throws SQLException {
Connection connection = DriverManager.getConnection(url, user, passwd);
System.out.println(getSql(tableName, params, current, size));
PreparedStatement preparedStatement = connection.prepareStatement(getSql(tableName, params, current, size));
ResultSet set = preparedStatement.executeQuery();
List list = convertList(set);
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
return list;
}
public String getSql(String tableName, Map params, int current, int size) {
String sql = "select * from " + tableName;
sql += " where ";
Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
sql = sql + entry.getKey() + " = " + "'" + entry.getValue() + "'" + " AND ";
}
sql += "1 = 1 ";
sql += " limit " + (current - 1) * size + "," + size;
// Page
return sql;
}
public List convertList(ResultSet rs) throws SQLException {
List list = new ArrayList();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount(); //Map rowData;
while (rs.next()) { //rowData = new HashMap(columnCount);
Map rowData = new HashMap();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}
return list;
}
}
\ No newline at end of file
package com.yeejoin.amos.boot.module.cas.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.yeejoin.amos.boot.module.cas.api.service.IdxBizSyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@RestController
@Api(tags = "Api")
@RequestMapping(value = "/idx-biz-sync", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class IdxBizSyncController {
@Autowired
private IdxBizSyncService idxBizSyncService;
@TycloudOperation(ApiLevel = UserType.AGENCY ,needAuth = false)
@RequestMapping(value = "/page/{tableName}", method = RequestMethod.GET)
@ApiOperation(httpMethod = "GET", value = "列表分页查询", notes = "列表分页查询")
@ResponseBody
public List listPage(@PathVariable String tableName,
@RequestParam(value = "current",defaultValue = "1") int current,
@RequestParam(value = "size",defaultValue = "10") int size,
@RequestBody Map params) throws Exception{
List list = idxBizSyncService.selectAllPageQuery(tableName, params, current, size);
return list;
}
}
#DB properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://172.16.10.210:3306/amos_idx_biz?allowMultiQueries=true&serverTimezone=GMT%2B8\
spring.datasource.url=jdbc:mysql://36.46.151.113:5432/project_test?allowMultiQueries=true&serverTimezone=GMT%2B8\
&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=Yeejoin@2020
spring.datasource.username=admin
spring.datasource.password=Yeejoin@2023
##eureka properties:
eureka.client.service-url.defaultZone =http://172.16.10.210:10001/eureka/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment