OrderCreateAfterJob.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\jobs\order;
  3. use app\services\order\StoreOrderCartInfoServices;
  4. use app\services\order\StoreOrderCreateServices;
  5. use app\services\order\StoreOrderComputedServices;
  6. use app\services\user\UserServices;
  7. use crmeb\basic\BaseJobs;
  8. use crmeb\services\CacheService;
  9. use crmeb\traits\QueueTrait;
  10. use think\facade\Log;
  11. /**
  12. * 订单创建
  13. * Class OrderCreateAfterJob
  14. * @package app\jobs
  15. */
  16. class OrderCreateAfterJob extends BaseJobs
  17. {
  18. use QueueTrait;
  19. /**
  20. * 清理订单确认生成缓存
  21. * @param int $uid
  22. * @param string $unique
  23. * @return bool
  24. * @throws \Psr\SimpleCache\InvalidArgumentException
  25. */
  26. public function delOrderCache(int $uid, string $unique)
  27. {
  28. CacheService::redisHandler()->delete('user_order_' . $uid . $unique);
  29. return true;
  30. }
  31. /**
  32. * 删除购物车和更新用户收货地址
  33. * @return bool
  34. */
  35. public function delCartAndUpdateAddres($orderInfo, $group)
  36. {
  37. try {
  38. /** @var StoreOrderCreateServices $orderCreate */
  39. $orderCreate = app()->make(StoreOrderCreateServices::class);
  40. $orderCreate->orderCreateAfter($orderInfo, $group);
  41. } catch (\Throwable $e) {
  42. Log::error('更新用户信息和删除购物车失败,失败原因:' . $e->getMessage());
  43. }
  44. return true;
  45. }
  46. /**
  47. * 订单后置处理
  48. * @param $userInfo
  49. * @param $orderInfo
  50. * @param $data
  51. * @param $activity
  52. * @return bool
  53. */
  54. public function compute($userInfo, $orderInfo, $data, $activity)
  55. {
  56. if (!$userInfo || !$orderInfo || $userInfo['uid'] != $orderInfo['uid']) {
  57. return true;
  58. }
  59. $uid = (int)$orderInfo['uid'];
  60. $orderId = (int)$orderInfo['id'];
  61. try {
  62. $spread_uid = $spread_two_uid = 0;
  63. $cartInfo = $data['cartInfo'] ?? [];
  64. $priceData = $data['priceData'] ?? [];
  65. $addressId = $data['addressId'] ?? 0;
  66. /** @var StoreOrderCreateServices $createService */
  67. $createService = app()->make(StoreOrderCreateServices::class);
  68. $spread_ids = [];
  69. if ($cartInfo && $priceData) {
  70. /** @var StoreOrderCartInfoServices $cartServices */
  71. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  72. [$cartInfo, $spread_ids] = $createService->computeOrderProductTruePrice($orderInfo, $cartInfo, $priceData, $addressId, $uid, $userInfo);
  73. $cartServices->updateCartInfo($orderId, $cartInfo);
  74. }
  75. $orderData = [];
  76. /** @var UserServices $userServices */
  77. $userServices = app()->make(UserServices::class);
  78. if ($spread_ids) {
  79. [$spread_uid, $spread_two_uid] = $spread_ids;
  80. } else {
  81. $spread_uid = $userServices->getSpreadUid($uid);
  82. if ($spread_uid > 0 && sys_config('brokerage_level', 2) == 2) {
  83. $spread_two_uid = $userServices->getSpreadUid($spread_uid, [], false);
  84. }
  85. }
  86. if ($spread_uid > 0) {
  87. $orderData['spread_uid'] = $spread_uid;
  88. }
  89. if ($spread_two_uid > 0) {
  90. $orderData['spread_two_uid'] = $spread_two_uid;
  91. }
  92. if ($cartInfo && (isset($activity['type']) && !in_array($activity['type'], [1, 2, 3, 5, 8]))) {
  93. /** @var StoreOrderComputedServices $orderComputed */
  94. $orderComputed = app()->make(StoreOrderComputedServices::class);
  95. if ($userServices->checkUserPromoter($spread_uid)) $orderData['one_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'one_brokerage', false);
  96. if ($userServices->checkUserPromoter($spread_two_uid)) $orderData['two_brokerage'] = $orderComputed->getOrderSumPrice($cartInfo, 'two_brokerage', false);
  97. }
  98. if ($orderData) $createService->update(['id' => $orderId], $orderData);
  99. } catch (\Throwable $e) {
  100. Log::error('计算订单实际优惠、积分、邮费、佣金失败,原因:' . $e->getMessage());
  101. }
  102. return true;
  103. }
  104. }