<?php
class getshop extends SystemAction {
	public function getItemInfo() {
		$item_id = $this -> segment(4);
		if (empty($item_id)) {
			echo json_encode(array("errorCode" => '1'));
			exit ;
		}
		$db = System::load_sys_class('model');
		$info = $db -> GetOne("select q_uid,zongrenshu,q_user_code from `@#_shoplist` where `id` ='$item_id' and `q_showtime` = 'Y'");
		if (empty($info)) {
			echo json_encode(array("errorCode" => '1'));
			exit ;
		}
		System::load_sys_fun("user");
		$user = get_user_name($info['q_uid'], "username");
		$info['user'] = $user;
		$info['errorCode'] = 0;
		echo json_encode($info);
		exit ;
	}

	public function getList() {
		$db = System::load_sys_class('model');
		$info = $db -> GetList("select qishu,xsjx_time,id,thumb,title,q_uid,q_user,q_end_time from `@#_shoplist` where `q_showtime` = 'Y' order by `q_end_time` ASC");
		if (empty($info)) {
			echo json_encode(array("errorCode" => '1'));
			exit ;
		}
		foreach ($info as $k => $v) {
			$tmp = substr($v['q_end_time'], 0, 10) - time();
			$time_arr[] = $tmp;
			$info[$k]['seconds'] = $tmp;
		}
		if ($this -> segment(4) > 0 && $this -> segment(4) < max($time_arr)) {
			echo json_encode(array("errorCode" => '1'));
			exit ;
		}
		$arr = array("errorCode" => 0, "maxSeconds" => max($time_arr), "listItems" => $info);
		echo json_encode($arr);
		die ;
	}

	public function lottery_shop_json() {
		$this -> check_run();
		$db = System::load_sys_class('model');
		if (!isset($_GET['gid'])) {
			echo json_encode(array("error" => '1'));
			exit ;
		}
		$gid = trim($_GET['gid']);
		$times = (int)System::load_sys_config('system', 'goods_end_time');
		if (!$times) {$times = 1;
		}
		$gid = safe_replace($gid);
		$gid = str_ireplace("select", "", $gid);
		$gid = str_ireplace("union", "", $gid);
		$gid = str_ireplace("'", "", $gid);
		$gid = str_ireplace("%27", "", $gid);
		$gid = trim($gid, ',');
		if (!$gid) {
			$info = $db -> GetOne("select * from `@#_shoplist` where `q_showtime` = 'Y' order by `q_end_time` ASC");
		} else {
			$infos = $db -> GetList("select * from `@#_shoplist` where `q_showtime` = 'Y' order by `q_end_time` ASC limit 0,4");
			$gid = @explode('_', $gid);
			$info = false;
			foreach ($infos as $infov) {
				if (!in_array($infov['id'], $gid)) {
					$info = $infov;
					break;
				}
			}
		}
		if (!$info) {
			echo json_encode(array("error" => '1'));
			exit ;
		}
		if ($info['xsjx_time']) {$info['q_end_time'] = $info['q_end_time'] + $times;
		}
		System::load_sys_fun("user");
		$user = unserialize($info['q_user']);
		$user = get_user_name($info['q_uid'], "username");
		$uid = $info['q_uid'];
		$upload = G_UPLOAD_PATH;
		$q_time = substr($info['q_end_time'], 0, 10);
		if ($q_time <= time()) {
			$db -> Query("update `@#_shoplist` SET `q_showtime` = 'N' where `id` = '$info[id]' and `q_showtime` = 'Y' and `q_uid` is not null");
			echo json_encode(array("error" => '-1'));
			exit ;
		}
		$user_shop_number = $db -> GetOne("select sum(gonumber) as gonumber from `@#_member_go_record` where `uid`= '$uid' and `shopid` = '$info[id]' and `shopqishu` = '$info[qishu]'");
		$user_shop_number = $user_shop_number['gonumber'];
		$times = $q_time - time();
		echo json_encode(array("error" => "0", "user_shop_number" => "$user_shop_number", "user" => "$user", "zongrenshu" => $info['zongrenshu'], "q_user_code" => $info['q_user_code'], "qishu" => $info['qishu'], "upload" => $upload, "thumb" => $info['thumb'], "id" => $info['id'], "uid" => "$uid", "title" => $info['title'], "user" => $user, "times" => $times));
		exit ;
	}

	public function lottery_shop_get() {
		if (isset($_POST['lottery_shop_get'])) {
			$db = System::load_sys_class('model');
			$times = (int)System::load_sys_config('system', 'goods_end_time');
			$gid = isset($_POST['gid']) ? abs(intval($_POST['gid'])) : exit();
			$info = $db -> GetOne("select id,xsjx_time,q_end_time from `@#_shoplist` where `id` ='$gid' and `q_showtime` = 'Y'");
			if (!$info) {
				echo "no";
				exit ;
			}
			if ($info['xsjx_time']) {$info['q_end_time'] = $info['q_end_time'] + $times;
			}
			$q_time = substr($info['q_end_time'], 0, 10);
			if ($q_time <= time()) {
				$db -> Query("update `@#_shoplist` SET `q_showtime` = 'N' where `id` = '$info[id]' and `q_showtime` = 'Y' and `q_uid` is not null");
			}
			echo $q_time - time();
			exit ;
		}
	}

	public function lottery_shop_set() {
		if (isset($_POST['lottery_sub'])) {
			$db = System::load_sys_class('model');
			$times = (int)System::load_sys_config('system', 'goods_end_time');
			$gid = isset($_POST['gid']) ? abs(intval($_POST['gid'])) : exit();
			$info = $db -> GetOne("select id,xsjx_time,thumb,title,q_uid,q_user,q_end_time from `@#_shoplist` where `id` ='$gid'");
			if (!$info || empty($info['q_end_time'])) {
				echo '0';
				exit ;
			}
			if ($info['xsjx_time']) {$info['q_end_time'] = $info['q_end_time'] + $times;
			}
			$times = str_ireplace(".", "", $info['q_end_time']);
			$q_time = substr($info['q_end_time'], 0, 10);
			$q = false;
			if (time() >= $q_time) {
				$q = $db -> Query("update `@#_shoplist` SET `q_showtime` = 'N' where `id` = '$gid' and `q_showtime` = 'Y' and `q_uid` is not null");
			}
			if ($q)
				echo '1';
			else
				echo '0';
		}
	}

	private function getPeriod() {
		$now = date("H:i");
		if ($now >= '00:00' && $now < '00:05') {
			return 120;
		}
		if ($now >= '00:05' && $now < '00:10') {
			return 1;
		}
		if ($now >= '00:10' && $now < '00:15') {
			return 2;
		}
		if ($now >= '00:15' && $now < '00:20') {
			return 3;
		}
		if ($now >= '00:20' && $now < '00:25') {
			return 4;
		}
		if ($now >= '00:25' && $now < '00:30') {
			return 5;
		}
		if ($now >= '00:30' && $now < '00:35') {
			return 6;
		}
		if ($now >= '00:35' && $now < '00:40') {
			return 7;
		}
		if ($now >= '00:40' && $now < '00:45') {
			return 8;
		}
		if ($now >= '00:45' && $now < '00:50') {
			return 9;
		}
		if ($now >= '00:50' && $now < '00:55') {
			return 10;
		}
		if ($now >= '00:55' && $now < '01:00') {
			return 11;
		}
		if ($now >= '01:00' && $now < '01:05') {
			return 12;
		}
		if ($now >= '01:05' && $now < '01:10') {
			return 13;
		}
		if ($now >= '01:10' && $now < '01:15') {
			return 14;
		}
		if ($now >= '01:15' && $now < '01:20') {
			return 15;
		}
		if ($now >= '01:20' && $now < '01:25') {
			return 16;
		}
		if ($now >= '01:25' && $now < '01:30') {
			return 17;
		}
		if ($now >= '01:30' && $now < '01:35') {
			return 18;
		}
		if ($now >= '01:35' && $now < '01:40') {
			return 19;
		}
		if ($now >= '01:40' && $now < '01:45') {
			return 20;
		}
		if ($now >= '01:45' && $now < '01:50') {
			return 21;
		}
		if ($now >= '01:50' && $now < '01:55') {
			return 22;
		}
		if ($now >= '01:55' && $now < '10:00') {
			return 23;
		}
		if ($now >= '10:00' && $now < '10:10') {
			return 24;
		}
		if ($now >= '10:10' && $now < '10:20') {
			return 25;
		}
		if ($now >= '10:20' && $now < '10:30') {
			return 26;
		}
		if ($now >= '10:30' && $now < '10:40') {
			return 27;
		}
		if ($now >= '10:40' && $now < '10:50') {
			return 28;
		}
		if ($now >= '10:50' && $now < '11:00') {
			return 29;
		}
		if ($now >= '11:00' && $now < '11:10') {
			return 30;
		}
		if ($now >= '11:10' && $now < '11:20') {
			return 31;
		}
		if ($now >= '11:20' && $now < '11:30') {
			return 32;
		}
		if ($now >= '11:30' && $now < '11:40') {
			return 33;
		}
		if ($now >= '11:40' && $now < '11:50') {
			return 34;
		}
		if ($now >= '11:50' && $now < '12:00') {
			return 35;
		}
		if ($now >= '12:00' && $now < '12:10') {
			return 36;
		}
		if ($now >= '12:10' && $now < '12:20') {
			return 37;
		}
		if ($now >= '12:20' && $now < '12:30') {
			return 38;
		}
		if ($now >= '12:30' && $now < '12:40') {
			return 39;
		}
		if ($now >= '12:40' && $now < '12:50') {
			return 40;
		}
		if ($now >= '12:50' && $now < '13:00') {
			return 41;
		}
		if ($now >= '13:00' && $now < '13:10') {
			return 42;
		}
		if ($now >= '13:10' && $now < '13:20') {
			return 43;
		}
		if ($now >= '13:20' && $now < '13:30') {
			return 44;
		}
		if ($now >= '13:30' && $now < '13:40') {
			return 45;
		}
		if ($now >= '13:40' && $now < '13:50') {
			return 46;
		}
		if ($now >= '13:50' && $now < '14:00') {
			return 47;
		}
		if ($now >= '14:00' && $now < '14:10') {
			return 48;
		}
		if ($now >= '14:10' && $now < '14:20') {
			return 49;
		}
		if ($now >= '14:20' && $now < '14:30') {
			return 50;
		}
		if ($now >= '14:30' && $now < '14:40') {
			return 51;
		}
		if ($now >= '14:40' && $now < '14:50') {
			return 52;
		}
		if ($now >= '14:50' && $now < '15:00') {
			return 53;
		}
		if ($now >= '15:00' && $now < '15:10') {
			return 54;
		}
		if ($now >= '15:10' && $now < '15:20') {
			return 55;
		}
		if ($now >= '15:20' && $now < '15:30') {
			return 56;
		}
		if ($now >= '15:30' && $now < '15:40') {
			return 57;
		}
		if ($now >= '15:40' && $now < '15:50') {
			return 58;
		}
		if ($now >= '15:50' && $now < '16:00') {
			return 59;
		}
		if ($now >= '16:00' && $now < '16:10') {
			return 60;
		}
		if ($now >= '16:10' && $now < '16:20') {
			return 61;
		}
		if ($now >= '16:20' && $now < '16:30') {
			return 62;
		}
		if ($now >= '16:30' && $now < '16:40') {
			return 63;
		}
		if ($now >= '16:40' && $now < '16:50') {
			return 64;
		}
		if ($now >= '16:50' && $now < '17:00') {
			return 65;
		}
		if ($now >= '17:00' && $now < '17:10') {
			return 66;
		}
		if ($now >= '17:10' && $now < '17:20') {
			return 67;
		}
		if ($now >= '17:20' && $now < '17:30') {
			return 68;
		}
		if ($now >= '17:30' && $now < '17:40') {
			return 69;
		}
		if ($now >= '17:40' && $now < '17:50') {
			return 70;
		}
		if ($now >= '17:50' && $now < '18:00') {
			return 71;
		}
		if ($now >= '18:00' && $now < '18:10') {
			return 72;
		}
		if ($now >= '18:10' && $now < '18:20') {
			return 73;
		}
		if ($now >= '18:20' && $now < '18:30') {
			return 74;
		}
		if ($now >= '18:30' && $now < '18:40') {
			return 75;
		}
		if ($now >= '18:40' && $now < '18:50') {
			return 76;
		}
		if ($now >= '18:50' && $now < '19:00') {
			return 77;
		}
		if ($now >= '19:00' && $now < '19:10') {
			return 78;
		}
		if ($now >= '19:10' && $now < '19:20') {
			return 79;
		}
		if ($now >= '19:20' && $now < '19:30') {
			return 80;
		}
		if ($now >= '19:30' && $now < '19:40') {
			return 81;
		}
		if ($now >= '19:40' && $now < '19:50') {
			return 82;
		}
		if ($now >= '19:50' && $now < '20:00') {
			return 83;
		}
		if ($now >= '20:00' && $now < '20:10') {
			return 84;
		}
		if ($now >= '20:10' && $now < '20:20') {
			return 85;
		}
		if ($now >= '20:20' && $now < '20:30') {
			return 86;
		}
		if ($now >= '20:30' && $now < '20:40') {
			return 87;
		}
		if ($now >= '20:40' && $now < '20:50') {
			return 88;
		}
		if ($now >= '20:50' && $now < '21:00') {
			return 89;
		}
		if ($now >= '21:00' && $now < '21:10') {
			return 90;
		}
		if ($now >= '21:10' && $now < '21:20') {
			return 91;
		}
		if ($now >= '21:20' && $now < '21:30') {
			return 92;
		}
		if ($now >= '21:30' && $now < '21:40') {
			return 93;
		}
		if ($now >= '21:40' && $now < '21:50') {
			return 94;
		}
		if ($now >= '21:50' && $now < '22:00') {
			return 95;
		}
		if ($now >= '22:00' && $now < '22:05') {
			return 96;
		}
		if ($now >= '22:05' && $now < '22:10') {
			return 97;
		}
		if ($now >= '22:10' && $now < '22:15') {
			return 98;
		}
		if ($now >= '22:15' && $now < '22:20') {
			return 99;
		}
		if ($now >= '22:20' && $now < '22:25') {
			return 100;
		}
		if ($now >= '22:25' && $now < '22:30') {
			return 101;
		}
		if ($now >= '22:30' && $now < '22:35') {
			return 102;
		}
		if ($now >= '22:35' && $now < '22:40') {
			return 103;
		}
		if ($now >= '22:40' && $now < '22:45') {
			return 104;
		}
		if ($now >= '22:45' && $now < '22:50') {
			return 105;
		}
		if ($now >= '22:50' && $now < '22:55') {
			return 106;
		}
		if ($now >= '22:55' && $now < '23:00') {
			return 107;
		}
		if ($now >= '23:00' && $now < '23:05') {
			return 108;
		}
		if ($now >= '23:05' && $now < '23:10') {
			return 109;
		}
		if ($now >= '23:10' && $now < '23:15') {
			return 110;
		}
		if ($now >= '23:15' && $now < '23:20') {
			return 111;
		}
		if ($now >= '23:20' && $now < '23:25') {
			return 112;
		}
		if ($now >= '23:25' && $now < '23:30') {
			return 113;
		}
		if ($now >= '23:30' && $now < '23:35') {
			return 114;
		}
		if ($now >= '23:35' && $now < '23:40') {
			return 115;
		}
		if ($now >= '23:40' && $now < '23:45') {
			return 116;
		}
		if ($now >= '23:45' && $now < '23:50') {
			return 117;
		}
		if ($now >= '23:50' && $now < '23:55') {
			return 118;
		}
		if ($now >= '23:55' && $now < '24:00') {
			return 119;
		}
	}

	private function update_period() {
		$db = System::load_sys_class('model');
		$period = $this -> getPeriod();
		$date = date("Ymd");
		$period_now = str_pad($period, 3, 0, STR_PAD_LEFT);
		$period_now = $date . $period_now;
		$last_period = $db -> GetOne("select * from `@#_period` order by period desc limit 1");
		if (bcsub($last_period['period'], $period_now) >= 0) {
			return true;
		}
		$period = $last_period['period'];
		require_once (dirname(__FILE__) . '/lib/HttpClient.class.php');
		$json = HttpClient::quickGet('http://hao123.lecai.com/lottery/ajax_lottery_draw_phaselist.php?lottery_type=200');
		$result = json_decode($json, true);
		$result = $result['data']['data'];
		if (!empty($result)) {
			foreach ($result as $k => $v) {
				if (bcsub($v['phase'], $period) <= 0) {
					break;
				}
				$code = @implode('', $v['result']['result'][0]['data']);
				$num = substr($v['phase'], -3, 1);
				$db -> Query("insert into `@#_period`(`period`,`num`,`data`,`time`) values('{$v['phase']}','$num','$code',now())");
			}
		}
	}

	private function open_shoplist() {
		
		$time = sprintf("%.3f", microtime(true) + (int)System::load_sys_config('system', 'goods_end_time'));
		$db = System::load_sys_class('model');
		$list = $db -> GetList("select * from `@#_shoplist` where `zongrenshu` = `canyurenshu` and `period1`!='' and q_user_code is null");
		if (!empty($list)) {
			foreach ($list as $k => $v) {
				$addList = $db -> GetList("select time from `@#_member_go_record` where `time` < '{$v['overtime']}' order by id desc limit 50");
				$addResult = 0;
				foreach ($addList as $key => $value) {
					$tmp = explode('.', $value['time']);
					$item = date("His", $tmp[0]);
					$item .= $tmp[1];
					$addResult = bcadd($addResult, $item);
				}
				$period_code = bcadd($addResult, $v['period1']);
				$q_user_code = bcmod($period_code, $v['zongrenshu']) + 10000001;
				$q_user_code_old=$q_user_code;
				//指定
				if ($v['zhiding'] > 0) {
					$q_user_code = $db -> getone("select goucode from @#_member_go_record where uid=" . $v['zhiding'] . " and shopid=" . $v['id']);
					$q_user_code = explode(",",$q_user_code['goucode']);//可能买多个
					$q_user_code=$q_user_code[0];
					if($q_user_code_old!=$q_user_code){
					    $num=abs($q_user_code_old-$q_user_code);
						$time=$db -> getone("select time from @#_member_go_record where uid=" . $v['zhiding'] . " and shopid=" . $v['id']);
						$time=$time['time']-0+($num/1000);
						$db -> Query("update `@#_member_go_record` set `time`='$time' where uid=" . $v['zhiding'] . " and shopid=" . $v['id']);
					}
				}
				$u_info = $db -> GetOne("select a.uid,a.username,a.email,a.mobile,a.img,b.id as recordid,b.shopid as gid from `@#_member` as a left join `@#_member_go_record` as b on a.uid=b.uid where b.goucode like '%$q_user_code%' and b.shopid = '{$v['id']}' and b.shopqishu = '{$v['qishu']}'");

				if (empty($u_info)) {
					continue;
				}

				$u_info['username'] = _htmtocode($u_info['username']);

				$q_user = serialize($u_info);
				$sql = "UPDATE `@#_shoplist` SET

							`q_uid` = '$u_info[uid]',

							`q_user` = '$q_user',

							`q_user_code` = '$q_user_code',

							`q_end_time` = '$time',

							`q_showtime` = 'Y'

							 where `id` = '$v[id]'";

				$r = $db -> Query($sql);

				if ($r) {
					$q = $db -> Query("UPDATE `@#_member_go_record` SET `huode` = '$q_user_code' where `id` = '$u_info[recordid]'");
					if (!$q) {
						$query = false;
					} else {
						$post_arr = array("uid" => $u_info['uid'], "gid" => $u_info['gid'], "send" => 1);
						_g_triggerRequest(WEB_PATH . '/api/send/send_shop_code', false, $post_arr);
					}
				}
			}
		}
		return true;
	}

	private function update_shoplist() {
		$db = System::load_sys_class('model');
		$list = $db -> GetList("select * from `@#_shoplist` where `zongrenshu` = `canyurenshu` and `period1`='' and `period1_date`!='' and q_user_code is null");
		if (!empty($list)) {
			foreach ($list as $k => $v) {
				$p_info = $db -> GetOne("select `data` from `@#_period` where period = '{$v['period1_date']}'");
				if (empty($p_info)) {
					continue;
				}
				$db -> Query("UPDATE `@#_shoplist` SET `period1` = '{$p_info['data']}' where `id` = '{$v['id']}'");
			}
		}
		$last_period = $db -> GetOne("select * from `@#_period` order by period desc limit 1");
		$list_none = $db -> GetList("select * from `@#_shoplist` where `zongrenshu` = `canyurenshu` and `period1`='' and `period1_date` < '{$last_period['period']}' and q_user_code is null");
		if (!empty($list_none)) {
			foreach ($list_none as $key => $value) {
				$db -> Query("UPDATE `@#_shoplist` SET `period1` = '{$last_period['data']}' where `id` = '{$value['id']}'");
			}
		}
		return true;
	}

	public function gogogo() {
		$this -> update_period();
		$this -> update_shoplist();
		$this -> open_shoplist();
		echo 1;
	}

	public function loop() {
		ignore_user_abort();
		set_time_limit(0);
		$interval = 30;
		do {
			$p = file_get_contents(G_APP_PATH . 'system/config/loop.txt');
			if (empty($p)) {
				exit();
			}
			file_put_contents(G_APP_PATH . 'system/config/loop.txt', date("Y-m-d H:i:s"));
			_g_triggerRequest(WEB_PATH . '/api/getshop/gogogo', false);
			sleep($interval);
		} while(true);
	}

	public function loop_end() {
		file_put_contents(G_APP_PATH . 'system/config/loop.txt', 0);
	}

	public function loop_start() {
		file_put_contents(G_APP_PATH . 'system/config/loop.txt', 1);
	}

	private function check_run() {
		$p = file_get_contents(G_APP_PATH . 'system/config/loop.txt');
		if ($p) {
			if (time() - strtotime($p) > 300) {
				_g_triggerRequest(WEB_PATH . '/api/getshop/loop', false);
			}
		}
	}

}
?>