1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- /*
- *
- *
- *
- */
- package com.hboxs.template.method;
- import com.hboxs.common.Setting;
- import com.hboxs.common.utils.SettingUtils;
- import freemarker.template.SimpleScalar;
- import freemarker.template.TemplateMethodModel;
- import freemarker.template.TemplateModelException;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.stereotype.Component;
- import java.math.BigDecimal;
- import java.util.List;
- /**
- * 模板方法 - 货币格式化
- */
- @Component("currencyMethod")
- public class CurrencyMethod implements TemplateMethodModel {
- @SuppressWarnings("rawtypes")
- public Object exec(List arguments) throws TemplateModelException {
- if (arguments != null && !arguments.isEmpty() && arguments.get(0) != null && StringUtils.isNotEmpty(arguments.get(0).toString())) {
- boolean showSign = false;
- boolean showUnit = false;
- if (arguments.size() == 2) {
- if (arguments.get(1) != null) {
- showSign = Boolean.valueOf(arguments.get(1).toString());
- }
- } else if (arguments.size() > 2) {
- if (arguments.get(1) != null) {
- showSign = Boolean.valueOf(arguments.get(1).toString());
- }
- if (arguments.get(2) != null) {
- showUnit = Boolean.valueOf(arguments.get(2).toString());
- }
- }
- Setting setting = SettingUtils.get();
- BigDecimal amount = new BigDecimal(arguments.get(0).toString());
- String price = setting.setScale(amount).toString();
- if (showSign) {
- price = setting.getCurrencySign() + price;
- }
- if (showUnit) {
- price += setting.getCurrencyUnit();
- }
- return new SimpleScalar(price);
- }
- return null;
- }
- }
|