All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nbcp.myoql.code.generator.tool.CrudCodeGeneratorUtil.kt Maven / Gradle / Ivy

The newest version!
package nbcp.myoql.code.generator.tool

import nbcp.base.comm.JsonMap
import nbcp.base.extend.AllFields
import nbcp.base.extend.AsString
import nbcp.myoql.db.comm.BaseMetaData
import nbcp.myoql.db.es.component.EsBaseMetaEntity
import nbcp.myoql.db.mongo.component.MongoBaseMetaCollection
import nbcp.myoql.db.sql.base.SqlBaseMetaTable
import nbcp.myoql.code.generator.tool.freemarker.*

object CrudCodeGeneratorUtil {
    /**
     * 生成基础的CRUD接口
     */
    @JvmStatic
    fun genMongoMvcCrud(group: String, pkg: String, entity: BaseMetaData): String {
        return gen(group, entity, "/myoql-template/mongo/kotlin-mvc-mongo-template-crud.ftl").replace("@pkg@", pkg);
    }

    @JvmStatic
    fun genMySqlMvcCrud(group: String, pkg: String, entity: BaseMetaData): String {
        return gen(group, entity, "/myoql-template/mysql/kotlin-mvc-mysql-template-crud.ftl").replace("@pkg@", pkg);
    }

    @JvmStatic
    fun genEsMvcCrud(group: String, pkg: String, entity: BaseMetaData): String {
        return gen(group, entity, "/myoql-template/es/kotlin-mvc-es-template-crud.ftl").replace("@pkg@", pkg);
    }

    /**
     * 生成Vue列表页面
     */
    @JvmStatic
    fun genVue2List(group: String, entity: BaseMetaData): String {
        return gen(group, entity, "/vue2-template/vue-list-template.ftl");
    }


    @JvmStatic
    fun genVue3List(group: String, entity: BaseMetaData): String {
        return gen(group, entity, "/vue3-template/vue-list-template.ftl");
    }
    /**
     * 生成Vue卡片页面
     */
    @JvmStatic
    fun genVue2Card(group: String, entity: BaseMetaData): String {
        return gen(group, entity, "/vue2-template/vue-card-template.ftl");
    }

    @JvmStatic
    fun genVue3Card(group: String, entity: BaseMetaData): String {
        return gen(group, entity, "/vue3-template/vue-card-template.ftl");
    }

    /**
     * 生成Vue引用
     */
    @JvmStatic
    fun genVue2Ref(group: String, entity: BaseMetaData): String {
        return gen(group, entity, "/vue2-template/vue-ref-template.ftl");
    }
    /**
     * 生成Vue引用
     */
    @JvmStatic
    fun genVue3Ref(group: String, entity: BaseMetaData): String {
        return gen(group, entity, "/vue3-template/vue-ref-template.ftl");
    }

    @JvmStatic
    fun genVue2RefInputTable(entityClass: Class<*>): String{
        return proc(
            "/vue2-template/vue-ref-input-table-template.ftl",
            CrudCodeTemplateData(
                "",
                entityClass,
                "",
                ""
            )
        )
    }
    @JvmStatic
    fun genVue3RefInputTable(entityClass: Class<*>): String{
        return proc(
            "/vue3-template/vue-ref-input-table-template.ftl",
            CrudCodeTemplateData(
                "",
                entityClass,
                "",
                ""
            )
        )
    }
    @JvmStatic
    fun genVue2RefCard(entityClass: Class<*>): String {
        return proc(
            "/vue2-template/vue-ref-card-template.ftl",
            CrudCodeTemplateData(
                "",
                entityClass,
                "",
                ""
            )
        )
    }

    @JvmStatic
    fun genVue3RefCard(entityClass: Class<*>): String {
        return proc(
            "/vue3-template/vue-ref-card-template.ftl",
            CrudCodeTemplateData(
                "",
                entityClass,
                "",
                ""
            )
        )
    }


    @JvmStatic
    fun proc(fileName: String, jsonValue: CrudCodeTemplateData): String {
        return FreemarkerUtil.process(fileName, jsonValue)
    }

    private fun gen(group: String, metaEntity: BaseMetaData, fileName: String): String {

        var idKey = ""
        var entityClass = metaEntity.entityClass

        if (metaEntity is MongoBaseMetaCollection<*>) {
            idKey = "id"
        } else if (metaEntity is SqlBaseMetaTable<*>) {
            idKey = metaEntity.getAutoIncrementKey().AsString {metaEntity.getUks().firstOrNull { it.size == 1 }?.firstOrNull().AsString("id") }
        } else if (metaEntity is EsBaseMetaEntity<*>) {
            idKey = "id"
        }

        return proc(
            fileName,
            CrudCodeTemplateData(
                group,
                entityClass,
                metaEntity.tableName,
                idKey
            )
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy