// 获取数据
function sqlGenSelect() {
    let userAccess
    // 判断用户表权限
    if (TABLE_INFO.is_acl && !ALLOW_PUBLIC_ACCESS) {
        let udr = []
        udr.push(`'${USER_INFO.user_id}'`)
        udr.push(`'${USER_INFO.role_id}'`)
        udr.push(`'${USER_INFO.organization_id}'`)

        let columnIds = []
        TABLE_STRUCT.forEach(item => {
            if (item.is_acl == 1) {
                columnIds.push(`'${item.column_id}'`)
            }

            if (item.search_value) {
                searchStr += ` AND ${item.column_name} ${sql_operator[item.query_type]} '${item.search_value}'`
            }
        })

        userAccess = SQL.query(
            DB_NAME,
            `SELECT DISTINCT column_name FROM dm_table_columns_acl WHERE column_id IN (${columnIds.split(',')}) AND access_id IN (${udr.split(',')}) AND level > 0 AND delete_time IS NULL;`,
            TABLE_INFO.table_id
        )
    } else {
        TABLE_STRUCT.forEach(item => {
            if (item.search_value) {
                searchStr += ` AND ${item.column_name} ${sql_operator[item.query_type]} '${item.search_value}'`
            }
        })
    }

    let value = payload.get().query[COLUMN_KEY]
    // 兼容 /user/1 这种通过 path 传值的方式
    if (!value) {
        // 声明是否使用上一级的脚本
        var useParent = scriptPath != payload.get().path.substring(1)

        if (useParent) {
            value = payload.get().path.split("/").pop();
        } else {
            return sqlGenList();
        }
    }

    var cols = []
    for (var i = 0; i < TABLE_STRUCT.length; i++) {
        if (TABLE_STRUCT[i].is_show == 1) {
            if (TABLE_STRUCT[i].is_acl == 1 && !userAccess.includes(TABLE_STRUCT[i].column_name)) {
                continue
            }
            cols.push(TABLE_STRUCT[i].column_name + ' as ' + TABLE_STRUCT[i].field_name)
        }
    }

    const chooseData = cols.join(',');
    // const chooseData = "*";

    stmt = `SELECT ${chooseData} FROM ${TABLE_NAME} WHERE delete_time IS NULL AND ${COLUMN_KEY} = ? ${searchStr}`

    if (TABLE_INFO.before_hook) {
        runtime.call(TABLE_INFO.before_hook)
    }

    SQL_STMTS.push(stmt)
    result = SQL.query(
        DB_NAME,
        stmt, value
    )

    if (TABLE_INFO.after_hook) {
        runtime.call(TABLE_INFO.after_hook)
    }

    // let result = []
    // res.forEach(t => {
    //     const newObj = Object.keys(t).reduce((acc, key) => {
    //         acc[underscoreToCamel(key.toLowerCase())] = t[key];
    //         return acc;
    //     }, {});
    //     result.push(newObj)
    // });
    if (result) {
        return okMsg(result)
    }
    return errMsg(404, '查询失败')
}