OrderCancel.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. use app\common\model\order\RiceCardOrder;
  16. use app\common\model\card\UserRiceCard;
  17. /**
  18. * 定时任务:商城订单
  19. * Class Order
  20. * @package app\console\task
  21. */
  22. class OrderCancel extends Task
  23. {
  24. // 当前任务唯一标识
  25. private $taskKey = 'OrderCancel';
  26. // 任务执行间隔时长 (单位:秒)
  27. protected $taskExpire = 30;
  28. // 当前商城ID
  29. private $storeId;
  30. /**
  31. * 任务处理
  32. * @param array $param
  33. * @throws \think\Exception
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function handle(array $param)
  39. {
  40. ['storeId' => $this->storeId] = $param;
  41. $this->setInterval($this->storeId, $this->taskKey, $this->taskExpire, function () {
  42. // echo $this->taskKey . PHP_EOL;
  43. $this->closeEvent();// 未支付订单自动关闭
  44. $this->closeRiceCardEvent();//超时自动关闭米卡订单
  45. $this->riceCardRevodeEvent();//超时自动撤回米卡
  46. });
  47. }
  48. /**
  49. * 未支付订单自动关闭
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function closeEvent()
  55. {
  56. // 自动关闭订单的分钟数
  57. $closeMinutes = (int)$this->getOrderCancelSetting()['close_minutes'];
  58. if(env('SERVE_ENV')=='test'){
  59. $closeMinutes = 5;//测试超时订单5分钟
  60. }
  61. // 执行自动关闭
  62. if ($closeMinutes > 0) {
  63. $service = new OrderService;
  64. $service->closeEvent($this->storeId, $closeMinutes);
  65. }
  66. }
  67. //关闭米卡订单
  68. private function closeRiceCardEvent(){
  69. // 自动关闭订单的分钟数
  70. $closeMinutes = (int)$this->getOrderCancelSetting()['close_minutes'];
  71. // 执行自动关闭
  72. if ($closeMinutes > 0) {
  73. $riceCardOrder = new RiceCardOrder;
  74. $riceCardOrder->closeEvent($this->storeId, $closeMinutes);
  75. }
  76. }
  77. //米卡撤回
  78. private function riceCardRevodeEvent(){
  79. $userRiceCard = new UserRiceCard;
  80. $minute = 60*24;//一天不领 给撤回
  81. // $minute = 5;
  82. $userRiceCard->revokeEvent($minute);
  83. }
  84. /**
  85. * 获取商城交易设置
  86. * @return array|mixed
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. private function getOrderCancelSetting()
  92. {
  93. return SettingModel::getItem('order_cancel', $this->storeId);
  94. }
  95. }