const API = require("../../lib/API.js")
const axios = require("axios")
const { BaseStdResponse } = require("../../BaseStdResponse.js")

// 获取楼栋
class GetDyList extends API {
    constructor() {
        super()

        this.setPath("/Power/GetPowerData")
        this.setMethod("POST")
    }

    async onRequest(req, res) {
        try {
            const { type, pid } = req.body
            if ([type].some(value => value === '' || value === null || value === undefined))
                return res.json({
                    ...BaseStdResponse.MISSING_PARAMETER
                })
            if (type !== 'buildlist')
                if ([pid].some(value => value === '' || value === null || value === undefined))
                    return res.json({
                        ...BaseStdResponse.MISSING_PARAMETER
                    })

            const url = `https://hqpay.ctbu.edu.cn/weixin/ashx/frmuser.ashx?test=${type}${type !== 'buildlist' ? '&pid=' + pid : ''}`
            const response = await axios.get(url, {
                proxy: false
            })
            if (!response || !response.data)
                return res.json({
                    ...BaseStdResponse.ERR,
                    msg: "请稍后再试"
                })

            res.json({
                ...BaseStdResponse.OK,
                data: response.data ?? []
            })
        } catch (error) {
            this.logger?.error(`${error.stack}`)
            return res.json({
                ...BaseStdResponse.ERR,
                msg: `${error.message ?? "请稍后再试"}`
            })
        }
    }
}

module.exports.GetDyList = GetDyList
