|
|
@@ -3,27 +3,36 @@ package com.usoftchina.bi.server.service.dashboard;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fasterxml.jackson.databind.type.CollectionType;
|
|
|
import com.usoftchina.bi.core.base.*;
|
|
|
import com.usoftchina.bi.core.exception.MyException;
|
|
|
+import com.usoftchina.bi.core.utils.CollectionUtils;
|
|
|
import com.usoftchina.bi.core.utils.EncryUtil;
|
|
|
import com.usoftchina.bi.server.dao.chart.ChartsConfigMapper;
|
|
|
import com.usoftchina.bi.server.dao.dashboard.DashboardsMapper;
|
|
|
+import com.usoftchina.bi.server.dao.dataSource.DataConnectorMapper;
|
|
|
import com.usoftchina.bi.server.dao.user.UserMapper;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
import com.usoftchina.bi.server.model.bo.DashOrder;
|
|
|
+import com.usoftchina.bi.server.model.bo.Screen;
|
|
|
import com.usoftchina.bi.server.model.bo.ShareReqBO;
|
|
|
import com.usoftchina.bi.server.model.po.ChartConfig;
|
|
|
import com.usoftchina.bi.server.model.po.Dashboards;
|
|
|
+import com.usoftchina.bi.server.model.po.DataConnector;
|
|
|
import com.usoftchina.bi.server.model.vo.configVo.ChangeOrderInfo;
|
|
|
+import com.usoftchina.bi.server.model.vo.configVo.ColumnRenameInfo;
|
|
|
import com.usoftchina.bi.server.model.vo.configVo.DashboardsInfo;
|
|
|
-import com.usoftchina.bi.server.model.vo.dataVo.DashboardCopyInfo;
|
|
|
-import com.usoftchina.bi.server.model.vo.dataVo.DashboardOrderInfo;
|
|
|
-import com.usoftchina.bi.server.model.vo.dataVo.DataSourceCopyInfo;
|
|
|
+import com.usoftchina.bi.server.model.vo.dataVo.*;
|
|
|
import com.usoftchina.bi.server.service.chart.ChartsUtilService;
|
|
|
import com.usoftchina.bi.core.utils.GetTokenDataUtil;
|
|
|
import com.usoftchina.bi.server.service.common.MessageLogService;
|
|
|
import com.usoftchina.bi.server.service.dataSource.DataConnectorService;
|
|
|
+import org.apache.poi.hssf.usermodel.*;
|
|
|
+import org.apache.poi.hssf.util.HSSFCellUtil;
|
|
|
+import org.apache.poi.hssf.util.HSSFColor;
|
|
|
+import org.apache.poi.ss.format.CellFormatType;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
@@ -33,6 +42,10 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
@@ -53,6 +66,8 @@ public class DashboardsService {
|
|
|
private MessageLogService messageLogService;
|
|
|
@Autowired
|
|
|
private DefaultDashboardService defaultDashboardService;
|
|
|
+ @Autowired
|
|
|
+ private DataConnectorMapper dataConnectorMapper;
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(DashboardsService.class);
|
|
|
|
|
|
@@ -420,4 +435,198 @@ public class DashboardsService {
|
|
|
return new RepEntity(RepCode.success, dashboards.getId());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 导出报表数据
|
|
|
+ * @param dashboardExportInfo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public HSSFWorkbook export(DashboardExportInfo dashboardExportInfo){
|
|
|
+ HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
+ int dashboardId = dashboardExportInfo.getDashboardId();
|
|
|
+ List<DataSourceExportInfo> dataSourceExportInfoList = dashboardExportInfo.getData();
|
|
|
+ for (DataSourceExportInfo dataInfo : dataSourceExportInfoList) {
|
|
|
+ RepEntity<DataConnector> repEntity = dataConnectorService.getConnector(dataInfo.getDataSourceId());
|
|
|
+ if (repEntity.getCode() == 200) {
|
|
|
+ DataConnector dataConnector = repEntity.getData();
|
|
|
+ String tableName = dataConnector.getLoadObject();
|
|
|
+ List<Screen> screenList = dataInfo.getFilter();
|
|
|
+ String condition = parseFilter(screenList);
|
|
|
+ List<Map<String, Object>> dataList = dataConnectorMapper.getExportData(tableName, condition);
|
|
|
+ List<ColumnRenameInfo> columnRenameInfo = JSON.parseArray(dataConnector.getColumnConfig(), ColumnRenameInfo.class);
|
|
|
+ createExcelSheet(workbook, dataConnector.getDataName(), dataList, columnRenameInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return workbook;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void createExcelSheet(HSSFWorkbook workbook, String sheetName, List<Map<String, Object>> dataList, List<ColumnRenameInfo> columnRenameInfo) {
|
|
|
+ //添加sheet
|
|
|
+ HSSFSheet sheet = workbook.createSheet(sheetName);
|
|
|
+ HSSFRow row = sheet.createRow(0);
|
|
|
+ HSSFCell cell;
|
|
|
+ HSSFCellStyle cellStyle = getCellStyle(workbook);
|
|
|
+ //设置标题
|
|
|
+ for (int i = 0, len = columnRenameInfo.size(); i < len; i++) {
|
|
|
+ cell = row.createCell(i);
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
+ cell.setCellValue(columnRenameInfo.get(i).getColumnLable());
|
|
|
+ }
|
|
|
+ //设置数据
|
|
|
+ for (int i = 0, len = dataList.size(); i < len; i++) {
|
|
|
+ row = sheet.createRow(i + 1);
|
|
|
+ Map<String, Object> data = dataList.get(i);
|
|
|
+ for (int j = 0, size = columnRenameInfo.size(); j < size; j++) {
|
|
|
+ cell = row.createCell(j);
|
|
|
+ int dataType = convertToCellType(columnRenameInfo.get(j).getColumnType());
|
|
|
+ Object value = data.get(columnRenameInfo.get(j).getColumnName());
|
|
|
+ if (9 == dataType) {
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
|
|
|
+ Date dataValue = null;
|
|
|
+ try {
|
|
|
+ dataValue = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(String.valueOf(value));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ cell.setCellValue(dataValue);
|
|
|
+ }else {
|
|
|
+ cell.setCellType(dataType);
|
|
|
+ cell.setCellValue(StringUtils.isEmpty(value) ? "" : String.valueOf(value));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private int convertToCellType(String columnType) {
|
|
|
+ switch (columnType) {
|
|
|
+ case "categorical":
|
|
|
+ return HSSFCell.CELL_TYPE_STRING;
|
|
|
+ case "Timestamp":
|
|
|
+ return 9;
|
|
|
+ case "BigDecimal":
|
|
|
+ return HSSFCell.CELL_TYPE_NUMERIC;
|
|
|
+ case "scale":
|
|
|
+ return HSSFCell.CELL_TYPE_NUMERIC;
|
|
|
+ default:
|
|
|
+ return HSSFCell.CELL_TYPE_STRING;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private HSSFCellStyle getCellStyle(HSSFWorkbook workbook) {
|
|
|
+ HSSFCellStyle style = workbook.createCellStyle();
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setFontName("仿宋_GB2312");// 字体
|
|
|
+ font.setFontHeightInPoints((short) 12);// 字号
|
|
|
+ font.setBoldweight(new Short("8"));
|
|
|
+ font.setColor((short)64);// 颜色
|
|
|
+ style.setFont(font);
|
|
|
+ style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
|
|
|
+ style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
|
|
|
+ style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
|
|
|
+ style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
|
|
|
+ style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
|
|
|
+ style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
|
|
|
+ style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
|
|
|
+ return style;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String parseFilter(List<Screen> screenList){
|
|
|
+ StringBuilder condition = new StringBuilder(" where 1=1 ");
|
|
|
+ if (CollectionUtils.isEmpty(screenList)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (Screen screen : screenList) {
|
|
|
+ String columnName = screen.getColumnName();
|
|
|
+ String columnType = screen.getColumnType();
|
|
|
+ String symbol = screen.getSymbol();
|
|
|
+ String value = screen.getValue();
|
|
|
+ if (columnType != "time" && !("time".equals(columnType))) {
|
|
|
+ String symbVal = getSymbAndVal(symbol, value, columnType);
|
|
|
+ condition.append(" and " + columnName + " " + symbVal);
|
|
|
+ }else if (columnType == "time" || "time".equals(columnType)) {
|
|
|
+ String symbVal = getTimeSymbAndVal(symbol, value);
|
|
|
+ condition.append(" and " + columnName + " " + symbVal);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return condition.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTimeSymbAndVal(String symbol, String value) {
|
|
|
+ String values = "" + value;
|
|
|
+ String tar = "";
|
|
|
+ if ("between".equals(symbol)) {
|
|
|
+ String[] str = value.split(",");
|
|
|
+ String str1 = str[0];
|
|
|
+ String str2 = str[1];
|
|
|
+ tar = "between to_date('" + str1 + "','yyyy-MM-dd') and to_date('" + str2 + "','yyyy-MM-dd')";
|
|
|
+ } else {
|
|
|
+ tar = symbol + " '" + values + "'";
|
|
|
+ }
|
|
|
+ return tar;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getSymbAndVal(String symbol, String value, String columnType) {
|
|
|
+ String values = "" + value;
|
|
|
+ String tar = "";
|
|
|
+ if ("contain".equals(symbol)) {
|
|
|
+ if ("categorical".equals(columnType)) {
|
|
|
+ tar = "in " + getContainsCate(value);
|
|
|
+ } else {
|
|
|
+ tar = "like '%" + values + "%'";
|
|
|
+ }
|
|
|
+ } else if ("notContain".equals(symbol)) {
|
|
|
+ if ("categorical".equals(columnType)) {
|
|
|
+ tar = "not in " + getContainsCate(value);
|
|
|
+ } else {
|
|
|
+ tar = "not like '%" + values + "%'";
|
|
|
+ }
|
|
|
+ } else if ("startsWith".equals(symbol)) {
|
|
|
+ tar = "like '" + values + "%'";
|
|
|
+ } else if ("endsWith".equals(symbol)) {
|
|
|
+ tar = "like '%" + values + "'";
|
|
|
+ } else if ("null".equals(symbol)) {
|
|
|
+ tar = "is null";
|
|
|
+ } else if ("notNull".equals(symbol)) {
|
|
|
+ tar = "is not null";
|
|
|
+ } else if ("between".equals(symbol)) {
|
|
|
+ tar = "";
|
|
|
+ String[] str = value.split(",");
|
|
|
+ String str1 = str[0];
|
|
|
+ String str2 = str[1];
|
|
|
+ tar = "between '" + str1 + "' and '" + str2 + "'";
|
|
|
+ } else if("notInclude".equals(symbol)) {
|
|
|
+ tar = "not like '%" + values + "%'";
|
|
|
+ } else if("include".equals(symbol)){
|
|
|
+ tar = "like '%" + values + "%'";
|
|
|
+ } else if("equals".equals(symbol)){
|
|
|
+ tar = "=" + " '" + values + "'";
|
|
|
+ } else if("notEquals".equals(symbol)){
|
|
|
+ tar = "!=" + " '" + values + "'";
|
|
|
+ }else {
|
|
|
+ tar = symbol + " '" + values + "'";
|
|
|
+ }
|
|
|
+ return tar;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getContainsCate(String value) {
|
|
|
+ List<String> val = new ArrayList<>();
|
|
|
+ CollectionType javaType = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
|
|
|
+ try {
|
|
|
+ val = objectMapper.readValue(value, javaType); //这里不需要强制转换
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ String valueString = "";
|
|
|
+ Iterator isList = val.iterator();
|
|
|
+ while (isList.hasNext()) {
|
|
|
+ String v = String.valueOf(isList.next());
|
|
|
+ valueString = valueString + ", '" + v + "'";
|
|
|
+ }
|
|
|
+ valueString = valueString.replaceFirst(",", "(") + ")";
|
|
|
+ return valueString;
|
|
|
+ }
|
|
|
+
|
|
|
}
|