<?php

namespace Admin\Controller;

use Think\Controller;

/**
 * 投放控制器
 * @author 鹿文学
 */
class LaunchController extends ThinkController {
    
    const model_name = 'LaunchPlatform';
		
    const model_names = 'LaunchRecord';

    /**
     * 投放平台列表
		 * @author 鹿文学
     */
    public function platform(){
			
				$extend['status'] = 1;
				
				if(!empty($_REQUEST['name'])) {$extend['name']=array('like','%'.$_REQUEST['name'].'%');}
				if(!empty($_REQUEST['promote_id'])) {$extend['promote_id']=$_REQUEST['promote_id'];}
				if(is_numeric($_REQUEST['group']) && $_REQUEST['group']==1) {$extend['mark']=1;}else{$extend['mark']=0;}
				
				$this->meta_title = "投放平台列表";
        
				$this->m_title = '投放平台';
				$this->m_url = 'Launch/platform';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Merchandise/lists','status'=>1])->find());
				
				
        $data = D(self::model_name)->lists($_GET['p'],$extend);
				
				$this->assign('list_data',$data['data']);
				$this->assign('_page',$data['page']);
				
				$this->display();
				
    }
		
		/**
     * 添加投放平台
		 * @author 鹿文学
     */
		public function platform_add() {
			if(IS_POST) {
				
				$platform = D(self::model_name);
				
				if($platform->update($_POST)) {
					$this->ajaxReturn(['status'=>1,'info'=>'添加成功'],'json');
				} else {
					$this->ajaxReturn(['status'=>0,'info'=>$platform->getError()],'json');
				}
				
			} else {
				$this->ajaxReturn(['status'=>0,'info'=>'请求有误'],'json');
			}
		}
		
		/**
     * 修改投放平台
		 * @author 鹿文学
     */
		public function platform_edit() {
			if(IS_POST) {
				
				$platform = D(self::model_name);
				
				if($platform->update($_POST)) {
					$this->ajaxReturn(['status'=>1,'info'=>'编辑成功'],'json');
				} else {
					$this->ajaxReturn(['status'=>0,'info'=>$platform->getError()],'json');
				}
				
			} else {
				$this->ajaxReturn(['status'=>0,'info'=>'请求有误'],'json');
			}
		}
		
		/**
     * 删除投放平台
		 * @param array/integer  $ids  要删除的投放平台编号
		 * @author 鹿文学
     */
		public function platform_del($ids=null){
			
        $platform = D(self::model_name);
				
				if($platform->del()) {
					$this->success($platform->getError());
				} else {
					$this->error($platform->getError());
				}
				
    }
		
		/**
     * 投放数据列表
		 * @author 鹿文学
     */
		public function data($p=1) {
			
			$currentdate = mktime(0,0,0,date('m'),date('d'),date('Y'));
			
			$map = array();
			if(is_numeric($_REQUEST['game_id']) && $_REQUEST['game_id']>0) {$map['game_id']=$_REQUEST['game_id'];}
			if(is_numeric($_REQUEST['promote_id']) && $_REQUEST['promote_id']>0) {$map['promote_id']=$_REQUEST['promote_id'];}
			if(is_numeric($_REQUEST['platform_id']) && $_REQUEST['platform_id']>0) {$map['platform_id']=$_REQUEST['platform_id'];}
			
			$launchrecord = D(self::model_names);
			
			/* 根据时间判别是否含有今天来分开获取数据 */
			if(!empty($_REQUEST['timestart']) && !empty($_REQUEST['timeend'])) {
				$start = strtotime($_REQUEST['timestart']);
				$end = strtotime($_REQUEST['timeend']);
				if($start<=$end){
					if($end>=$currentdate) {
					
						$data = $launchrecord->lists($p,$map,$start,$currentdate+86399);
						
					} else {
						
						$data = $launchrecord->data($p,$map,array('duration'=>['between',[$start,$end+86399]]));

					}
				}
			} elseif(!empty($_REQUEST['timestart']) && empty($_REQUEST['timeend'])) {
				$start = strtotime($_REQUEST['timestart']);

				$data = $launchrecord->lists($p,$map,$start,$currentdate+86399);

			} elseif(empty($_REQUEST['timestart']) && !empty($_REQUEST['timeend'])) {
				$end = strtotime($_REQUEST['timeend']);
				if($end>=$currentdate) {
					
					$data = $launchrecord->lists($p,$map,'',$currentdate+86399);
					
				} else {
					
					$data = $launchrecord->data($p,$map,array('duration'=>['lt',$end+86399]));

				}
			} else {
				/* 默认获取截止到昨天的数据 */
				$data = $launchrecord->data($p,$map,array('duration'=>['lt',$currentdate]));
				
			}
			
			if($data){
				
				$current = $data['current'];
				$total = $data['total'];
				$current['duration'] = '当页汇总';
				$total['duration'] = '全部汇总';
				if($map['game_id']) {$total['game_id']=$current['game_id']=$map['game_id'];}
				if($map['platform_id']) {$total['platform_id']=$current['platform_id']=$map['platform_id'];}
				
				$this->assign('list_data',$data['data']);
				$this->assign('_page',$data['page']);
				$this->assign('current',$current);
				$this->assign('total',$total);
				
				$export = $data['data'];
				$export[] = $current;
				$export[] = $total;
				/* 用于导出数据 */
				file_put_contents(dirname(__FILE__).'/access_data_launch.txt',json_encode($export));
				
			}
			
			$this->meta_title = "投放数据列表";
			
			$this->m_title = '投放数据';
			$this->m_url = 'Launch/data';
			$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Merchandise/lists','status'=>1])->find());
			
			$this->display();
			
		}
		
		
}