<?php namespace Pan\Api;
use Dever;
use Dever\Helper\Cmd;
class Task
{
    private $type = array
    (
        1 => 'quark',
    );
    
    # 执行任务
    public function run()
    {
        $id = Dever::input('id');
        $task = Dever::db('task', 'pan')->find(array('id' => $id, 'status' => array('in', '1,3')));
        if ($task) {
            $partition = Dever::call("manage/common.system", array(false, false));
            $partition['id'] = $id;
            Cmd::run('task.runAct', $partition, 'pan');
        }
        return '操作成功，后台程序运行中，请点击右上角刷新页面';
    }

    public function runAct()
    {
        $id = Dever::input('id');
        $partition = Dever::input();
        $id = $partition['id'];
        unset($partition['id']);
        $task_db = Dever::db('task', 'pan', 'default', $partition);
        $task = $task_db->find(array('id' => $id));
        $task_db->update(array('id' => $id), array('status' => 2));
        $account = Dever::db('account', 'pan', 'default', $partition)->find($task['account_id']);
        $class = $this->type[$account['type']];
        Dever::load($class, 'pan')->run($account, $task, $partition);
        $task_db->update(array('id' => $id), array('status' => 3));
    }

    # 导入分享
    public function import()
    {
        $id = Dever::input('set')['account_task_id'] ?? false;
        if ($id) {
            $task = Dever::db('task', 'pan')->find(array('id' => $id));
            if ($task) {
                $partition = Dever::call("manage/common.system", array(false, false));
                $partition['id'] = $id;
                Cmd::run('task.importAct', $partition, 'pan');
            }
        }
        
        return '操作成功，后台程序运行中，请点击右上角刷新页面';
    }

    public function importAct()
    {
        $id = Dever::input('id');
        $partition = Dever::input();
        $id = $partition['id'];
        unset($partition['id']);
        $task = Dever::db('task', 'pan', 'default', $partition)->find(array('id' => $id));
        $account = Dever::db('account', 'pan', 'default', $partition)->find($task['account_id']);
        $class = $this->type[$account['type']];
        Dever::load($class, 'pan')->import($account, $task, $partition);
    }

    # 取消分享
    public function delete()
    {
        $id = Dever::input('id');
        $data = Dever::db('task_data', 'pan')->select(array('id' => array('in', $id)));
        if ($data) {
            $share_id = array();
            $account = array();
            foreach ($data as $k => $v) {
                if (!$account) {
                    $account = Dever::db('account', 'pan')->find($v['account_id']);
                }
                $share_id[] = $v['share_id'];
            }
            $class = $this->type[$account['type']];
            $state = Dever::load($class, 'pan')->delete($share_id, $account['cookie']);
            if ($state) {
                Dever::db('task_data', 'pan')->delete(array('id' => array('in', $id)));
            }
        }
        return '取消分享成功';
    }
}