优化账账相对问题
This commit is contained in:
		
							parent
							
								
									13b4d92f6f
								
							
						
					
					
						commit
						30a5ae7f0d
					
				|  | @ -0,0 +1,5 @@ | ||||||
|  | <?xml version="1.0" encoding='gb2312'?> | ||||||
|  | 	<module displayname="gl" name="gl"> | ||||||
|  | 		<dependencies> | ||||||
|  | 		</dependencies> | ||||||
|  | 	</module> | ||||||
|  | @ -0,0 +1,94 @@ | ||||||
|  | package nccloud.web.gl.accountrep.action; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Map; | ||||||
|  | import java.util.stream.Collectors; | ||||||
|  | 
 | ||||||
|  | import nc.vo.pub.BusinessException; | ||||||
|  | import nccloud.framework.service.ServiceLocator; | ||||||
|  | import nccloud.pubitf.gl.account.IAccountReportWebService; | ||||||
|  | 
 | ||||||
|  | public class AccountBalanceTotalQueryAction { | ||||||
|  |     public AccountBalanceTotalQueryAction() { | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Object doQuery(Map<String, Object> paraMap) throws BusinessException { | ||||||
|  |         // 科目余额表数据 | ||||||
|  |         Map<String, Object> result = ((IAccountReportWebService) ServiceLocator.find(IAccountReportWebService.class)).queryAccBalance(paraMap); | ||||||
|  |         // 科目辅助余额表数据 | ||||||
|  |         Map<String, Object> result1 = ((IAccountReportWebService) ServiceLocator.find(IAccountReportWebService.class)).querySubjAssBalanceBooks(paraMap); | ||||||
|  |         List<Map<String, Object>> data = (List<Map<String, Object>>) result.get("data"); | ||||||
|  |         List<Map<String, Object>> data1 = (List<Map<String, Object>>) result1.get("data"); | ||||||
|  | 
 | ||||||
|  |         // 过滤掉 data1 中 assname 为空的数据 | ||||||
|  |         data1 = data1.stream() | ||||||
|  |                 .filter(item -> item.get("assname") != null && !item.get("assname").toString().trim().isEmpty()) | ||||||
|  |                 .collect(Collectors.toList()); | ||||||
|  | 
 | ||||||
|  |         // 创建一个映射,根据 acccode 快速查找 data 中的元素 | ||||||
|  |         Map<String, Map<String, Object>> dataMap = data.stream() | ||||||
|  |                 .collect(Collectors.toMap(item -> (String) item.get("acccode"), item -> item)); | ||||||
|  | 
 | ||||||
|  |         // 构造新的 Map,存储每个父级科目的期末余额之和 | ||||||
|  |         Map<String, Double> sumMap = new HashMap<>(); | ||||||
|  | 
 | ||||||
|  |         // 构造新的列表 | ||||||
|  |         List<Map<String, Object>> mergedList = new ArrayList<>(); | ||||||
|  | 
 | ||||||
|  |         for (Map<String, Object> item : data1) { | ||||||
|  |             Map<String, Object> linkMap = (Map<String, Object>) item.get("link"); | ||||||
|  |             String acccode = (String) linkMap.get("acccode"); | ||||||
|  |             String parentAcccode = acccode.substring(0, 4); // 假设父级科目编码是前4位 | ||||||
|  | 
 | ||||||
|  |             Map<String, Object> parentItem = dataMap.get(parentAcccode); | ||||||
|  |             if (parentItem != null) { | ||||||
|  |                 String endlocamountStr = (String) item.get("endlocamount"); | ||||||
|  |                 double endlocamount = parseEndlocamount(endlocamountStr); | ||||||
|  | 
 | ||||||
|  |                 // 更新 sumMap | ||||||
|  |                 sumMap.put(parentAcccode, sumMap.getOrDefault(parentAcccode, 0.0) + endlocamount); | ||||||
|  | 
 | ||||||
|  |                 // 构造新的 Map | ||||||
|  |                 Map<String, Object> newItem = new HashMap<>(); | ||||||
|  |                 newItem.put("pacccode", parentAcccode); | ||||||
|  |                 newItem.put("paccname", parentItem.get("accname")); | ||||||
|  |                 newItem.put("pendlocamount", parentItem.get("endlocamount")); | ||||||
|  |                 newItem.put("acccode", acccode); | ||||||
|  |                 newItem.put("accname", parentItem.get("accname")); // 假设 data1 中没有 accname,使用父级的 accname | ||||||
|  |                 newItem.put("assname", item.get("assname")); | ||||||
|  |                 newItem.put("endlocamount", endlocamount); | ||||||
|  |                 mergedList.add(newItem); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // 计算每个父级科目的 endlocamount 和所有关联 data1 中 endlocamount 的差值 | ||||||
|  |         for (Map<String, Object> item : mergedList) { | ||||||
|  |             String parentAcccode = (String) item.get("pacccode"); | ||||||
|  |             double endlocamount = (double) item.get("pendlocamount"); | ||||||
|  |             double sumEndlocamount = sumMap.get(parentAcccode); | ||||||
|  |             double difference = sumEndlocamount - endlocamount; | ||||||
|  |             item.put("difference", difference); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // 输出合并后的列表 | ||||||
|  |         for (Map<String, Object> item : mergedList) { | ||||||
|  |             System.out.println(item); | ||||||
|  |         } | ||||||
|  |         return mergedList; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // 解析 endlocamount 字符串为 double 类型 | ||||||
|  |     private static double parseEndlocamount(String endlocamountStr) { | ||||||
|  |         if (endlocamountStr == null || endlocamountStr.isEmpty()) { | ||||||
|  |             return 0.0; | ||||||
|  |         } | ||||||
|  |         // 去除逗号 | ||||||
|  |         endlocamountStr = endlocamountStr.replace(",", ""); | ||||||
|  |         try { | ||||||
|  |             return Double.parseDouble(endlocamountStr); | ||||||
|  |         } catch (NumberFormatException e) { | ||||||
|  |             return 0.0; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue