<?php
namespace Manage\Src;

use Dever;
use Dever\Loader\Project as Config;
use Dever\Routing\Input;

class Project
{
    /**
     * get
     *
     * @return string
     */
    public function get()
    {
        $list = Config::read();

        //unset($list['manage']);

        /*
        取消排序，放到数据表中排序
        $order = array();
        foreach($list as $v)
        {
        $order[] = $v['order'];
        }
        array_multisort($order, SORT_DESC, $list);
         */

        return $list;
    }

    /**
     * 生成key->value的形式
     *
     * @return string
     */
    public function key()
    {
        $list = Config::read();

        //unset($list['manage']);

        return $list;
    }

    /**
     * set
     *
     * @return string
     */
    public function set()
    {
        $config = $this->get();
        $key = Input::get('project');
        $index = Input::get('index', 'lang');
        $value = Input::get('value');

        Config::update($key, $index, $value);
    }

    /**
     * name
     *
     * @return string
     */
    public function name($key)
    {
        $project = Config::load($key);

        if ($project) {
            return $project['lang'];
        } else {
            return $key;
        }
    }

    /**
     * config
     *
     * @return string
     */
    public function config($key)
    {
        $project = Config::load($key);

        if ($project) {
            return $project;
        } else {
            return $key;
        }
    }

    /**
     * path
     *
     * @return string
     */
    public function path($key)
    {
        $project = Config::load($key);

        if ($project) {
            return $project['path'];
        } else {
            return $key;
        }
    }

    /**
     * setup
     *
     * @return string
     */
    public function setup($key)
    {
        $project = Config::load($key);

        if ($project) {
            return isset($project['setup']) ? $project['setup'] : $project['path'];
        } else {
            return $key;
        }
    }

    public function loop()
    {
        while (true) {
            $this->cron();
            sleep(10);
        }
    }

    /**
     * cron
     *
     * @return string
     */
    public function cron()
    {
        $time = time();

        # 获取所有的计划任务
        $data = Dever::db('manage/cron')->get(array('where_ldate' => $time));

        if ($data) {
            # 比如属于weixin项目的group_list
            # weixin.mazephp.com/data.group_list
            foreach ($data as $k => $v) {
                Dever::daemon($v['interface'], $v['project']);

                $param['where_id'] = $v['id'];
                $param['set_ldate'] = $v['ldate'] + $v['time'];
                if ($v['time'] <= 0) {
                    $param['set_state'] = 2;
                }
                Dever::db('manage/cron')->update($param);
            }
        }
    }

    /**
     * 将昨天的signature数据删除
     *
     * @return string
     */
    public function signature()
    {
        $time = time() - 86400;

        Dever::db('manage/signature')->deleteByDay(array('where_time' => $time));

        $path = Path::month('signature');
        $file = $path . 'signature_' . date('Y_m_d', $time);
        unlink($file);
    }
}
