OrderPayHandelJob.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\StoreOrderCartInfoServices;
  13. use app\services\order\StoreOrderServices;
  14. use app\services\order\StoreOrderStatusServices;
  15. use app\services\product\sku\StoreProductAttrValueServices;
  16. use app\services\product\sku\StoreProductVirtualServices;
  17. use crmeb\basic\BaseJobs;
  18. use crmeb\traits\QueueTrait;
  19. use think\facade\Log;
  20. /**
  21. * 支付成功:自动发送卡密、处理次卡商品核销信息
  22. * Class OrderPayHandelJob
  23. * @package app\jobs\order
  24. */
  25. class OrderPayHandelJob extends BaseJobs
  26. {
  27. use QueueTrait;
  28. /**
  29. * @param $orderInfo
  30. * @return bool
  31. */
  32. public function doJob($orderInfo)
  33. {
  34. if (!$orderInfo) {
  35. return true;
  36. }
  37. //待发货状态
  38. if ($orderInfo['status'] != 0) {
  39. return true;
  40. }
  41. /** @var StoreOrderServices $orderService */
  42. $orderService = app()->make(StoreOrderServices::class);
  43. /** @var StoreOrderStatusServices $statusService */
  44. $statusService = app()->make(StoreOrderStatusServices::class);
  45. /** @var StoreOrderCartInfoServices $services */
  46. $services = app()->make(StoreOrderCartInfoServices::class);
  47. $orderInfo['cart_info'] = $cartInfoList = $services->getOrderCartInfo((int)$orderInfo['id']);
  48. try {
  49. switch ($orderInfo['product_type']) {
  50. case 1://卡密商品:自动发送
  51. $title = $content = $disk_info = $virtual_info = '';
  52. $disk_info = $orderInfo['cart_info'][$orderInfo['cart_id'][0] ?? 0]['cart_info']['productInfo']['attrInfo']['disk_info'] ?? '';
  53. $unique = $orderInfo['cart_info'][$orderInfo['cart_id'][0]]['cart_info']['productInfo']['attrInfo']['unique'];
  54. //活动订单共用原商品规格卡密
  55. if (in_array($orderInfo['type'], [1, 2, 3, 5]) && $orderInfo['activity_id']) {
  56. /** @var StoreProductAttrValueServices $skuValueServices */
  57. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  58. $attrValue = $skuValueServices->getUniqueByActivityUnique($unique, (int)$orderInfo['activity_id'], (int)$orderInfo['type'], ['unique', 'disk_info']);
  59. if ($attrValue) {
  60. $disk_info = $attrValue['disk_info'] ?? '';
  61. $unique = $attrValue['unique'] ?? '';
  62. }
  63. }
  64. if ($disk_info) {
  65. $title = '虚拟密钥发放';
  66. $content = '您购买的密钥商品已支付成功,支付金额' . $orderInfo['pay_price'] . '元,订单号:' . $orderInfo['order_id'] . ',密钥:' . $disk_info . ',感谢您的光临!';
  67. $virtual_info = '密钥自动发放:' . $disk_info;
  68. $value = '密钥:' . $disk_info;
  69. // $remark = '密钥自动发放:' . $disk_info;
  70. } else {
  71. /** @var StoreProductVirtualServices $virtualService */
  72. $virtualService = app()->make(StoreProductVirtualServices::class);
  73. $cardList = $virtualService->getOrderCardList(['store_id' => $orderInfo['store_id'], 'attr_unique' => $unique, 'uid' => 0], (int)$orderInfo['total_num']);
  74. $title = '虚拟卡密发放';
  75. $virtual_info = '卡密已自动发放,';
  76. // $remark = '卡密已自动发放';
  77. $value = '';
  78. if ($cardList) {
  79. $content = '您购买的卡密商品已支付成功,支付金额' . $orderInfo['pay_price'] . '元,订单号:' . $orderInfo['order_id'] . ',';
  80. $update = [];
  81. $update['order_id'] = $orderInfo['order_id'];
  82. $update['uid'] = $orderInfo['uid'];
  83. foreach ($cardList as $virtual) {
  84. $virtualService->update($virtual['id'], $update);
  85. $content .= '卡号:' . $virtual['card_no'] . ';密码:' . $virtual['card_pwd'] . "\n";
  86. $virtual_info .= '卡号:' . $virtual['card_no'] . ';密码:' . $virtual['card_pwd'] . ';';
  87. $value .= '卡号:' . $virtual['card_no'] . ';密码:' . $virtual['card_pwd'];
  88. // $remark .= ',卡号:' . $virtual['card_no'] . ';密码:' . $virtual['card_pwd'] . ';';
  89. }
  90. $content .= ',感谢您的光临!';
  91. }
  92. }
  93. //修改订单虚拟备注
  94. $orderService->update(['id' => $orderInfo['id']], ['status' => 1, 'delivery_type' => 'fictitious', 'virtual_info' => $virtual_info]);
  95. $data['id'] = $orderInfo['id'];
  96. $data['uid'] = $orderInfo['uid'];
  97. $data['order_id'] = $orderInfo['order_id'];
  98. $data['title'] = $title;
  99. $data['value'] = $value;
  100. $data['content'] = $content;
  101. $data['is_integral'] = 0;
  102. event('notice.notice', [$data, 'kami_deliver_goods_code']);
  103. $statusService->save([
  104. 'oid' => $orderInfo['id'],
  105. 'change_type' => 'delivery_fictitious',
  106. 'change_message' => '卡密自动发货',
  107. 'change_time' => time()
  108. ]);
  109. break;
  110. case 4://次卡商品:处理核销数据
  111. foreach ($cartInfoList as $cart_id => $cartInfo) {
  112. $info = $cartInfo['cart_info'] ?? [];
  113. $attrInfo = $info['productInfo']['attrInfo'] ?? [];
  114. $data = ['write_start' => 0, 'write_end' => 0];
  115. if ($attrInfo) {
  116. switch ($attrInfo['write_valid'] ?? 1) {
  117. case 1://永久
  118. break;
  119. case 2://购买后n天
  120. if (isset($attrInfo['write_days']) && $attrInfo['write_days']) {
  121. $data['write_start'] = time();
  122. $data['write_end'] = bcadd((string)$data['write_start'], (string)bcmul((string)$attrInfo['write_days'], '86400'));
  123. }
  124. break;
  125. case 3://具体时间段
  126. if ((isset($attrInfo['write_start']) && $attrInfo['write_start']) || (isset($attrInfo['write_end']) && $attrInfo['write_end'])) {
  127. $data['write_start'] = $attrInfo['write_start'] ?? 0;
  128. $data['write_end'] = $attrInfo['write_end'] ?? 0;
  129. }
  130. break;
  131. }
  132. }
  133. $services->update(['oid' => $orderInfo['id'], 'cart_id' => $cart_id], $data);
  134. }
  135. break;
  136. }
  137. } catch (\Throwable $e) {
  138. Log::error('订单虚拟商品自动发放失败,原因:' . $e->getMessage());
  139. }
  140. return true;
  141. }
  142. }