const API = require("../../../lib/API")
const db = require("../../../plugin/DataBase/db")
const AccessControl = require("../../../lib/AccessControl")
const { BaseStdResponse } = require("../../../BaseStdResponse")

class GetReqLogDetail extends API {
    constructor() {
        super();

        this.setPath('/Admin/User/GetReqLogDetail')
        this.setMethod('GET')
    }

    async onRequest(req, res) {
        let {
            uuid,
            session,
            id
        } = req.query

        if ([uuid, session, id].some(value => value === '' || value === null || value === undefined))
            return res.json({
                ...BaseStdResponse.MISSING_PARAMETER
            })

        // 检查 session
        if (!await AccessControl.checkSession(uuid, session))
            return res.status(401).json({
                ...BaseStdResponse.ACCESS_DENIED
            })

        // 检查权限
        let permission = await AccessControl.getPermission(uuid)
        if (!permission.includes("admin"))
            return res.json({
                ...BaseStdResponse.PERMISSION_DENIED
            })

        let sql = `
               SELECT 
                    l.*,
                    u.username,
                    u.avatar
                FROM 
                    requestLog l
                LEFT JOIN 
                    users u
                ON 
                    l.create_user = u.uuid
                WHERE 
                    l.id = ?
            `
        let rows = await db.query(sql, [id])

        if (!rows || rows.length === 0)
            return res.json({
                ...BaseStdResponse.MISSING_FILE,
                msg: '获取日志数据失败！'
            })

        res.json({
            ...BaseStdResponse.OK,
            data: rows[0]
        })
    }
}

module.exports.GetReqLogDetail = GetReqLogDetail