<?php
/**
 * 主播模块
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class LiveAnchorModel extends Model{
    /**直播列表
     * @param int $type
     * @param array $map
     * @param string $join
     * @param string $order
     * @param int $p
     * @param int $pagesize
     * @return array
     */
public function get_live_list($type =     0,$map = array(), $join = '', $order = '', $p = 1, $pagesize = 0){
        $pagesize = empty($pagesize) ? C('WEB_GOODS_PAGE') : $pagesize;
        $p = empty($p) ? 1 : $p;
        $map['a.status'] = 1;
        $map['a.is_delete'] = 0;
        if($type==1){
            $map['a.is_recommend'] = 1;
        }
        $order = empty($order) ? 'a.is_recommend desc,lh.id desc,lh.liveonlinecount desc,a.id desc,a.updatetime desc' : $order;
        $map['lh.begin_time'] =  array('egt',strtotime(date('Y-m-d'.'00:00:00',NOW_TIME)));//直播有效时间是今日开始
        $map['lh.end_time'] = 0;
        //获取正在开播的主播列表
        $map['lh.is_effect'] = 1;
        $join = 'LEFT JOIN __LIVE_HISTORY__ lh ON lh.roomnumber=a.roomnumber';
        $field='a.*,lh.liveonlinecount,lh.praise_number,lh.liveimg,lh.livebrief';
        $livelist = page_list('LiveAnchor', $map, $order, $field, $pagesize, $join, $p);
        if($livelist['list']){
            foreach($livelist['list'] as $key=>$val){

                //在线观看人数基数
                // $online_people_rate = idtoname('online_people_rate','LiveSet','value','name');
                $online_people_rate = getconfig('ONLINE_PEOPLE_RATE');
                //end

                $livelist['list'][$key]['number']=intval($online_people_rate + $val['liveonlinecount']);
                //1105新加
                $livelist['list'][$key]['cate_name']=idtoname($val['cate_id'],'LiveCategory','name','id',false);
                $livelist['list'][$key]['liveimg'] =thumb(format_url($val['liveimg']),355,355);
                //end
                $u_map=array();
                $u_map['id']=$val['user_id'];
                $member_author=M("Member")->where($u_map)->field('nickname,username,mobile,header_img')->find();
                if($member_author){
                    $livelist['list'][$key]['name']=D('Chat')->getnickname($member_author);
                    if(empty($livelist['list'][$key]['name'])) $livelist['list'][$key]['name'] = trim(C('WEB_LIVES_NAME'));

                    if($member_author['header_img']) $livelist['list'][$key]['header_img']=$member_author['header_img'];
                    else $livelist['list'][$key]['header_img']='';
                }
                if($val['city_id']) $livelist['list'][$key]['city']=idtoname($val['city_id'],"Area",'name');
                else $livelist['list'][$key]['city'] = '石家庄';

                if($live_type==1) $livelist['list'][$key]['live_type'] = 1;
            
        }

        }else{
            $livelist = '暂无数据';
        }
        
        return $livelist;
    }
    /**获取预告列表
     * @param array $map
     * @param string $join
     * @param string $order
     * @param int $p
     * @param int $pagesize
     */
    public function get_trailer_list($map=array(), $join = '', $order = '', $p = 1, $pagesize = 0){
        $pagesize = empty($pagesize) ? C('WEB_GOODS_PAGE') : $pagesize;
        $p = empty($p) ? 1 : $p;
        $map['a.status'] = 1;
        $map['a.is_delete'] = 0;
        $map['a.begin_time']=array('gt',NOW_TIME);
        $order = empty($order) ? 'a.begin_time desc,a.id desc' : $order;
        $livelist = page_list('LiveTrailer', $map, $order, 'a.*', $pagesize, $join, $p);
        if($livelist){
            foreach($livelist['list'] as $key=>$val){
                $a_map['id']=$val['anchor_id'];
                $anchor=M("LiveAnchor")->where($a_map)->Field('user_id,city_id')->find();
                $u_map=array();
                $u_map['id']=$anchor['user_id'];
                if($anchor['city_id']){
                    $livelist['list'][$key]['city']=idtoname($anchor['city_id'],"Area",'name');
                }
                $member_author=M("Member")->where($u_map)->field('nickname,username,mobile,header_img')->find();
                if($member_author){
                    $livelist['list'][$key]['name']=D('Chat')->getnickname($member_author);
                    if(empty($member_author['header_img'])){
                        $livelist['list'][$key]['header_img']=C('HEAD_DEFAULT_IMG');
                    }else{
                        $livelist['list'][$key]['header_img']=$member_author['header_img'];
                    }
                }
                $livelist['list'][$key]['index_img']=$val['index_img'];
                $livelist['list'][$key]['yugao_time']=$val['begin_time']-NOW_TIME;

            }
            return $livelist;
        }        
    }

    /**
     * 会员申请主播
     */
    public function please_live_anthor($data){
        $return=array('status'=>0);
        $id=$data['id'];
        if($data['id']>0){
            $map=array();
            $map['id']=$data['id'];
            unset($data['id']);
            $data['updatetime']=NOW_TIME;
            $res=M("LiveAnchor")->where($map)->save($data);
        }else{
            if(empty($data['cate_id'])){
                $return['msg']='请选择主播分类';
                return $return;
            }
            if(empty($data['front_card'])){
                $return['msg']='请上传身份证正面';
                return $return;
            }
            if(empty($data['negative_card'])){
                $return['msg']='请上传身份证反面';
                return $return;
            }
            if(empty($data['negative_card'])){
                $return['msg']='请手持身份证正面照片';
                return $return;
            }
            $data['status']=2;
            $data['addtime']=NOW_TIME;
            $res=M("LiveAnchor")->add($data);
            // $c_map=array();
            // $c_map['name']='room_number';
            // $room_config=M("LiveSet")->where($c_map)->getField('value');
            $room_config=getconfig('ROOM_NUMBER');
            $roomnumber=$room_config.$res;
            $check=$this->check_live($roomnumber);
            if($check==0){
                $return['msg']='该房间号已被注册';
                return $return;
            }
            $map=$room=array();
            $map['id']=$res;
            $room['roomnumber']=$roomnumber;
            M("LiveAnchor")->where($map)->save($room);
        }
        if($res){
            $return['status']=1;
            if($id>0){
                $return['msg']='提交成功!';
            }else{
                $return['msg']='提交成功，请耐心等待审核！';
            }
            $return['url']=getjumpurl('JUMP_8');
            return $return;
        }else{
            $return['msg']='提交失败';
            return $return;
        }
        return $return;
    }
    /**
     *房间号验证是否重复
     */
    public function check_live($roomnumber){
        $map=array();
        if(empty($roomnumber)) return false;
        $map['roomnumber']=$roomnumber;
        $count=M("LiveAnchor")->where($map)->count();
        if($count>0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 获取直播分类
     */
    public function getcate(){
        $map['status']=1;
        $map['is_delete']=0;
        $cate_info=M("LiveCategory")->where($map)->field(true)->order('id desc')->select();
        $cate_arr = array('0'=>array('id'=>0,'name'=>'全部','pid'=>0));
        foreach($cate_info as $key=>$val){
            $cate_arr[] = $val;
            if(str_exists($val['icon'],'http://')){
                $cate_info[$key]['icon']=$val['icon'];
            }else{
                $cate_info[$key]['icon']='http://'.$_SERVER['HTTP_HOST'].$val['icon'];
            }
           
        }
        return $cate_arr;
    }

    /**
     * 主播关注列表
     */
    public function get_collect_live($map=array(),$join='',$order='',$p=1,$pagesize=0){
        $memberinfo=checkuserauthtype();if(empty($memberinfo)) return false;
        $pagesize=empty($pagesize)?C('WEB_GOODS_PAGE'):$pagesize;
        $p=empty($p)?1:$p;
        $map['a.status']=1;
        $map['a.user_id']=$memberinfo['id'];
        $field='s.*,a.*,s.user_id as live_user';
        $join=" LEFT JOIN __LIVE_ANCHOR__ s ON a.anchor_id=s.id ";
        $collectlive=page_list('LiveFollow',$map,$order,$field,$pagesize,$join,$p);
        foreach($collectlive['list'] as $key=>$val){
            $u_map=array();
            $u_map['id']=$val['anchor_id'];
            $member_author=M("Member")->where($u_map)->field('nickname,username,mobile,header_img')->find();
            if($member_author){
                $collectlive['list'][$key]['name']=D('Chat')->getnickname($member_author);
                if(empty($member_author['header_img'])){
                    $collectlive['list'][$key]['header_img']=C('HEAD_DEFAULT_IMG');
                }else{
                    $collectlive['list'][$key]['header_img']=$member_author['header_img'];
                }
            }
        }
        return $collectlive;
    }

    /**删除关注
     * @param $id
     */
    public function delete_collect($id,$memberinfo){
        if(empty($id)){
            $return['msg'] = '参数错误';
            $return['status']=0;
            return $return;
        }
        $map['id']=$id;
        $map['user_id']=$memberinfo['id'];
        $result=M('LiveFollow')->where($map)->delete();
        if($result){
            $return['msg']='取消成功';
            $return['status']=1;
        }else{
            $return['msg']='取消失败';
            $return['status']=0;
        }
        return $return;
    }
    /**关注主播
     * @return array
     */
    public function follow_live($roomnumber,$memberinfo=''){
        $return = array('status'=>0,'msg'=>'');
        // $memberinfo = checkuserauthtype();
        if(empty($memberinfo)){//登录后操作
            $return['status'] = 0;
            $return['msg'] = '请先登录';
            $return['url'] = getjumpurl('JUMP_1');
            return $return;
        }
        if(empty($roomnumber)){
            $return['status'] = 0;
            $return['msg'] = '请选择要关注的主播';
            return $return;
        }
        $map=$au_map=array();
        $map['roomnumber']=$roomnumber;
        $author_ids = M("LiveAnchor")->where($map)->field('id,user_id')->find();
        $author_id=$author_ids['id'];
        $map_co['anchor_id']=$author_id;
        $map_co['user_id']=$memberinfo['id'];
        $favinfo = M('LiveFollow')->where($map_co)->count();
        //1211
        if($memberinfo['id']==$author_ids['user_id']){
            $return['status'] = 0;
            $return['msg'] = '自己不能关注自己哦';
            return $return;
        }
        //end
        if ($favinfo) {
            $favinfo =  M('LiveFollow')->where($map_co)->delete();
            $return['status'] = 2;
            $return['data']='关注';
            $return['msg'] = '取消关注成功!';
        }else {
            $data['anchor_id'] = $author_id;
            $data['user_id'] = $memberinfo['id'];
            $data['addtime'] = NOW_TIME;
            $data['status'] = 1;
            $res = M('LiveFollow')->add($data);
            if($res){
                $return['status'] = 1;
                $return['data']='已关注';
                $return['msg'] = '关注成功';
            }else{
                $return['status'] = 0;
                $return['msg'] = '关注失败';
            }
        }
        return $return;
    }

    /**
     * 播放历史
     */
    public function get_live_history($roomnumber,$join='',$order='',$p=1,$pagesize=0){
        // $pagesize=empty($pagesize)?C('WEB_GOODS_PAGE'):$pagesize;
        $pagesize=12;
        $p=empty($p)?1:$p;
        $map=array();
        if(empty($roomnumber)) return false;
        $map['roomnumber']=$roomnumber;
        $map['end_time']=array('gt',0);
        $order['id']='desc';
        $live_history=page_list('LiveHistory',$map,$order,true,$pagesize,$join,$p);
        foreach($live_history['list'] as $key=>$val){
            $time=format_decimal(($val['end_time']-$val['begin_time'])%86400/3600);
            if($time<0){$time=0;}
            $live_history['list'][$key]['hours']=$time;
            // $live_history['list'][$key]['begin_time']=time_format($val['begin_time']);
            $live_history['list'][$key]['begin_time']=date("Y-m-d H:i",$val['begin_time']);
            $live_history['list'][$key]['end_time']=time_format($val['end_time']);
            $live_history['list'][$key]['liveonlinecount']=$val['liveonlinecount']?$val['liveonlinecount']:0;
            $live_history['list'][$key]['liveimg']=thumb($val['liveimg'],500,500,'noimg_500_500.png');
            $goods_info=unserialize($val['goods_info']);
            $live_history['list'][$key]['goods_count']=count($goods_info)?count($goods_info):0;
        }
        return $live_history;
    }
	/**
     * 主播收益
     */
    public function get_live_divided($roomnumber,$p=1,$pagesize=0){
        $pagesize=empty($pagesize)?C('WEB_GOODS_PAGE'):$pagesize;
        $p=empty($p)?1:$p;
        if(empty($roomnumber)) return false;
        $map=array();$map['roomnumber']=$roomnumber;
        $anchor_uid=M("LiveAnchor")->where($map)->getField('user_id');
		
		$map=array();
		$map['status']=1;
        $map['anchor_id']=$anchor_uid;
        // $map['divided_status']=array("in",'1,2');
        $live_divided=page_list('LiveDivided',$map,'',true,$pagesize,'',$p);
		if($live_divided['list']){
			foreach($live_divided['list'] as $key=>$val){
				$live_divided['list'][$key]['goods_name']=idtoname($val['goods_id'],"Goods",'goods_name');
				$live_divided['list'][$key]['goods_sn']=idtoname($val['goods_id'],"Goods",'goods_sn');
				$live_divided['list'][$key]['order_time']=time_format(idtoname($val['order_id'],"OrderInfo",'addtime'));
				if($val['goods_attr']){
					$live_divided['list'][$key]['goods_attr']=D("Common/GoodsAttr")->goods_to_attrname($val['goods_attr']);
				}else{
					$live_divided['list'][$key]['goods_attr']='无';
				}
				$status_name = '未分佣';
				$divided_time='未分佣';
				if($val['status_name'] == 1) {
					$divided_time='待分佣';
					$status_name = '待分佣';
				}else if($val['status_name'] == 2) {
					$divided_time=time_format($val['divided_time']);
					$status_name = '已分佣';
				}
				$live_divided['list'][$key]['divided_time']=$divided_time;
				$live_divided['list'][$key]['status_name']=$status_name;
			}
		}
        return $live_divided;
    }

    /**
     * 主播个人主页
     */
    public function anchor_info($id,$roomnumber='',$memberinfo){
        if(empty($id))return false;
        $map=$f_map=$u_map=array();
        $map['id']=$id;
        $anchor_info=M("LiveAnchor")->where($map)->field(true)->find();
        //获取昵称和头像
        $u_map['id']=$anchor_info['user_id'];
        $member_author=M("Member")->where($u_map)->field('nickname,username,mobile,header_img')->find();
        if($member_author){
            $anchor_info['name']=D('Chat')->getnickname($member_author);
            if(empty($anchor_info['name'])) $anchor_info['name'] = '汉潮会员';
            $anchor_info['header_img']=$member_author['header_img'];            
        }
        //获取该主播的关注数
        $f_map['anchor_id']=$id;
        $flow_count=M("LiveFollow")->where($f_map)->count();
        $anchor_info['follow_count']=$flow_count;
        //获取正在直播的
        // $now_live= D("Chat")->runHistory('query');
        // $online_people_rate = idtoname('online_people_rate','LiveSet','value','name');
        $online_people_rate = getconfig('ONLINE_PEOPLE_RATE');
        $now_live = D("Chat")->runHistory('query','',$roomnumber,0,'',$memberinfo);
        if($now_live){
            $now_live['number']=intval($online_people_rate+$now_live['liveonlinecount']);
            $anchor_info['now_live']=$now_live;
        }
        
        //判断是否关注
        $map_co=array();
        if($memberinfo){
            $map_co['anchor_id']=$id;
            $map_co['user_id']=$memberinfo['id'];
            $favinfo = M('LiveFollow')->where($map_co)->count();
            $anchor_info['collect_count']=$favinfo;
        }
        return $anchor_info;
    }

    /**根据会员找主播信息
     * @param $user_id
     */
    public function get_userby_live($user_id){
        if(empty($user_id))return false;
        $l_map = array();$l_map['user_id']=$user_id;
        $l_map['is_delete']=0;
        $LiveAnchor=M("LiveAnchor")->where($l_map)->field('roomnumber,status,content')->find();
        if($LiveAnchor){
            $model = M('LiveFollow');
            $map = array();
			$map['status']=1;
            $map['anchor_id']=$LiveAnchor['id'];
            $map['user_id'][]=array('gt',0);
            $map['user_id'][]=array('neq',$user_id);
            $fans_num = $model->where($map)->count();
            $LiveAnchor['fans_num'] = $fans_num;
			
			$map = array();$map['status']=1;$map['user_id']=$user_id;
			$follow_num=$model->where($map)->count();
			$LiveAnchor['follow_num'] = $follow_num;
			
            return $LiveAnchor;
        }else{
            return false;
        }
    }

    /**保存预告直播
     * @param array $data
     */
    public function save_trailer($data=array()){
        $return=array('status'=>1);
        $id=$data['id'];
        if(empty($data)){
            $return['status']=0;
            $return['msg']='参数错误';
            return $return;
        }
        if(empty($data['begin_time'])){
            $return['status']=0;
            $return['msg']='开播时间不能为空';
            return $return;
        }
        if(empty($data['anchor_id'])){
            $return['status']=0;
            $return['msg']='主播不能为空';
            return $return;
        }
        $c_map=$t_map=array();
        $c_map['name']='live_trailer_num';
        $config=M("LiveSet")->where($c_map)->getField('value');
        $t_map['anchor_id']=$data['anchor_id'];
        $trailer_count=M("LiveTrailer")->where($t_map)->count();
        if($trailer_count>$config){
            $return['status']=0;
            $return['msg']='已超过预告数量';
            return $return;
        }
        if(array_filter($data['ids'])){
            $g_map=array();
            $g_map['id']=array('in',$data['ids']);
            $goods_info=M("Goods")->where($g_map)->field('id,goods_name,shop_price,img,sell_count')->select();
            if($goods_info){
                $data['goods_info']=serialize($goods_info);
            }
        }
        $data['status']=1;
        if($id){
            $data['update_time']=NOW_TIME;
            $l_map['id']=$data['id'];
            unset($data['ids']);
            unset($data['id']);
            unset($data['__hash__']);
            $res=M("LiveTrailer")->where($l_map)->save($data);
        }else{
            $res=M("LiveTrailer")->add($data);
        }

        if($res){
            $return['status']=1;
            $return['msg']='提交成功';
            return $return;
        }else{
            $return['status']=0;
            $return['msg']='提交失败';
            return $return;
        }
        return $return;
    }

    /**删除预告直播
     * @param $id
     * @return array
     */
    public function delete_trailer($id){
        $return=array('status'=>1,'msg'=>'撤销成功！');
        if(empty($id)){
            $return['status']=0;
            $return['msg']='参数错误';
            return $return;
        }
        $map['id']=$id;
        $res=M("LiveTrailer")->where($map)->delete();
        if(!$res){
            $return['status']=0;
            $return['msg']='撤销失败';
            return $return;
        }
        return $return;
    }

    /**
     * 会员中心获取主播分佣信息
     */
    public function get_live_deieved_list($map=array(),$join='',$order='',$p=1,$pagesize=0){
        $memberinfo = checkuserauthtype();
        if(empty($memberinfo)){//登录后操作
            $return['status'] = 0;
            $return['msg'] = '请先登录';
            $return['url'] = getjumpurl('JUMP_1');
            return $return;
        }
        $pagesize = empty($pagesize) ? C('WEB_GOODS_PAGE') : $pagesize;
        $map['anchor_id']=$memberinfo['id'];
        $map['status']=1;
        $map['divided_status']=array('in','1,2');
        $order="divided_time desc,id desc";
        $live_list=page_list('LiveDivided',$map,$order,true,$pagesize,$join,$p);
        if($live_list['list']){
            foreach($live_list['list'] as $key=>$val){
                $live_list['list'][$key]['goods_name']=idtoname($val['goods_id'],"Goods",'goods_name');
                $live_list['list'][$key]['goods_attr']=D("Common/GoodsAttr")->goods_to_attrname($val['goods_attr']);
               $live_list['list'][$key]['divided_time']=time_format($val['divided_time']);
            }
        }
        $divided_map=array();
        $divided_map['anchor_id']=$memberinfo['id'];
        $divided_map['status']=1;
        $divided_map['divided_status']=2;
        $live_list['total_price']=M("LiveDivided")->where($divided_map)->sum("balance_money");
        $divided_map=array();
        $divided_map['anchor_id']=$memberinfo['id'];
        $divided_map['status']=1;
        $divided_map['divided_status']=1;
        $live_list['no_divided_price']=M("LiveDivided")->where($divided_map)->sum("balance_money");
        return $live_list;
    }

    /**预告开播
     * @param $room 房间号
     * @param $tid 预告id
     */
    public function start_live($room,$tid){
        $return=array('status'=>1);
        if(empty($tid)){
            $return['status']=0;
            $return['msg']='参数错误';
            return $return;
        }
        //获取预告信息
        $t_map=array();
        $t_map['id']=$tid;
        $t_map['status']=1;
        $t_map['is_delete']=0;
        $t_info=M("LiveTrailer")->where($t_map)->field(true)->find();
        //开播
        $result = D('Chat')->runHistory('start','',$room,1);
        if($result['status']==1){
            $map=array();
            $map['roomnumber']=$room;
            $map['end_time']=0;
            $history_id=M("LiveHistory")->where($map)->getField('id');
            //修改播放历史信息
            if($history_id){
                $h_map=array();
                $h_map['id']=$history_id;
                $h_data['liveimg']=$t_info['index_img'];
                $h_data['livebrief']=$t_info['brief'];
                $h_data['goods_info']=$t_info['goods_info'];
                $h_data['is_effect']=1;
                $res=M("LiveHistory")->where($h_map)->save($h_data);
                //作废更改预告信息
                if($res){
                    $t_data['status']=0;
                    $t_data['is_delete']=1;
                    M("LiveTrailer")->where($t_map)->save($t_data);
                }
            }
        }else{
            $return['status']=0;
            $return['msg']=$result['msg'];
            return $return;
        }
        return $return;
    }

    /**获取分类或者店铺
     * @param $keyword
     * @param $type 1分类3店铺2商品
     * @return array
     */
    public function get_param_info($keyword,$type){
        $return=array('status'=>1);
        if(empty($type)){
            $return['status']=0;
            $return['msg']='请选择搜索的内容！';
            return $return;
        }
        $map=array();
        if($type==1){
            $model="GoodsCategory";
            $map['pid']=0;
        }elseif($type==3){
            $model="Shops";
            $map['status']=1;
        }
        $map['name']=array('LIKE','%'.$keyword.'%');
        $info=M($model)->where($map)->field('id,name')->select();
        if(empty($info)){
            $return['msg']='未获取到关键词信息';
            $return['status']=0;
        }
        $return['data']=$info;
        return $return;
    }

    /**根据关键词获取商品信息
     * @return array
     */
    public function getgoods($goods_ids,$data,$p = 1, $pagesize = 0,$user_info='',$room_id='',$param=''){
        $return = array('status'=>0,'msg'=>'未获取到数据');
        $jf_ids = M('GoodsIntegral')->where('status=1')->getField('goods_id',true);
        if($jf_ids){
            $jf_ids = implode(',',$jf_ids);
            $where['id']=array('NOTIN',$jf_ids);
        }
        if($goods_ids){
            if($jf_ids){
                $where['id']  = array(array('in',$goods_ids),array('NOTIN',$jf_ids),'and');
            }else{
                $where['id'] = array('in',$goods_ids);
            }
            // $where['id'] = array('in',$goods_ids);
        }else{
            if($data['cate_key']==2) {
                $where['goods_name'] = array('LIKE', '%' . $data['search_key'] . '%');
            }else{
                $where['name'] = array('LIKE', '%' . $data['search_key'] . '%');
            }
            if($data['cate_key']==1){
                $where['cat_id'] = $data['search_id'];
            }
            if($data['cate_key']==3){
                $where['shop_id'] = $data['search_id'];
            }
            //排除掉已经添加过的商品
            $live_history_info = D('Chat')->runHistory('query','',$room_id,0,$param,$user_info);//查询主播直播记录            
            if($live_history_info && $live_history_info['goods_info']){
                $live_goods_info = $live_history_info['goods_info'];
                $live_goods_info_arr = unserialize($live_goods_info);
                foreach($live_goods_info_arr as $k=>$v){$goodsarr[] = $v['id'];}
                $where['id'] = array('not in',$goodsarr);
            }
        }
        // $field = array('id','goods_name','sell_count','shop_price','img');
        $field = '*';        
        $where['status'] = 1;
        $where['is_delete'] = 0;
        $where['is_offline'] = 1;
        $goods_list =page_list('Goods', $where, '', $field, $pagesize, '', $p); 
        if(count($goods_list['list'])>0){
            $return['status'] = 1;
            // foreach($goods_list['list'] as $k=>$v){
            //     $goods_list['list'][$k]['images'] = thumb($v['img'],200,200);
            //     $goods_list['list'][$k]['img'] = thumb($v['img'],200,200);
            //     //1105
            //     // $goods_list['list'][$k]['addname'] = '确认添加';
            //     $goods_list['list'][$k]['addname'] = '/images/nocheck_duig.png';
            //     //end               
            //     //得到该商品主播应得的佣金
            //     $goods_list['list'][$k]['balance_price']=D("LiveDivided")->get_goods_balancemoney($v['id'],1,$v['shop_price']);
            // }
            $goods_list['list']=D('Takeout')->format_data($goods_list['list'],2,0,$param);
            $return['data'] = $goods_list; 
        }
        return $return;     
    }

    /**格式化推荐商品信息(更新存储)
     * @return array
     */
    public function formatgoods($data=array(),$user=''){


        $return = array('status'=>0,'msg'=>'商品信息错误');
        $checkinfo = D('Chat')->checkAnchor($user);//检查当前主播权限
        if($checkinfo['status']!=1){
            $return['msg'] = $checkinfo['msg'];
            return $return;
        }

        //1126
        $param=array();
        $param['map'] = 1;
        $live_history_info = D('Chat')->runHistory('query','',$data['room_id'],0,$param,$user);//查询主播直播记录
        //end
        // if(empty($live_history_info)){
        //     $return['msg'] = '直播间未开播';
        //     return $return;
        // }
        if($live_history_info) $live_goods_info = $live_history_info['goods_info'];
        $i = 1;
        if($live_goods_info){
            $arrsort = $arrids = array();
            $live_goods_info_arr = unserialize($live_goods_info);
            foreach($live_goods_info_arr as $k=>$v){
                $arrsort[] = $v['sort'];
                $arrids[] = $v['id'];
            }  
            $i = max($arrsort)+1;  
        }

        $param['uid']=$user['id'];
        $param['shop_id']=$user['shop_id'];//追加当前会员匹配的门店
        $param['little_shop_id']=$user['HClittle']['bind_littleshop_id'];//追加当前会员匹配的团长
        $list = $this->getgoods($data['goods_id'],'',1,0,$user,$data['room_id'],$param);//可接受数组
        // $list = $this->getgoods($data['goods_id'],'');//可接受数组
        $goods_list = $list['data']['list'];
         foreach($goods_list as $k=>$v){
            if(in_array($v['id'],$arrids)){
                $return['msg'] = '商品已存在，请勿重复添加';
                return $return;
            }
            $goods_list[$k]['sort'] = $i;
            $live_goods_info_arr[$i] = $goods_list[$k];
            $i++;
        }


        $save['goods_info'] = serialize($live_goods_info_arr);
        if($live_history_info){            
            $where['id'] = $live_history_info['id'];
            M('LiveHistory')->where($where)->save($save);
        }else{
            $new_lives = D('Chat')->runHistory('start','',$data['room_id'],0,$data,$user, $save['goods_info']);//查询主播直播记录
            $return['new_lives'] = $new_lives;
        }        
        foreach($goods_list as $k=>$v){
            $goods_list[$k]['url'] = geturl('Home/Goods/index',array('id'=>$v['id']),true,true);
            $goods_list[$k]['Murl'] = geturl('Mobile/Goods/goods_cont',array('id'=>$v['id']),true,true);
        } 
        $return['status'] = 1;
        $return['goods_list'] = $goods_list;
        $return['total_count'] = count($live_goods_info_arr); 
        return $return;    
    }

    /**
   * 设置腾讯云云通信群组ID
   * $id  直播记录id
   * $user_id  房间id
   * @return array
   */
  public function chat_room_id($id='',$room_id=''){
    $return=array('status'=>0);if(empty($room_id))return $return;
    $room=$la_save=$where=array();
    $map = array();
    $map['roomnumber'] = $room_id;    
    $liveanchor = M('LiveAnchor')->where($map)->field('id,user_id,roomnumber,group_id')->find();
    if(!empty($liveanchor['group_id'])){
        $group_id=$liveanchor['group_id'];
    }
    if(empty($group_id)){
        $memberinfo=idtoname($liveanchor['user_id'],'Member','username,nickname,mobile,id,register_tencent');    
        //325新加创建聊天室房间
        $ims= new \Common\Vendor\JimIM;
        $username =$memberinfo['id']<10?'hc0'.$memberinfo['id']:'hc'.$memberinfo['id'];$password='hanhcao';            
        if(empty($memberinfo['register_tencent'])){                
            $nickname=D("Chat")->getnickname($memberinfo);                
            $ims->register($username,$password,$nickname);
        }
        $name=$username;
        $owner=$username;
        $room_id=$ims->createRoom($name,$owner);//创建聊天室
        if($room_id['chatroom_id']){
            $group_id=$room_id['chatroom_id'];
        }
    }
    
    if(empty($group_id)){
        $return['msg'] = '房间id创建错误，请联系系统管理员';
        return $return;
    }  
    $saves=$la_save=array();
    $saves['group_id']=$la_save['group_id']=$group_id;
    $saves['is_live'] = 1;
    M('LiveAnchor')->where($map)->save($saves);
    if($id)M('LiveHistory')->where('id='.$id)->save($la_save);
    $return=array('status'=>1,'group_id'=>$la_save['group_id']);
    return $return;
  }
  /**
     * 我的关注列表  ----正在使用
     */
    public function get_collect_livenew($memberinfo){
        $memberinfo=$memberinfo;if(empty($memberinfo)) return false;
		$map['status']=1;
        $map['user_id'][]=array('gt',0);
		$map['user_id'][]=$memberinfo['id'];
        $map['anchor_id']=array('neq',$memberinfo['id']);		
        $collectlive=page_list('LiveFollow',$map);
        
        foreach($collectlive['list'] as $key=>$val){
            $u_map=$mmap=array();
            $u_map['id']=$val['anchor_id'];
            $user_id=M("LiveAnchor")->where($u_map)->getField('user_id');
            $mmap['id'] = $user_id;
            $mmap['status'] = 1;
            $member_author = M('Member')->where($mmap)->field('username,nickname,mobile,id,header_img')->find();
            if($member_author){
                $collectlive['list'][$key]['name']=D('Chat')->getnickname($member_author);
                // $collectlive['list'][$key]['name']=show_user_name($member_author['id']);
                if(empty($member_author['header_img'])){
                    if(C('HEAD_DEFAULT_IMG')) $collectlive['list'][$key]['header_img']=C('HEAD_DEFAULT_IMG');
                    else $collectlive['list'][$key]['header_img']='';
                    
                }else{
                    $collectlive['list'][$key]['header_img']=format_url($member_author['header_img']);
                }
            }

            if($val['anchor_id'] != 0){ //被关注人是主播
                $city=idtoname($val['anchor_id'],'LiveAnchor','city_id','id',false);
                if($city){
                    $collectlive['list'][$key]['city']=idtoname($city,"Area",'name');
                }
                $collectlive['list'][$key]['is_anchor']=1;
                //同时得到主播正在进行的直播
                $roomnumber=idtoname($val['anchor_id'],'LiveAnchor','roomnumber');
                $collectlive['list'][$key]['roomnumber']=$roomnumber;
                $h_map=array();
                if($map['is_dgz']==1){
                  $h_map['begin_time']=array('egt',strtotime(date('Y-m-d'.'00:00:00',NOW_TIME)));//直播有效时间是今日开始
                  $h_map['end_time'] = 0;
                }

                $h_map['is_effect'] = 0;
                $h_map['roomnumber'] =$roomnumber;
                //直播,to_id
                $living=M("LiveHistory")->where($h_map)->field('liveimg,liveonlinecount,id as hid')->find();

                if($living){
                    if($living['liveimg']){
                      $livelist['list'][$key]['liveimg']=format_url($living['liveimg']);
                    }else{
                      $indeximg=idtoname($val['anchor_id'],'LiveAnchor','index_img','id',false);
                      $livelist['list'][$key]['liveimg']=format_url($indeximg);
                    }
                    $collectlive['list'][$key]['liveonlinecount']=$living['liveonlinecount'];
                    $collectlive['list'][$key]['hid']=$living['hid'];
                    $collectlive['list'][$key]['to_name']=idtoname($living['to_id'],"LiveTopic",'name');
                }
            }else{
                $collectlive['list'][$key]['is_anchor']=0;

            }
        }
        return $collectlive;
    }
	/**
     * 我的粉丝列表  ----正在使用
     */
    public function get_fans_list($memberinfo){
        $memberinfo=$memberinfo;if(empty($memberinfo)) return false;
        // $map['a.status']=1;
        // $map['a.anchor_id']=$memberinfo['id'];
        // $map['a.user_id'][]=array('gt',0);
		// $map['a.user_id'][]=array('neq',$memberinfo['id']);

		// $field='s.id,a.anchor_id,a.user_id,a.addtime,a.status as astatus,a.user_id,a.id as fid';
        // $join=" LEFT JOIN __MEMBER__ s ON a.user_id=s.id ";
        // $collectlive=page_list('LiveFollow',$map,$order,$field,$pagesize,$join,$p);
		
		$map['status']=1;
        $anchor_id = idtoname($memberinfo['id'],'LiveAnchor','id','user_id',false);

        $map['anchor_id']=$anchor_id;
        $map['user_id'][]=array('gt',0);
		$map['user_id'][]=array('neq',$memberinfo['id']);
        $order='id desc';
		
        $collectlive=page_list('LiveFollow',$map,$order);




        foreach($collectlive['list'] as $key=>$val){
            $u_map=array();$u_map['id']=$val['user_id'];
            $member_author=M("Member")->where($u_map)->field('id,header_img')->find();
            if($member_author){
                $collectlive['list'][$key]['uid']=$member_author['id'];
                $collectlive['list'][$key]['name']=show_user_name($member_author['id']);
                $collectlive['list'][$key]['roomnumber']=idtoname($val['user_id'],'LiveAnchor','roomnumber','user_id',false);
				if(empty($member_author['header_img'])){
					if(C('HEAD_DEFAULT_IMG')) $collectlive['list'][$key]['header_img']=C('HEAD_DEFAULT_IMG');
					else $collectlive['list'][$key]['header_img']='';
				}else{
					$collectlive['list'][$key]['header_img']=format_url($member_author['header_img']);
				}
                $collectlive['list'][$key]['addtime']=time_format($val['addtime']);
				
				$collectlive['list'][$key]['fans_num']=$collectlive['list'][$key]['follow_num']=0;
				$map=array();$map['status']=1;$map['anchor_id']=$val['user_id'];
				$map['user_id'][]=array('gt',0);$map['user_id'][]=array('neq',$val['user_id']);
				$collectlive['list'][$key]['fans_num']=M('LiveFollow')->where($map)->count();
				
				$map=array();$map['status']=1;$map['user_id']=$val['user_id'];
				$collectlive['list'][$key]['follow_num']=M('LiveFollow')->where($map)->count();
            }
        }
        return $collectlive;
    }

    //腾讯云通信1113新加
    public function usersig($user_id){
        // 初始化
      $return=array('status'=>1);
      $u_id=array();
      $sex=array('0' => '未知','1' => '男','2' => '女');//性别标识1男2女3未知
      $sexid=array('0' => 'Gender_Type_Unknown','1' => 'Gender_Type_Male','2' => 'Gender_Type_Female');//性别代码标识1男2女3未知
      $judge=false;//保存标识

      $u_id['id']=$user_id;
      $user=M("Member")->where($u_id)->field('id,nickname,username,header_img,sex,register_tencent')->find();
      $user['nickname'] = idtoname($user_id,'Member','nickname;username;id');//获取最佳昵称

      if ($user) {//获取用户信息成功
          $im= new \Common\Vendor\Webim;
          // 
          $usersig=$im->getusersig(C('USERSIG'));
          $im->set_user_sig($usersig['info']);//设置当前用户sig
          // 判断是否有SIG,0为没有
          if ($user['register_tencent']==0) {
               $ret = $im->account_import($user_id);//导入用户
               // $ret = $im->account_update($user_id,$user['nickname'],$sex[$user['sex']],$user['header_img']);//配置用户信息
               $ret = $im->account_info($user_id);//获取用户信息

               $sig_save['register_tencent']=1;
               M('Member')->where('id='.$u_id['id'])->save($sig_save);
          }else{

               $ret = $im->account_info($user_id);//获取用户信息
               // 比对信息，如果比对不一样，就更改为数据库样式
               if ($ret['info']['UserProfileItem']['0']['ProfileItem']['0']['Value']!=$user['nickname']) {//昵称
                   $judge=true;
               }
               if ($ret['info']['UserProfileItem']['0']['ProfileItem']['1']['Value']!=$sexid[$user['sex']]) {//性别
                   $judge=true;
               }
               if ($ret['info']['UserProfileItem']['0']['ProfileItem']['3']['Value']!=$user['header_img']) {//头像
                   $judge=true;
               }

               if ($judge) {
                   $ret = $im->account_update($user_id,$user['nickname'],$sex[$user['sex']],$user['header_img']);//配置用户信息
                   $ret = $im->account_info($user_id);//获取用户信息
               }
          }
          
         $usersig=$im->getusersig($user_id);
         $return['user_sig']=$usersig['info'];
         $return['user_userid']=$user['id'];
         $return['user_nickname']=$ret['info']['UserProfileItem']['0']['ProfileItem']['0']['Value'];
         $return['user_header_img']=$ret['info']['UserProfileItem']['0']['ProfileItem']['3']['Value'];
      }else{
         $return['status']=0;
         $return['msg']='获取用户信息出错';
      }
      return $return;
    }

    public function cont($param){
        $return=array('status'=>1);
        if(empty($param['id'])){
            $return['status']=3;
            $return['msg']='参数错误';
            return $return;
        }
        if($param['shop_id']){
            $map=array();
            $map['id']=$param['shop_id'];
            $shop_name=M("Shops")->where($map)->getField('name');
        }else{
            $shop_name='暂无配送门店';
        }
        $goods_id=$param['id'];
        $type=$param['type'];
        $goods_info= D("Common/Goods")->get_goods_item($goods_id);
        if($goods_info['status']==0){
            $return['status']=3;
            $return['msg']=$goods_info['msg'];
            return $return;
        }
        //商品相册
        $where['goods_id']=$goods_id;
        $goods_imgs = M("GoodsGallery")->where($where)->select();
        if (empty($goods_imgs)) $goods_imgs[0]['goods_img'] = $goods_info['data']['img'];
        foreach($goods_imgs as $key=>$val){
            $goods_imgs[$key]['goods_img']=$val['goods_img'];
        }
        //获取商品属性和规格
        $data = D("Common/GoodsAttr")->get_shop_attr($goods_id);
        //商品最小价格
        $price_info=D('Common/Takeout')->get_min_price($param['shop_id'],$goods_id);
        $goods_info['data']['stock'] = D('Wxsmall/Takeout')->format_stock($param['shop_id'],$param);//根据当前定位的门店信息取出对门店商品库存
        $goods_info['data']['min_price_format']=format_price($price_info['goods_price']);
        $goods_info['data']['min_price']=$price_info['goods_price'];
        $goods_info['data']['goods_imgs']=$goods_imgs;
        //判断是否收藏
        $where = $map = array();
        //是否收藏
        $map['uid'] = $param['uid'];
        $map['goods_id'] = $goods_id;
        $map['type'] = $type;
        if ($type == 1) {
            $where['goods_id'] = $goods_id;
            $map['integral_id'] = M('GoodsIntegral')->where($where)->getField('id');
        }
        $is_collection = M('GoodsCollect')->where($map)->count();
        //购物车数量
        $map=array();
        $map['user_id']=$param['uid'];
        $map['goods_id'] = $goods_id;
        $map['cart_type'] = 3;
        $cart_num_list=M("Cart")->where($map)->field('goods_num')->select();
        $cart_num = 0;
        foreach($cart_num_list as $k=>$v){
            $cart_num += $v['goods_num'];
        }
        $goods_info['data']['is_collection']=$is_collection;
        $return['goods_info']=$goods_info['data'];
        $return['goods_info']['shop_name']=$shop_name;
        if($data['newarr']){
            $attr_msg='';
            $i=0;
            foreach ($data['newarr'] as $key => $value) {
                if($value['options']){
                    foreach ($value['options'] as $k => $va) {
                        $data['newarr'][$key]['options'][$k]['title_name']=$value['name'];
                    }
                }
                
                if($i==0){
                    $attr_msg .= $value['name'];
                }else{
                    $attr_msg .=','.$value['name'];
                }
                
            }
            $return['attr_msg']=$attr_msg;
        }        
        $return['goods_attrs']=$data;    
        $return['cart_num']=$cart_num?$cart_num:1;
        return $return;
    }
    public function add_cart($param=array()){
        $return = array('status'=>3,'msg'=>'参数错误');
        $model='Common/Takeout';
        $data=array();
        $shop_id = $param['shop_id'];
        $param['sub_list'] = json_decode(htmlspecialchars_decode($param['sub_list']),true);
        if(empty($param['sub_list'])){
            $return['msg']='请选择商品';
            return $return;
        }
        if(empty($param['shop_id'])){
            $return['msg']='无门店可配送';
            return $return;
        }
        $param['sub_list']= array_filter($param['sub_list']);
        foreach($param['sub_list'] as $k=>$v){
            $attr_info = '';
            if ($v['spec_format']) {
                $goodsattr_info = array(
                    'key' => $v['spec_format']['key'],
                    'name' => $v['spec_format']['name']
                );
                $attr_info = serialize($goodsattr_info);
            }
            if($param['add_type']=='cont'){
                $map = array();
                $map['user_id'] = $param['uid'];
                $map['shop_id'] = $shop_id;
                $map['goods_id']=$v['id'];
                if($attr_info){
                    $map['goods_attr'] = $attr_info;
                }
                $map['cart_type']=3;
                $goods_num = M('Cart')->where($map)->getField('goods_num');
                if($goods_num>0){
                    $v['num'] = $v['num']+$goods_num;
                }
            }
            $map = array();
            $map['goods_id']=$v['id'];
            $map['shop_id']=$shop_id;
            $map['goods_stock']=$v['spec_format']? $v['spec_format']['key'] : 0;
            $sgsinfo=M('ShopsGoodsStock')->where($map)->field(true)->find();
            $map = array();
            $map['id']=$v['id'];
            $goods_info=M('Goods')->where($map)->field('goods_name,img,market_price,goods_sn,cat_id,promote_id,promote_price,promote_start_time,promote_end_time,sale_type')->find();   
            if($v['num']<=0){
                $return['msg'] = '请加入商品【'.$goods_info['goods_name'].'】数量';
                return $return;
            }
            if($goods_info['sale_type']==1){//组合商品库存
                $goods_number = D('Wxsmall/Takeout')->get_group_goods_stock($v['id'],$shop_id);
                if($v['num']>$goods_number){
                    $return['msg'] = $goods_info['goods_name'].'【库存不足，库存量'.$goods_number.'】';
                    return $return;
                }
            }else{
                if($sgsinfo){
                    if($v['num']>$sgsinfo['goods_number']){
                        $return['msg'] = $goods_info['goods_name'].'【库存不足，库存量'.$sgsinfo['goods_number'].'】';
                        return $return;
                    }
                }else{
                    $return['msg'] = $goods_info['goods_name'].'【库存不足】';
                    return $return;
                }
            }
            $savedata[$k]['user_id'] = $param['uid'];
            $savedata[$k]['goods_id']=$v['id'];
            $savedata[$k]['shop_id']=$shop_id;
            $savedata[$k]['goods_name']=$goods_info['goods_name'];
            $savedata[$k]['goods_sn']=$goods_info['goods_sn'];
            $savedata[$k]['goods_img']=$goods_info['img'];
            $savedata[$k]['catid']=$goods_info['cat_id'];
            $savedata[$k]['goods_num']=$v['num'];
            $savedata[$k]['goods_attr']=$attr_info;
            if($v['spec_format']){
                $map = array();
                $map['attr_key'] = $v['spec_format']['key'];
                $map['goods_id'] = $v['id'];
                $memberinfo=M('Member')->where('id='.intval($param['uid']))->field(true)->find();
                $price_info=D('Pad/Cashier')->get_min_price($shop_id,$v['id'],$memberinfo,$v['spec_format']['key']);
            }else{
                $price_info=D('Common/Takeout')->get_min_price($shop_id,$v['id']);//获取商品最终价格
            }
            $savedata[$k]['shop_price']=$price_info['goods_price'];
            $savedata[$k]['all_price']=$v['num']*$price_info['goods_price'];
            $savedata[$k]['price_type'] = $price_info['price_type'];
            $savedata[$k]['cart_type']=3;//26直播购物
            $savedata[$k]['live_history_id']=$param['live_history_id'];//直播记录id
            $savedata[$k]['live_anchor_id'] = $param['live_anchor_id'];//主播id
            //活动id
         // if($savedata[$k]['price_type'] == 3){
         //     $savedata[$k]['cart_id'] = $price_info['act_id'];
         // }else{
                $ret = D('Common/Promot')->goods_to_promot($v['id'],0,$savedata[$k]['cart_type'],$shop_id);
                if($ret['count'] > 0){
                    $act_info = current($ret['list']);
                    $savedata[$k]['cart_id'] = $act_info['id'];
                }
            // }
        }
        if($param['add_type']!='cont'){
            $map = array();
            $map['user_id'] = $param['uid'];
            $map['shop_id'] = $shop_id;
            $map['cart_type']=3;
            M('Cart')->where($map)->delete();
        }
        $cmodel = M("Cart");
        if($param['add_type']=='cont'){

            foreach($savedata as $k=>$v){
                $map = array();
                $map['user_id'] = $param['uid'];
                $map['shop_id'] = $shop_id;
                $map['goods_id']=$v['goods_id'];
                $map['cart_type']=$param['type'];
                $cart_count=$cmodel->where($map)->count();
                if($cart_count>0){
                    $result = $cmodel->where($map)->save($v);

                }else {
                    $result = $cmodel->add($v);
                }
            }
        }else {
            foreach ($savedata as $k => $v) {
                $result = $cmodel->add($v);
            }
        }
        if($result){
            $map=array();
            $map['user_id']=$param['uid'];
            $map['cart_type']=3;
            $cart_num_list=$cmodel->where($map)->field('goods_num')->select();
            $cart_num = 0;
            foreach($cart_num_list as $k=>$v){
                $cart_num += $v['goods_num'];
            }
            $return['status'] = 1;
            $return['cart_num'] = $cart_num;
            $return['msg'] = '操作成功';
        }
        return $return;
    }
    //获取微信小程序appid
    public function get_small_appid($morecode='SHOP'){
        $wx = new \Common\Vendor\Weixinopen;
        $siteid=C('appid');
        $cachename_moresmall='config_moresmall.php';
        $config_morelist=$wx->wxcache($cachename_moresmall);
        $wx_shop_appid=$config_morelist[$siteid.'_'.$morecode];
        return $wx_shop_appid;
    }
    //end
	public function sub_anchor_info($getdata){
		$return = array('status'=>3,'msg'=>'参数错误');
		if(!$getdata['user_id']){return $return;}
		if(!$getdata['cate_id']){$return['msg'] = '分类必须选择';return $return;}
		// if(!$getdata['brief']){$return['msg'] = '小标题必须填写';return $return;}
		if(!$getdata['index_img']){$return['msg'] = '封面图必须选择';return $return;}
		if(!$getdata['front_card']){$return['msg'] = '身份证正面必须选择';return $return;}
		if(!$getdata['negative_card']){$return['msg'] = '身份证反面必须选择';return $return;}
		if(!$getdata['handheld_card']){$return['msg'] = '手持身份证照片必须选择';return $return;}
		if(!$getdata['shop_id']){$return['msg'] = '门店必须选择';return $return;}
		if(!$getdata['content']){$return['msg'] = '简介必须填写';return $return;}
		if($getdata){
			if($getdata['id'] && $getdata['status'] == 1){
				$return['msg'] = '审核成功后不能再次编辑主播信息';
			}else{
				if($getdata['id'] && $getdata['status'] == 0){
					$ret = M("LiveAnchor")->save($getdata);
				}
				if($getdata['id'] && $getdata['status'] == 2){
					$getdata['status'] = 0;
					$ret = M("LiveAnchor")->save($getdata);
				}
				if(!$getdata['id'] && $getdata['user_id']){
					$ret = M("LiveAnchor")->add($getdata);
					
					// $c_map = array();
     //                $c_map['name']='room_number';
					// $room_config=M("LiveSet")->where($c_map)->getField('value');
                    // 
                    $room_config = getconfig('ROOM_NUMBER');
					$data = array();$data['roomnumber']=$room_config.$ret;
					$map = array();$map['id']=$ret;
					M("LiveAnchor")->where($map)->save($data);
				}
				if($ret){
					$return['status'] = 1;
					$return['msg'] = '保存成功';
				}else{
					$return['msg'] = '保存失败';
				}
			}
			return $return;
		}
	}

    //1126一小时之内是否发起过直播
    public function launch_lives($room_id,$memberinfo=''){
        $return = array('status'=>3,'msg'=>'参数错误');
        if(empty($room_id)) return $return;

        $where=$params=array();
        $model = M('LiveHistory');
        // $go_offline_time = idtoname('go_offline_time','LiveSet','value','name');
        $go_offline_time = getconfig('GO_OFFLINE_TIME');
        $where['begin_time'][] = array('elt',NOW_TIME);//主播掉线重连判断
        $where['begin_time'][] = array('egt',NOW_TIME-($go_offline_time*3600));//主播掉线重连判断
        $where['end_time'] = 0;
        $count = $model->where($where)->field(true)->count();



        if($count>0){
            $params['map'] = 1;
            $history = D('Chat')->runHistory('query','',$room_id,0,$params,$memberinfo);
            
        }
        if(!$history){
            $return['status'] = 1;
            $return['data']['is_reset'] = 1;
        }else{
            $return['status'] = 1;
            $return['data']['history'] = $history;
        }
        
        return $return;
    }

    /**添加主播分佣信息*/
    public function give_livehis($order_id){
        if(empty($order_id))return false;
        $map=array();
        $map['order_id']=$order_id;
        $map['live_anchor_id']=array('neq',0);
        $count=M('OrderGoods')->where($map)->count();
        if(!$count)return false;
        $order_goods=M('OrderGoods')->where($map)->field(true)->select();
        foreach ($order_goods as $key => $value) {
            if($value['live_anchor_id']){
                $this->guide_livehier($value);
            }
        }
    }
    /**添加主播分佣信息
     * @param array $data=array(
            'goods_id'=>商品id,
     *      'goods_number'=>商品数量,
     *      'anchor_id'=>主播会员id,
     *      'order_id'=>订单id,
     *      'order_goods_id'=>订单商品id,
     *      'order_status'=>订单状态,0未付款,1已付款已确认
     *      'divided_status'=>分佣状态
     *      'shop_price'=>商品价格
     *      'goods_attr'=>商品属性
     * )
     * @return array
     */
    public function guide_livehier($data){
        $return=array('status'=>1);
        $map=array();
        $map['anchor_id']=intval($data['live_anchor_id']);
        $map['goods_id']=$data['goods_id'];
        $map['order_goods_id']=$data['id'];
        $attr='';
        if($data['goods_attr']){
            $goods_attr=unserialize($data['goods_attr']);       
            $attr=$goods_attr['key'];
        }
        $map['goods_attr']=$attr;
        $count=M('LiveDivided')->where($map)->count();
        if($count){
            $return['status']=0;
            $return['msg']='已分佣';
            return $return;
        }                
        $anchor_status=C('ANCHOR_STATUS');
        $anchor_market=C('ANCHOR');
        if($anchor_status==1)$data['balance_money']=$data['goods_price']*$data['goods_number']*$anchor_market/100;
        if($anchor_status==2)$data['balance_money']=$data['profit']*$data['goods_number']*$anchor_market/100;

        $data['anchor_id']=intval($data['live_anchor_id']);
        $data['goods_attr']=$attr;
        $data['order_goods_id']=$data['id'];
        $data['update_time'] = NOW_TIME;
        $data['add_time'] = NOW_TIME;
        $data['divided_status']=1;
        $data['divided_time']=NOW_TIME;
        $res = M("LiveDivided")->add($data);
        if($res){
            $anchor_id=idtoname(intval($data['live_anchor_id']),'LiveAnchor','user_id');
            $anchor_name=idtoname($anchor_id,"Member",'username;nickname;mobile');
            account_change($anchor_id,$data['balance_money'],8,5,'主播'.$anchor_name.'推荐商品,得到分成');
            $return['status']=1;
            $return['msg']='添加成功';
            return $return;
        }else{
            $return['status']=0;
            $return['msg']='添加主播分佣统计信息失败';
            return $return;
        }
        return $return;
    }
    
    /**
     * 获取im信息
     * @param $memberinfo会员信息
     * @param $is_publish 是否是主播
     */
    public function getchatinit($memberinfo,$room_id='',$is_publish=0){
        $chat=array();
        //325新加
        $username =$memberinfo['id']<10?'hc0'.$memberinfo['id']:'hc'.$memberinfo['id'];$password='hanhcao';
        $chat['username'] = $username;            
        $chat['password'] = $password;
        $chat['group_id'] = '';
        if($memberinfo['nickname']) $chat['nickname'] = $memberinfo['nickname'];
        else $chat['nickname'] = $memberinfo['username'];
        
        $im= new \Common\Vendor\JimIM;
        $signature=$im->signature();//获取客户端调用参数
        $chat['signature'] = $signature;
        if(empty($memberinfo['register_tencent'])){
            $nickname=D("Chat")->getnickname($memberinfo);
            $res=$im->register($username,$password,$nickname);
            if($res){
                $map=$data=array();
                $map['id']=$memberinfo['id'];
                $data['register_tencent']=1;
                M('Member')->where($map)->save($data);                        
            }
        }
        $map=array();
        if($room_id)  $map['roomnumber']=$room_id;
        else $map['user_id']=$memberinfo['id'];
        $map['status']=1;
        $group_id=M("LiveAnchor")->where($map)->order('id desc')->getField('group_id');
        if(!empty($group_id)){
            $chat['group_id']=$group_id;
            if($is_publish==0){
                $members[] = $username;
                $room_ret=$im->addMembersRoom($group_id,$members);
            }
        }return $chat;
      //end
    }
    //1227
    /**
   * 腾讯云云通信房间全员广播
   * $groupid  房间群组id
   * $text  消息内容
   */
  public function live_webim_broadcast($groupid,$text){
    $im= new \Common\Vendor\Webim;
    $usersig=$im->getusersig(C('USERSIG'));//获取用户sig
    $im->set_user_sig($usersig['info']);//设置当前用户sig
    $ret = $im->send_group_system($groupid,$text);//广播
    return $ret;
  }
    //end
}