UnpaidOrderCancelJob.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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;
  12. use app\services\activity\seckill\StoreSeckillServices;
  13. use app\services\order\StoreOrderCartInfoServices;
  14. use app\services\order\StoreOrderRefundServices;
  15. use app\services\order\StoreOrderServices;
  16. use crmeb\basic\BaseJobs;
  17. use crmeb\traits\QueueTrait;
  18. use think\facade\Log;
  19. /**
  20. * 未支付订单到期取消
  21. * Class UnpaidOrderCancelJob
  22. * @package crmeb\jobs
  23. */
  24. class UnpaidOrderCancelJob extends BaseJobs
  25. {
  26. use QueueTrait;
  27. public function doJob($orderId)
  28. {
  29. /** @var StoreOrderServices $services */
  30. $services = app()->make(StoreOrderServices::class);
  31. $orderInfo = $services->get($orderId);
  32. if (!$orderInfo) {
  33. return true;
  34. }
  35. if ($orderInfo->paid) {
  36. return true;
  37. }
  38. if ($orderInfo->is_del) {
  39. return true;
  40. }
  41. if ($orderInfo->pay_type == 'offline') {
  42. return true;
  43. }
  44. /** @var StoreOrderCartInfoServices $cartServices */
  45. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  46. $cartInfo = $cartServices->getOrderCartInfo($orderId);
  47. /** @var StoreOrderRefundServices $refundServices */
  48. $refundServices = app()->make(StoreOrderRefundServices::class);
  49. try {
  50. $res = $refundServices->transaction(function () use ($orderInfo, $refundServices) {
  51. //回退积分和优惠卷
  52. $refundServices->integralAndCouponBack($orderInfo, 'cancel');
  53. //回退库存和销量
  54. $refundServices->regressionStock($orderInfo);
  55. return true;
  56. });
  57. if ($res) {
  58. $orderInfo->is_del = 1;
  59. $orderInfo->mark = '订单未支付已超过系统预设时间';
  60. $orderInfo->save();
  61. }
  62. return $res;
  63. } catch (\Throwable $e) {
  64. Log::error('自动取消订单失败,失败原因:' . $e->getMessage());
  65. return false;
  66. }
  67. }
  68. }