thinkphp5.1,redis抢购实现

<?php
namespace app\index\controller;
 
use think\App;
use think\Controller;
use think\Db;
use think\facade\Config;
 
class Index extends Controller
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->id = input('id');
        $store =  DB::name('goods')->where('id', $this->id)->value('count');
        $redis = new \Redis();
        $redis->connect(Config::get('redis.host'),Config::get('redis.port'),9000);
        $redis->del('goods_store'); // 删除库存列表
        $res=$redis->llen('goods_store'); //返回库存长度,这里已经是0
        $count=$store-$res;
        for($i=0;$i<$count;$i++){
            $redis->lpush('goods_store',1); //列表推进50个,模拟50个商品库存
        }
        $redis->lLen('goods_store');
    }
 
    public function index()
    {
        $id =  $this->id; //商品编号
        $redis = new \Redis();
        $redis->connect(Config::get('redis.host'),Config::get('redis.port'),9000);
        if(!$id){
            return $this->insertlog(0);//记录失败日志
        }
        $count=$redis->lpop('goods_store'); //减少库存,返回剩余库存数
        if(!$count){ //库存为0
 
            $this->insertlog(0); //记录秒杀失败的 日志
 
            return false;
 
        }else{// 有库存
 
            $ordersn = $this->build_order_no(); //订单号随机生成
            $uid = 1; //用户id随机生成,正式项目可以启用登录的session
 
            $status = 1; // 订单状态
 
            $data = Db::name("goods")->field("count,amount")->where("id",$id)->find();//查找商品
            if(!$data){
                return $this->insertlog(0); //商品不存在
            }
 
 
 
            $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')]); //订单入库
 
            $res = Db::name("goods")->where("id",$id)->setDec("count"); //库存减少
 
            if($res){
                $this->insertlog(); //记录成功的日志
            }else{
                $this->insertlog(0);//记录失败的日志
            }
 
 
 
 
 
 
 
 
        }
    }
 
    //生成唯一订单
 
    function build_order_no(){
 
        return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
 
    }
 
 
 
// 记录日志 状态1成功 0失败
 
    function insertlog($status=1){
 
        return Db::name("log")->insertGetId(["count"=>1,"status"=>$status,"addtime"=>date('Y-m-d H:i:s')]);
 
    }
 
 
}

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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PHP柚子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值