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

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

        this.setPath('/Lepao/Records')
        this.setMethod('GET')
    }

    async onRequest(req, res) {
        let { uuid, session, name, lepao_account, email, pagesize, current } = req.query

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

        // 校验分页参数
        if (isNaN(pagesize) || pagesize <= 0) {
            return res.json({
                ...BaseStdResponse.ERR,
                msg: '参数错误'
            })
        }

        if (isNaN(current) || current <= 0) {
            return res.json({
                ...BaseStdResponse.ERR,
                msg: '参数错误'
            })
        }

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

        // 计算分页的 offset
        const offset = (current - 1) * pagesize

        let sql = `
                SELECT 
                    r.id,
                    r.uuid,
                    r.time,
                    r.result,
                    r.lepao_account,
                    a.name,
                    a.user_avatar
                FROM 
                    lepao_record r
                LEFT JOIN 
                    lepao_account a
                ON 
                    r.lepao_account = a.student_num
                WHERE 
                    (r.uuid = ? OR a.create_user = ?)
            `

        let countSql = `
            SELECT COUNT(*) AS total
            FROM lepao_record r
            JOIN lepao_account a ON r.lepao_account = a.student_num
            WHERE (r.uuid = ? OR a.create_user = ?)
        `

        let params = [uuid, uuid]
        let countParams = [uuid, uuid]

        if (lepao_account) {
            sql += ` AND r.lepao_account = ?`
            countSql += ` AND r.lepao_account = ?`
            params.push(lepao_account)
            countParams.push(lepao_account)
        }

        if (email) {
            sql += ` AND a.email = ?`
            countSql += ` AND a.email = ?`
            params.push(email)
            countParams.push(email)
        }

        if (name) {
            sql += ` AND a.name = ?`
            countSql += ` AND a.name = ?`
            params.push(name)
            countParams.push(name)
        }

        sql += `
            ORDER BY id DESC
            LIMIT ? OFFSET ?;
        `
        params.push(String(pagesize), String(offset))

        let rows = await db.query(sql, params)
        let countResult = await db.query(countSql, countParams)

        if (!rows || !countResult)
            return res.json({
                ...BaseStdResponse.DATABASE_ERR
            })

        let total = countResult[0].total

        res.json({
            ...BaseStdResponse.OK,
            data: rows,
            pagination: {
                current,
                pagesize,
                total
            }
        })
    }
}

module.exports.GetLepaoRecords = GetLepaoRecords;