<?php
namespace Sdk\Controller;
use Think\Controller;
use App\Logic\UserLogic;
use App\Model\AdvModel;
class NoticeController extends BaseController{
    
    // 系统公告弹窗
    // author yyh 2018.10.18
    public function today_notice(){
        $request = json_decode(base64_decode(file_get_contents("php://input")),true);
        $map['game_id'] = $request['game_id'];
				$time = time();
        $map['start_time'] = ['elt',$time];
        
				$map['_string'] = ' end_time >= '.$time.' or end_time = 0 ';
        $map['status'] = 1;
        $notice = M('Notice','tab_');
        $list = $notice
                ->field('title,content')
                ->where($map)
                ->order('level desc')
                ->select();
        $this->new_set_message(200, "success", $list);
    }
    //消息中心通知
    // author yyh 2018.10.18
    // type  1 公告  2分享平台币奖励  3分享积分奖励
    public function notice_list(){
        $request = json_decode(base64_decode(file_get_contents("php://input")),true);
        $map['game_id'] = $request['game_id'];
        $map['start_time'] = ['elt',time()];
        $time = time();
				$map['_string'] = ' end_time >= '.$time.' or end_time = 0 ';
				
				
				
        $map['status'] = 1;
        $notice = M('Notice','tab_');//type = 1
        $share = M('ShareRecord','tab_');//type = 2
        $pointrecord = M('PointRecord','tab_');//type = 3
        $page = intval($request['p']);
        $page = $page ? $page : 1; //默认显示第一页数据
        $row =  intval($request['row'])?:10;
        //广告数据
        $notice_list = $notice
                ->field('id as notice_id,title,start_time as create_time,1 as type')
                ->where($map)
                ->order('level desc')
                ->select();
								
				foreach($notice_list as $key => $value) {
					$notice_list[$key]['read'] = 1;
				}				
					
        //分享邀请好友平台币奖励数据
        $share_data = $share
                     ->field('id as notice_id,user_account,award_coin,game_id,create_time,read')
                     ->where(['invite_id'=>$request['user_id'],'award_coin'=>['gt',0]])
                     ->select();
        $share_list = [];
        foreach ($share_data as $key => $value) {
            $share_list[$key]['title'] = '恭喜您成功获得'.$value['award_coin'].'个平台币奖励!';
            $share_list[$key]['create_time'] = $value['create_time'];
            $share_list[$key]['notice_id'] = $value['notice_id'];
            $share_list[$key]['type'] = 2;
            $share_list[$key]['read'] = $value['read'];
        }
        //分享邀请好友积分奖励数据
        $precord_data = $pointrecord
                        ->field('id as notice_id,point,be_invited_id,invited_game_id,create_time,read')
                        ->where(['type_id'=>6,'user_id'=>$request['user_id'],'point'=>['gt',0]])
                        ->select();
        foreach ($precord_data as $key => $value) {
            $precord_list[$key]['title'] = '恭喜您成功获得'.$value['point'].'个积分奖励!';
            $precord_list[$key]['create_time'] = $value['create_time'];
            $precord_list[$key]['notice_id'] = $value['notice_id'];
            $precord_list[$key]['type'] = 3;
            $precord_list[$key]['read'] = $value['read'];
        }

        $new_list[0] = $notice_list;
        $new_list[1] = $share_list;
        $new_list[2] = $precord_list;
				
        foreach ($new_list as $key => $value) {
            if(empty($value)){
                unset($new_list[$key]);
            }
        }
        $new_list_new = array_values($new_list);
        foreach ($new_list_new as $key => $value) {
            foreach ($value as $k => $v) {
                $new_arr[] = $v;
            }
        }
				
				
				
        $new_data_arr = my_sort($new_arr,'create_time',SORT_DESC);
				
				$count = count($new_data_arr);
				
				if($count<=$row){
            if($page==1) {
            $new_data = $new_data_arr;						
					} else {
						$new_data = [];
					}
        }else{
            $size=$row;//每页显示的记录数
            $pnum = ceil($count / $size); //总页数,ceil()函数用于求大于数字的最小整数
            //用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度)
            $new_data = array_slice($new_data_arr, ($page-1)*$size, $size);
        }
        
				
				$this->new_set_message(200, "success", $new_data?$new_data:[]);
    }
    //通知详情
    //yyh 2018.10.18
    public function notice_detail(){
        $request = json_decode(base64_decode(file_get_contents("php://input")),true);
				
        $map['id'] = $request['notice_id'];
        switch ($request['type']) {
            case '1':
                $model = M('Notice','tab_');
                $field = 'title,content,start_time as create_time';
                break;
            
            case '2':
                $model = M('ShareRecord','tab_');
                $map['invite_id'] = $request['user_id'];
                $field = 'user_account,award_coin,game_id,create_time';
								$model->where($map)->setField('read',1);
								$share_count= $model->where(['invite_id'=>$request['user_id'],'award_coin'=>['gt',0],'read'=>0])->count();
                break;

            case '3':
                $model = M('PointRecord','tab_');
                $map['user_id'] = $request['user_id'];
                $field = 'point,be_invited_id,invited_game_id,create_time';
								$model->where($map)->setField('read',1);
								$point_count= $model->where(['type_id'=>6,'user_id'=>$request['user_id'],'point'=>['gt',0],'read'=>0])->count();
                break;
        }
        $detail = $model
                    ->field($field)
                    ->where($map)
                    ->find();
        switch ($request['type']) {
            case '1':
                $data['title'] = $detail['title'];
                $data['content'] = $detail['content'];
                $data['create_time'] = $detail['create_time'];
                $data['type'] = $request['type'];
                break;
            case '2':
								$game_name = get_game_name($detail['game_id']);
                $data['title'] = '恭喜您成功获得'.$detail['award_coin'].'个平台币奖励!';
                $data['content'] .= '奖励平台币:'.$detail['award_coin'];
                if($detail['game_id']>0){
                    $data['content'] .= '
分享游戏:'.get_game_name($detail['game_id']);
                }
                $data['content'] .= '
邀请好友账号:'.$detail['user_account'];
                $data['content'] .= '
奖励时间:'.date('Y-m-d H:i:s',$detail['create_time']);
                $data['create_time'] = $detail['create_time'];
                $data['type'] = $request['type'];
                $data['award_coin'] = $detail['award_coin']>0?$detail['award_coin']:0;
                $data['game_name'] = !empty($game_name)?$game_name:'';
                $data['user_account'] = !empty($detail['user_account'])?$detail['user_account']:'';
								$data['name']='平台币奖励';
								$data['unread_count'] = $share_count;
                break;

            case '3':
								$game_name = get_game_name($detail['invited_game_id']);
								$user_account=get_user_entity($detail['be_invited_id'])['account'];
                $data['title'] = '恭喜您成功获得'.$detail['point'].'个积分奖励!';
                $data['content'] .= '奖励积分:'.$detail['point'];
                if($detail['invited_game_id']>0){
                    $data['content'] .= '
分享游戏:'.get_game_name($detail['invited_game_id']);
                }
                $data['content'] .= '
邀请好友账号:'.get_user_entity($detail['be_invited_id'])['account'];
                $data['content'] .= '
奖励时间:'.date('Y-m-d H:i:s',$detail['create_time']);
                $data['create_time'] = $detail['create_time'];
                $data['type'] = $request['type'];
                $data['award_coin'] = $detail['point']>0?$detail['point']:0;
								$data['game_name'] = !empty($game_name)?$game_name:'';
                $data['user_account'] = !empty($user_account)?$user_account:'';
								$data['name'] = '积分奖励';
								$data['unread_count'] = $point_count;
                break;
        }
						
        $this->new_set_message(200, "success", $data);
    }

    public function boon_list(){
        $request = json_decode(base64_decode(file_get_contents("php://input")),true);
        $map['start_time'] = ['elt',time()];
        $map['end_time'] = [['egt',time()],['eq',0],'or'];
        $page = intval($request['p']);
        $user_id = $request['user_id'] ? $request['user_id'] : 0; 
        $page = $page ? $page : 1; //默认显示第一页数据
        $row =  intval($request['row'])?:10;
        $list = M('Boon','tab_')
                ->field('tab_boon.id,title,start_time,end_time,show,reward,way,cover,material_url,tab_boon_watched.id as iswatched')
                ->join('tab_boon_watched on tab_boon_watched.boon_id = tab_boon.id and user_id='.$user_id,'left')
                ->where($map)
                ->order('tab_boon.sort desc,tab_boon.id desc')
                ->page($page,$row)
                ->select();
								
        foreach ($list as $key => $value) {
            $list[$key]['cover'] = new_icon_url($value['cover']); 
            if($value['material_url']!=''){
                $list[$key]['material_url'] = str_replace('./','http://'.$_SERVER['HTTP_HOST'].'/',$value['material_url']); 
            }
            if($value['iswatched']>0){
                $list[$key]['iswatched'] = 1;
            }else{
                $list[$key]['iswatched'] = 0;
            }
        }
				
				$pointtype = M('PointType','tab_')->field('id,point,time_of_day')->find(8);
				
        $data = array(
            "list"=>$list,
						'boon_time'=>$pointtype['time_of_day']?$pointtype['time_of_day']:0,
        );
        $this->new_set_message(200, 'success', $data);
    }
    //观看广告福利
    //author yyh 2018 10 23
    public function watch_boon_reward(){
        $request = json_decode(base64_decode(file_get_contents("php://input")),true);
				
        $user_id = $request['user_id'];
        $boon_id = $request['boon_id'];
        $user_data = M('User','tab_')
                        ->field('id as user_id,account as user_account')
                        ->where(['user_id'=>$user_id])
                        ->find();
        if(empty($user_data)){
            $this->new_set_message(1004, '账号不存在', []);
        }
        $boonmap['start_time'] = ['elt',time()];
        $boonmap['end_time'] = [['egt',time()],['eq',0],'or'];
        $boonmap['id'] = $boon_id;
				
        $boon_data = M('Boon','tab_')->field('id,reward')->where($boonmap)->find();
				
				
        if(empty($boon_data)){
            $this->new_set_message(1128, '没有符合条件的广告', []);
        }

        $record_data = M('PointRecord as pr','tab_')
                        ->field('pr.id,time_of_day')
                        ->where(['type_id'=>8,'user_id'=>$user_id,'day'=>$boon_id])
                        ->join('tab_point_type pt on pt.id = pr.type_id')
                        ->find();
        $point_type_data = M('PointType','tab_')->field('id,time_of_day')->find(8);
				
				
        if(!empty($record_data)){
            $this->new_set_message(1128, '该广告已奖励过', []);
        }
        if(empty($point_type_data)){
            $this->new_set_message(1130, '该广告没有积分', []);
        }
        $watched_data = M('BoonWatched','tab_')->field('id')->where(['boon_id'=>$boon_id,'user_id'=>$user_id])->find();
        if(empty($watched_data)){
            $w['boon_id'] = $boon_id;
            $w['user_id'] = $user_id;
            $w['game_id'] = $request['game_id'];
            $w['create_time'] = time();
            $wres = M('BoonWatched','tab_')->add($w);
        }
        $model = M('PointRecord','tab_');
        $map["FROM_UNIXTIME(register_time,'%Y-%m-%d')"] = $time;
        $record_data_count = $model->where(["FROM_UNIXTIME(create_time,'%Y-%m-%d')"=>date('Y-m-d',time()),'user_id'=>$user_id,'type_id'=>8])->count();
        if($record_data_count<$point_type_data['time_of_day']){											
				
				$day = $record_data_count - $point_type_data['time_of_day']+1;	

				
            $model->startTrans();
            $user_logic = new UserLogic();
            $user_result = $user_logic->operationPoint($user_id,$boon_data['reward'],1);
            $d['type_id'] = 8;
            $d['user_id'] = $user_id;
            $d['point'] = $boon_data['reward'];
            $d['create_time'] = time();
            $d['type'] = 1;
            $d['day'] = $boon_id;
            $record_result = $model->add($d);
            if($user_result !== false && $record_result !== false){
                $model->commit();																
								
								if($day < 1) {$day = abs($day);$return = '恭喜您成功获得'.$boon_data['reward'].'积分奖励,'.($day>0?'今日还有'.$day.'次机会哦':'今日奖励机会已用完');}else{$return='谢谢观看';}
								
								
                $this->new_set_message(200, $return, []);
            }else{
                $model->rollback();
                $this->new_set_message(0, '奖励失败', []);
            }
        }else{
            $this->new_set_message(1129, '今日广告福利奖励次数已使用完', []);
        }
    }
    //退出sdk弹出广告
    //yyh 
    public function sdk_logout_adv(){
        $model = new AdvModel();
        $data = $model->getAdv("loginout_sdk",1);
        $data = $data[0];
        if(empty($data)){
            $this->new_set_message(1033,"暂无数据",[]);
        }else{
            $this->new_set_message(200,"成功",$data);
        }
    }
    //猜你喜欢
    //yyh
    public function guess_like(){
        $request = json_decode(base64_decode(file_get_contents("php://input")),true);
        $page = intval($request['p']);
        $page = $page ? $page : 1; //默认显示第一页数据
        $row =  intval($request['row'])?:10;

        $map['start_time'] = ['elt',time()];
        $map['end_time'] = [['egt',time()],['eq',0],'or'];
        $map['status'] = 1;
        $list = M('Guess','tab_')->field('title,url,icon')->where($map)->order('sort desc')->page($page,$row)->select();
        foreach ($list as $key => $value) {
            $list[$key]['icon'] = new_icon_url($value['icon']);
        }
        $data = array(
            "status"=>200,
            "list"=>$list,
        );
        $this->new_set_message(200,"成功",$list);
    }
    //获取客服问题  帮助中心
    //yyh 2018 10 26
    public function get_customer_question(){
        $map1['status'] = 1;
        $map1['istitle'] = 1;
        $first = M('Kefuquestion')->field('id,title as first_title,titleurl as mark')->where($map1)->order('sort desc')->group('title')->select();
        $map2['status'] = 1;
        $map2['istitle'] = 2;
        $second = M('Kefuquestion')->field('zititle as second_title,titleurl as mark')->where($map2)->order('sort desc')->select();
        foreach ($first as $key => $value) {
            $first[$key]['second_title'] = [];
            foreach ($second as $k => $v) {
                if($value['mark']==$v['mark']){
                    $first[$key]['second_title'][] = $v['second_title'];
                }
            }
						if(empty($first[$key]['second_title'])) {
							unset($first[$key]);
						}
        }
        $this->new_set_message(200,"成功",array_merge($first));
    }
    //帮助中心客服
    //yyh 2018 10 26
    public function customer_contact(){
        $data['APP_QQ'] = C('APP_QQ');
        $data['APP_TEL'] = C('APP_TEL');
        $data['QQ_GROUP_KEY'] = C('QQ_GROUP_KEY');
        $data['APP_QQ_GROUP'] = C('APP_QQ_GROUP');
        $data['APP_EMAIL'] = C('APP_EMAIL');
        $this->new_set_message(200,"成功",$data);
    }
    //获取客服问题  帮助中心
    //yyh 2018 10 26
    public function customer_question_list(){
        $request = json_decode(base64_decode(file_get_contents("php://input")),true);
        $map2['status'] = 1;
        $map2['istitle'] = 2;
        $map2['titleurl'] = $request['mark'];
        $data = M('Kefuquestion')->field('zititle as title,contend')->where($map2)->select();
        $this->new_set_message(200,"成功",$data);
    }
}