OtherOrderJob.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\message\sms\SmsSendServices;
  13. use app\services\order\OtherOrderServices;
  14. use app\services\order\StoreOrderEconomizeServices;
  15. use app\services\user\member\MemberCardServices;
  16. use app\services\user\UserBillServices;
  17. use app\services\user\UserServices;
  18. use app\services\wechat\WechatUserServices;
  19. use crmeb\basic\BaseJobs;
  20. use crmeb\traits\QueueTrait;
  21. use think\facade\Log;
  22. /**
  23. * 订单消息队列
  24. * Class OtherOrderJob
  25. * @package app\jobs
  26. */
  27. class OtherOrderJob extends BaseJobs
  28. {
  29. use QueueTrait;
  30. /**
  31. * 执行订单支付成功发送消息
  32. * @param $order
  33. * @return bool
  34. */
  35. public function doJob($order)
  36. {
  37. //更新用户支付订单数量
  38. try {
  39. $this->setUserPayCountAndPromoter($order);
  40. } catch (\Throwable $e) {
  41. Log::error('更新用户订单数失败,失败原因:' . $e->getMessage());
  42. }
  43. // 计算用户节省金额
  44. try {
  45. $this->setEconomizeMoney($order);
  46. } catch (\Throwable $e) {
  47. Log::error('计算节省金额,失败原因:' . $e->getMessage());
  48. }
  49. try {
  50. $this->sendMemberIntegral($order);
  51. } catch (\Throwable $e) {
  52. Log::error('消费积分返还失败,失败原因:' . $e->getMessage());
  53. }
  54. return true;
  55. }
  56. /**
  57. * 设置用户购买次数和检测时候成为推广人
  58. * @param $order
  59. */
  60. public function setUserPayCountAndPromoter($order)
  61. {
  62. /** @var UserServices $userServices */
  63. $userServices = app()->make(UserServices::class);
  64. $userInfo = $userServices->get($order['uid']);
  65. if ($userInfo) {
  66. if (!$userInfo->is_promoter) {
  67. /** @var OtherOrderServices $orderServices */
  68. $orderServices = app()->make(OtherOrderServices::class);
  69. $price = $orderServices->sum(['paid' => 1, 'uid' => $userInfo['uid']], 'pay_price');
  70. $status = is_brokerage_statu($price);
  71. if ($status) {
  72. $userInfo->is_promoter = 1;
  73. }
  74. }
  75. $userInfo->save();
  76. }
  77. }
  78. /**
  79. * 发送模板消息和客服消息
  80. * @param $order
  81. * @return bool
  82. */
  83. public function sendServicesAndTemplate($order)
  84. {
  85. try {
  86. /** @var WechatUserServices $wechatUserServices */
  87. $wechatUserServices = app()->make(WechatUserServices::class);
  88. if ($order['is_channel'] == "wechat") {//公众号发送模板消息
  89. $openid = $wechatUserServices->uidToOpenid($order['uid'], 'wechat');
  90. if (!$openid) {
  91. return true;
  92. }
  93. $wechatTemplate = new WechatTemplateJob();
  94. $wechatTemplate->sendOrderPaySuccess($openid, $order);
  95. } else if ($order['is_channel'] == "routine") {//小程序发送模板消息
  96. $openid = $wechatUserServices->uidToOpenid($order['uid'], 'routine');
  97. if (!$openid) {
  98. return true;
  99. }
  100. $tempJob = new RoutineTemplateJob();
  101. $tempJob->sendMemberOrderSuccess($openid, $order['pay_price'], $order['order_id']);
  102. }
  103. } catch (\Exception $e) {
  104. }
  105. }
  106. /**
  107. * 支付成功短信提醒
  108. * @param string $order_id
  109. */
  110. public function mssageSendPaySuccess($order)
  111. {
  112. $switch = (bool)sys_config('lower_order_switch');
  113. //模板变量
  114. $pay_price = $order['pay_price'];
  115. $order_id = $order['order_id'];
  116. /** @var SmsSendServices $smsServices */
  117. $smsServices = app()->make(SmsSendServices::class);
  118. /** @var UserServices $userService */
  119. $userService = app()->make(UserServices::class);
  120. $userPhone = $userService->value(['uid' => $order['uid']], 'phone');
  121. if ($userPhone) {
  122. $smsServices->send($switch, $userPhone, compact('order_id', 'pay_price'), 'PAY_SUCCESS_CODE');
  123. }
  124. }
  125. /**
  126. * 购买会员赠送优惠券
  127. * @param $uid
  128. */
  129. public function setCoupon($uid)
  130. {
  131. // /** @var StoreCouponIssueServices $couponService */
  132. // $couponService = app()->make(StoreCouponIssueServices::class);
  133. // $couponInfo = $couponService->sendMemberCoupon($uid);
  134. // if (!$couponInfo) Log::error('优惠券发送失败,失败原因:没发现会员优惠券或者优惠券数量为0');
  135. }
  136. /**
  137. * 线下付款奖励积分
  138. * @param $order
  139. * @return bool
  140. */
  141. public function sendMemberIntegral($order)
  142. {
  143. //只有线下付款才奖励
  144. if ($order['type'] == 3) {
  145. $order_give_integral = sys_config('order_give_integral');
  146. $order_integral = bcmul($order_give_integral, (string)$order['pay_price'], 0);
  147. /** @var UserServices $userService */
  148. $userService = app()->make(UserServices::class);
  149. $userInfo = $userService->getUserInfo($order['uid']);
  150. if (!$userInfo) return false;
  151. if ($userInfo['is_money_level'] > 0) {
  152. //看是否开启消费返积分翻倍奖励
  153. /** @var MemberCardServices $memberCardService */
  154. $memberCardService = app()->make(MemberCardServices::class);
  155. $integral_rule_number = $memberCardService->isOpenMemberCardCache('integral');
  156. if ($integral_rule_number) {
  157. $order_integral = bcadd($order_integral, $integral_rule_number, 2);
  158. }
  159. }
  160. if ($order_integral > 0) {
  161. $balance = bcadd(abs($userInfo['integral']), abs($order_integral), 0);
  162. $userService->update(['uid' => $order['uid']], ['integral' => $balance]);
  163. /** @var UserBillServices $userBillServices */
  164. $userBillServices = app()->make(UserBillServices::class);
  165. $userBillServices->income('order_give_integral', $order['uid'], (int)$order_integral, (int)$balance, $order['id']);
  166. }
  167. }
  168. }
  169. /**
  170. * @param $order
  171. */
  172. public function setEconomizeMoney($order)
  173. {
  174. //只有线下付款才计算节省
  175. if ($order['type'] == 3) {
  176. /** @var StoreOrderEconomizeServices $economizeService */
  177. $economizeService = app()->make(StoreOrderEconomizeServices::class);
  178. /** @var MemberCardServices $memberRightService */
  179. $memberRightService = app()->make(MemberCardServices::class);
  180. $isOpenOfflin = $memberRightService->isOpenMemberCardCache('offline');
  181. if ($isOpenOfflin) {
  182. $save = [
  183. 'uid' => $order['uid'],
  184. 'order_id' => $order['order_id'],
  185. 'order_type' => 2,
  186. 'pay_price' => $order['pay_price'],
  187. 'offline_price' => bcsub($order['money'], $order['pay_price'], 2)
  188. ];
  189. $economizeService->addEconomize($save);
  190. }
  191. }
  192. }
  193. }