需求:
- 直接出资比例:计算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);
}
}
}