Order.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }
  47. /**
  48. * 未支付订单自动关闭
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. private function closeEvent()
  54. {
  55. // 自动关闭订单的天数
  56. $closeDays = (int)$this->getTradeSetting()['close_days'];
  57. // 执行自动关闭
  58. if ($closeDays > 0) {
  59. $service = new OrderService;
  60. $service->closeEvent($this->storeId, $closeDays);
  61. }
  62. }
  63. /**
  64. * 自动确认收货订单的天数
  65. * @throws \think\Exception
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. private function receiveEvent()
  71. {
  72. // 取消n天以前的的未付款订单
  73. $receiveDays = (int)$this->getTradeSetting()['receive_days'];
  74. // 执行自动确认收货
  75. if ($receiveDays > 0) {
  76. $service = new OrderService;
  77. $service->receiveEvent($this->storeId, $receiveDays);
  78. }
  79. }
  80. /**
  81. * 获取商城交易设置
  82. * @return array|mixed
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. private function getTradeSetting()
  88. {
  89. return SettingModel::getItem('trade', $this->storeId)['order'];
  90. }
  91. }