|

楼主 |
发表于 2017-3-22 09:09:20
|
显示全部楼层
不是啊,没有用默认的流水号,是自己写的java方法,这应该还没自动添加到数据库把。
- // 获得基础信息自动编码
- @SuppressWarnings("finally")
- public static JSONObject getBaseCode(JSONObject params, ActionContext context) throws SQLException, NamingException {
-
- String sourceName = "AllInOneDataSource";
- String modelName = params.getString("modelName");
- String tableName = params.getString("tableName");
- String newCode = "";
-
- String sqlGetCodePrefix = "select CONCAT(prefix"+
- ", IF(IFNULL(isHasYear,0)=1,year(CURDATE()),'')"+
- ", IF(IFNULL(isHasMonth,0)=1,right(CONCAT('0',month(CURDATE())),2),'')"+
- ", IF(IFNULL(isHasDay,0)=1,right(CONCAT('0',day(CURDATE())),2),'')"+
- ", IF(IFNULL(isHasHour,0)=1,right(CONCAT('0',hour(CURTIME())),2),'')"+
- ", IF(IFNULL(isHasMinute,0)=1,right(CONCAT('0',MINUTE(CURTIME())),2),'')"+
- ", IF(IFNULL(isHasSecond,0)=1,right(CONCAT('0',SECOND(CURTIME())),2),'')"+
- ") as codePrefix"+
- ",IFNULL(isHasSequence,0) as isHasSequence"+
- ",if(IFNULL(isHasSequence,0)=1,sequenceLength,0) as sequenceLength"+
- ",IFNULL(currentCode,0) as currentCode"+
- " from sys_coderule"+
- " left join sys_template_model on sys_coderule.modelUid = sys_template_model.code"+
- " where modelName = '"+ modelName + "' and tableName = '"+ tableName +"' ";
-
- Connection conn = context.getConnection(sourceName);
- Statement stmt = null;
- Statement stmtUpdate = null;
- ResultSet rs = null;
- JSONObject result = new JSONObject();
- try{
- conn.setAutoCommit(false);
-
- stmt = conn.createStatement();
- rs = stmt.executeQuery(sqlGetCodePrefix);
-
- int newCurrentCode = 0;
- boolean isHasSequence = false;
- if (rs.next()) {
- String codePrefix = rs.getString("codePrefix");
- isHasSequence = rs.getBoolean("isHasSequence");
-
- if(isHasSequence)
- {
- int sequenceLength = rs.getInt("sequenceLength");
- newCurrentCode = rs.getInt("currentCode") + 1;
-
- StringBuilder strBuilder = new StringBuilder();
- for(int i=0;i<sequenceLength;i++){
- strBuilder.append("0");
- }
- String newSequence = strBuilder.toString() + newCurrentCode ;
- newCode = codePrefix + newSequence.substring(newSequence.length() - sequenceLength);
- }
- else
- {
- newCode = codePrefix;
- }
- }
-
- rs.close();
-
- if(isHasSequence && !"".equals(newCode)){
- stmtUpdate = conn.createStatement();
- stmtUpdate.executeUpdate("update sys_coderule INNER JOIN sys_template_model on sys_coderule.modelUid = sys_template_model.code set currentCode = " + newCurrentCode + " where sys_template_model.modelName='"+ modelName + "' ");
- }
-
- conn.commit();
- // 如果modelName未在sys_coderule进行配置,则newCode为空字符串
- result.put("newCode", newCode);
-
- }catch(Exception ex){
- conn.rollback();
- result = new JSONObject();
- // 如果发生异常,则newCode为空字符串
- result.put("newCode", "");
- } finally {
- if(stmt != null) stmt.close();
- if(rs != null) rs.close();
- if(conn!=null)conn.close();
-
- return result;
- }
-
- }
复制代码
|
|