无聊,网上瞎逛看到的自己摸索着实现了下,挺好玩,做个记录


  1. <?php
  2. namespace app\index\controller;
  3.  
  4. use think\App;
  5. use think\Controller;
  6. use think\Db;
  7. use think\facade\Config;
  8.  
  9. class Index extends Controller
  10. {
  11. public function __construct(App $app = null)
  12. {
  13. parent::__construct($app);
  14. $this->id = input('id');
  15. $store = DB::name('goods')->where('id', $this->id)->value('count');
  16. $redis = new \Redis();
  17. $redis->connect(Config::get('redis.host'),Config::get('redis.port'),9000);
  18. $redis->del('goods_store'); // 删除库存列表
  19. $res=$redis->llen('goods_store'); //返回库存长度,这里已经是0
  20. $count=$store-$res;
  21. for($i=0;$i<$count;$i++){
  22. $redis->lpush('goods_store',1); //列表推进50个,模拟50个商品库存
  23. }
  24. $redis->lLen('goods_store');
  25. }
  26.  
  27. public function index()
  28. {
  29. $id = $this->id; //商品编号
  30. $redis = new \Redis();
  31. $redis->connect(Config::get('redis.host'),Config::get('redis.port'),9000);
  32. if(!$id){
  33. return $this->insertlog(0);//记录失败日志
  34. }
  35. $count=$redis->lpop('goods_store'); //减少库存,返回剩余库存数
  36. if(!$count){ //库存为0
  37.  
  38. $this->insertlog(0); //记录秒杀失败的 日志
  39.  
  40. return false;
  41.  
  42. }else{// 有库存
  43.  
  44. $ordersn = $this->build_order_no(); //订单号随机生成
  45. $uid = 1; //用户id随机生成,正式项目可以启用登录的session
  46.  
  47. $status = 1; // 订单状态
  48.  
  49. $data = Db::name("goods")->field("count,amount")->where("id",$id)->find();//查找商品
  50. if(!$data){
  51. return $this->insertlog(0); //商品不存在
  52. }
  53.  
  54.  
  55.  
  56. $result = Db::name("order")->insert(["order_sn"=>$ordersn,"user_id"=>$uid,"goods_id"=>$id,"price"=>$data['amount'],"status"=>$status,'addtime'=>date('Y-m-d H:i:s')]); //订单入库
  57.  
  58. $res = Db::name("goods")->where("id",$id)->setDec("count"); //库存减少
  59.  
  60. if($res){
  61. $this->insertlog(); //记录成功的日志
  62. }else{
  63. $this->insertlog(0);//记录失败的日志
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. }
  74. }
  75.  
  76. //生成唯一订单
  77.  
  78. function build_order_no(){
  79.  
  80. return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
  81.  
  82. }
  83.  
  84.  
  85.  
  86. // 记录日志 状态1成功 0失败
  87.  
  88. function insertlog($status=1){
  89.  
  90. return Db::name("log")->insertGetId(["count"=>1,"status"=>$status,"addtime"=>date('Y-m-d H:i:s')]);
  91.  
  92. }
  93.  
  94.  
  95. }

测试数据库:

CREATE TABLE `goods` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `count` int(10) unsigned NOT NULL,
  `status` tinyint(255) NOT NULL DEFAULT '1',
  `title` varchar(255) DEFAULT NULL,
  `amount` decimal(10,2) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `log` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `addtime` datetime DEFAULT NULL,
  `status` tinyint(1) unsigned DEFAULT NULL,
  `count` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1172 DEFAULT CHARSET=utf8;
CREATE TABLE `order` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `order_sn` varchar(50) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `order_id` int(11) DEFAULT NULL,
  `price` decimal(10,2) DEFAULT NULL,
  `status` tinyint(1) DEFAULT NULL,
  `addtime` datetime DEFAULT NULL,
  `goods_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1959 DEFAULT CHARSET=utf8;

相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!