<?php

/**

 * 直播记录模块

 * @Copyright (C) 2016 汉潮 All rights reserved.

 * @License http://www.hanchao9999.com

 * @Author xiaoq <570035208@qq.com>

 */



namespace Wxsmall\Model;

use Think\Model;

class LiveHistoryModel extends Model{

    /**获取礼物信息

     * @return array

     */

    public function getgiftinfo($id=0){

        $return = array('status'=>0,'msg'=>'未获取到数据');

        if(empty($id))return $return;

        $model = M('LiveGift');

        $where['id'] = $id;   

        $field = array('id','gift_name','money_type','needcoin');

        $gift_list = $model->where($where)->field($field)->find();

        if(count($gift_list)>0){

            $return['status'] = 1;

            $return['data'] = $gift_list; 

        }

        return $return;     

    }

    public function get_history_recode($param = array()){

        $history_id = $param['history_id'];

        $history_link_id = $param['history_link_id'];

        $return=array('status'=>1,'msg'=>'');

        if(empty($history_id)){

            $return['status']=0;

            $return['msg']='参数错误';

            return $return;

        }

        $map=$l_map=array();

        $map['id']=$history_id;

        $history_info=M("LiveHistory")->where($map)->field('roomnumber,goods_info,begin_time,end_time')->find();

        if($history_info){

            $goods_info=unserialize($history_info['goods_info']);

            if($goods_info){

                $return['data']['goods_info']=$goods_info;

            }

        }

        if($history_link_id){

            $where['start_time'] = array('egt',$history_info['begin_time']);

            $where['stop_time'] = array('elt',$history_info['end_time']);

            $where['stream'] = $history_info['roomnumber'];

        }else{

            $where['id']=$history_link_id;

        }

        $history_link=M("LiveHistoryLink")->where($where)->field('domain,app,stream,uri')->find();

        $return['data']['history_link_info']=$history_link;

        return $return;

    }



    /**获取录制视频地址

     * @param string $StreamName

     * @return array

     */

    public function getrecordlist($StreamName=''){

        $return = array('status'=>0);

        if(empty($StreamName)){

            $return['msg'] = '请输入房间编号!';

            return $return;

        }

        $map=array();

        $map['a.roomnumber']=$StreamName;

        $map['a.is_effect']=1;
        $map['hl.stop_time'] = array('neq',0);

        //1203

        // $map['a.end_time'] = array('neq',0);

        $map['_string']='hl.start_time>=a.begin_time and hl.stop_time<=a.end_time';

       // $data = M('LiveHistory')->alias('a')->where($map)->JOIN('LEFT JOIN __LIVE_HISTORY_LINK__ hl ON a.roomnumber=hl.stream')->field('a.*,hl.*,hl.id as link_id,a.id as id')->limit(10)->order('a.id desc ')->group("a.id")->select();
       // if($data) {

       //      foreach ($data as $key => $val) {

       //          $data[$key]['recordurl'] = 'http://'.C('WEB_LIVE_DOMAIN').'/'.$val['mp4_uri'];

       //          $data[$key]['start_time'] = time_format($val['start_time']);

       //          $data[$key]['stop_time'] = time_format($val['stop_time']);

       //          $data[$key]['begin_time'] = time_format($val['begin_time']);

       //          $data[$key]['end_time'] = time_format($val['end_time']);

       //          $data[$key]['liveimg'] = $val['liveimg'];

       //          $data[$key]['goods_info'] = unserialize($val['goods_info']);

       //          $goods_info=array_filter(unserialize($val['goods_info']));

       //          $data[$key]['goods_count'] =count($goods_info)?count($goods_info):0;



       //      }

       //      $return['status'] = 1;

       //      $return['data'] = $data;

       //      $return['recordcount'] =count($data);

       //  }else{

       //      $return['msg'] = '未找到相关录播信息';

       //  }

        //1229
        // $online_people_rate = idtoname('online_people_rate','LiveSet','value','name');
        $online_people_rate = getconfig('ONLINE_PEOPLE_RATE');
        $pagesize = empty($pagesize) ? C('WEB_GOODS_PAGE') : $pagesize;
        $p = empty($p) ? 1 : $p;
        $join = 'LEFT JOIN __LIVE_HISTORY_LINK__ hl ON a.roomnumber=hl.stream';
        $field = 'a.*,hl.*,hl.id as link_id,a.id as id';
        $order = 'hl.id desc';
        $data = page_list('LiveHistory',$map,$order, $field, $pagesize, $join, $p);
        if($data['list'] && !empty($data['list']) && $data['list'] !=null){
            foreach ($data['list'] as $key => $val) {

                $data['list'][$key]['recordurl'] = 'http://'.getconfig('WEB_LIVE_DOMAIN').'/'.$val['mp4_uri'];

                $data['list'][$key]['start_time'] = time_format($val['start_time']);

                $data['list'][$key]['stop_time'] = time_format($val['stop_time']);

                $data['list'][$key]['begin_time'] = time_format($val['begin_time']);

                $data['list'][$key]['end_time'] = time_format($val['end_time']);

                $data['list'][$key]['liveimg'] = format_url($val['liveimg']);
                $data['list'][$key]['liveonlinecount'] = intval($online_people_rate+$val['liveonlinecount']);

                $data['list'][$key]['goods_info'] = unserialize($val['goods_info']);

                $goods_info=array_filter(unserialize($val['goods_info']));

                $data['list'][$key]['goods_count'] =count($goods_info)?count($goods_info):0;



            }

            $return['status'] = 1;

            $return['data'] = $data;

            $return['data']['recordcount'] =count($data);

        }else{
            $return['status'] = 3;
            $return['msg'] = '未找到相关录播信息';

        }

        return $return;

    }

}