Created
May 22, 2013 08:37
-
-
Save goocarlos/5626118 to your computer and use it in GitHub Desktop.
每天生成 N 个中奖机会到奖池,从奖池进行抽奖,代码数据库操作部分依赖 Codeigniter。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//CREATE TABLE IF NOT EXISTS `pool` ( | |
//`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, | |
//`active_time` datetime NOT NULL COMMENT '活动时间', | |
//`used_time` datetime DEFAULT NULL COMMENT '被使用时间', | |
//`status` tinyint(4) NOT NULL, | |
//PRIMARY KEY (`id`), | |
//KEY `active_time_status` (`active_time`,`status`) | |
//) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; | |
/** | |
* 构造奖池(按每天所需中奖数量) | |
* 该方法为手动操作方法,连同 GET 形势的密钥访问,用以重建或清空奖池 | |
* @author luyuz | |
*/ | |
public function set_up_pool() | |
{ | |
if ($this->admin_key != $this->input->get('admin_key', TRUE)) { | |
die('Admin Key ERROR!'); | |
} | |
/** | |
* 是否要清空未抽奖池 | |
*/ | |
$is_empty = $this->input->get('is_empty', TRUE) ? $this->input->get('admin_key', TRUE) : TRUE; | |
/** | |
* 每天生成多少个中奖机会 | |
*/ | |
$everyday_quantity = $this->input->get('everyday_quantity', TRUE) ? $this->input->get('everyday_quantity', TRUE) : 30; | |
/** | |
* 开始生成日期(含) | |
*/ | |
$start_date = $this->input->get('start_date', TRUE) ? $this->input->get('start_date', TRUE) : '20130522'; | |
/** | |
* 结束生成日期(含) | |
*/ | |
$end_date = $this->input->get('end_date', TRUE) ? $this->input->get('end_date', TRUE) : '20130613'; | |
// 开始从时间范围内生成时间戳 | |
$start_timestamp = mktime(00, 00, 00, substr($start_date, 4, 2), substr($start_date, 6, 2), substr($start_date, 0, 4)); | |
$end_timestamp = mktime(23, 59, 59, substr($end_date, 4, 2), substr($end_date, 6, 2), substr($end_date, 0, 4)); | |
$timestamp_list = array(); | |
$curr_day = $start_timestamp; | |
$curr_day_add = strtotime('+1 day', $start_timestamp); | |
while ($curr_day <= $end_timestamp) { | |
for ($o = 0; $o < $everyday_quantity; $o++) { | |
$timestamp = rand($curr_day, $curr_day_add); | |
$timestamp_list[] = date("Y-m-d H:i:s", $timestamp); | |
} | |
$curr_day = strtotime('+1 day', $curr_day); | |
$curr_day_add = strtotime('+1 day', $curr_day); | |
} | |
// 清空未抽奖池 | |
if ($is_empty) { | |
$this->db->delete($this->table['pool'], array('status' => 0)); | |
} | |
// 写入数据库 | |
sort($timestamp_list); | |
foreach ($timestamp_list as $row) { | |
$data = array( | |
'active_time' => $row, | |
'status' => 0, | |
); | |
$this->db->insert($this->table['pool'], $data); | |
} | |
die('Finish!'); | |
} | |
/** | |
* 以当前时间从奖池抽奖一次 | |
*/ | |
private function _lucky_draw() | |
{ | |
$now = date("Y-m-d H:i:s"); | |
$this->db->where(array('status' => 0, 'active_time <=' => $now)); | |
$this->db->limit(1); | |
$this->db->update($this->table['pool'], array('status' => 1, 'used_time' => $now)); | |
if ($this->db->affected_rows() > 0) { | |
return TRUE; | |
} else { | |
return FALSE; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment