CurrencyMethod.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. *
  3. *
  4. *
  5. */
  6. package com.hboxs.template.method;
  7. import com.hboxs.common.Setting;
  8. import com.hboxs.common.utils.SettingUtils;
  9. import freemarker.template.SimpleScalar;
  10. import freemarker.template.TemplateMethodModel;
  11. import freemarker.template.TemplateModelException;
  12. import org.apache.commons.lang.StringUtils;
  13. import org.springframework.stereotype.Component;
  14. import java.math.BigDecimal;
  15. import java.util.List;
  16. /**
  17. * 模板方法 - 货币格式化
  18. */
  19. @Component("currencyMethod")
  20. public class CurrencyMethod implements TemplateMethodModel {
  21. @SuppressWarnings("rawtypes")
  22. public Object exec(List arguments) throws TemplateModelException {
  23. if (arguments != null && !arguments.isEmpty() && arguments.get(0) != null && StringUtils.isNotEmpty(arguments.get(0).toString())) {
  24. boolean showSign = false;
  25. boolean showUnit = false;
  26. if (arguments.size() == 2) {
  27. if (arguments.get(1) != null) {
  28. showSign = Boolean.valueOf(arguments.get(1).toString());
  29. }
  30. } else if (arguments.size() > 2) {
  31. if (arguments.get(1) != null) {
  32. showSign = Boolean.valueOf(arguments.get(1).toString());
  33. }
  34. if (arguments.get(2) != null) {
  35. showUnit = Boolean.valueOf(arguments.get(2).toString());
  36. }
  37. }
  38. Setting setting = SettingUtils.get();
  39. BigDecimal amount = new BigDecimal(arguments.get(0).toString());
  40. String price = setting.setScale(amount).toString();
  41. if (showSign) {
  42. price = setting.getCurrencySign() + price;
  43. }
  44. if (showUnit) {
  45. price += setting.getCurrencyUnit();
  46. }
  47. return new SimpleScalar(price);
  48. }
  49. return null;
  50. }
  51. }