<?php class Test { private static $instance = null; private static $conn=null; // 用单列模式 实例化Redis public static function Redis() { if (self::$instance == null) { $redis=new \Redis(); $redis->connect('127.0.0.1',6379); self::$instance = $redis; } return self::$instance; } public static function mysql($sql,$type) { $mysql_conf = array( 'host' => '127.0.0.1', 'db' => 'big', 'db_user' => 'root', 'db_pwd' => 'root', ); $mysqli = @new mysqli($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']); if ($mysqli->connect_errno) { die("could not connect to the database:\n" . $mysqli->connect_error);//诊断连接错误 } $mysqli->query("set names 'utf8';");//编码转化 $select_db = $mysqli->select_db($mysql_conf['db']); if (!$select_db) { die("could not connect to the db:\n" . $mysqli->error); } $res = $mysqli->query($sql); if($type==1){ $res = $res->fetch_assoc(); } return $res; } // 将商品库存循环到lpush的num里 public function doPageSaveNum() { $redis=self::Redis(); $goods_id=1; $sql="select id, num, money from ims_hotmallstore_goods where id=".$goods_id; $goods= $this->mysql($sql,1); if(!empty($goods)){ for($i=1; $i<=$goods['num']; $i++){ $redis->lpush('num',$i); } die('成功!'); }else{ $this->echoMsg(0,'商品不存在。'); } } // 抢购下单 public function doPageGoodsStore() { $goods_id=1; $sql="select id, num, money from ims_hotmallstore_goods where id=".$goods_id; $goods=$this->mysql($sql,1); $redis=self::Redis(); $count=$redis->lpop('num');//每次从num取出1 if($count==0){ $this->echoMsg(0,'no num redis'); } $this->doPageGoodsOrder($goods,1); } public function orderNo() { return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); } // 下单更新库存 public function doPageGoodsOrder($goods,$goods_number) { $user_id=rand(1,500); $orderNo=$this->orderNo(); $number=$goods['num']-$goods_number; if($number<0){ $redis=self::Redis(); $redis->lpush('user',$user_id); return $this->echoMsg(0,'库存扣减失败'.$user_id); } $order['user_id']=$user_id; $order['goods_id']=$goods['id']; $order['number']=$goods_number; $order['price']=$goods['money']; $order['status']=1; $order['sku_id']=2; $order['order_sn']=$orderNo; $order['create_time']=date('Y-m-d H:i:s'); $this->pdo_insert_order($order); $sql="update ims_hotmallstore_goods set num=num-".$goods_number." where num>0 and id=".$goods['id']; $res=$this->mysql($sql,2); if(!empty($res)){ return $this->echoMsg(1,'库存扣减成功'.$number); }elseif (condition) { $redis=self::Redis(); $redis->lpush('user',$user_id); return $this->echoMsg(0,'库存扣减失败'.$user_id); } } function pdo_insert_order($order) { $sql="insert into ims_order(user_id,goods_id,number,price,status,sku_id,order_sn,create_time) values('{$order['user_id']}','{$order['goods_id']}','{$order['number']}','{$order['price']}','{$order['status']}','{$order['sku_id']}','{$order['order_sn']}','{$order['create_time']}')"; $res=$this->mysql($sql,2); return $res; } // 保存日志 function echoMsg($status,$msg) { $time=time(); $sql="insert into ims_order_log(status,msg,create_time)values('{$status}','{$msg}','{$time}')"; $res=$this->mysql($sql,2); } } // 调用--将商品库存循环到lpush的num里 if($_GET['i']==1){ $model = new Test; $model->doPageSaveNum(); } // 调用--高并发抢购下单 if($_GET['i']==2){ $model = new Test; $model->doPageGoodsStore(); } ?>
sql CREATE TABLE ims_hotmallstore_goods ( id int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(50) NOT NULL COMMENT ‘商品名称’, type_id int(11) NOT NULL COMMENT ‘商品分类’, img text NOT NULL COMMENT ‘商品图片’, money decimal(10,2) NOT NULL COMMENT ‘售价’, money2 decimal(10,2) NOT NULL COMMENT ‘原价’, is_show int(11) NOT NULL DEFAULT ‘1’ COMMENT ‘1.上架2.下架’, uniacid int(11) NOT NULL COMMENT ‘小程序id’, inventory int(11) NOT NULL COMMENT ‘库存’, details text NOT NULL COMMENT ‘详情’, store_id int(11) NOT NULL COMMENT ‘商家id’, sales int(11) NOT NULL COMMENT ‘销量’, logo varchar(100) NOT NULL, num int(11) NOT NULL, is_gg int(11) NOT NULL DEFAULT ‘2’ COMMENT ‘是否开启规格’, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE ims_order ( id int(11) NOT NULL AUTO_INCREMENT, order_sn char(32) NOT NULL, user_id int(11) NOT NULL, status int(11) NOT NULL DEFAULT ‘0’, goods_id int(11) NOT NULL DEFAULT ‘0’, sku_id int(11) NOT NULL DEFAULT ‘0’, number int(11) NOT NULL, price int(10) NOT NULL COMMENT ‘价格:单位为分’, create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=5940 DEFAULT CHARSET=utf8 COMMENT=‘订单表’;
CREATE TABLE ims_order_log ( id int(11) NOT NULL AUTO_INCREMENT, status int(11) NOT NULL DEFAULT ‘0’, msg text CHARACTER SET utf8, create_time int(10) DEFAULT NULL, PRIMARY KEY (id), KEY status (status) ) ENGINE=InnoDB AUTO_INCREMENT=4864 DEFAULT CHARSET=gb2312 COMMENT=‘订单日志表’;