package com.changhong.epc.parsing.service.autosubmit.tool.function; import com.changhong.epc.parsing.service.autosubmit.info.FormCpn; import com.changhong.epc.parsing.service.autosubmit.info.impl.cpn.ChildCpn; import com.changhong.epc.parsing.service.autosubmit.prop.Attribute; import com.changhong.epc.parsing.service.autosubmit.tool.FormCpnTool; import com.googlecode.aviator.runtime.function.AbstractFunction; import com.googlecode.aviator.runtime.type.AviatorDouble; import com.googlecode.aviator.runtime.type.AviatorObject; import org.apache.commons.lang3.math.NumberUtils; import java.math.BigDecimal; import java.util.Map; import java.util.Objects; public class Avg extends AbstractFunction{ @Override public String getName() { return "AVG"; } @Override public AviatorObject call(Map env, AviatorObject cpnKey) { Object cpnObj = cpnKey.getValue(env); if(!(cpnObj instanceof FormCpn)) return new AviatorDouble(0); FormCpn parent = ((FormCpn) cpnObj).getParentCpn(); if(!(parent instanceof ChildCpn)) return new AviatorDouble(NumberUtils.createNumber(Objects.toString(parent.get(Attribute.value), "0"))); double sum = FormCpnTool.sumValByCpn((FormCpn) cpnObj); if(BigDecimal.valueOf(sum).compareTo(BigDecimal.valueOf(0)) == 0) return new AviatorDouble(sum); else return new AviatorDouble(sum / ((ChildCpn) parent).getFormCpnValues().size()); } }