Java后台生成NO2016012701(代码+年月日+流水号)这样的流水编号

记录今日较大的点。

参考文章:
https://blog.csdn.net/weixin_44538107/article/details/87740611
https://blog.csdn.net/jianqiangdexiaohai/article/details/81240176

项目过程中需要自动生成编号,不在数据库生成,而是在后台代码生成,根据客户对流水号的要求比较高并必须按照规范,而且组合起来清晰易懂并不会有重复。故参考了上述两篇文的代码后,结合自己的业务需要加以改编:
1 首先从数据库查询出已有的最大编号
2 拼成NO+当前时间格式的字符串
3 判断最大编号是否存在,最大编号中是否包含当天时间
4 如果最大编号不存在,则直接生成第一个编号NO201601270001
5 如果最大编号存在且包涵当天时间,则给最大编号加1,拼成编码

注意:以下代码演示里做的是OPP20190801 这样的生成,少了日子。

下面是Controller方法,由于两篇文章其中各自使用的Json类未知,所以 结合我的项目细节加以稍微的改编。项目是springboot加bootstrap+vue。在像上述文章做json数据传值时,结合自己项目的封装返回值R类型,故由前台发起请求,请求方法返回可json串到前台,在进行页面赋值。

@RequestMapping("/getCommentCode")
//@RequiresPermissions("tbusinesschance:save")
public R getCommentCode(){
	List<String> bhList  = new ArrayList<String>();
	String currentMonth = null;
	int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
	if(month < 10) {
		currentMonth = "0" + month; 
	} else {
		currentMonth = month + "";
	}
	List<TBusinesschanceEntity> partList = tBusinesschanceService.getPartList(currentMonth);
	
	for (TBusinesschanceEntity tBusinesschanceEntity : partList) {
		bhList.add(tBusinesschanceEntity.getBusinesschanceid());
	}
	String max_code = "";//最新编号
	String comment_code = "";//做好的编号
	if(bhList.size() > 0) {//得到该月最新(最大号)的一个编号
		max_code = bhList.get(0);
	}
	// 时间字符串产生方式
	SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
	// 组合流水号前一部分,时间字符串,如:201907
	String bcid_pfix = format.format(new Date()); 
	//判断数据库是否有数据
	if (max_code != null && max_code.contains(bcid_pfix)) {
		// 从OOP20190803 截取字符串最后2位,结果:03
		String uid_end = max_code.substring(9);//substring(8) 
		// 把String类型的03转化为int类型的3
		int endNum = Integer.parseInt(uid_end); 
		 // 结果4
		int tmpNum =  endNum + 1; // 100 + 1 + 1 = 结果102
		//已经加了1        再给他在拼成OOP20190804
		comment_code = "OOP" + bcid_pfix + ("0" + tmpNum);
	} else {
		//数据库没数据时,输出OOP20190801
		comment_code = "OOP" + bcid_pfix + "01";
	}
	return R.ok().put("comment_code", comment_code);
}

Js代码
无参—Get请求
$("#businesschanceid").val(r.comment_code) ;值赋给页面

function getCommentCode(){
	$.ajax({
		type: "GET",
	    url: baseURL + "tbusinesschance/getCommentCode",
	    contentType: "application/json",
	    //data: JSON.stringify(ids),
	    success: function(r){
			if(r.code == 0){
				$("#businesschanceid").val(r.comment_code);
			}else{
				alert(r.msg);
			}
		}
	});
}

这样html页面定义的id 前后台交互。

这里再说下xml文件写sql语句 有个问题想说出来
开始,是仿照别人的抄来一段更改:

<select id="queryList" resultType="xxx.server.businesschance.entity.TBusinesschanceEntity">
		select * from t_businesschance bc
		<where>
			bc.`isdel`="1"
		1  	<if test="businesschanceid !=null and businesschanceid.trim() != ''">
		2	    	and bc.`businesschanceid like concat('%',#{value},'%')
------------------------------这里想要的效果是-----------------------------
	Controller传来的value(eg:07)   让businesschanceid 做模糊查询查询出所有是该位是07的编号。
可是不知test的值是否要与下面bc.`businesschanceid      #{value} 一样,所以不论怎么改,照网上
搜的更改方法改了_parameter 都是报	here is no getter for property named 'businesschanceid' in 'class xxx.User'。 实在xml里写sql语句不太会所以这里出错了好久。
	后来调整了sql的写法,然后就对了。

			</if>
		</where>
		<choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

以下是改后的sql

<!-- 查询编号集合 -->
<select id="getPartList" resultType="com.rlc.modules.server.businesschance.entity.TBusinesschanceEntity">
		select * from t_businesschance tbc
		<where>
			tbc.businesschanceid like concat('_______',#{value},'__') 
			order by tbc.id desc
---------------------------不在这里用人家的模板,而是简单写了,就对了。不在报	 	here is no getter for property named 'businesschanceid' in xxx.xxx了
这里的这个错误如果有高手了解,还请告知以下。感谢!!!!!!!
	</where>
	</select>

猜你喜欢

转载自blog.csdn.net/publicstaticfinal/article/details/99486780