1.需求:申请单改成YCSQD_日期_1 2 3累加,跨年后重新从1开始累加,派车单改成申请单编号_CCD
2.找到相关代码
原申请单编号生成代码:YCSQD_20170410_1、YCSQD_20170410_2、YCSQD_20170411_1、YCSQD_20170411_2
@Override
public synchronized String getNextApplyId(){
String prefix = applyIdPrefix;
String date = null;
String oldId = null;
if (Util.isEmpty(applyIdPrefix)) {
prefix = com.jsecode.platform.util.SysConfig.getString("veh.use.apply.applyid.prefix");
applyIdPrefix = prefix;
}
String today = DateUtil.formatDate(new Date(), "yyyyMMdd");
Integer maxApplyId = null;
StringBuffer newId = new StringBuffer();
newId.append(prefix).append("_").append(today).append("_");
if (Util.isNotEmpty(maxApplyIdCache)) {
date = maxApplyIdCache[0].toString();
oldId = maxApplyIdCache[1].toString();
Integer in = null;
if (today.equals(date)) {
in = Integer.parseInt(oldId);
in ++;
} else {
in = 1;
maxApplyIdCache[0] = today;
}
maxApplyIdCache[1] = in;
maxApplyId = in;
} else {
date = today;
String sql = "select apply_id from (select apply_id from vd_veh_use_apply_info where apply_id like ? order by create_time desc) where rownum = 1";
SQLQuery query = this.getSession().createSQLQuery(sql);
query.setParameter(0, likeValue(newId.toString()));
oldId = (String) query.addScalar("apply_id", StandardBasicTypes.STRING).uniqueResult();
Integer in = 1;
if (Util.isNotEmpty(oldId)) {
String sn = oldId.replace(newId, "");
try{
in = Integer.parseInt(sn);
in ++;
}catch(NumberFormatException e){
}
}
maxApplyIdCache = new Object[]{ date, in };
maxApplyId = in;
}
newId.append(maxApplyId);
return newId.toString();
}
修改后申请单编号:YCSQD_20170410_1、YCSQD_20170410_2、YCSQD_20170411_3、YCSQD_20170412_4、YCSQD_20180101_1
修改后的代码:
@Override
public synchronized String getNextApplyId() {
String prefix = applyIdPrefix;
String date = null;
String oldId = null;
if (Util.isEmpty(applyIdPrefix)) {
prefix = com.jsecode.platform.util.SysConfig.getString("veh.use.apply.applyid.prefix");
applyIdPrefix = prefix;
}
String today = DateUtil.formatDate(new Date(), "yyyyMMdd");
String todayYear = DateUtil.formatDate(new Date(), "yyyy");
// String today = "20180101";
// String todayYear = "2018";
Integer maxApplyId = null;
StringBuffer newId = new StringBuffer();
newId.append(prefix).append("_").append(today).append("_");
Integer in = null;
//内存中存在的申请单编号[20170412, 25]
if (Util.isNotEmpty(maxApplyIdCache)) {
//20170412
date = maxApplyIdCache[0].toString();
//2017
String dateYear = date.substring(0, 4);
//25
oldId = maxApplyIdCache[1].toString();
in = Integer.parseInt(oldId);
//说明已经跨年
if (!(todayYear.equals(dateYear))) {
in = 0;
maxApplyIdCache[0] = today;
}
//没有跨年,则累加
in++;
maxApplyIdCache[1] = in;
maxApplyId = in;
} else {
date = today;
String sql = "select apply_id from (select apply_id from vd_veh_use_apply_info where apply_id like ? order by create_time desc) where rownum = 1";
SQLQuery query = this.getSession().createSQLQuery(sql);
query.setParameter(0, likeValue(newId.toString()));
oldId = (String) query.addScalar("apply_id", StandardBasicTypes.STRING).uniqueResult();
//oldTd YCSQD_20170410_15
if (Util.isNotEmpty(oldId)) {
String[] o = oldId.split("_");
//年月日
String oYearMday = o[1];
//年
String oYear = oYearMday.substring(0, 4);
if (!(todayYear.equals(oYear))) {
in = 0;
} else {
String sn = oldId.replace(newId, "");
try {
in = Integer.parseInt(sn);
} catch (NumberFormatException e) {
}
}
in++;
} else {
//1.如果oldId没有值,就去数据库查出最新的一条申请单编号YCSQD_20170410_17
String a = queryApplyId();
String[] a1 = a.split("_");
String inYearMday = a1[1];//数据库中取出年月日20170410
//取出年2017
String year = inYearMday.substring(0, 4);
//2.如果系统的年份和数据库查出来的数据年份不一样,说明已经跨年了
if (!(todayYear.equals(year))) {
in = 0;
} else {
in = Integer.parseInt(a1[2]);
}
in++;
}
maxApplyIdCache = new Object[] { date, in };
maxApplyId = in;
}
newId.append(maxApplyId);
return newId.toString();
}
public String queryApplyId() {
HibernateParams params = new HibernateParams();
StringBuilder sb = new StringBuilder();
sb.append("select apply_id from(select t.apply_id,t.create_time from vd_veh_use_apply_info t order by create_time desc) where rownum = 1");
List list = findBySql(sb.toString());
if(Util.isEmpty(list)){
return null;
}else{
return (String) list.get(0);
}
}
原派车单编号:申请单编号_CCD1、申请单编号_CCD2
原代码
@Override
public synchronized String getNextAssignId(String applyId){
String prefix = assignIdPrefix;
if (Util.isEmpty(assignIdPrefix)) {
prefix = com.jsecode.platform.util.SysConfig.getString("veh.use.apply.assignid.prefix");
assignIdPrefix = prefix;
}
Integer maxAssignId = maxAssignIdCache.get(applyId);
StringBuffer newid = new StringBuffer();
newid.append(applyId).append("_").append(prefix);
if (Util.isNotEmpty(maxAssignId)) {
maxAssignId ++;
} else {
String sql = "select veh_assign_id from (select veh_assign_id from vd_veh_assign_list where apply_id = ? order by create_time desc) where rownum = 1";
SQLQuery query = this.getSession().createSQLQuery(sql);
query.setParameter(0, applyId);
String oldId = (String) query.addScalar("veh_assign_id", StandardBasicTypes.STRING).uniqueResult();
Integer in = 1;
if (Util.isNotEmpty(oldId)) {
String sn = oldId.replace(newid, "");
try{
in = Integer.parseInt(sn);
in ++;
}catch(NumberFormatException e){
}
}
maxAssignId = in;
}
maxAssignIdCache.put(applyId, maxAssignId);
newid.append(maxAssignId);
return newid.toString();
}
修改后派车单编号:申请单编号_CCD
修改后的代码:
@Override
public synchronized String getNextAssignId(String applyId){
String prefix = assignIdPrefix;
if (Util.isEmpty(assignIdPrefix)) {
prefix = com.jsecode.platform.util.SysConfig.getString("veh.use.apply.assignid.prefix");
assignIdPrefix = prefix;
}
/*Integer maxAssignId = maxAssignIdCache.get(applyId);*/
StringBuffer newid = new StringBuffer();
newid.append(applyId).append("_").append(prefix);
/*if (Util.isNotEmpty(maxAssignId)) {
maxAssignId ++;
} else {
String sql = "select veh_assign_id from (select veh_assign_id from vd_veh_assign_list where apply_id = ? order by create_time desc) where rownum = 1";
SQLQuery query = this.getSession().createSQLQuery(sql);
query.setParameter(0, applyId);
String oldId = (String) query.addScalar("veh_assign_id", StandardBasicTypes.STRING).uniqueResult();
Integer in = 1;
if (Util.isNotEmpty(oldId)) {
String sn = oldId.replace(newid, "");
try{
in = Integer.parseInt(sn);
in ++;
}catch(NumberFormatException e){
}
}
maxAssignId = in;
}
maxAssignIdCache.put(applyId, maxAssignId);*/
/*newid.append(maxAssignId);*/
return newid.toString();
}
测试OK: