最小可用值的获取之_根据当前科目编码获取最小可用值

Demo

    /**
     * 获取一个新的科目编码
     *
     * @param parentCode 上级科目代码
     * @author 吕嘉鸣
     */
    @Override
    public String getNewCode(String subjectTableName, Long deptId, String parentCode) {
        String oldCode = parentCode;
        parentCode = parentCode + "___";
        String newCode = sysSubjectMapper.getNewCode(subjectTableName, deptId, parentCode);
        if (null == newCode || "".equals(newCode)) {
            return oldCode + "001";
        } else {
            String tailCode = newCode.substring(newCode.length() - 3, newCode.length());

            newCode = oldCode + String.format("%03d", Integer.valueOf(tailCode) + 1);
        }
        return newCode;
    }

    /**
     * 根据当前科目编码获取最小可用值
     *
     * @param tableName 科目表名
     * @param deptId 机构id
     * @param parentCode 上级科目代码
     * @return 未使用的最小的科目编码
     * @author 吕嘉鸣(idea)
     */

    @Select(" SELECT code AS newCode " +
            " FROM ${tableName} AS t1 WHERE  t1.`code` LIKE #{parentCode}  AND  right(code, 3) != '999' AND t1.deleted = 0" +
            " AND not exists(select code  FROM ${tableName} AS t2 WHERE t2.dept_id = #{deptId} AND t2.deleted = 0 AND  CONVERT(t2.code,SIGNED)=CONVERT(t1.code,SIGNED)+1  ) order by CONVERT(right(code, 3),SIGNED) limit 1;")
    String getNewCode(String tableName,Long deptId,String parentCode);
原文地址:https://www.cnblogs.com/ideaAI/p/14762372.html