thinkphp5利用redis有序集合将三十分钟内未付款订单取消

在thinkphp5中我们可以用框架自带的很多方法

能不能直接用redis的原生方法来操作呢?

见以下代码,以下代码将三十分钟内没有付款的订单作废

```

setName('runCancelOrder')->setDescription("计划任务 runCancelOrder");
}

//调用SendMessage 这个类时,会自动运行execute方法
protected function execute(Input $input, Output $output){
$output->writeln('Date Crontab job start...');
$this->run();//执行任务
$output->writeln('Date Crontab job end...');
}

public function run()
{
$time = time()-1800;
echo date("Y-m-d H:i:s",$time)."\r\n";
//$list = Cache::store('redis')->rawCommand("zrange", "order_list", "0", "-1");
//$list = Cache::store('redis')->rawCommand("ZRANGEBYSCORE", "order_list", "-inf", $time);
$list = Cache::store('redis')->ZRANGEBYSCORE("order_list", "-inf", $time);
foreach($list as $k=>$order_sn){
echo $order_sn."\r\n";
//$res = Cache::store('redis')->rawCommand("zscore", "order_list", $v);
//print_r(date("Y-m-d H:i:s",$res));
Db::startTrans();
try{
$orderinfo = Db::name('order')->where('order_sn', $order_sn)->where('pay_status', 0)->find();
//增加到新表记录临时数据
$write_val = Db::name('order_unpaid')->insert($orderinfo);
if($write_val>0){
Db::name('order')->where(['id'=>$orderinfo['id']])->delete();
M('goods')->where(['id'=>$orderinfo['goods_id']])->dec('sold_number',$orderinfo['buy_number'])->inc('goods_number',$orderinfo['buy_number'])->update();
}
Db::commit();
//更新库存
Cache::store('redis')->inc('goods_store_'.$orderinfo['goods_id'], $orderinfo['buy_number']);
//更新订单缓存
GoodsOrder::updateCache($orderinfo);
//清除有序集合中订单
Cache::store('redis')->rawCommand("zrem", "order_list", $order_sn);
}catch(\Exception $e){
Db::rollback();
}
}
}

}

```

    A+
发布日期:2022年01月13日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: