GroovyExtensions.groovy 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import com.dderp.common.entity.order.OrderPart
  2. import com.dderp.common.entity.order.OrderPartParamValue
  3. import com.dderp.common.entity.product.PartParameter
  4. import com.dderp.common.entity.product.ProductInfo
  5. import org.apache.commons.lang3.StringUtils
  6. class GroovyExtensions {
  7. static void apply() {
  8. List.metaClass {
  9. findQuotedElement = { Map<String, String> conditions ->
  10. delegate.find { element ->
  11. conditions.every { condition, value -> element[condition] == value }
  12. }
  13. }
  14. findQuotedElements = { Map<String, String> conditions ->
  15. delegate.findAll { element ->
  16. conditions.every { condition, value -> element[condition] == value }
  17. }
  18. }
  19. }
  20. //下面的代码参考官网的元编程
  21. OrderPart.metaClass {
  22. quoteProduct = { ProductInfo product ->
  23. delegate.metaClass.@"$product" = product
  24. return delegate
  25. }
  26. quoteParameter = {String paramName ->
  27. def orderPart = delegate as OrderPart
  28. def productInfo = delegate.product as ProductInfo
  29. def partInfo = productInfo.findPartInfo(orderPart.idPart)
  30. PartParameter partParameter = partInfo.parameterList.find {(it.productParameter != null) && (StringUtils.equalsIgnoreCase(it.productParameter.parameterName, paramName)) }
  31. if (partParameter == null) {
  32. return null
  33. }
  34. OrderPartParamValue orderPartParamValue = orderPart.paramValueList.find {it.idParameter == partParameter.idParameter}
  35. if (orderPartParamValue == null) {
  36. return null
  37. }
  38. return orderPartParamValue
  39. }
  40. quoteParameterValue = {String paramName ->
  41. def orderPart = delegate as OrderPart
  42. OrderPartParamValue orderPartParamValue = orderPart.quoteParameter paramName
  43. if (orderPartParamValue == null) {
  44. return null
  45. }
  46. return orderPartParamValue.paramValue
  47. }
  48. }
  49. }
  50. }