<?php
# 电子刊基本信息接口 本类下所有接口，均生成缓存
namespace Main\Src;

use Dever;
use Main\Lib\Core;

class Journal_v1 extends Core
{
    public function getCate()
    {
        $id = Dever::input('system', 1);

        $cate = array();
        if ($id > 0) {
            $cate = Dever::db('journal/cate')->one($id);
            $this->data['cate'] = $cate;
        }

        return $cate;
    }

    # 根据分类获取小刊列表
    public function getList()
    {
        if ($this->app) {
            return $this->getAppList();
        }
        $data = array();

        if ($data) {
            return $data;
        }

        $cate = $this->getCate();

        # 小刊列表
        $where = array();
        if (isset($cate['id'])) {
            $where['cate_id'] = $cate['id'];
        }
        if (isset($cate['journal_id'])) {
            $this->data['push'] = Dever::db('journal/info')->getOne($cate['journal_id']);
        } else {
            # 获取最新的小刊
            $this->data['push'] = Dever::db('journal/info')->getNew();
        }

        $this->data['push'] = $this->info($this->data['push']);
        $this->data['button'] = array();
        $this->data['push'] = $this->getButton($this->data['push']);

        
        $data = Dever::db('journal/info')->getAllByPage($where);

        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->info($v);
                $data[$k] = $this->getButton($data[$k]);
            }
            
            $this->data['journal'] = $data;
        } else {
            $this->data['journal'] = array();
        }

        $this->data['end']['call'] = 'main/journal_v1.getList_noCache';

        $this->data['code'] = 1;
        if (checkVersion()->journal_buy == 2) {
            $this->data['code'] = 2;
        }

        return $this->data;
    }

    public function getList_noCache($data)
    {
        if ($data['push'] && $data['push']['hot'] == 1) {
            $num_ding = Dever::load('act/lib/num')->getCounter($data['push']['id']);
            if ($num_ding && $num_ding >= $data['push']['num_ding']) {
                $data['push']['num_ding'] = $num_ding;
                $data['push'] = $this->ding($data['push']);
            }
        }

        if ($data['journal']) {
            foreach ($data['journal'] as $k => $v) {
                if ($v['hot'] == 1) {
                    $num_ding = Dever::load('act/lib/num')->getCounter($v['id']);
                    if ($num_ding && $num_ding >= $v['num_ding']) {
                        $data['journal'][$k]['num_ding'] = $num_ding;
                        $data['journal'][$k] = $this->ding($data['journal'][$k]);
                    }
                }
            }
        }

        return $data;
    }

    # 根据分类获取小刊列表 app
    public function getAppList()
    {
        $data = array();

        if ($data) {
            return $data;
        }

        $cate = $this->getCate();

        # 小刊列表
        $where = array();
        if (isset($cate['id'])) {
            $where['cate_id'] = $cate['id'];
        }
        $cateList = Dever::db('journal/cate')->getAll();
        $this->data['cateList'] = $cateList;

        $time = time();
        $day = (7 * 86400);

        $where['cate_id_1'] = 1;
        $where['time'] = $time - $day;

        # 获取最新的小刊
        $this->data['push'] = Dever::db('journal/info')->getNew($where);

        $this->data['push'] = $this->ding($this->data['push']);

        $this->data['button'] = array();
        //$this->data['push'] = $this->getButton($this->data['push']);
        //$this->data['push'] = $this->getAppSet($this->data['push']);

        
        $data = Dever::db('journal/info')->getAllByPage($where);

        if ($data) {
            
            foreach ($data as $k => $v) {
                $data[$k] = $this->info($v);
                //$data[$k] = $this->getButton($data[$k]);
                //$data[$k] = $this->getAppSet($data[$k]);
            }
            
            $this->data['journal'] = $data;
        } else {
            $this->data['journal'] = array();
        }

        return $this->data;
    }

    public function ding($data)
    {
        $data = Dever::load('journal/api')->ding($data);

        return $data;
    }

    # 获取详细信息
    private function info($data, $subscribe = true)
    {
        if ($data['pdate'] > 0) {
            $data['pdate_time'] = date('Y-m-d H:i:s', $data['pdate']);
        }

        # 已开始
        $data['ytype'] = 1;
        if (isset($data['pdate']) && $data['pdate'] > 0) {
            if ($data['pdate'] >= time()) {
                # 未到时间
                $data['ytype'] = 2;
            }
            $data['ydate_time'] = date('Y-m-d H:i:s', $data['pdate']);
        }

        $data = $this->ding($data);

        if ($data['pic_view_bg']) {
            $data['pic_view_bg'] = explode(',', $data['pic_view_bg']);
        } else {
            $data['pic_view_bg'] = array();
        }

        $data['buy'] = intval($data['buy']);

        return $data;
    }

    # 小刊查看详情 即将废弃
    public function view()
    {
        $id = $this->check();
        $this->data['info'] = $this->info($this->data['info'], false);
        $this->button($this->data['info']);

        return $this->data;
    }

    # 按钮设置 即将废弃
    private function button($info)
    {
        $this->data['button'] = array();
        $this->setButton();

        $uid = $this->data['uid'];

        # 查看是否订阅
        $subscribe = 2;
        if ($uid) {
            $subscribe = Dever::load('act/lib/subscribe')->get($info['id'], $uid, 4);
        }
        
        $this->data['info']['subscribe'] = 2;

        # 查看是否有排行榜
        $this->data['info']['active'] = $info['top'];

        # 查看是否有活动
        /*
        $active = Dever::db('journal/active')->one(array('id' => $info['id']));
        $this->data['info']['active'] = 2;
        if ($active && $active['status'] == 1) {
            $this->data['info']['active'] = 1;
        }
        
        $time = time();
        if ($active && $time >= $active['start'] && $time < $active['end']) {
            $this->data['info']['active'] = 1;
        }
        */

        if ($this->data['info']['active'] == 1) {
            $this->setButton('share', 1);
        } else {
            $this->setButton('share', -1);
        }

        if ($info['buy'] == 2) {
            # 免费的
            $this->setButton('read', 1);
            $this->setButton('buy', -1);
            $this->setButton('share', -1);
        } elseif ($subscribe == 1) {
            $this->data['info']['subscribe'] = 1;
            # 已订阅

            if ($info['ytype'] == 2) {
                # 预售
                $this->setButton('read', 3);
            } else {
                # 需要购买
                $this->setButton('read', 1);
            }
            $this->setButton('buy', 1);

            if (checkVersion()->journal_buy == 2) {
                $this->setButton('share', -1);
            }
        } else {
            if ($info['ytype'] == 2) {
                # 预售
                $this->setButton('read', 3);
            } else {
                # 需要购买
                $this->setButton('read', 2);
            }
            
            $this->setButton('buy', 1);

            if (checkVersion()->journal_buy == 2) {
                $this->setButton('buy', -1);
                $this->setButton('share', -1);
            }
        }

        # 仅app有效
        if ($this->app) {
        	# 分享相关
        	if (isset($this->data['info']['share_content'])) {
	            $source = Dever::db('source/info')->one(1);
	            $this->data['info']['share_link'] = $source['code'];
	        }

	        # 会员状态
        	$this->data['info']['vip_state'] = 1;
            $this->data['info']['code_state'] = 1;
	        $version_code = Dever::input('version_code');
	        if ($version_code) {
	            $source_type = Dever::input('source_type');
	            $version_where['source_type'] = $source_type;
	            $version_where['name'] = $version_code;
	            $version = Dever::db('main/version')->getOne($version_where);
	            if ($version && $version['vip'] > 0) {
	               $this->data['info']['vip_state'] = $version['vip'];
                   $this->data['info']['code_state'] = $version['button'];
                   if ($this->source_type == 'ios' && $this->data['info']['code_state'] == 2) {
                        $this->setButton('read', 1);
                   }
	            }
	        }

	        # 会员随便看
            if ($uid > 0) {
                $this->data['info']['vip'] = 2;
                $vip = Dever::load('vip/lib/member')->get($uid, $info['cate_id']);
                if ($vip > 0) {
                    $this->data['info']['subscribe'] = 1;
                    $this->data['info']['vip'] = 1;
                    $this->setButton('read', 1);
                }

                # 是否在外站订阅过
                $user = Dever::db('passport/user')->one($uid);
                if ($user && $user['mobile']) {
                    $mobileWhere['mobile'] = $user['mobile'];
                    $mobileWhere['product_id'] = $info['id'];
                    $mobile = Dever::db('journal/mobile')->one($mobileWhere);
                    if ($mobile) {
                        $this->data['info']['subscribe'] = 1;
                        $this->setButton('read', 1);
                    }
                }
            }

            if ($this->source_type == 'ios' && $this->data['info']['id'] == 26) {
                $this->setButton('share', -1);
            }
        } else {
        	# 小程序下的判断，ios里关闭支付
	        $applet_type = Dever::input('applet_type');
	        if ($applet_type == 2 && isset($this->data['config']['ios_pay']) && $this->data['config']['ios_pay'] == 1 && $info['buy'] == 1) {
	            $this->setButton('buy', -1);
	            //$this->setButton('share', -1);
	        }
        }
    }

    private function setButton($key = false, $status = false, $name = false)
    {
        # 按钮状态，-1为不显示
        if (!$this->data['button']) {
            $this->data['button'] = array
            (
                # 第一个按钮 1为阅读、2为兑换、3为预售
                'read' => array('status' => -1, 'name' => '阅读'),
                # 第二个按钮
                'buy' => array('status' => -1, 'name' => '订阅'),
                # 第三个按钮
                'share' => array('status' => 1, 'name' => '订阅排行榜'),
            );

            if ($key == -1) {
                unset($this->data['button']['buy']);
            }
        }

        if ($key && isset($this->data['button'][$key])) {
            if ($status) {
                $this->data['button'][$key]['status'] = $status;
            }
            if ($name) {
                $this->data['button'][$key]['name'] = $name;
            }
        }
    }

    # 验证小刊是否可以阅读
    public function subscribe()
    {
        $subscribe = 2;
        $id = Dever::input('id');
        $this->check();
        $this->data['info'] = $this->info($this->data['info'], false);
        if ($this->data['info']['ytype'] == 2) {
            $subscribe = 3;
        } elseif ($this->data['uid'] > 0) {
            $cache = false;
            if ($this->data['info']['hot'] == 1) {
                $cache = false;
            }
            $subscribe = Dever::load('act/lib/subscribe')->get($id, $this->data['uid'], 4, $cache);
        }

        return array('subscribe' => $subscribe);
    }

    # 小刊查看详情
    public function getView()
    {
        $this->check();
        $this->data['button'] = array();
        $this->data['info'] = $this->getButton($this->info($this->data['info'], false));

        return $this->data;
    }

    # 按钮设置
    private function getButton($info)
    {
        //$info['subscribe'] = 2;

        # 查看是否有排行榜
        $info['active'] = $info['top'];

        if ($info['active'] == 1) {
            $this->setButton('share', 1);
        } else {
            $this->setButton('share', -1);
        }

        if ($info['buy'] == 2) {
            # 免费的
            $this->setButton('read', 1);
            $this->setButton('buy', -1);
            $this->setButton('share', -1);
        } else {
            if ($info['ytype'] == 2) {
                # 预售
                $this->setButton('read', 3);
            } else {
                # 需要购买 这里需要调用info_check接口了
                $this->setButton('read', 2);
            }
            
            $this->setButton('buy', 1);

            /*
            if (checkVersion()->journal_buy == 2) {
                $this->setButton('buy', -1);
                $this->setButton('share', -1);
            }
            */
        }

        # 小程序下的判断，ios里关闭支付
        $applet_type = Dever::input('applet_type');
        if ($applet_type == 2 && isset($this->data['config']['ios_pay']) && $this->data['config']['ios_pay'] == 1 && $info['buy'] == 1) {
            $this->setButton('buy', -1);
            //$this->setButton('share', -1);
        }

        $info['button'] = $this->data['button'];

        return $info;
    }

    # 小刊查看详情 app下查看，仅仅为了区分缓存
    public function getAppView()
    {
        $this->info();

        $this->data['info'] = $this->getAppSet($this->data['info']);

        return $this->data;
    }

    private function getAppSet($info)
    {
        # 分享相关
        if (isset($info['share_content'])) {
            $source = Dever::db('source/info')->one(1);
            $info['share_link'] = $source['code'];
        }

        # 会员状态
        $info['vip_state'] = 1;
        $info['code_state'] = 1;
        $version_code = Dever::input('version_code');
        if ($version_code) {
            $source_type = Dever::input('source_type');
            $version_where['source_type'] = $source_type;
            $version_where['name'] = $version_code;
            $version = Dever::db('main/version')->getOne($version_where);
            if ($version && $version['vip'] > 0) {
               $info['vip_state'] = $version['vip'];
               $info['code_state'] = $version['button'];

               if ($this->source_type == 'ios' && $info['code_state'] == 2) {
                    $this->setButton('read', 1);
               }
            }
        }

        # 会员随便看
        if ($this->data['uid'] > 0) {
            $info['vip'] = 2;
            $vip = Dever::load('vip/lib/member')->get($this->data['uid'], $info['cate_id']);
            if ($vip > 0) {
                $info['subscribe'] = 1;
                $info['vip'] = 1;
                $this->setButton('read', 1);
            }
            # 是否在外站订阅过 防止缓存失效，这里先注释
            /*
            $user = Dever::db('passport/user')->one($this->data['uid']);
            if ($user && $user['mobile']) {
                $mobileWhere['mobile'] = $user['mobile'];
                $mobileWhere['product_id'] = $info['id'];
                $mobile = Dever::db('journal/mobile')->one($mobileWhere);
                if ($mobile) {
                    $info['subscribe'] = 1;
                    $this->setButton('read', 1);
                }
            }
            */
        }
        

        

        if ($this->source_type == 'ios' && $info['id'] == 26) {
            $this->setButton('share', -1);
        }

        $info['button'] = $this->data['button'];

        return $info;
    }

    # 购买阅读页
    public function buy()
    {
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }
        $id = $this->check();

        # 获取购买说明
        $buy = Dever::db('journal/buy')->one(array('id' => $id));

        $this->data['buy_desc'] = '';
        if ($buy) {
            $this->data['buy_desc'] = $buy['buy_content'];
        }

        $this->data['buy_applet_notice'] = '在此购买单本可直接阅读电子刊，购买多本，直接累计积分，不会生成兑换码。';
        if ($buy && isset($buy['buy_applet_notice']) && $buy['buy_applet_notice']) {
            $this->data['buy_applet_notice'] = $buy['buy_applet_notice'];
        }

        if ($this->data['info']['buy'] == 2) {
            $this->data['buy_list'] = array();
            return $this->data;
        }
        # 获取购买列表
        $this->data['buy_list'] = Dever::db('journal/buy_num')->getAll(array('info_id' => $id));

        if ($this->data['info']['cash'] <= 0) {
            $this->data['info']['cash'] = 100000;
        }
        if ($this->data['buy_list']) {
            foreach ($this->data['buy_list'] as $k => $v) {
                if (!$v['name']) {
                    $this->data['buy_list'][$k]['name'] = '购买' . $v['num'] . '本';
                }

                $price = $v['num'] * $this->data['info']['cash'];
                if (!$v['price']) {
                    $this->data['buy_list'][$k]['price'] = $price;
                }

                $this->data['buy_list'][$k]['cash'] = $price;
            }
        }

        return $this->data;
    }

    # 修改为本数排行榜
    public function top()
    {
        $id = $this->check();

        $config_id = 31;
        if ($id > $config_id || $id == 26 || $id == 30) {
            $this->data['top'] = Dever::load('act/lib/num')->getList($id);
        } else {
            $this->data['score'] = Dever::load('act/lib/score')->getList($id);
        }

        # 获取活动说明
        $this->data['active'] = Dever::db('journal/active')->one(array('id' => $id));


        if (!$this->data['active']) {
            $this->data['active'] = (object) array();
            return $this->data;
        }

        /*
        $time = time();
        if ($this->data['active'] && $time >= $this->data['active']['start'] && $time < $this->data['active']['end']) {
            $this->data['active']['status'] = 1;
        } else {
            $this->data['active']['status'] = 2;
        }

        if ($time < $this->data['active']['start']) {
            $this->data['active']['status'] = 3;
        }
        */

        return $this->data;
    }


    # 小刊阅读页
    public function read()
    {
        if (!$this->data['uid']) {
            Dever::alert('错误的用户信息');
        }
        # 验证是否可以阅读
        $id = $this->check();
        $this->data['info'] = $this->info($this->data['info'], false);

        # 默认不能看 为1则可以直接看了，为2则不能看，要验证权限，为了高效并发，暂时去掉验证
        $subscribe = 1;
        $add_num = false;

        if ($subscribe == 2) {
        	# APP专用
	        if ($this->app) {
	        	# 会员随便看
		        $vip = Dever::load('vip/lib/member')->get($this->data['uid'], $this->data['info']['cate_id']);

		        if ($vip <= 0) {
		            # 是否在外站订阅过
		            $user = Dever::db('passport/user')->one($this->data['uid']);
		            if ($user && $user['mobile']) {
		                $mobileWhere['mobile'] = $user['mobile'];
		                $mobileWhere['product_id'] = $id;
		                $mobile = Dever::db('journal/mobile')->one($mobileWhere);
		                if ($mobile) {
		                    $vip = 1;
		                }
		            }
		        }

		        if ($vip > 0) {
		        	$subscribe = 1;
		        }
	        }

	        if ($subscribe == 2) {
	        	$subscribe = Dever::load('act/lib/subscribe')->get($id, $this->data['uid'], 4);
	        }

	        if ($this->data['info']['buy'] == 1 && $subscribe == 2) {
	            Dever::alert('未订阅');
	        }
        }

        if ($this->data['info']['buy'] == 2 && $this->data['info']['status'] == 2) {
            # 免费刊，增加订阅量
            $add_num = true;
        }

        # 获取最新10条内容
        $where = array();
        $where['info_id'] = $id;
        $data = Dever::db('journal/content')->getAll($where);
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->readContent($v);
            }
            $this->data['content'] = $data;
        } else {
            $this->data['content'] = array();
        }

        $this->data['end']['id'] = $id;
        $this->data['end']['uid'] = $this->data['uid'];
        $this->data['end']['add_num'] = $add_num;

        return $this->data;
    }

    public function readContent($info)
    {
        $name = $info['name'];
        $focus = $info['focus'];
        $author_id = $info['author_id'];
        $content = $info['content'];
        unset($info['name']);
        unset($info['focus']);
        unset($info['author_id']);
        unset($info['content']);

        if (($info['type'] == 1 || $info['type'] == 7 || $info['type'] == 8) && $info['text']) {
            $info['text'] = Dever::array_decode($info['text']);
            if ($info['text']) {
                foreach ($info['text'] as $k => $v) {
                    if (isset($info['text'][$k]['bgcolor_type']) && $info['text'][$k]['bgcolor_type'] == array(2)) {
                        $info['text'][$k]['bgcolor'] = '';
                    }
                }
            }
            
        } else {
            unset($info['text']);
        }

        if ($info['type'] == 10 && $info['palaces']) {
            $info['palaces'] = Dever::array_decode($info['palaces']);
        } else {
            unset($info['palaces']);
        }

        if ($info['type'] == 21 && $info['talk']) {
            $info['talk'] = Dever::array_decode($info['talk']);
            foreach ($info['talk'] as $k => $v) {
                $info['talk'][$k]['talk_type'] = implode(',', $v['talk_type']);
                $info['talk'][$k]['talk_location'] = implode(',', $v['talk_location']);
            }
        } else {
            unset($info['talk']);
        }

        if ($info['type'] == 3) {
            $info['info'] = array();
            $info['info']['name'] = $name;
            $info['info']['focus'] = $focus;
            $info['info']['author_id'] = $author_id;
            $info['info']['content'] = $content;
            $info['info'] = Dever::load('content/lib/article')->getContent($info['info'], $this->data['uid']);
        } elseif ($info['type'] == 4 || $info['type'] == 5) {
            $info['video'] = convert($info['video'], 'mp4', 'journal/content', $info['id'], 'video');
            $info = Dever::load('video/lib/vod')->avinfo($info, 'video', 'journal/content');
            if ($info['type'] == 5) {
                $info['name'] = $name;
            }
        } elseif ($info['type'] == 6) {
            $info['name'] = $name;
            //音频先不转码
            //$info['music'] = convert($info['music'], 'mp3', 'journal/content', $info['id'], 'music');
        } elseif ($info['type'] == 11) {
            $info['info'] = Dever::load('content/lib/article')->get($info['article_id']);
        } elseif ($info['type'] == 12) {
            $info['info'] = Dever::load('video/lib/vod')->get($info['vod_id']);
        } elseif ($info['type'] == 13) {
            $info['info'] = Dever::load('video/lib/live')->get($info['live_id']);

            if ($this->data['uid'] > 0) {
                $info['info']['note'] = Dever::load('act/lib/note')->get($this->data['uid'], $info['info']['id'], 3);
                
            } else {
                $info['info']['note'] = 2;
            }
        }

        if (isset($info['info']['author_id']) && $info['info']['author_id']) {
            $info['info']['author'] = Dever::db('content/author')->one($info['info']['author_id']);
        }

        if (isset($info['pic']) && $info['pic']) {
            $info['pic'] = str_replace('http://', 'https://', $info['pic']);
        }

        return $info;
    }

    private function check()
    {
        $id = Dever::input('id');
        if (!$id) {
            Dever::alert('小刊已下架');
        }

        $status = false;
        if (strstr($id, 'preview_')) {
            $id = str_replace('preview_', '', $id);
            $status = true;
        }

        $this->data['info'] = Dever::load('act/order')->getJournal($id);

        if (!$this->data['info']) {
            Dever::alert('小刊已下架');
        }

        if ($status) {
            $this->data['info']['status'] = '2';
        }

        return $id;
    }
}