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

发布时间:2022-01-13 17:09:24 阅读:1365次

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

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

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


<?php namespace app\command\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\facade\Log;
use think\facade\Cache;
use app\common\model\GoodsOrder;
class runCancelOrder extends Command
{
    protected function configure(){
        $this->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();
            }
        }
    }

}

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:thinkphp5利用redis有序集合将三十分钟内未付款订单取消 出自老鄢博客 | 欢迎分享