生成日期相关流水号代码片段,流水号格式:日期+三位流水号
2 回复
@Table("BA_KV")
public class KV {
@Name
@Column("k")
private String k;
@Column("v")
private int v;
@Column("c")
private String c;
public String getK() {
return k;
}
public void setK(String k) {
this.k = k;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
}
@IocBean
public class OrderService {
@Inject
private Dao dao;
/**
* 生成订单流水号,格式:20160721001,
* @return
*/
public synchronized String generaterNextNumber() {
Cnd cnd = Cnd.NEW();
cnd.and("c", "=", "订单");
String k = Times.format("yyyyMMdd", Times.now());
cnd.and("k", "=", k);
KV kv = dao.fetch(KV.class, cnd);
if (kv == null) {
kv = new KV();
kv.setK(k);
kv.setV(1);
kv.setC("订单");
dao.insert(kv);
} else {
kv.setV(kv.getV() + 1);
dao.update(kv);
}
int v = kv.getV();
return String.format("%s%03d", k, v);
}
}
添加回复
请先登陆