
// ==========================================================================
// 所属模块：DevGenCode
// 生成日期：2024-02-20 21:43:27 +0800 CST
// 生成路径: api/dm/template/function.js
// 生成人：xScript_Engine
// 数据表：dm_tables - DevGenTables
//        dm_tables_column - DevGenTablesColumn
// ==========================================================================

function main() {
    // 验证用户权限
    'use api/user/acl/excerpt.js'

    const tableId = payload.get().path.split('/').pop()

    let tablesInfo = SQL.query('system_sql', 'SELECT * FROM dm_tables WHERE table_id =?', tableId)
    if (!tablesInfo) {
        return errMsg(404, '未找到数据')
    }

    tablesInfo = tablesInfo[0]

    let tableInfo = Object.keys(tablesInfo).reduce((acc, key) => {
        acc[underscoreToCamel(key.toLowerCase())] = tablesInfo[key];
        return acc;
    }, {});

    let columns = SQL.query('system_sql', 'SELECT * FROM dm_table_columns WHERE table_id =?', tableId)
    if (!columns) {
        return errMsg(404, '未找到数据')
    }

    let result = []
    columns.forEach(t => {
        const newObj = Object.keys(t).reduce((acc, key) => {
            acc[underscoreToCamel(key.toLowerCase())] = t[key];
            return acc;
        }, {});
        result.push(newObj)
    });

    tableInfo.columns = result

    // return okMsg(tableInfo)

    let et = fs.read('api/dm/template/vue/edit-vue.template').toString()
    let lt = fs.read('api/dm/template/vue/list-vue.template').toString()

    let ef = ex.template(et, tableInfo).toString()
    let lf = ex.template(lt, tableInfo).toString()

    let res = {
        edit: ef,
        list: lf
    }

    return okMsg(res)
}