代码生成支持机构索引

This commit is contained in:
2024-09-28 15:13:46 +08:00
parent 4d5b531f2f
commit 8f40e93de7
2 changed files with 58 additions and 0 deletions

43
script/addIndex.groovy Normal file
View File

@@ -0,0 +1,43 @@
import groovy.io.FileType
def camelToSnake(String str) {
str = str[0].toLowerCase() + str[1..-1]
if (str.endsWith("Entity")) {
str = str.substring(0, str.length() - 6)
}
return str.replaceAll("([A-Z]+)", "_\$1").toLowerCase()
}
new File("../src/main/java").eachFileRecurse(FileType.FILES) {
if (it.name.endsWith(".java")) {
def content = it.text
if (content.contains("extends OrgCommonEntity")) {
if (content.contains("@Table")) {
println("文件名: ${it.name} 包含table注解, 跳过")
} else {
def lines = it.readLines()
def lineNo = lines.findIndexOf { it.contains("extends OrgCommonEntity") }
lines.add(lineNo , """
@jakarta.persistence.Table(indexes = @jakarta.persistence.Index(name ="idx_${camelToSnake(it.name.substring(0, it.name.indexOf(".java")))}_org_id", columnList = "organization_id"))
""")
it.text = lines.join("\n")
println("文件名: ${it.name} 添加索引成功")
}
}
}
}