// ==========================================================================
// 所属模块：fn
// 生成日期：2024/10/16
// 生成路径: /api/x/function.js
// 生成人：xScript_Engine
// 数据表：system
// ==========================================================================

response.headers.add('X-Script-Version', 'fn-24.10.20')

function main() {
    // APPID认证
    'use api/lib/userPassport.js'

    // runtime.exec('api/x/hook.js', ...args)

    let fnId = payload.get().query.f

    // let app = SQL.cache.query(DB_NAME, 1800, 'SELECT * FROM fn_apps WHERE fn_id =?', fnId)
    let app = SQL.query(DB_NAME, 'SELECT * FROM fn_apps WHERE delete_time IS NULL AND (fn_id =? OR alias =?);', fnId, fnId)
    if (!app) {
        return errMsg(500, "逻辑函数不存在")
    }
    DB_NAME = 'develop_sql'
    app = app[0]

    if (!app.cache_life || app.cache_life == 0) {
        // 声明缓存
        payload.set.header("Cache-Control", ['skip-cache'])
        response.headers.set(
            'Cache-Control',
            'no-transform,no-cache,immutable'
        )
    }

    return runtime.call(`${app.code};main()`)
}