|
|
@@ -74,7 +74,24 @@ public class PrintServiceImpl implements PrintService {
|
|
|
@Override
|
|
|
public byte[] export(String userName, String reportName, String whereCondition, String otherParameters,
|
|
|
String exportFileType) {
|
|
|
- Map<String, Object> result = print(userName, reportName, whereCondition, otherParameters, exportFileType, null);
|
|
|
+ return export(userName, null, reportName, whereCondition, otherParameters, exportFileType);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public byte[] export(String userName, String profile, String reportName, String whereCondition,
|
|
|
+ String otherParameters, String exportFileType) {
|
|
|
+ DataSource dataSource = null;
|
|
|
+ if (!StringUtils.isEmpty(profile)) {
|
|
|
+ dataSource = getDataSource(userName, profile);
|
|
|
+ } else {
|
|
|
+ // 为空,说明不是来自B2C或B2B的请求,而是UAS系统
|
|
|
+ dataSource = getUASDataSource(userName);
|
|
|
+ }
|
|
|
+ if (dataSource == null) {
|
|
|
+ throw new ReportException("获取数据源失败");
|
|
|
+ }
|
|
|
+ Map<String, Object> result = print(userName, reportName, whereCondition, otherParameters, exportFileType, null,
|
|
|
+ dataSource);
|
|
|
if (!CollectionUtils.isEmpty(result)) {
|
|
|
return (byte[]) result.get("data");
|
|
|
}
|
|
|
@@ -84,14 +101,30 @@ public class PrintServiceImpl implements PrintService {
|
|
|
@Override
|
|
|
public Map<String, Object> preview(String userName, String reportName, String whereCondition,
|
|
|
String otherParameters, Integer pageIndex) {
|
|
|
- return print(userName, reportName, whereCondition, otherParameters, null, pageIndex);
|
|
|
+ return preview(userName, null, reportName, whereCondition, otherParameters, pageIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
|
|
|
+ String otherParameters, Integer pageIndex) {
|
|
|
+ DataSource dataSource = null;
|
|
|
+ if (!StringUtils.isEmpty(profile)) {
|
|
|
+ dataSource = getDataSource(userName, profile);
|
|
|
+ } else {
|
|
|
+ // 为空,说明不是来自B2C或B2B的请求,而是UAS系统
|
|
|
+ dataSource = getUASDataSource(userName);
|
|
|
+ }
|
|
|
+ if (dataSource == null) {
|
|
|
+ throw new ReportException("获取数据源失败");
|
|
|
+ }
|
|
|
+ return print(userName, reportName, whereCondition, otherParameters, null, pageIndex, dataSource);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Integer previewFirstPage(final String userName, final String reportName, final String whereCondition,
|
|
|
- final String otherParameters, final String pdfFilePath) {
|
|
|
+ public Integer previewFirstPage(final String userName, final String profile, final String reportName,
|
|
|
+ final String whereCondition, final String otherParameters, final String pdfFilePath) {
|
|
|
// 先生成第一页pdf
|
|
|
- final Integer pageSize = writePdfData(userName, reportName, whereCondition, otherParameters,
|
|
|
+ final Integer pageSize = writePdfData(userName, profile, reportName, whereCondition, otherParameters,
|
|
|
pdfFilePath.replace(".pdf", "_1.pdf"), 1);
|
|
|
|
|
|
// 再开线程生成后面页的pdf、总的pdf(即未分页的pdf)、纯数据excel
|
|
|
@@ -100,17 +133,18 @@ public class PrintServiceImpl implements PrintService {
|
|
|
public void run() {
|
|
|
// 生成之后2~5页的pdf,防止数据量较大的情况下,卡在生成总的pdf阶段,此时查看前5页也不会卡顿
|
|
|
for (int i = 2; i <= Math.min(pageSize, 5); i++) {
|
|
|
- writePdfData(userName, reportName, whereCondition, otherParameters,
|
|
|
+ writePdfData(userName, profile, reportName, whereCondition, otherParameters,
|
|
|
pdfFilePath.replace(".pdf", "_" + i + ".pdf"), i);
|
|
|
}
|
|
|
// 生成总的pdf
|
|
|
- writePdfData(userName, reportName, whereCondition, otherParameters, pdfFilePath, null);
|
|
|
+ writePdfData(userName, profile, reportName, whereCondition, otherParameters, pdfFilePath, null);
|
|
|
writePagedPdfFiles(pdfFilePath);
|
|
|
// 生成纯数据excel
|
|
|
File file = new File(pdfFilePath.replace(".pdf", ".xls"));
|
|
|
// 文件无效(不存在或过期),创建
|
|
|
if (!isFileValid(file.getPath())) {
|
|
|
- byte[] data = export(userName, reportName, whereCondition, otherParameters, "xls_with_only_data");
|
|
|
+ byte[] data = export(userName, profile, reportName, whereCondition, otherParameters,
|
|
|
+ "xls_with_only_data");
|
|
|
if (ArrayUtils.isEmpty(data)) {
|
|
|
throw new ReportException("报表导出失败:" + reportName);
|
|
|
}
|
|
|
@@ -126,17 +160,26 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* 获取第pageIndex页的pdf数据并写入pdfFilePath路径下
|
|
|
*
|
|
|
* @param userName
|
|
|
+ * 不为null;当前账套用户名
|
|
|
+ * @param profile
|
|
|
+ * 可选(UAS系统不必传递该参数),用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
|
|
|
* @param reportName
|
|
|
+ * 不为null;需要预览的报表的名称,不带任何后缀(如预览采购单,即为"Purchase")
|
|
|
* @param whereCondition
|
|
|
+ * 可为null;where之后的条件(包括where)
|
|
|
* @param otherParameters
|
|
|
+ * 若模板已指定需要的参数,则不可为null;其他参数,区别于whereCondition,报表某些字段的值取决于这些参数;
|
|
|
+ * JSON格式,数据为键值对
|
|
|
* @param pdfFilePath
|
|
|
+ * 该报表对应的pdf绝对路径
|
|
|
* @param pageIndex
|
|
|
+ * 页码
|
|
|
* @return 总页数
|
|
|
*/
|
|
|
- public Integer writePdfData(String userName, String reportName, String whereCondition, String otherParameters,
|
|
|
- String pdfFilePath, Integer pageIndex) {
|
|
|
+ private Integer writePdfData(String userName, String profile, String reportName, String whereCondition,
|
|
|
+ String otherParameters, String pdfFilePath, Integer pageIndex) {
|
|
|
File file = new File(pdfFilePath);
|
|
|
- Map<String, Object> result = preview(userName, reportName, whereCondition, otherParameters, pageIndex);
|
|
|
+ Map<String, Object> result = preview(userName, profile, reportName, whereCondition, otherParameters, pageIndex);
|
|
|
byte[] data = null;
|
|
|
Integer pageSize = null;
|
|
|
if (result != null && result.containsKey("data") && result.containsKey("pageSize")) {
|
|
|
@@ -200,10 +243,19 @@ public class PrintServiceImpl implements PrintService {
|
|
|
|
|
|
@Override
|
|
|
public String generateFileName(String userName, String reportName, String whereCondition, String otherParameters) {
|
|
|
+ return generateFileName(userName, null, reportName, whereCondition, otherParameters);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String generateFileName(String userName, String profile, String reportName, String whereCondition,
|
|
|
+ String otherParameters) {
|
|
|
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(reportName)) {
|
|
|
return null;
|
|
|
}
|
|
|
StringBuilder stringBuilder = new StringBuilder(userName);
|
|
|
+ if (!StringUtils.isEmpty(profile)) {
|
|
|
+ stringBuilder.append(profile);
|
|
|
+ }
|
|
|
if (!StringUtils.isEmpty(whereCondition)) {
|
|
|
stringBuilder.append(whereCondition);
|
|
|
}
|
|
|
@@ -243,7 +295,7 @@ public class PrintServiceImpl implements PrintService {
|
|
|
* @return
|
|
|
*/
|
|
|
private Map<String, Object> print(String userName, String reportName, String whereCondition, String otherParameters,
|
|
|
- String exportFileType, Integer pageIndex) {
|
|
|
+ String exportFileType, Integer pageIndex, DataSource dataSource) {
|
|
|
try {
|
|
|
resourceService.syncResources(userName);
|
|
|
} catch (URISyntaxException | IOException e) {
|
|
|
@@ -297,12 +349,6 @@ public class PrintServiceImpl implements PrintService {
|
|
|
|
|
|
Connection connection = null;
|
|
|
try {
|
|
|
- // 获取数据源
|
|
|
- DataSource dataSource = getDataSource(userName);
|
|
|
- if (dataSource == null) {
|
|
|
- throw new ReportException("获取数据源失败");
|
|
|
- }
|
|
|
-
|
|
|
connection = dataSource.getConnection();
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
|
|
@@ -372,17 +418,13 @@ public class PrintServiceImpl implements PrintService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据当前账套用户名,从主数据库master表获取账套数据库配置信息,作为报表模板的数据源
|
|
|
+ * 根据当前账套用户名,从主数据库master表获取(UAS系统)账套数据库配置信息,作为报表模板的数据源
|
|
|
*
|
|
|
* @param userName
|
|
|
* 当前账套用户名
|
|
|
* @return
|
|
|
*/
|
|
|
- private DataSource getDataSource(String userName) {
|
|
|
- // 如果userName是B2C,直接获取配置好的B2C数据源
|
|
|
- if (userName.equals("B2C")) {
|
|
|
- return ContextUtils.getApplicationContext().getBean("b2cDataSource", DruidDataSource.class);
|
|
|
- }
|
|
|
+ private DataSource getUASDataSource(String userName) {
|
|
|
// 默认数据源(主数据库)
|
|
|
DruidDataSource defaultDataSource = ContextUtils.getApplicationContext().getBean("defaultSob",
|
|
|
DruidDataSource.class);
|
|
|
@@ -445,6 +487,32 @@ public class PrintServiceImpl implements PrintService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据当前账套用户名和profile配置,获取相应B2C、B2B数据源
|
|
|
+ *
|
|
|
+ * @param userName
|
|
|
+ * 当前账套用户名
|
|
|
+ * @param profile
|
|
|
+ * 用于标识请求源(B2C、B2B)是正式、测试还是开发版本:prod、test、dev
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private DataSource getDataSource(String userName, String profile) {
|
|
|
+ // 如果userName是B2C,直接获取配置好的B2C数据源
|
|
|
+ if (userName.equalsIgnoreCase("B2C")) {
|
|
|
+ if (profile.equalsIgnoreCase("dev")) {
|
|
|
+ return ContextUtils.getApplicationContext().getBean("b2cDevDataSource", DruidDataSource.class);
|
|
|
+ } else if (profile.equalsIgnoreCase("test")) {
|
|
|
+ return ContextUtils.getApplicationContext().getBean("b2cTestDataSource", DruidDataSource.class);
|
|
|
+ } else if (profile.equalsIgnoreCase("prod")) {
|
|
|
+ return ContextUtils.getApplicationContext().getBean("b2cProdDataSource", DruidDataSource.class);
|
|
|
+ } else {
|
|
|
+ throw new ReportException("profile只能为:dev、test或prod,不可为" + profile);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new ReportException("暂时不支持" + userName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 以不同的格式导出报表
|
|
|
*
|