<?php
/**
 *
 */

include_once(dirname(dirname(__FILE__)) . "/library/publicBase.php");

class weibo extends publicBase {

    public function home() {}

    public function ajax_weibo_list() {
        $status = $this->post('status') + 0;
        $cur_page = $this->post('currentPage') ? $this->post('currentPage') : 1;
        $page_size = $this->post('pageSize') ? $this->post('pageSize') : 20;

        $condition = array();
        $condition['status'] = $status;

        $obj = new mWeibo();
        $total = $obj->getWeiboTotal($condition);
        $list = $obj->getWeiboList($condition, $cur_page, $page_size);

        $rdata = array(
            'total' => $total,
            'per_page' => $page_size,
            'last_page' => ceil($total / $page_size),
            'cur_page' => $cur_page,
            'list' => $list,
        );

        $this->ajax_json(true, '获取成功', $rdata);
    }

    public function ajax_weibo_detail() {
        $id = $this->post('id') + 0;

        $obj = new mWeibo();
        $data = $obj->getWeiboDetailById($id);

        $this->ajax_json(true, '获取成功', $data);
    }

    public function ajax_set_weibo_status() {
        $id = $this->post('id') + 0;
        if (!$id) $this->ajax_json(true, '参数错误');

        $status = $this->post('status') + 0;

        $obj = new mWeibo();
        $data = $obj->updateWeibo($id, array('status' => $status));

        $this->ajax_json(true, '设置成功', $data);
    }

    public function ajax_edit_weibo() {
        $id = trim($this->post('id'));
        $text = trim($this->post('answer'));
        $refer = trim($this->post('desc'));
        $status = $this->post('status') + 0;

        if ($id) {
            $obj = new mWeibo();
            $res = $obj->updateWeibo($id, array('text' => $text, 'refer' => $refer, 'status' => $status));
            if (!$res) $this->ajax_json(false, '保存失败');

            $this->ajax_json(true, '保存成功');
        }

        $data = array(
            'uid' => '1',
            'text' => $text,
            'refer' => $refer,
            'pic_ids' => '',
            'video_url' => '',
            'video_cover' => '',
            'created_at' => date('Y-m-d H:i:s', time()),
            'status' => $status,
        );

        $obj = new mWeibo();
        $res = $obj->addWeibo($data);
        if (!$res) $this->ajax_json(false, '保存失败');

        $this->ajax_json(true, '保存成功');
    }

    public function ajax_get_spider_list() {
        $date = trim($this->post('date'));
        $uid = trim($this->post('uid'));
        $cur_page = $this->post('currentPage') ? $this->post('currentPage') : 1;
        $page_size = $this->post('pageSize') ? $this->post('pageSize') : 20;

        $obj = new mWeiboBehavior();
        $list = $obj->getSpiderBehavior($date, $uid, $cur_page, $page_size);
        $total = $obj->getSpiderBehaviorTotal($date, $uid);

        $rdata = array(
            'total' => $total,
            'per_page' => $page_size,
            'last_page' => ceil($total / $page_size),
            'cur_page' => $cur_page,
            'list' => $list,
        );

        $this->ajax_json(true, '获取成功', $rdata);
    }

    public function ajax_hot_list() {
        $type = $this->post('type') + 0;
        $sdate = trim($this->post('sdate'));
        $edate = trim($this->post('edate'));
        $cur_page = $this->post('currentPage') ? $this->post('currentPage') : 1;
        $page_size = $this->post('pageSize') ? $this->post('pageSize') : 20;

        $obj = new mWeiboBehavior();
        $list = $obj->getHotBehavior($type, $sdate, $edate, $cur_page, $page_size);
        $total = $obj->getHotBehaviorTotal($sdate, $edate);

        $rdata = array(
            'total' => $total,
            'per_page' => $page_size,
            'last_page' => ceil($total / $page_size),
            'cur_page' => $cur_page,
            'list' => $list,
        );

        $this->ajax_json(true, '获取成功', $rdata);
    }

    public function show_detail() {
        $this->view['wid'] = $this->get('wid') + 0;
    }

    public function ajax_update_cookie() {
        $cookie = trim($this->get('cookie'));
        if (empty($cookie)) $this->ajax_json(false, '参数错误');

        $type = $this->get('type') + 0;
        $obj = new mWeiboBehavior();
        $robj = $obj->initRedis();
        $robj->set(_RC_WEIBO_LOGIN_COOKIE, $cookie);

        if ($type == 0) {
            shell_exec("nohup /usr/bin/php /data1/www/zhishiku.yizherenxin.cn/queue/crontab/spider_behavior_data.php > /dev/null 2>&1 &");
            $this->ajax_json(true, '设置成功');
        }

        shell_exec("nohup /usr/bin/php  /data1/www/zhishiku.yizherenxin.cn/queue/crontab/spider_behavior_delta.php > /dev/null 2>&1 &");
        $this->ajax_json(true, '设置成功');
    }
}