流水號的工具類

@Component
public class CreateCodeUtil {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public  String createCode(){
        //查詢數據庫中最大的流水號
        String sql = "select MAX(a.FILE_CODE+0) maxCode FROM t_file_info a";
        List<Map<String,Object>> list;
        //調用templet方法
        list = jdbcTemplate.queryForList(sql);
        String resultCode = "";
        String maxCode = "";
        Object object = list.get(0).get("maxCode");
        if (list.size() > 0 && 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));
            //計算結果
            endCode = endCode+10000+1;
            resultCode = preCode+endCode.toString().substring(1);
        }else {
            resultCode = preCode+"0001";
        }
        return resultCode;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章