<?php
defined('G_IN_SYSTEM')or exit('No permission resources.');
System::load_app_fun('my','go');
System::load_app_fun('user','go');
System::load_sys_fun("user");

class lottery extends SystemAction {

	private $db;
	private $userinfo;
	private $prize = array();

	public function __construct(){

		$this->db = System::load_sys_class("model");
		$this->userinfo = System::load_app_class('base','member')->get_user_info();
	}

	public function init() {
		$LotteryList = $this->db->Getlist("SELECT * FROM `@#_activity_lottery` ORDER BY id DESC LIMIT 100");
		include templates(ROUTE_M, "lottery_index");
	}

	public function submit() {
		$z1 =$this->db->GetOne("SELECT * from `@#_zhuanpan` where `id` = '1'");
		$z2 =$this->db->GetOne("SELECT * from `@#_zhuanpan` where `id` = '2'");
		$z3 =$this->db->GetOne("SELECT * from `@#_zhuanpan` where `id` = '3'");
		$z4 =$this->db->GetOne("SELECT * from `@#_zhuanpan` where `id` = '4'");
		$z5 =$this->db->GetOne("SELECT * from `@#_zhuanpan` where `id` = '5'");
		$z6 =$this->db->GetOne("SELECT * from `@#_zhuanpan` where `id` = '6'");
		$z7 =$this->db->GetOne("SELECT * from `@#_zhuanpan` where `id` = '7'");
		
		#                      奖项     说明     红包价格   概率
		$this->prize[] = array('七等奖', $z1['money'].'元红包', $z1['money'],  array($z1['jilv'],100) );
		$this->prize[] = array('六等奖', $z2['money'].'元红包', $z2['money'], array($z2['jilv'],100) );
		$this->prize[] = array('五等奖', $z3['money'].'元红包', $z3['money'], array($z3['jilv'],100) );
		$this->prize[] = array('四等奖', $z4['money'].'元红包', $z4['money'], array($z4['jilv'],100) );
		$this->prize[] = array('三等奖', $z5['money'].'元红包', $z5['money'], array($z5['jilv'],100) );
		$this->prize[] = array('二等奖', $z6['money'].'元红包', $z6['money'], array($z6['jilv'],100) );
		$this->prize[] = array('一等奖', $z7['money'].'元红包', $z7['money'], array($z7['jilv'],100) );

		function _return($ok, $desc, $round = 0, $left=0) {
			$data = array();
			$data['ok'] = $ok;
			$data['desc'] = $desc;
			$data['round'] = $round;
			$data['left'] = $left;
			echo json_encode($data);
			die;
		}
		$user_fu =$this->db->GetOne("SELECT * from `@#_zp_fufen`");
		$fufen_code = $user_fu['z_fufen'];
		if ( !$this->userinfo ) {
			_return(false,'您还没有登陆，无法参与抽奖哦');
		} else if ( $this->userinfo['score'] <= $fufen_code ) {
			_return(false,'抱歉，您的抽奖次数用完了。');
		}

		$p = $this->probability();
		if ( $p == -1 ) {
			_return(true, '哎呀，姿势不对吧，竟然没中奖！', -1);
		} else {
			list($title,$desc,$money) = $this->prize[$p];
			$round = $this->round($p);
			$left = $this->userinfo['score'] -$fufen_code;

			$this->db->Query("UPDATE `@#_member` SET score = score - $fufen_code,`money` = `money` + $money where (`uid` = '".$this->userinfo['uid']."')");
			$this->db->Query("INSERT INTO `@#_activity_lottery`(uid,prize,money,time,title,`desc`)VALUES('".$this->userinfo['uid']."','$p','$money','".time()."','{$title}','{$desc}')");
			$this->db->Query("INSERT INTO `@#_member_account` (`uid`, `type`, `pay`, `content`, `money`, `time`) VALUES ('".$this->userinfo['uid']."', '1', '账户', '大转盘抽奖[{$title}]红包', '$money', '".time()."')");

			_return(true, '恭喜您抽中['.$title.']'.$desc."\n红包已自动充值到您的余额内了。", $round, $left);
		}

	}

	# 获取范围
	private function round($p) {
		$width = 360 / 7;
		$a = $p * $width;
		$b = $a + $width;
		return mt_rand($a + 10 ,$b - 10);
	}

	# 随机一个概率出来
	private function probability() {
		//return rand(0,6);
		$probability_all = array(0,0);
		foreach($this->prize as $i=>$val) {
			list($title,$desc,$money,$probability) = $val;
			$probability_all[0] += $probability[0];
			$probability_all[1] += $probability[1];
		}
		if ( empty($this->prize) ) {
			return -1;
		}


		$probability_all[1] = intval($probability_all[1] / count($this->prize));

		$yes = mt_rand(1,$probability_all[1]);

		$prize = -1;
		if ( $probability_all[0] <= 0 || $probability_all[1] <= 0 || $yes > $probability_all[0] ) {
		} else {
			$list = array();
			$add = 0;
			$total = 0;
			foreach($this->prize as $i=>$val) {
				list($title,$desc,$money,$probability) = $val;
				if ( $probability[0] <= 0 ) {
					continue;
				}
				$total = $add += $probability[0];
				$list[$add] = $i;
			}

			$yes = mt_rand(1,$total);
			foreach ($list as $k => $v) {
				if ( $yes <= $k ) {
					$prize = $v;
					break;
				}
			}
		}
		return $prize;
	}
}
