Plantask.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Db;
  5. class Plantask extends Controller
  6. {
  7. //关于本文件里的计划任务
  8. //只有 public 方法,auto_开头的才是计划任务,其他private 方法都是工具方法
  9. ////////////////////////////////////////下面都是计划任务方法///////////////////////////////////////////////////////////////
  10. //未支付订单自动取消时长
  11. public function auto_order_cancel(){
  12. $order_auto_cancel_minite = config('site.order_auto_cancel_minite');
  13. $canceltime = time() - ($order_auto_cancel_minite * 60);
  14. $where = [
  15. 'status' => 0,
  16. 'createtime' => ['lt',$canceltime],
  17. ];
  18. Db::startTrans();
  19. $list = Db::name('order')->where($where)->limit(20)->lock(true)->select();
  20. if(empty($list)){
  21. echo 'empty';
  22. Db::rollback();
  23. exit;
  24. }
  25. foreach($list as $key => $order){
  26. $update = [
  27. 'status' => 10,
  28. 'cancle_time' => time(),
  29. ];
  30. //修改订单状态
  31. $order_id = $order['id'];
  32. $order_rs = Db::name('order')->where('id',$order_id)->update($update);
  33. if($order_rs === false){
  34. Db::rollback();
  35. echo '保存失败';
  36. exit;
  37. }
  38. //车票还回去
  39. $order_road = Db::name('order_road')->where('order_id',$order_id)->lock(true)->select();
  40. foreach($order_road as $key => $road){
  41. $chufabanci = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->lock(true)->find();
  42. if(!empty($chufabanci)){
  43. $update = [
  44. 'ticket_remain' => $chufabanci['ticket_remain'] + $road['ticket_number'],
  45. ];
  46. $rs_ticket = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->update($update);
  47. if($rs_ticket === false){
  48. Db::rollback();
  49. echo '保存失败';
  50. exit;
  51. }
  52. }
  53. }
  54. }
  55. Db::commit();
  56. echo '取消成功';
  57. exit;
  58. }
  59. /////////////////////////////////////////下面都是工具方法////////////////////////////////////////////////
  60. }