<?php
/* versionbeta:name
 * versionbeta:number
 * versionbeta:site
 * versionbeta:pubtime
 */
class mod_act_lottery extends inc_mod_default {
	function get_lottery($id) {
		$obj_db = cls_obj::db();
		$arr_award = $arr_award_lottery = array();
		$obj_lottery = $obj_db->get_one("select * from " . cls_config::DB_PRE . "act_lottery where lottery_id='" . $id . "'");
		if(empty($obj_lottery)) cls_error::on_exit('exit',tab_other_language::get('活动不存在'));
		$obj_lottery['lottery_desc'] = fun_get::filter($obj_lottery['lottery_desc'] , true);
		$obj_result = $obj_db->select("select * from " . cls_config::DB_PRE . "act_lottery_award where award_lottery_id='" . $id . "'");
		$total_rate = 0;
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$total_rate+=$obj_rs['award_rate'];
			if($obj_rs['award_type'] == 1) $obj_rs['award_name'] = tab_other_language::get("{msg1}元",$obj_rs['award_name']);
			$obj_lottery['award_list']['id_' . $obj_rs['award_id']] = $obj_rs;
		}
		if($obj_lottery['lottery_method']==0) {
			$num = $obj_lottery['lottery_limit_user'];
			if($num == 0) {
				$num = -1;
			} else {
				$obj_record = $obj_db->get_one("select count(1) as 'num' from " . cls_config::DB_PRE . "act_lottery_record where record_user_id='" . cls_obj::get("cls_user")->uid . "' and record_lottery_id='" . $obj_lottery['lottery_id'] . "'");
				$num = $num - $obj_record['num'];
				if( $num < 0 ) $num = 0;
			}
		} else {
			$obj_record = $obj_db->get_one("select count(1) as 'num' from " . cls_config::DB_PRE . "act_lottery_piao where piao_user_id='" . cls_obj::get("cls_user")->uid . "' and piao_lottery_id='" . $obj_lottery['lottery_id'] . "' and piao_usetime=0");
			$num = $obj_record['num'];
		}
		$obj_lottery['num'] = $num;
		if($obj_lottery['lottery_type']==0) {
			$obj_lottery['award'] = tab_act_lottery::get_award($obj_lottery,$obj_lottery['award_list']);
		} else {
			$arr = array();
			$ii = 0;
			foreach($obj_lottery['award_list'] as $item) {
				$rate = number_format($item['award_rate']/$obj_lottery['lottery_rate']*100,2);
				$arr[] = array(
					"name" => $item['award_name'],
					"rate" => $rate,
					"id" => $item['award_id'],
					'pic' => fun_get::url($item['award_pic'])
				);
				$ii = $ii + $rate;
			}
			$i = 100-$ii;
			if($i>0) {
				$arr[] = array(
					"name" => tab_other_language::get("谢谢参与"),
					"rate" => $i,
					"id" => 0,
					'pic' => ''
				);
			}
			$obj_lottery['award'] = fun_format::json($arr);
		}
		return $obj_lottery;
	}
	//取获奖记录
	function get_award_log($id) {
		$uid = cls_obj::get("cls_user")->uid;
		if(empty($uid)) $uid = -1;
		$wx_id = cls_obj::get("cls_session")->get("wx_id");
		if(empty($wx_id)) $wx_id = -1;
		$arr_state = tab_act_lottery::get_perms("log_state");
		$arr_return = array();
		$obj_result = cls_obj::db()->select("select a.log_datetime,a.log_id,a.log_state,b.award_name,a.log_datetime,c.user_netname from " . cls_config::DB_PRE . "act_lottery_log a left join " . cls_config::DB_PRE . "act_lottery_award b on a.log_award_id=b.award_id left join " . cls_config::DB_PRE . "sys_user c on a.log_user_id=c.user_id where (log_user_id='" . $uid . "' or log_wx_id='" . $wx_id . "') and log_lottery_id='" . $id . "' order by log_id desc limit 0,10");
		while($obj_rs = cls_obj::db()->fetch_array($obj_result)) {
			$obj_rs['state'] = array_search($obj_rs['log_state'] , $arr_state);
			$arr_return[] = $obj_rs;
		}
		return $arr_return;
	}
	//抽奖
	function get_award() {
		$obj_db = cls_obj::db();
		$id = (int)fun_get::get("id");
		$obj_lottery = $obj_db->get_one("select * from " . cls_config::DB_PRE . "act_lottery where lottery_id='" . $id . "'");
		if(empty($obj_lottery)) return array("code" => 500 , "msg" => tab_other_language::get("活动不存在"));
		$obj_result = $obj_db->select("select * from " . cls_config::DB_PRE . "act_lottery_award where award_lottery_id='" . $id . "'");
		$total_rate = 0;
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$total_rate+=$obj_rs['award_rate'];
			$obj_lottery['award_list']['id_' . $obj_rs['award_id']] = $obj_rs;
		}
		//if($obj_lottery['lottery_type']==1) $obj_lottery['lottery_rate'] = $total_rate;
		$arr_return = tab_act_lottery::get_award($obj_lottery,$obj_lottery['award_list']);
		return $arr_return;
	}
	function get_lottery_award() {
		$obj_db = cls_obj::db();
		$uid = cls_obj::get("cls_user")->uid;
		$arr_where = array("log_user_id='" . cls_obj::get("cls_user")->uid . "'");
		$arr_return = array("list" => array());
		$str_where = empty($arr_where) ? "" : " where " . implode(" and " , $arr_where);
		$lng_page = fun_get::get("page");
		$arr_return["pageinfo"] = $obj_db->get_pageinfo(cls_config::DB_PRE."act_lottery_log a left join " . cls_config::DB_PRE . "act_lottery_award b on a.log_award_id=b.award_id" , $str_where , $lng_page , 10);
		$obj_result = $obj_db->select("select a.*,b.award_name,b.award_type FROM ".cls_config::DB_PRE."act_lottery_log a left join " . cls_config::DB_PRE . "act_lottery_award b on a.log_award_id=b.award_id " . $str_where . " order by log_id desc" . $arr_return['pageinfo']['limit']);
		while( $obj_rs = $obj_db->fetch_array($obj_result) ) {
			$obj_rs['log_sendtime'] = empty($obj_rs['log_sendtime']) ? "" : date("Y-m-d H:i",$obj_rs['log_sendtime']);
			$arr_return["list"][] = $obj_rs;
		}
		return $arr_return;
	}
	function on_address_save() {
		$id = (int)fun_get::get("id");
		$arr_fields = array(
			"log_name" => fun_get::get("name"),
			"log_tel" => fun_get::get("tel"),
			"log_address" => fun_get::get("address"),
		);
		$arr = cls_obj::db()->on_update(cls_config::DB_PRE."act_lottery_log",$arr_fields,"log_id='".$id."'");
		return $arr;
	}
	function get_red() {
		$id = (int)fun_get::get("id");
		$uid = cls_obj::get('cls_user')->uid;
		$obj_log = cls_obj::db()->get_one("select a.log_sendtime,a.log_state,c.award_name,b.lottery_red_shopname,b.lottery_reg_greet,b.lottery_red_beta,b.lottery_red_title from " . cls_config::DB_PRE . "act_lottery_log a left join " . cls_config::DB_PRE . "act_lottery b on a.log_lottery_id=b.lottery_id left join " . cls_config::DB_PRE . "act_lottery_award c on a.log_award_id=c.award_id where log_id='" . $id . "' and log_user_id='" . $uid . "'");
		if(empty($obj_log)) return array("code" => 500 , "msg" => tab_other_language::get("红包信息不存在"));
		if($obj_log['log_state']>0) return array("code" => 500 , "msg" => tab_other_language::get("您已在{msg1}领取了红包，不能重复领取",date('Y-m-d H:i',$obj_log['log_sendtime'])));
		if($obj_log['log_state']<0) return array("code" => 500 , "msg" => tab_other_language::get("红包无效，不能领取"));
		$money = (int)$obj_log['award_name'];
		if(empty($money) || $money<1) return array("code" => 500 , "msg" => tab_other_language::get("红包数值不正确，请管理员重新配置"));
		$arrx = tab_weixin_redpack::on_send('' , $money , $obj_log['lottery_red_title'] , $obj_log['lottery_red_shopname'] , $obj_log['lottery_reg_greet'] , $obj_log['lottery_red_beta']);
		if($arrx['code'] != 0) return $arrx;
		cls_obj::db()->on_exe("update " . cls_config::DB_PRE . "act_lottery_log set log_sendtime='" . TIME . "',log_state=1 where log_id='" . $id . "'");
		return array("code" => 0 , "msg" => "");
	}
}