<?php
/**
 *
 */
include_once(SERVER_ROOT . "/model/mBase.php");


class mWeibo extends mBase {

    private $tbl;

    public function __construct() {
        $this->obj = new dWeibo();
        $this->tbl = 'spider_weibo';
    }

    public function getWeiboList($condition, $page_num, $page_size) {
        $offset = ($page_num - 1) * $page_size;

        $where = "1=1 ";
        if (!empty($condition)) {
            foreach ($condition as $key => $val) {
                if (is_array($val)) {
                    $val = implode(',', $val);
                    $where .= " and {$key} in ({$val})";
                } else {
                    $where .= " and {$key}={$val}";
                }
            }
        }

        $list = $this->obj->selectAll($this->tbl, array('sql' => $where, 'vals' => array()), 'created_at desc', array($offset, $page_size));
        if (empty($list)) return array();

        foreach ($list as &$v) {
            $v['name'] = '微博粉丝';
            if ($v['uid'] == 2282201403) $v['name'] = '张宝旬';
            if ($v['uid'] == 1) $v['name'] = '管理员';

            $pic_ids = json_decode($v['pic_ids'], 1);
            $pic_arr = array();
            if ($pic_ids) {
                foreach ($pic_ids as $pic_id) {
                    $pic_arr[] = $this->getPicOssUrl($pic_id, $v['created_at']);
                }
            }
            $v['pic_arr'] = $pic_arr;

            if ($v['video_url']) $v['video_url'] = $this->getVideoOssUrl($v['wid'], $v['created_at']);
            if ($v['video_cover']) $v['video_cover'] = $this->getPicOssUrl($v['video_cover'], $v['created_at']);
        }
        return $list;
    }

    public function getWeiboTotal($condition) {
        $where = "1=1 ";
        if (!empty($condition)) {
            foreach ($condition as $key => $val) {
                if (is_array($val)) {
                    $val = implode(',', $val);
                    $where .= " and {$key} in ({$val})";
                } else {
                    $where .= " and {$key}={$val}";
                }
            }
        }

        return $this->obj->count($this->tbl, array('sql' => $where, 'vals' => array()));
    }

    public function getWeiboDetailById($id) {
        $data = $this->getWeiboById($id);
        if (empty($data)) return array();

        $pic_ids = json_decode($data['pic_ids'], 1);
        foreach ($pic_ids as $pic_id) {
            $data['pic_arr'][] = $this->getPicOssUrl($pic_id, $data['created_at']);
        }
        if ($data['video_url']) $data['video_url'] = $this->getVideoOssUrl($data['wid'], $data['created_at']);
        if ($data['video_cover']) $data['video_cover'] = $this->getPicOssUrl($data['wid'], $data['created_at']);

        return $data;
    }

    public function getWeiboById($id) {
        return $this->obj->select($this->tbl, array('sql' => '`id`=?', 'vals' => array($id)));
    }

    public function updateWeibo($id, $data) {
        return $this->obj->update($this->tbl, $data, array('sql' => '`id`=?', 'vals' => array($id)));
    }

    public function addWeibo($data) {
        return $this->obj->insert($this->tbl, $data);
    }

    public function getWeiboDataPicSavePath($picid, $created_at) {
        return date("Y-m", strtotime($created_at)) . '/' . $picid . '.jpg';
    }

    public function getWeiboDataVideoSavePath($wid, $created_at) {
        return date("Y-m", strtotime($created_at)) . '/' . $wid . '.mp4';
    }

    public function getPicOssUrl($picid, $created_at) {
        return ALIYUN_OSS_URI . date("Y-m", strtotime($created_at)) . '/' . $picid . '.jpg';
    }

    public function getVideoOssUrl($picid, $created_at) {
        return ALIYUN_OSS_URI . date("Y-m", strtotime($created_at)) . '/' . $picid . '.mp4';
    }
}