Book.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\console\task;
  13. use app\console\service\Book as OrderService;
  14. use app\console\model\Setting as SettingModel;
  15. /**
  16. * 定时任务:报名订单
  17. * Class Book
  18. * @package app\console\task
  19. */
  20. class Book extends Task
  21. {
  22. // 当前任务唯一标识
  23. private $taskKey = 'Book';
  24. // 任务执行间隔时长 (单位:秒)
  25. protected $taskExpire = 30;
  26. // protected $taskExpire = 60 * 30;
  27. // 当前商城ID
  28. private $storeId;
  29. /**
  30. * 任务处理
  31. * @param array $param
  32. * @throws \think\Exception
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function handle(array $param)
  38. {
  39. ['storeId' => $this->storeId] = $param;
  40. $this->setInterval($this->storeId, $this->taskKey, $this->taskExpire, function () {
  41. echo $this->taskKey . PHP_EOL;
  42. // 未支付订单自动关闭
  43. $this->closeEvent();
  44. // 已发货订单自动确认收货
  45. $this->receiveEvent();
  46. });
  47. }
  48. /**
  49. * 未支付订单自动关闭
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. private function closeEvent()
  55. {
  56. // 自动关闭订单的有效期
  57. $closeHours = (int)$this->getTradeSetting()['closeHours'];
  58. // 执行自动关闭
  59. if ($closeHours > 0) {
  60. $service = new OrderService;
  61. $service->closeEvent($this->storeId, $closeHours);
  62. }
  63. }
  64. /**
  65. * 自动确认收货订单的天数
  66. * @throws \think\Exception
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. private function receiveEvent()
  72. {
  73. // 取消n天以前的的未付款订单
  74. $receiveDays = (int)$this->getTradeSetting()['receive_days'];
  75. // 执行自动确认收货
  76. if ($receiveDays > 0) {
  77. $service = new OrderService;
  78. $service->receiveEvent($this->storeId, $receiveDays);
  79. }
  80. }
  81. /**
  82. * 获取商城交易设置
  83. * @return array|mixed
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. private function getTradeSetting()
  89. {
  90. return SettingModel::getItem('trade', $this->storeId)['order'];
  91. }
  92. }