Order.php 3.9 KB

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