1.报表切换数据源——余额表
This commit is contained in:
		
							parent
							
								
									45645e3bdc
								
							
						
					
					
						commit
						502fedf8b5
					
				|  | @ -18,7 +18,7 @@ import java.util.Date; | |||
| public class StrongOperateQueryPlugin extends AbstractReportListDataPlugin implements Plugin { | ||||
| 
 | ||||
|     // 交易明细单据编码 | ||||
|     public static final String BEI_INTELPAY = "bei_intelpay"; | ||||
|     public static final String BEI_INTELPAY = "bei_bankbalance"; | ||||
| 
 | ||||
|     @Override | ||||
|     public DataSet query(ReportQueryParam reportQueryParam, Object o) throws Throwable { | ||||
|  | @ -32,7 +32,7 @@ public class StrongOperateQueryPlugin extends AbstractReportListDataPlugin imple | |||
|         // 被动付款入账 | ||||
|         DataSet beiIntelpay1 = QueryServiceHelper.queryDataSet(this.getClass().getName(), BEI_INTELPAY, | ||||
|                 "id,billno as shjh_billno,company.number as shjh_orgnumber,company.name as shjh_orgname," + | ||||
|                         "bank.number as banknumber,biztime as shjh_biztime,transbalance as shjh_amountjz,accountbank.id as accountbankid," + | ||||
|                         "bank.number as banknumber,bizdate as shjh_biztime,amount as shjh_amountjz,accountbank.id as accountbankid," + | ||||
|                         "company.shjh_isshare as shjh_companyisshare", | ||||
|                 new QFilter[]{q1, q2}, null); | ||||
|         // 合作金融机构 bd_finorginfo | ||||
|  |  | |||
|  | @ -19,199 +19,163 @@ import java.util.Date; | |||
|  */ | ||||
| public class StrongSharingQueryPlugin extends AbstractReportListDataPlugin implements Plugin { | ||||
| 
 | ||||
|     // 交易明细单据编码 | ||||
|     public static final String BEI_INTELPAY = "bei_intelpay"; | ||||
|     // 余额表 | ||||
|     public static final String BEI_INTELPAY = "bei_bankbalance"; | ||||
|     //合作金融机构 | ||||
|     private static final String BD_FINORGINFO = "bd_finorginfo"; | ||||
| 
 | ||||
|     private static final Log log = LogFactory.getLog(StrongSharingQueryPlugin.class); | ||||
| 
 | ||||
|     @Override | ||||
|     public DataSet query(ReportQueryParam reportQueryParam, Object o) throws Throwable { | ||||
| 
 | ||||
|         FilterInfo filter = reportQueryParam.getFilter(); | ||||
|         Date shjh_jzdate = filter.getDate("shjh_jzdate");//基准日期 | ||||
|         QFilter q1 = new QFilter("bizdate", QCP.equals, shjh_jzdate);//交易日期 | ||||
|         String shjhIsshare = filter.getString("shjh_isshare");//公司属性 | ||||
|         if (shjhIsshare.equals("1")) { | ||||
|             q1.and(new QFilter("company.shjh_isshare", QCP.equals, "1"));//公司属性为强共享 | ||||
|         }else { | ||||
|             q1.and(new QFilter("company.shjh_isshare", QCP.equals, "0"));//公司属性为弱共享 | ||||
|         } | ||||
|         Date shjh_jzdate = filter.getDate("shjh_jzdate"); // 基准日期 | ||||
|         Date shjh_tnbjdate = filter.getDate("shjh_tnbjdate"); // 同年比较日期 | ||||
|         String shjhIsshare = filter.getString("shjh_isshare"); // 公司属性 | ||||
| 
 | ||||
|         DataSet result1 = null; | ||||
|         // 被动付款入账 | ||||
|         DataSet beiIntelpay1 = QueryServiceHelper.queryDataSet(this.getClass().getName(), BEI_INTELPAY, | ||||
|         // 1. 查询基准日期的数据 | ||||
|         DataSet result1 = queryDataByDate(shjh_jzdate, shjhIsshare, "shjh_amountjz"); | ||||
| 
 | ||||
|         // 2. 查询同年比较日期的数据 | ||||
|         DataSet result2 = queryDataByDate(shjh_tnbjdate, shjhIsshare, "shjh_amounttn"); | ||||
| 
 | ||||
|         // 3. 合并两个数据集并累加金额 | ||||
|         DataSet finalResult = mergeAndSumResults(result1, result2); | ||||
| 
 | ||||
|         // 4. 添加小计行 | ||||
|         return addSubtotal(finalResult); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 按日期查询数据 | ||||
|      */ | ||||
|     private DataSet queryDataByDate(Date date, String shjhIsshare, String amountFieldName) { | ||||
|         QFilter qFilter = new QFilter("bizdate", QCP.equals, date); | ||||
|         qFilter.and(new QFilter("company.shjh_isshare", QCP.equals, shjhIsshare)); | ||||
| 
 | ||||
|         // 查询余额表数据 | ||||
|         DataSet beiIntelpay = QueryServiceHelper.queryDataSet( | ||||
|                 this.getClass().getName(), | ||||
|                 BEI_INTELPAY, | ||||
|                 "id,billno as shjh_billno,company.number as shjh_orgnumber,company.name as shjh_orgname," + | ||||
|                         "bank.number as banknumber,biztime as shjh_biztime,transbalance as shjh_amountjz,accountbank.id as accountbankid," + | ||||
|                         "company.shjh_isshare as shjh_companyisshare", | ||||
|                 q1.toArray(), null); | ||||
|         // 合作金融机构 bd_finorginfo | ||||
|         DataSet bdAccountbanks1 = QueryServiceHelper.queryDataSet(this.getClass().getName(), | ||||
|                 BD_FINORGINFO, "id,number as finorginfonumber,bank_cate.name as shjh_bankname", null, null); | ||||
|                         "bank.number as banknumber,bizdate as shjh_biztime,amount as " + amountFieldName + | ||||
|                         ",accountbank.id as accountbankid,company.shjh_isshare as shjh_companyisshare", | ||||
|                 qFilter.toArray(), | ||||
|                 null | ||||
|         ); | ||||
| 
 | ||||
|         // 查询银行信息 | ||||
|         DataSet bdAccountbanks = QueryServiceHelper.queryDataSet( | ||||
|                 this.getClass().getName(), | ||||
|                 BD_FINORGINFO, | ||||
|                 "id,number as finorginfonumber,bank_cate.name as shjh_bankname", | ||||
|                 null, | ||||
|                 null | ||||
|         ); | ||||
| 
 | ||||
|         try { | ||||
|             // 去重处理 | ||||
|             DataSet distinctBeiIntelpay = beiIntelpay1.distinct(); | ||||
|             DataSet distinctBdAccountbanks = bdAccountbanks1.distinct(); | ||||
|             DataSet distinctBeiIntelpay = beiIntelpay.distinct(); | ||||
|             DataSet distinctBdAccountbanks = bdAccountbanks.distinct(); | ||||
| 
 | ||||
|             // 根据被动付款入账 开户银行 银行类别(查所有满足日期的交易明细) | ||||
|             // 关联银行信息 | ||||
|             DataSet joinedDataSet = distinctBeiIntelpay.join(distinctBdAccountbanks) | ||||
|                     .on("banknumber", "finorginfonumber") | ||||
|                     .select(new String[]{"shjh_billno", "shjh_orgnumber", "shjh_orgname", "shjh_bankname", "banknumber", | ||||
|                             "shjh_biztime", "shjh_amountjz", "accountbankid","shjh_companyisshare"}) | ||||
|                     .select(new String[]{ | ||||
|                             "shjh_billno", "shjh_orgnumber", "shjh_orgname", | ||||
|                             "shjh_bankname", "banknumber", "shjh_biztime", | ||||
|                             amountFieldName, "accountbankid", "shjh_companyisshare" | ||||
|                     }) | ||||
|                     .finish(); | ||||
| 
 | ||||
|             // 按银行账户id分组,取最大日期的集合 | ||||
|             // 按银行账户id分组,取最大日期的记录 | ||||
|             DataSet maxBizdate = joinedDataSet.groupBy(new String[]{"accountbankid"}) | ||||
|                     .max("shjh_biztime") | ||||
|                     .finish(); | ||||
| 
 | ||||
|             // 分组后和源集合联表:除账户id,日期的其他字段进行组合返回前台 | ||||
|             // 获取最终有效的记录 | ||||
|             DataSet result = joinedDataSet.join(maxBizdate) | ||||
|                     .on("accountbankid", "accountbankid") | ||||
|                     .on("shjh_biztime", "shjh_biztime") | ||||
|                     .select(new String[]{"shjh_billno", "shjh_orgnumber", "shjh_orgname", "shjh_bankname", "banknumber", | ||||
|                             "shjh_biztime", "shjh_amountjz","accountbankid","shjh_companyisshare"}) | ||||
|                     .select(new String[]{ | ||||
|                             "shjh_billno", "shjh_orgnumber", "shjh_orgname", | ||||
|                             "shjh_bankname", "banknumber", "shjh_biztime", | ||||
|                             amountFieldName, "accountbankid", "shjh_companyisshare" | ||||
|                     }) | ||||
|                     .finish(); | ||||
| 
 | ||||
|             // 再次根据银行编码分组 求和余额 | ||||
|             result1 = result.groupBy(new String[]{"shjh_orgnumber", "shjh_orgname", "shjh_bankname","banknumber","shjh_companyisshare"}).sum("shjh_amountjz").finish(); | ||||
| 
 | ||||
| //            // 分组后和源集合联表:除银行编码,余额的其他字段进行组合返回前台 | ||||
| //            result1 = result.join(finish1) | ||||
| //                    .on("banknumber","banknumber") | ||||
| //                    .on("shjh_amountjz","shjh_amountjz") | ||||
| //                    .select(new String[]{"shjh_billno", "shjh_orgnumber", "shjh_orgname", "shjh_bankname", "banknumber", | ||||
| //                            "shjh_biztime", "shjh_amountjz","accountbankid"}) | ||||
| //                    .finish(); | ||||
|             // 按组织信息和银行分组求和 | ||||
|             return result.groupBy(new String[]{ | ||||
|                     "shjh_orgnumber", "shjh_orgname", "shjh_bankname", "shjh_companyisshare" | ||||
|             }).sum(amountFieldName).finish(); | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|             log.error("查询数据出错", e); | ||||
|             throw e; | ||||
|         } finally { | ||||
|             // 确保在操作完成后关闭数据集 | ||||
|             if (beiIntelpay1 != null) { | ||||
|                 try { | ||||
|                     beiIntelpay1.close(); | ||||
|                 } catch (Exception e) { | ||||
|                     e.printStackTrace(); | ||||
|                 } | ||||
|             } | ||||
|             if (bdAccountbanks1 != null) { | ||||
|                 try { | ||||
|                     bdAccountbanks1.close(); | ||||
|                 } catch (Exception e) { | ||||
|                     e.printStackTrace(); | ||||
|                 } | ||||
|             } | ||||
|             closeDataSet(beiIntelpay); | ||||
|             closeDataSet(bdAccountbanks); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|         Date shjh_tnbjdate = filter.getDate("shjh_tnbjdate");//同年比较日期 | ||||
|         QFilter q2 = new QFilter("bizdate", QCP.equals, shjh_tnbjdate);//交易日期 | ||||
|         if (shjhIsshare.equals("1")) { | ||||
|             q2.and(new QFilter("company.shjh_isshare", QCP.equals, "1"));//公司属性为强共享 | ||||
|         }else { | ||||
|             q2.and(new QFilter("company.shjh_isshare", QCP.equals, "0"));//公司属性为弱共享 | ||||
|         } | ||||
| 
 | ||||
|         DataSet result2 = null; | ||||
|         // 原始查询 | ||||
|         DataSet beiIntelpay2 = QueryServiceHelper.queryDataSet(this.getClass().getName(), BEI_INTELPAY, | ||||
|                 "id,billno as shjh_billno,company.number as shjh_orgnumber,company.name as shjh_orgname," + | ||||
|                         "bank.number as banknumber,biztime as shjh_biztime,transbalance as shjh_amounttn,accountbank.id as accountbankid," + | ||||
|                         "company.shjh_isshare as shjh_companyisshare", | ||||
|                 q2.toArray(), null); | ||||
|         // 合作金融机构 bd_finorginfo | ||||
|         DataSet bdAccountbanks2 = QueryServiceHelper.queryDataSet(this.getClass().getName(), | ||||
|                 BD_FINORGINFO, "id,number as finorginfonumber,bank_cate.name as shjh_bankname", null, null); | ||||
| 
 | ||||
|         try { | ||||
|             // 去重处理 | ||||
|             DataSet distinctBeiIntelpay = beiIntelpay2.distinct(); | ||||
|             DataSet distinctBdAccountbanks = bdAccountbanks2.distinct(); | ||||
| 
 | ||||
|             // 根据被动付款入账 开户银行 银行类别(查所有满足日期的交易明细) | ||||
|             DataSet joinedDataSet = distinctBeiIntelpay.join(distinctBdAccountbanks) | ||||
|                     .on("banknumber", "finorginfonumber") | ||||
|                     .select(new String[]{"shjh_billno", "shjh_orgnumber", "shjh_orgname", "shjh_bankname", "banknumber", | ||||
|                             "shjh_biztime", "shjh_amounttn","accountbankid","shjh_companyisshare"}) | ||||
|                     .finish(); | ||||
| 
 | ||||
|             // 按银行账户id分组,取最大日期的集合 | ||||
|             DataSet maxBizdate = joinedDataSet.groupBy(new String[]{"accountbankid"}) | ||||
|                     .max("shjh_biztime") | ||||
|                     .finish(); | ||||
| 
 | ||||
|             // 分组后和源集合联表:除账户id,日期的其他字段进行组合返回前台 | ||||
|             DataSet result = joinedDataSet.join(maxBizdate) | ||||
|                     .on("accountbankid", "accountbankid") | ||||
|                     .on("shjh_biztime", "shjh_biztime") | ||||
|                     .select(new String[]{"shjh_billno", "shjh_orgnumber", "shjh_orgname", "shjh_bankname", "banknumber", | ||||
|                             "shjh_biztime", "shjh_amounttn","accountbankid","shjh_companyisshare"}) | ||||
|                     .finish(); | ||||
| 
 | ||||
|             // 再次根据银行编码分组 求和余额 | ||||
|             result2 = result.groupBy(new String[]{"shjh_orgnumber", "shjh_orgname", "shjh_bankname","banknumber","shjh_companyisshare"}).sum("shjh_amounttn").finish(); | ||||
| 
 | ||||
| //            // 分组后和源集合联表:除银行编码,余额的其他字段进行组合返回前台 | ||||
| //            result2 = result.join(finish1) | ||||
| //                    .on("banknumber","banknumber") | ||||
| //                    .on("shjh_amounttn","shjh_amounttn") | ||||
| //                    .select(new String[]{"shjh_billno", "shjh_orgnumber", "shjh_orgname", "shjh_bankname", "banknumber", | ||||
| //                            "shjh_biztime", "shjh_amounttn","accountbankid"}) | ||||
| //                    .finish(); | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
|         } finally { | ||||
|             // 确保在操作完成后关闭数据集 | ||||
|             if (beiIntelpay2 != null) { | ||||
|                 try { | ||||
|                     beiIntelpay2.close(); | ||||
|                 } catch (Exception e) { | ||||
|                     e.printStackTrace(); | ||||
|                 } | ||||
|             } | ||||
|             if (bdAccountbanks2 != null) { | ||||
|                 try { | ||||
|                     bdAccountbanks2.close(); | ||||
|                 } catch (Exception e) { | ||||
|                     e.printStackTrace(); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| //        result1.print(true); | ||||
| //        result2.print(true); | ||||
| 
 | ||||
|         // 根据资金组织编码名称,银行名称进行左连接 | ||||
|         DataSet finalResult = result1.leftJoin(result2) | ||||
|     /** | ||||
|      * 合并两个数据集并累加金额 | ||||
|      */ | ||||
|     private DataSet mergeAndSumResults(DataSet result1, DataSet result2) { | ||||
|         // 左连接两个数据集 | ||||
|         DataSet merged = result1.leftJoin(result2) | ||||
|                 .on("shjh_orgname", "shjh_orgname") | ||||
|                 .on("shjh_bankname", "shjh_bankname") | ||||
|                 .on("shjh_orgnumber", "shjh_orgnumber") | ||||
|                 .on("shjh_companyisshare", "shjh_companyisshare") | ||||
|                 .select(new String[]{ "shjh_bankname","shjh_orgnumber", "shjh_orgname","shjh_companyisshare", "shjh_amountjz", "shjh_amounttn"}) | ||||
|                 .select(new String[]{ | ||||
|                         "shjh_bankname", "shjh_orgnumber", "shjh_orgname", | ||||
|                         "shjh_companyisshare", "shjh_amountjz", "shjh_amounttn" | ||||
|                 }) | ||||
|                 .finish(); | ||||
| //        finalResult.print(true); | ||||
|         return merged; | ||||
|     } | ||||
| 
 | ||||
|         //分组小计 银行名称 基准余额,同年比较余额 | ||||
|         DataSet groupDataSet = finalResult.groupBy(new String[]{"shjh_bankname"}).sum("shjh_amountjz").sum("shjh_amounttn").finish(); | ||||
|         // 由于分组计算之后,Dataset的字段少了一个,需要通过addField加回来,为之后union做准备 | ||||
|     /** | ||||
|      * 添加小计行 | ||||
|      */ | ||||
|     private DataSet addSubtotal(DataSet dataSet) { | ||||
|         // 按银行名称分组小计 | ||||
|         DataSet groupDataSet = dataSet.groupBy(new String[]{"shjh_bankname"}) | ||||
|                 .sum("shjh_amountjz") | ||||
|                 .sum("shjh_amounttn") | ||||
|                 .finish(); | ||||
| 
 | ||||
|         // 添加缺少的字段 | ||||
|         groupDataSet = groupDataSet | ||||
|                 .addField("NULL", "shjh_companyisshare")//公司属性:强共享/弱共享 | ||||
|                 .addField("'-'", "shjh_orgnumber")//组织编码 | ||||
|                 .addField("'-'","shjh_orgname");//组织名称 | ||||
|                 .addField("NULL", "shjh_companyisshare") | ||||
|                 .addField("'-'", "shjh_orgnumber") | ||||
|                 .addField("'-'", "shjh_orgname"); | ||||
| 
 | ||||
|         StringBuilder selectExpression = new StringBuilder(); | ||||
|         selectExpression.append("shjh_bankname,shjh_orgnumber,shjh_orgname,shjh_companyisshare,shjh_amountjz,shjh_amounttn"); | ||||
|         // 重命名字段并添加"小计"后缀 | ||||
|         groupDataSet = groupDataSet.select( | ||||
|                 "concat(shjh_bankname, '小计') as shjh_bankname," + | ||||
|                         "shjh_orgnumber, shjh_orgname, shjh_companyisshare," + | ||||
|                         "shjh_amountjz, shjh_amounttn" | ||||
|         ); | ||||
| 
 | ||||
|         //  应用SELECT转换 | ||||
|         groupDataSet = groupDataSet.select("concat(shjh_bankname+'小计') as "+ selectExpression); | ||||
|         // union前,需要保证两个dataSet的字段序列一致,因此这里对sumDataSet对象重新排列字段序列 | ||||
|         groupDataSet = groupDataSet.select(String.valueOf(selectExpression)); | ||||
|         // union,此时groupDataSet会续在dataSet的底部 | ||||
|         DataSet unionDataSet = finalResult.union(groupDataSet); | ||||
|         // 合并原始数据和小计数据 | ||||
|         DataSet unionDataSet = dataSet.union(groupDataSet); | ||||
| 
 | ||||
| //        return unionDataSet; | ||||
|         // 按组织名称排序,这样,底部的合计数据,就会与上面的组织名称排在一起 | ||||
|         // 按银行名称排序 | ||||
|         return unionDataSet.orderBy(new String[]{"shjh_bankname"}); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 关闭数据集 | ||||
|      */ | ||||
|     private void closeDataSet(DataSet dataSet) { | ||||
|         if (dataSet != null) { | ||||
|             try { | ||||
|                 dataSet.close(); | ||||
|             } catch (Exception e) { | ||||
|                 log.error("关闭数据集出错", e); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue