UnpaidOrderCancelJob.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs\order;
  12. use app\services\order\StoreOrderRefundServices;
  13. use app\services\order\StoreOrderServices;
  14. use crmeb\basic\BaseJobs;
  15. use crmeb\traits\QueueTrait;
  16. use think\facade\Log;
  17. /**
  18. * 未支付订单到期取消
  19. * Class UnpaidOrderCancelJob
  20. * @package app\jobs
  21. */
  22. class UnpaidOrderCancelJob extends BaseJobs
  23. {
  24. use QueueTrait;
  25. /**
  26. * @param $orderId
  27. * @return bool|mixed
  28. */
  29. public function doJob($orderId)
  30. {
  31. /** @var StoreOrderServices $services */
  32. $services = app()->make(StoreOrderServices::class);
  33. $orderInfo = $services->get($orderId);
  34. if (!$orderInfo) {
  35. return true;
  36. }
  37. if ($orderInfo->paid) {
  38. return true;
  39. }
  40. if ($orderInfo->is_del) {
  41. return true;
  42. }
  43. if ($orderInfo->pay_type == 'offline') {
  44. return true;
  45. }
  46. /** @var StoreOrderRefundServices $refundServices */
  47. $refundServices = app()->make(StoreOrderRefundServices::class);
  48. try {
  49. $res = $refundServices->transaction(function () use ($orderInfo, $refundServices) {
  50. //回退积分和优惠卷
  51. $refundServices->integralAndCouponBack($orderInfo);
  52. //回退库存和销量
  53. $refundServices->regressionStock($orderInfo);
  54. return true;
  55. });
  56. if ($res) {
  57. $update = ['is_del' => 1, 'mark' => '订单未支付已超过系统预设时间'];
  58. $services->update((int)$orderInfo['id'], $update);
  59. }
  60. return $res;
  61. } catch (\Throwable $e) {
  62. Log::error('自动取消订单失败,失败原因:' . $e->getMessage());
  63. return false;
  64. }
  65. }
  66. }