123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace app\index\controller;
- use think\Controller;
- use think\Db;
- class Plantask extends Controller
- {
- //关于本文件里的计划任务
- //只有 public 方法,auto_开头的才是计划任务,其他private 方法都是工具方法
- ////////////////////////////////////////下面都是计划任务方法///////////////////////////////////////////////////////////////
- //未支付订单自动取消时长
- public function auto_order_cancel(){
- $order_auto_cancel_minite = config('site.order_auto_cancel_minite');
- $canceltime = time() - ($order_auto_cancel_minite * 60);
- $where = [
- 'status' => 0,
- 'createtime' => ['lt',$canceltime],
- ];
- Db::startTrans();
- $list = Db::name('order')->where($where)->limit(20)->lock(true)->select();
- if(empty($list)){
- echo 'empty';
- Db::rollback();
- exit;
- }
- foreach($list as $key => $order){
- $update = [
- 'status' => 10,
- 'cancle_time' => time(),
- ];
- //修改订单状态
- $order_id = $order['id'];
- $order_rs = Db::name('order')->where('id',$order_id)->update($update);
- if($order_rs === false){
- Db::rollback();
- echo '保存失败';
- exit;
- }
- //车票还回去
- $order_road = Db::name('order_road')->where('order_id',$order_id)->lock(true)->select();
- foreach($order_road as $key => $road){
- $chufabanci = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->lock(true)->find();
- if(!empty($chufabanci)){
- $update = [
- 'ticket_remain' => $chufabanci['ticket_remain'] + $road['ticket_number'],
- ];
- $rs_ticket = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->update($update);
- if($rs_ticket === false){
- Db::rollback();
- echo '保存失败';
- exit;
- }
- }
- }
- }
- Db::commit();
- echo '取消成功';
- exit;
- }
- /////////////////////////////////////////下面都是工具方法////////////////////////////////////////////////
- }
|