<?php

namespace Cas\Controller;

use Cas\Module\Lottery;
use Cas\Dao\LotteryUserHelp;
use Cas\Dao\LotteryUserHelpLog;
use KIF\Verify;
use KIF\Core\Request;


class UserHelp  extends EventsController {

	public function doTest() {

		$this->LotteryUserHelp = new LotteryUserHelp ();

		$condition = array();
		$helpData = $this->objLotteryUserHelp->fetchAll ($condition, 1000000, 'id desc');

		foreach ($helpData as $k => $v) {
			$this->LotteryUserHelp->modify(array('nums' => $v['num']), array('id' => $v['id']));
		}

		echo 'yes';die;
	}
	public function doApply() {
		
		# 1.必要参数验证
		$uid = $this->getRunTimeUid();
		if (!Verify::unsignedInt($uid)) {
			self::ajax_fail_exit('无效用户id');
		}

		$help_id = Request::g('help_id');
		if (!Verify::unsignedInt($help_id)) {
			self::ajax_fail_exit('无效试用id');
		}

		$source_user = Request::g('source_user');
		if (!Verify::unsignedInt($source_user)) {
			self::ajax_fail_exit('无效用户id');
		}

		if ($uid == $source_user) {
			self::ajax_fail_exit('不能给自己助力');
		}

		$help_session = Request::g('help_session');
		$state = $this->shareSession($help_id, $source_user, $help_session);
		if (!$state) {
			self::ajax_fail_exit('无效试用id');
		}
		
		$events_id = Request::g('events_id');
		if (!Verify::unsignedInt($events_id)) {
			self::ajax_fail_exit('无效试用id');
		}
		
		$this->LotteryUserHelpLog = new LotteryUserHelpLog ();

		$this->LotteryUserHelp = new LotteryUserHelp ();

		$totals = $this->LotteryUserHelpLog->totals(array('help_id' => $help_id));
		$this->LotteryUserHelp->modify(array('num' => $totals), array('id' => $help_id));

		$condition = array
		(
			'help_id' => $help_id,
			'events_id' => $events_id,
			'uid' => $uid,
		);
		$info = $this->LotteryUserHelpLog->fetchOne ( $condition);
		if ($info) {
			self::ajax_fail_exit('您只能助力一次');
			# modify
			$this->LotteryUserHelpLog->modify($condition, array('id' => $info['id']));
		} else {
			# add
			$id = $this->LotteryUserHelpLog->add($condition);
		}


		
		self::ajax_success_exit();
	}

	public function display() {
		return $this->render ();
	}
}