commit
345e48cdd9
@ -0,0 +1,3 @@
|
||||
alter table sys_clients add column settle_remark_tpl varchar(100) null default null after settle_hour;
|
||||
|
||||
insert into sys_configs values ('settle.remark_template','RoyalPay{date(MMdd)}{moniker}','清算文件备注模板');
|
@ -0,0 +1,93 @@
|
||||
package au.com.royalpay.payment.manage.support.abafile;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Create by davep at 2019-07-18 15:20
|
||||
*/
|
||||
public class SettleRemarkTemplateDescriber {
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
private final Pattern pattern = Pattern.compile("\\{[^\\{\\}]+\\}");
|
||||
private final JSONObject settle;
|
||||
private final Date settleDate;
|
||||
private String template;
|
||||
private List<Replacer> replacers;
|
||||
|
||||
public SettleRemarkTemplateDescriber(String template, JSONObject settle, Date settleDate) {
|
||||
this.template = template;
|
||||
this.settle = settle;
|
||||
this.settleDate = settleDate;
|
||||
replacers = new ArrayList<>();
|
||||
replacers.add(new ClientMonikerReplacer());
|
||||
replacers.add(new SettleDateReplacer());
|
||||
}
|
||||
|
||||
public String compile() {
|
||||
Matcher matcher = pattern.matcher(template);
|
||||
StringBuilder target = new StringBuilder();
|
||||
int startPos = 0;
|
||||
while (matcher.find()) {
|
||||
int start = matcher.start();
|
||||
int end = matcher.end();
|
||||
target.append(template, startPos, start);
|
||||
String group = StringUtils.substring(matcher.group(), 1, -1);
|
||||
String replacement = replacers.stream().filter(replacer -> replacer.match(group))
|
||||
.limit(1)
|
||||
.map(replacer -> replacer.replacement(group, SettleRemarkTemplateDescriber.this))
|
||||
.findFirst().orElse("");
|
||||
target.append(replacement);
|
||||
startPos = end;
|
||||
}
|
||||
target.append(template.substring(startPos));
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
interface Replacer {
|
||||
boolean match(String group);
|
||||
|
||||
String replacement(String group, SettleRemarkTemplateDescriber describer);
|
||||
}
|
||||
|
||||
class ClientMonikerReplacer implements Replacer {
|
||||
|
||||
@Override
|
||||
public boolean match(String group) {
|
||||
return "moniker".equals(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String replacement(String group, SettleRemarkTemplateDescriber describer) {
|
||||
return describer.settle.getString("client_moniker");
|
||||
}
|
||||
}
|
||||
|
||||
class SettleDateReplacer implements Replacer {
|
||||
private final String regex = "^date\\((\\w+)\\)$";
|
||||
|
||||
@Override
|
||||
public boolean match(String group) {
|
||||
return group.matches(regex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String replacement(String group, SettleRemarkTemplateDescriber describer) {
|
||||
Pattern dtPattern = Pattern.compile(regex);
|
||||
Matcher matcher = dtPattern.matcher(group);
|
||||
if (matcher.find()) {
|
||||
String datePattern = matcher.group(1);
|
||||
return DateFormatUtils.format(describer.settleDate, datePattern);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package au.com.royalpay.payment.manage.support.abafile;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Create by davep at 2019-07-18 15:27
|
||||
*/
|
||||
public class SettleRemarkTemplateDescriberTest {
|
||||
|
||||
@Test
|
||||
public void compile() {
|
||||
JSONObject settle = new JSONObject();
|
||||
settle.put("client_moniker", "PINE");
|
||||
SettleRemarkTemplateDescriber describer = new SettleRemarkTemplateDescriber("RoyalPay{date(MMdd)}{moniker}", settle, DateTime.parse("2019-07-18").toDate());
|
||||
String result = describer.compile();
|
||||
Assert.assertEquals("RoyalPay0718PINE", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compile2() {
|
||||
JSONObject settle = new JSONObject();
|
||||
settle.put("client_moniker", "AUPO");
|
||||
SettleRemarkTemplateDescriber describer = new SettleRemarkTemplateDescriber("Pay By App {date(ddMMyy)}", settle, DateTime.parse("2019-07-18").toDate());
|
||||
String res = describer.compile();
|
||||
Assert.assertEquals("Pay By App 180719", res);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue