<?php
namespace Smi\Model;

use Think\Model;

header("Content-type:text/html;charset=utf-8");
class SetElseModel extends Model
{
    /**
     * 保存表单
     * @return boolean
     */
    public function saveForm()
    {
        $id = $_POST['id'];
        if (I("refresh_time")) $data['refresh_time'] = I("refresh_time");
        $sure_get = I("sure_get");
        if (is_numeric($sure_get) && $sure_get > 0) {
            $data['sure_get'] = $sure_get;
        }
        if ($id) {
            $result = $this->where("`id`={$id}")->save($data);
        } else {
            $result = $this->add($data);
        }
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取数据刷新时间, 没有则定为 21:00
     */
    public function getLimitTime()
    {
        $row = M("setElse")->find();
        $refresh_time = $row['refresh_time'];
        if (!$refresh_time) {
            $refresh_time = "21:00";
        }
        $arr = explode(":", $refresh_time);
        $hour = $arr[0];
        $minute = $arr[1];
        // 上一天数据截止时间(戳)
        $limitTime = mktime($hour, $minute, 0, date("m"), date("d"), date("Y")) - 24 * 60 * 60;
        return $limitTime;
    }
}