<?php

namespace Journal\Src;

use Dever;

class Api
{
    public function data()
    {
        return Dever::db('journal/info')->state();
    }

    public function ding($data)
    {
        if (isset($data['num_add_ding']) && isset($data['num_ratio_ding'])) {

            if ($data['buy'] == 1 && $data['buy_type'] == 2) {
                # 获取明星的订阅数
                $star = Dever::db('journal/star')->state(array('info_id' => $data['id']));
                $data['num_ding'] = 0;
                if ($star) {
                    foreach ($star as $k => $v) {
                        $v = $this->starDing($v);
                        $data['num_ding'] += $v['num_ding'];
                    }
                }
                $data['num_up'] = $data['num_add_ding'] = 0;
                $data['num_ratio_ding'] = 1;

            } elseif ($data['buy'] == 1 && $data['buy_type'] == 1) {
                if (!$data['num_ding']) {
                    $stat = Dever::db('stat/journal')->getNumByJournal(array('journal_id' => $data['id']));
                    if (!$stat) {
                        $stat['buy_num'] = 0;
                    }

                    if (!$stat['buy_num']) {
                        $stat['buy_num'] = 0;
                    }
                    $data['num_ding'] = $stat['buy_num'];
                }

                $data['num_up'] = 0;
            }

            $data['num_ding'] = $data['num_ding'] + $data['num_up'];
            $data['buy_num'] = $data['num_ding'];
            $data['num_ding'] = round(($data['num_ding']+$data['num_add_ding'])*$data['num_ratio_ding']);
        }

        return $data;
    }

    public function starDing($data)
    {
        if (isset($data['num_add_ding']) && isset($data['num_ratio_ding'])) {

            if (!$data['num_ding']) {
                $stat = Dever::db('act/order')->getNumByStar(array('star_id' => $data['id'], 'product_id' => $data['info_id']));
                if (!$stat) {
                    $stat['buy_num'] = 0;
                }

                if (!$stat['buy_num']) {
                    $stat['buy_num'] = 0;
                }
                $data['num_ding'] = $stat['buy_num'];
            }
            
            $data['buy_num'] = $data['num_ding'];
            $data['num_ding'] = round(($data['num_ding']+$data['num_add_ding'])*$data['num_ratio_ding']);
        }

        return $data;
    }

    # 解析元信息
    public function avinfo($data, $url = 'video', $table = 'video/vod')
    {
        # 解析视频元信息
        if (!$data['video_info']) {
            $video_info = Dever::curl($data[$url] . '?avinfo');
            if ($video_info) {
                Dever::db($table)->update(array('where_id' => $data['id'], 'video_info' => $video_info));
            }
            
            $video_info = Dever::json_decode($video_info);
        } else {
            $video_info = Dever::json_decode($data['video_info']);
        }

        unset($data['video_info']);

        $data['video_width'] = 0;
        $data['video_height'] = 0;
        # 默认横屏
        $data['video_type'] = 1;
        if (isset($video_info['streams']) && $video_info['streams']) {
            foreach ($video_info['streams'] as $k => $v) {
                if (isset($v['width']) && isset($v['height'])) {
                    $data['video_width'] = $v['width'];
                    $data['video_height'] = $v['height'];
                    if ($data['video_width'] >= $data['video_height']) {
                        $data['video_type'] = 1;
                    } else {
                        $data['video_type'] = 2;
                    }
                    
                    break;
                }
            }
        }

        return $data;
    }
}