Order.php 3.7 KB

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