Skip to content

Commit

Permalink
fix: 导出数据日期格式错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron3S committed Jan 13, 2025
1 parent efb0927 commit aff6227
Showing 1 changed file with 43 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.nio.file.Files;
import java.sql.Clob;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -101,13 +103,17 @@ private void fullData(SQLQueryResult result) {
this.data.getData().clear();

this.getStateManager().getState().setTotal(result.getTotal());
this.fullDataViewData(this.data, result);
}


private void fullDataViewData(DataViewData viewData, SQLQueryResult result) {

this.data.setFields(result.getFields());
viewData.setFields(result.getFields());

Map<String, Integer> fieldNumMap = new HashMap<>();

this.data.getFields().forEach(field -> {
viewData.getFields().forEach(field -> {
if (fieldNumMap.containsKey(field.getName())) {
var fieldName = field.getName();
var num = fieldNumMap.get(field.getName());
Expand All @@ -118,16 +124,16 @@ private void fullData(SQLQueryResult result) {
}
});


for (List<Object> row : result.getData()) {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < row.size(); i++) {
map.put(this.data.getFields().get(i).getName(), row.get(i));
map.put(viewData.getFields().get(i).getName(), row.get(i));
}
this.data.getData().add(map);
viewData.getData().add(map);
}
}


private static void writeString(BufferedWriter writer, Object object) throws IOException {
var str = object.toString();

Expand All @@ -137,6 +143,35 @@ private static void writeString(BufferedWriter writer, Object object) throws IOE
writer.write(str);
}

private void writeCSVData(BufferedWriter writer, DataViewData viewData) throws IOException, SQLException {

for (Field field : viewData.getFields()) {
writeString(writer, field.getName());
writer.write(",");
}
for (Map<String, Object> row : viewData.getData()) {
for (Field field : viewData.getFields()) {
var obj = row.get(field.getName());
if (obj == null) {
writer.write("NULL");
writer.write(",");
} else if (obj instanceof Clob clob) {
writer.write(CodeUtils.escapeCsvValue(clob.getSubString(1, (int) clob.length())));
writer.write(",");
} else if (obj instanceof Date) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
writeString(writer, fmt.format(obj));
} else {
writeString(writer, row.get(field.getName()));
writer.write(",");
}
}
writer.newLine();
}

writer.newLine();
}

public void export(String scope) throws SQLException {
var session = SessionManager.getCurrentSession();

Expand All @@ -155,56 +190,16 @@ public void export(String scope) throws SQLException {
var writer = Files.newBufferedWriter(f.toPath());

if (scope.equals("current")) {
for (Field field : this.data.getFields()) {
writeString(writer, field.getName());
writer.write(",");
}
writer.newLine();

for (Map<String, Object> row : this.data.getData()) {
for (Field field : this.data.getFields()) {
if (row.get(field.getName()) == null) {
writer.write("NULL");
writer.write(",");
} else if (row.get(field.getName()) instanceof Clob clob) {
writer.write(CodeUtils.escapeCsvValue(clob.getSubString(1, (int) clob.length())));
writer.write(",");
} else {
writeString(writer, row.get(field.getName()));
writer.write(",");
}
}
writer.newLine();
}
this.writeCSVData(writer, this.data);
command.setOutput(String.format("%d rows exported", this.data.getData().size()));
}

if (scope.equals("all")) {
SQLQueryParams queryParams = new SQLQueryParams();
queryParams.setLimit(-1);
var result = this.loadDataInterface.loadData(queryParams);

for (Field field : result.getFields()) {
writer.write(field.getName());
writer.write(",");
}
writer.newLine();

for (List<Object> row : result.getData()) {
for (Object o : row) {
if (o == null) {
writer.write("NULL");
writer.write(",");
} else if (o instanceof Clob clob) {
writer.write(CodeUtils.escapeCsvValue(clob.getSubString(1, (int) clob.length())));
writer.write(",");
} else {
writer.write(o.toString());
writer.write(",");
}
}
writer.newLine();
}
var viewData = new DataViewData();
this.fullDataViewData(viewData, result);
command.setOutput(String.format("%d rows exported", result.getData().size()));
}
writer.flush();
Expand Down

0 comments on commit aff6227

Please sign in to comment.