import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
/*
* Available context bindings:
* SELECTION Iterable
packageName = "com.hongkun.bean.hkt;"
typeMapping = [
(~/(?i)int/) : "Integer",
(~/(?i)float|double|decimal|real/): "double",
(~/(?i)datetime|timestamp|date/) : "Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) }
}
def generate(table, dir) {
def className = javaName(table.getName(), true)
def classConment = table.getComment()
def fields = calcFields(table)
def dateStr = new Date().toLocaleString();
new File(dir, className + ".java").withPrintWriter("UTF-8") { out -> generate(out, className, classConment, fields,dateStr) }
}
def generate(out, className, classConment, fields,dateStr) {
out.println "package $packageName"
out.println ""
out.println "import io.swagger.annotations.ApiModel;"
out.println "import io.swagger.annotations.ApiModelProperty;"
out.println "import lombok.Data;"
out.println ""
out.println "import java.io.Serializable;"
out.println "import java.util.Date;"
out.println ""
out.println "/**"
out.println " * Description: ${classConment}"
out.println " *"
out.println " * @author fengxiaoyang"
out.println " * @date ${dateStr}"
out.println " */"
out.println "@Data"
out.println "@ApiModel(\"${classConment}\")"
out.println "public class $className implements Serializable {"
out.println ""
// out.println "\tprivate static final long serialVersionUID = " + Math.abs(new Random().nextLong()) + "L;"
// out.println ""
fields.each() {
// if (isNotEmpty(it.comment)) out.println "\t/** ${it.comment} */"
if (it.annos != "") out.println " ${it.annos}"
out.println "\tprivate ${it.type} ${it.name};"
out.println ""
}
out.println ""
out.println "}"
}
def calcFields(table) {
DasUtil.getColumns(table).reduce([]) { fields, col ->
def spec = Case.LOWER.apply(col.getDataType().getSpecification())
def typestr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
def comment = col.getComment()
fields += [[
name : fieldName(col.getName(), false),
type : typestr,
comment: comment,
annos : "\t@ApiModelProperty(value = \"" + comment + "\" )"]]
}
}
def javaName(str, capitalize) {
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
def isNotEmpty(content) {
return content != null && content.toString().trim().length() > 0
}
/**
* 我的字段会给字典字段和外键字段增加标识头,这里转属性时去掉
* @param str
* @param capitalize
* @return
*/
def fieldName(str, capitalize) {
//去除开头的标识
if (str.startsWith("dict_")) str = str.substring(5)
if (str.startsWith("fk_")) str = str.substring(3)
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
.collect { Case.LOWER.apply(it).capitalize() }
.join("")
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
capitalize || s.length() == 1 ? s : Case.LOWER.apply(s[0]) + s[1..-1]
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。