import com.dderp.common.entity.order.OrderPart import com.dderp.common.entity.order.OrderPartParamValue import com.dderp.common.entity.product.PartParameter import com.dderp.common.entity.product.ProductInfo import org.apache.commons.lang3.StringUtils class GroovyExtensions { static void apply() { List.metaClass { findQuotedElement = { Map conditions -> delegate.find { element -> conditions.every { condition, value -> element[condition] == value } } } findQuotedElements = { Map conditions -> delegate.findAll { element -> conditions.every { condition, value -> element[condition] == value } } } } //下面的代码参考官网的元编程 OrderPart.metaClass { quoteProduct = { ProductInfo product -> delegate.metaClass.@"$product" = product return delegate } quoteParameter = {String paramName -> def orderPart = delegate as OrderPart def productInfo = delegate.product as ProductInfo def partInfo = productInfo.findPartInfo(orderPart.idPart) PartParameter partParameter = partInfo.parameterList.find {(it.productParameter != null) && (StringUtils.equalsIgnoreCase(it.productParameter.parameterName, paramName)) } if (partParameter == null) { return null } OrderPartParamValue orderPartParamValue = orderPart.paramValueList.find {it.idParameter == partParameter.idParameter} if (orderPartParamValue == null) { return null } return orderPartParamValue } quoteParameterValue = {String paramName -> def orderPart = delegate as OrderPart OrderPartParamValue orderPartParamValue = orderPart.quoteParameter paramName if (orderPartParamValue == null) { return null } return orderPartParamValue.paramValue } } } }