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

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

        this.setPath('/IC/OrderHistory')
        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
            })


        try {
            let sql = 'SELECT * FROM ic_record WHERE create_user = ?'
            let rows = await db.query(sql, [uuid])
            if(!rows)
                return res.json({
                    ...BaseStdResponse.DATABASE_ERR
                })

            res.json({
                ...BaseStdResponse.OK,
                data: rows
            })
        } catch (error) {
            res.json({
                ...BaseStdResponse.ERR,
                msg: error.message || '获取信息失败！请稍后再试'
            })
        }
    }
}

module.exports.OrderHistory = OrderHistory