需求:
- 直接出資比例:計算A單位對B、C......單位的出資比例 (比如:A對B投資 80%)
- 間接出資比例:計算A單位對B單位投資的Ba、Bb......的間接出資比例 (比如:B投資Ba 60% 則A對Ba的投資比例計算方式:80% * 60%)
- 間接出資比例:計算A單位對Ba單位投資的Ba1、Ba2......的間接出資比例
- ......
代碼演示:
XML形式(有層級關係 ):
<Node unitTitle = "" unitCode ="" > <system_attrs> <attr code="" value=""/> </system_attrs> <VirtualNodeType/> <Node unitTitle = "" unitCode ="" > <system_attrs> <attr code="" value=""/> </system_attrs> <VirtualNodeType/> <Node> </Node> <Node> </Node> </Node> <Node> <Node> </Node> <Node> </Node> </Node> </Node>
package com.xxx.xxx.xxx; import java.math.BigDecimal; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.xxx.runner.Runner; import com.xxx.runner.RunnerParameter; import com.xxx.core.Context; /** * 出資比例數據提取計劃任務 * */ public class BillStockRightScalePlanTaskRunner extends Runner { public static Map<String, BigDecimal> scaleMap = new HashMap<String, BigDecimal>(); @Override public boolean excute(RunnerParameter param, Context context) { scaleMap.clear(); getStockRightScale(context); return true; } /** * 計算比例 * * @param context */ public static void getStockRightScale(Context context) { // 通過某方法調用得到對象 Tree tree String xml = tree.getXmlData(); // 解析字符串 Document document = null; try { document = DocumentHelper.parseText(xml); Element root = document.getRootElement(); Map<Element,BigDecimal> sonMap = new HashMap<Element,BigDecimal>(); Iterator<Element> nodeElement = root.elementIterator("Node"); while (nodeElement.hasNext()) { Element second = (Element) nodeElement.next(); String code = second.attributeValue("unitCode"); Iterator attrs = second.elementIterator("system_attrs"); while (attrs.hasNext()) { Element secondSystemAttrs = (Element) attrs.next(); Iterator secondAttr = secondSystemAttrs.elementIterator("attr"); while (secondAttr.hasNext()) { Element attrValue = (Element) secondAttr.next(); BigDecimal scale = new BigDecimal(attrValue.attributeValue("value").substring(2)); scaleMap.put(code, scale); sonMap.put(second,scale); } } } // 後續層級 遞歸 countStockRightScale(sonMap); // Set<String> keySet = scaleMap.keySet(); // for (String str : keySet) { // if(scaleMap.get(str).compareTo(new BigDecimal("100.00")) < 1){ // //System.out.println(str+"**********"+scaleMap.get(str)); // } // } } catch (DocumentException e) { e.printStackTrace(); } } /** * 後續層級 * @param context * @param sonMap */ private static void countStockRightScale(Map<Element, BigDecimal> sonMap) { Map<String, BigDecimal> tempScaleMap = new HashMap<String, BigDecimal>(); Map<Element,BigDecimal> tempMap = new HashMap<Element,BigDecimal>(); if(sonMap!= null && sonMap.size()>0){ Set<Entry<Element,BigDecimal>> entrySet = sonMap.entrySet(); for (Entry<Element, BigDecimal> entry : entrySet) { BigDecimal scale = entry.getValue(); Element element = entry.getKey(); Iterator<Element> nodeElement = element.elementIterator("Node"); while (nodeElement.hasNext()) { Element tempElement = (Element) nodeElement.next(); String code = tempElement.attributeValue("unitCode"); Iterator attrs = tempElement.elementIterator("system_attrs"); while (attrs.hasNext()) { Element secondSystemAttrs = (Element) attrs.next(); Iterator secondAttr = secondSystemAttrs.elementIterator("attr"); while (secondAttr.hasNext()) { Element attrValue = (Element) secondAttr.next(); BigDecimal value = new BigDecimal(attrValue.attributeValue("value").substring(2)); BigDecimal theScale = scale.divide(new BigDecimal("100")).multiply(value); if(scaleMap.get(code)!=null){ tempScaleMap.put(code, scaleMap.get(code).add(theScale).setScale(2,BigDecimal.ROUND_HALF_UP)); }else{ tempScaleMap.put(code, theScale.setScale(2,BigDecimal.ROUND_HALF_UP)); } tempMap.put(tempElement, theScale); } } } } scaleMap.putAll(tempScaleMap); } if(tempScaleMap.size()>0){ countStockRightScale(tempMap); } } }