Order.php 3.7 KB

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