TimedTask.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\DiscountCoupon;
  5. use app\common\model\Order;
  6. use think\Db;
  7. use think\Exception;
  8. /**
  9. * 定时任务
  10. */
  11. class TimedTask extends Api
  12. {
  13. protected $noNeedLogin = ['*'];
  14. protected $noNeedRight = '*';
  15. /**
  16. * 订单过期/优惠券过期自动取消
  17. * @return void
  18. */
  19. public function cancellation_order(){
  20. //订单过期自动取消
  21. $orderlist = Order::all(function ($query){
  22. $query->where(['type'=>1,'status'=>1])->field('id,create_time,discount_id');
  23. });
  24. foreach ($orderlist as $k=>$value){
  25. if($value){
  26. $create_time = strtotime($value['create_time']) + 1800;
  27. if($create_time < time()){
  28. Db::startTrans();
  29. try {
  30. Order::where('id',$value['id'])->update(['status'=>0]);
  31. if($value['discount_id']){
  32. DiscountCoupon::where('id',$value['discount_id'])->update(['status'=>1]);
  33. }
  34. Db::commit();
  35. }catch (Exception $exception){
  36. Db::rollback();
  37. $this->error($exception);
  38. }
  39. }
  40. }
  41. }
  42. //报价单过期自动取消
  43. $pjdorderlist = Order::all(function ($query){
  44. $query->where(['type'=>2,'status'=>1])->field('id,valid_time');
  45. });
  46. foreach ($pjdorderlist as $k=>$v){
  47. $valid_time = strtotime($v['valid_time']);
  48. if($valid_time < time()){
  49. Db::startTrans();
  50. try {
  51. Order::where('id',$v['id'])->update(['status'=>0]);
  52. Db::commit();
  53. }catch (Exception $exception){
  54. Db::rollback();
  55. $this->error($exception);
  56. }
  57. }
  58. }
  59. //优惠券过期
  60. $couponlist = DiscountCoupon::all(function ($query){
  61. $query->where(['status'=>1])->field('id,expiration_time');
  62. });
  63. foreach ($couponlist as $v){
  64. $valid_time = strtotime($v['expiration_time']);
  65. if($valid_time < time()){
  66. Db::startTrans();
  67. try {
  68. DiscountCoupon::where('id',$v['id'])->update(['status'=>3]);
  69. Db::commit();
  70. }catch (Exception $exception){
  71. Db::rollback();
  72. $this->error($exception);
  73. }
  74. }
  75. }
  76. }
  77. }