OtherOrderJob.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\order\OtherOrderServices;
  13. use app\services\order\StoreOrderEconomizeServices;
  14. use app\services\user\member\MemberCardServices;
  15. use app\services\user\UserServices;
  16. use crmeb\basic\BaseJobs;
  17. use crmeb\traits\QueueTrait;
  18. use think\facade\Log;
  19. /**
  20. * 订单消息队列
  21. * Class OrderJob
  22. * @package crmeb\jobs
  23. */
  24. class OtherOrderJob extends BaseJobs
  25. {
  26. use QueueTrait;
  27. /**
  28. * 执行订单支付成功发送消息
  29. * @param $order
  30. * @return bool
  31. */
  32. public function doJob($order)
  33. {
  34. //更新用户支付订单数量
  35. try {
  36. $this->setUserPayCountAndPromoter($order);
  37. } catch (\Throwable $e) {
  38. Log::error('更新用户订单数失败,失败原因:' . $e->getMessage());
  39. }
  40. // 计算用户节省金额
  41. try {
  42. $this->setEconomizeMoney($order);
  43. } catch (\Throwable $e) {
  44. Log::error('计算节省金额,失败原因:' . $e->getMessage());
  45. }
  46. //收银订单赠送积分
  47. try {
  48. $this->sendMemberIntegral($order);
  49. } catch (\Throwable $e) {
  50. Log::error('消费积分返还失败,失败原因:' . $e->getMessage());
  51. }
  52. return true;
  53. }
  54. /**
  55. * 设置用户购买次数和检测时候成为推广人
  56. * @param $order
  57. */
  58. public function setUserPayCountAndPromoter($order)
  59. {
  60. /** @var UserServices $userServices */
  61. $userServices = app()->make(UserServices::class);
  62. $userInfo = $userServices->get($order['uid']);
  63. if ($userInfo) {
  64. $userInfo->pay_count = $userInfo->pay_count + 1;
  65. if (!$userInfo->is_promoter) {
  66. /** @var OtherOrderServices $orderServices */
  67. $orderServices = app()->make(OtherOrderServices::class);
  68. $price = $orderServices->sum(['paid' => 1, 'uid' => $userInfo['uid']], 'pay_price');
  69. $status = is_brokerage_statu($price);
  70. if ($status) {
  71. $userInfo->is_promoter = 1;
  72. }
  73. }
  74. $userInfo->save();
  75. }
  76. }
  77. /** 线下付款奖励积分
  78. * @param $order
  79. * @return bool
  80. */
  81. public function sendMemberIntegral($order)
  82. {
  83. //只有线下付款才奖励
  84. if ($order['type'] == 3) {
  85. $order_give_integral = sys_config('order_give_integral');
  86. $order_integral = bcmul($order_give_integral, (string)$order['pay_price'], 0);
  87. /** @var UserServices $userService */
  88. $userService = app()->make(UserServices::class);
  89. $userInfo = $userService->getUserInfo($order['uid']);
  90. if (!$userInfo) return false;
  91. if ($userInfo['is_money_level'] > 0) {
  92. //看是否开启消费返积分翻倍奖励
  93. /** @var MemberCardServices $memberCardService */
  94. $memberCardService = app()->make(MemberCardServices::class);
  95. $integral_rule_number = $memberCardService->isOpenMemberCard('integral');
  96. if ($integral_rule_number) {
  97. $order_integral = bcadd($order_integral, $integral_rule_number, 2);
  98. }
  99. }
  100. if ($order_integral > 0) {
  101. $integral = bcadd(abs($userInfo['integral']), abs($order_integral), 2);
  102. $userService->update(['uid' => $order['uid']], ['integral' => $integral]);
  103. }
  104. }
  105. }
  106. /**
  107. * 计算节省金额
  108. * @param $order
  109. */
  110. public function setEconomizeMoney($order)
  111. {
  112. //只有线下付款才计算节省
  113. if ($order['type'] == 3) {
  114. /** @var StoreOrderEconomizeServices $economizeService */
  115. $economizeService = app()->make(StoreOrderEconomizeServices::class);
  116. /** @var MemberCardServices $memberRightService */
  117. $memberRightService = app()->make(MemberCardServices::class);
  118. $isOpenOfflin = $memberRightService->isOpenMemberCard('offline');
  119. if ($isOpenOfflin) {
  120. $save = [
  121. 'uid' => $order['uid'],
  122. 'order_id' => $order['order_id'],
  123. 'order_type' => 2,
  124. 'pay_price' => $order['pay_price'],
  125. 'offline_price' => bcsub($order['money'], $order['pay_price'], 2)
  126. ];
  127. $economizeService->addEconomize($save);
  128. }
  129. }
  130. }
  131. }