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

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

        this.setPath('/Goods/GetCount')
        this.setMethod('GET')
    }

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

        if ([uuid, session].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 sql = 'SELECT lepao_count FROM users WHERE uuid = ? '
        let rows = await db.query(sql, [uuid])

        if (!rows || rows.length === 0)
            return res.json({
                ...BaseStdResponse.MISSING_FILE,
                msg: '获取用户信息失败！'
            })

        const row = rows[0]
        res.json({
            ...BaseStdResponse.OK,
            data: {
                lepao_count: Number(row.lepao_count) || 0
            }
        })
    }
}

module.exports.GetCount = GetCount;