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

class SetAutoTime extends API {
    constructor() {
        super()

        this.noEncrypt()
        this.setPath('/Corn/SetAutoTime')
        this.setMethod('GET')
    }

    async onRequest(req, res) {
        try {
            res.json({
                ...BaseStdResponse.OK
            })

            this.logger.info('开始更换随机乐跑时间段')
            let sql = `
                        UPDATE lepao_account
                        SET today_auto_time = FLOOR(7 + (RAND() * 15))
                        WHERE auto_time = -1 AND auto_run = 1;
                    `
            let r = await db.query(sql)
            if (!r)
                return this.logger.error('更换随机乐跑时间段失败！')
            this.logger.info('更换随机乐跑时间段完成')
        } catch (error) {
            this.logger.error(error)
        }
    }
}

module.exports.SetAutoTime = SetAutoTime
