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

// 获取产品列表
class GetGoodsList extends API {
    constructor() {
        super();

        this.setPath('/Goods/List')
        this.setMethod('get')
    }

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

        let sql = `
            SELECT
                id,
                name,
                price,
                num,
                \`limit\`,
                icon,
                isHot,
                description,
                category,
                features
            FROM
                goods
            WHERE state = 1 
        `

        let params = []

        if (keyword) {
            sql += ` AND name LIKE ?`
            params.push(`%${keyword}%`)
        }

        sql += `
            ORDER BY 
                id DESC
        `
        let rows = await db.query(sql, params)

        if (!rows)
            return res.json({
                ...BaseStdResponse.ERR,
                msg: '获取商品失败！'
            })

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

module.exports.GetGoodsList = GetGoodsList;