public function table_init() { $sqlModel = new sql_model(); $sql = <<<EOF drop database killsec; EOF; $sqlModel->query($sql); $sql = <<<EOF create database killsec; EOF; $sqlModel->query($sql); $sql = <<<EOF use killsec; EOF; $sqlModel->query($sql); $sql = <<<EOF create table activity_killsec( activity_id int auto_increment primary key, name varchar(20), killsec_count tinyint(4), killsec_price decimal(6,2), activity_start_time datetime, killsec_start_time datetime, killsec_end_time datetime, create_time datetime, status tinyint(4) ); EOF; $sqlModel->query($sql); $sql = <<<EOF create table activity_killsec_instance( instance_id int auto_increment primary key, activity_id tinyint(4), killsec_count tinyint(4), killsec_price decimal(6,2), join_count tinyint(4), start_time datetime, close_time datetime, create_time datetime, status tinyint(4) ); EOF; $sqlModel->query($sql); $sql = <<<EOF create table activity_killsec_user( user_id int auto_increment primary key, instance_id tinyint(4), activity_id tinyint(4), join_time datetime, order_time datetime, pay_time datetime, create_time datetime, status tinyint(4) ); EOF; $sqlModel->query($sql); }
public function get_instance($filter) { $where_str = ''; if (count($filter) > 0) { $where_str = '1'; foreach ($filter as $k => $v) { if (is_array($v)) { $in = ''; foreach ($v as $sv) { $in .= $sv . ","; } $where_str .= " and " . $k . " in (" . trim($in, ',') . ")"; } else { $where_str .= " and " . $k . "='" . $v . "'"; } } } $sql = "select * from activity_killsec_instance where " . $where_str; $sqlModel = new sql_model(); return $sqlModel->query($sql); }
public function get_activity($activity_id) { $sql = "select * from activity_killsec where activity_id='{$activity_id}' and status='" . STATUS_TRUE . "'"; $sqlModel = new sql_model(); $sqlModel->query($sql); }
public function killsec_user_order_clear() { $log = new log_model(); $redisModel = new redis_model(); $sqlModel = new sql_model(); $instance = new instance_model(); $status_array = array(KILLSEC_STATUS_PREVIEW, KILLSEC_STATUS_GOING); $filter = array('status' => $status_array); $instance_list = $instance->get_instance($filter); foreach ($instance_list as $k => $v) { $total = $v['killsec_count']; if ($v['status'] == KILLSEC_STATUS_PREVIEW) { //开始前2分钟生成token if (strtotime($v['start_time']) - 20 * 60 <= time()) { //更新实例表状态 $sql = "update activity_killsec_instance set status='" . KILLSEC_STATUS_GOING . "' where instance_id='{$v['instance_id']}' and status=" . KILLSEC_STATUS_PREVIEW; $user_list = $sqlModel->query($sql); $log->write_log('更新实例表状态sql=' . $sql); $redisModel->save_token($v['activity_id'], $redisModel->create_token($v['killsec_count'])); } } elseif ($v['status'] == KILLSEC_STATUS_GOING) { $sql = "select * from activity_killsec_user where instance_id='{$v['instance_id']}'"; $user_list = $sqlModel->query($sql); $pay_count = 0; $order_userIds = array(); $payment_userIds = array(); if ($user_list && count($user_list) > 0) { foreach ($user_list as $sk => $sv) { if (empty($sv['order_time'])) { if (time() - strtotime($sv['join_time']) - ORDER_WAIT_TIME > 0) { //ORDER_WAIT_TIME后没下单 $order_userIds[] = $v['user_id']; } } elseif (empty($sv['pay_time'])) { if (time() - strtotime($sv['join_time']) - PAYMENT_WAIT_TIME > 0) { //PAYMENT_WAIT_TIME后没支付 $payment_userIds[] = $v['user_id']; } } elseif (!empty($sv['pay_time'])) { $pay_count += 1; } } } $userIds = $order_userIds + $payment_userIds; $count = count($userIds); if ($count > 0) { //删除过期用户 $sql = "delete from activity_killsec_user where user_id in(" . implode(',', $userIds) . ")"; $user_list = $sqlModel->query($sql); $log->write_log('删除过期用户sql=' . $sql); //更新参与数量 $sql = "update activity_killsec_instance set join_count ='" . $v['join_count'] - $count . "' where instance_id='{$v['instance_id']}'"; $user_list = $sqlModel->query($sql); $log->write_log('更新参与数量sql=' . $sql); //补充token $redisModel->save_token($v['activity_id'], $redisModel->create_token($count)); } if ($pay_count == $total) { //秒杀完成 $sql = "update activity_killsec_instance set status='" . KILLSEC_STATUS_FINISH . "' where instance_id='{$v['instance_id']}' and status=" . KILLSEC_STATUS_GOING; $user_list = $sqlModel->query($sql); $log->write_log('秒杀完成sql=' . $sql); } } } }