<?php
namespace Cas\Controller;
use KIF\Core\Request;
use Cas\Dao\LotteryEvents;
use Cas\Module\Lottery;
use KIF\Dao\SqlHelper;
use Cas\Dao\UserInfo;
use Cas\Module\LotteryLog;
use Cas\Dao\LotteryData;
use Cas\Module\User;
use Cas\Dao\LotteryUserExpress;
use Cas\Dao\Category;
use Cas\Dao\ArticleLikeCounter;
/**
 * 活动列表页 - 首页
 * lihuanchun
 */
class Index extends EventsController {
	

	public function doSetUser() {
		$objUserData = new User();
		$objUserData->setAvatar();

		echo 'yes';
	}
	/**
	 * 页面:默认新版首页
	 */
	public function doDefault() {
		
		# 检查是否移动端
		$title = '精彩活动';
		$this->checkIsFromMobile($title);
		
		$plat_form_id = $this->getPlatFormId();
		
		$thisTime = time();
		$this->objMLottery = new Lottery();

		# 获取登录地址
		$url = Request::g('url');
		$thisUrl = Request::schemeDomain().'/list/'.$url;
		$loginUrl = $this -> getLoginUrl($thisUrl);
		$this->setOutput('loginUrl', $loginUrl);
		$this->setOutput('url', $url);
		
		# 判断是否需要登录
		$ckLogin = $this->ckLogin();
		$this->setOutput('ckLogin', $ckLogin);
		
		# 获取用户信息
		$objUserData = new UserInfo();
		$uid = $this->getRunTimeUid();
		$userData = $objUserData -> get($uid);
		$this->setOutput('userData', $userData);

		# 获取焦点图
		$order = 'begin_time desc';
		$focusCondition = array(
			'display' => LotteryEvents::EVENT_DISPLAY_SHOW, // 已发布的
			'focus_display' => LotteryEvents::FOCUS_DISPLAY_SHOW,//焦点图
			'begin_time'	=> SqlHelper::addCompareOperator('<=', time()),
			'platform_ids' => SqlHelper::addCompareOperator('&', $plat_form_id),
		);
		$focusData =  $this->objMLottery -> getLotteryEventsList($focusCondition, "0,5", 'id desc');
		$this->setOutput('focusData', $focusData);

		# 热门活动
		$condition = array(
			'display' => LotteryEvents::EVENT_DISPLAY_SHOW, // 已发布的
			'list_display' => LotteryEvents::LIST_DISPLAY_SHOW, // 列表中是否显示
			'top_display' => LotteryEvents::TOP_DISPLAY_SHOW, // 是否置顶
			'begin_time'	=> SqlHelper::addCompareOperator('<=', time()),
			'platform_ids' => SqlHelper::addCompareOperator('&', $plat_form_id),
		);

		$totals = $this->objMLottery -> getLotteryEventsListNum($condition);
		$offset = 0;
		$size = 100;
		# 开始时间排序
		$order = 'begin_time desc';
		# 热门活动
		$eventsData =  $this->objMLottery -> getLotteryEventsList($condition, "{$offset},{$size}", $order);

		$TIME = time();
		$objArticleLikeCounter = new ArticleLikeCounter();
		foreach ($eventsData as $tmpKey => $tmpData) {
			# 剩余天数
			$rday = ($tmpData['end_time'] - $TIME) / (24 * 60 * 60);
			$eventsData[$tmpKey]['rday'] = ceil($rday);
			# 参与人数
			$objMLotteryLog = new LotteryLog ( null, $tmpData['id'] );
			$eventsData[$tmpKey]['user_total'] = $objMLotteryLog->getUserParticipateLogNumGroupByUid ();

			if ($tmpData['type'] == 9) {
				# 阅读人数
				$eventsData[$tmpKey]['click_num'] = $tmpData['click_num'] + ($tmpData['article_pass_base_num']?$tmpData['click_num'] + $tmpData['article_pass_base_num']:0);
				# 点赞人数
				$eventsData[$tmpKey]['likes'] = $objArticleLikeCounter->getLikes($tmpData['id']);
				$eventsData[$tmpKey]['likes'] = $eventsData[$tmpKey]['likes'] + $tmpData['article_praise'];
			}
		}

		# 获得福利
		$this->prize($eventsData, $plat_form_id, 5);

		# 获取分类
		$this->objDCategory = new Category ();
		$order = 'id desc';
		$condition = array ();
		$ids = $this->objDCategory->findIdsBy ( $condition, '0,50', $order );
		$categoryData = $this->objDCategory->gets ( $ids );
		$this->setOutput('categoryData', $categoryData);

		$this->setOutput('domain', Request::schemeDomain ());
		$this->setOutput('eventsData', $eventsData);
		$this->setOutput('title', '精彩活动');
		$this->setOutput('totals', ceil($totals / $size));
		$this->tpl = 'index';
		$this->setOutput('plat_form_id', $plat_form_id);
		$this->setOutput('display', 1);
		$this->setOutput('url', $url);
	}

	/**
	 * 页面:默认首页
	 *
	public function doOldDefault() {
		
		# 检查是否移动端
		$title = '精彩活动';
		$this->checkIsFromMobile($title);
		
		$plat_form_id = $this->getPlatFormId();
		
		$thisTime = time();
		$objMLottery = new Lottery();
		$condition = array(
			'display' => LotteryEvents::EVENT_DISPLAY_SHOW, // 已发布的
			'list_display' => LotteryEvents::LIST_DISPLAY_SHOW, // 列表中是否显示
			'begin_time'	=> SqlHelper::addCompareOperator('<=', time()),
			'platform_ids' => SqlHelper::addCompareOperator('&', $plat_form_id),
		);
		
		
		# 获取登录地址
		$url = Request::g('url');
		$thisUrl = Request::schemeDomain().'/list/'.$url;
		$loginUrl = $this -> getLoginUrl($thisUrl);
		$this->setOutput('loginUrl', $loginUrl);
		
		# 判断是否需要登录
		$ckLogin = $this->ckLogin();
		$this->setOutput('ckLogin', $ckLogin);
		
		# 获取用户信息
		$objUserData = new UserInfo();
		$uid = $this->getRunTimeUid();
		$userData = $objUserData -> get($uid);
		$this->setOutput('userData', $userData);
		
		
		
		$totals = $objMLottery -> getLotteryEventsListNum($condition);
		$offset = 0;
		$size = 6;
		$order = 'id desc';
		$eventsData = $allLotteryEventsIds =  $objMLottery -> getLotteryEventsList($condition, "{$offset},{$size}", $order);
		
		# 剩余天数
		$TIME = time();
		foreach ($eventsData as $tmpKey => $tmpData) {
			$rday = ($tmpData['end_time'] - $TIME) / (24 * 60 * 60);
			$eventsData[$tmpKey]['rday'] = ceil($rday);
		}
		
		$this->setOutput('eventsData', $eventsData);
		$this->setOutput('title', '精彩活动');
		$this->setOutput('totals', ceil($totals / $size));
		$this->tpl = 'index';
		$this->setOutput('plat_form_id', $plat_form_id);
		$this->setOutput('url', $url);
	}
	
	/**
	 * 事件:ajax翻页
	 */
	public function doAjax() {
		$page = Request::varGetInt('page', 1);
		$thisTime = time();
		$objMLottery = new Lottery();
		$plat_form_id = Request::g('plat_form_id');
		$condition = array(
			'display' => LotteryEvents::EVENT_DISPLAY_SHOW, // 已发布的
			'list_display' => LotteryEvents::LIST_DISPLAY_SHOW, // 列表中是否显示
			'top_display' => LotteryEvents::TOP_DISPLAY_SHOW, // 是否置顶
			'begin_time'	=> SqlHelper::addCompareOperator('<=', time()),
			'platform_ids' => SqlHelper::addCompareOperator('&', $plat_form_id),
		);
		$totals = $objMLottery -> getLotteryEventsListNum($condition);
		$order = 'begin_time desc';
		$size = 10;
		$offset = ($page - 1) * $size;
		$limit = "{$offset},{$size}";
		$eventsData = $allLotteryEventsIds =  $objMLottery -> getLotteryEventsList($condition,$limit,$order);
		
		# 剩余天数
		$TIME = time();
		foreach ($eventsData as $tmpKey => $tmpData) {
			$rday = ($tmpData['end_time'] - $TIME) / (24 * 60 * 60);
			$eventsData[$tmpKey]['rday'] = ceil($rday);
		}
		
		$this->tpl = 'ajax_index';
		$data = array_fill(0, 4, 1);
		$this->setOutput('eventsData', $eventsData);
		$this->ajax_success_exit($this->render(true));
	}
	
	public function display() {
		$this->setOutput('action', 'index');
		return $this->render();
	}
}