package com.jeeplus.modules.api.utils;
import com.jeeplus.modules.api.mapper.LlGiftsExchangeApiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
/**
* 自动生成流水编号的工具类
* 格式为:当天yyyyMMdd+四位有序编号,如201911010001
*/
@Component
public class CreateCodeUtil {
@Autowired
private LlGiftsExchangeApiMapper llGiftsExchangeApiMapper;
public String createCode(){
//调用取code最大值方法
Map<String,Object> map = llGiftsExchangeApiMapper.getMaxCode(); //取兑换申请表的code字段最大值 (String sql = "SELECT MAX(a.code+0) maxCode FROM ll_gifts_exchange a";)
String resultCode = "";
String maxCode = "";
Object object = map.get("maxCode");
if (object!=null) {
maxCode = new DecimalFormat("0").format((double)object);
}
//指定生成的时间格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
String preCode = simpleDateFormat.format(new Date());
//如果如果当天有生成的编码,直接在尾号加1;如果没有直接生成新的编码
if (maxCode!=null&&maxCode.contains(preCode)){
Integer endCode = Integer.parseInt(maxCode.substring(8)); // 截取字符串最后四位,结果:0001
//计算结果
endCode = endCode+10000+1; // 结果10002
resultCode = preCode+endCode.toString().substring(1);// 把10002首位的1去掉,再拼成201911010002字符串
}else {
resultCode = preCode+"0001";
}
return resultCode;
}
}