StorePromotionsJob.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\activity;
  12. use app\services\user\UserServices;
  13. use app\services\user\UserBillServices;
  14. use app\services\user\label\UserLabelRelationServices;
  15. use app\services\activity\coupon\StoreCouponIssueServices;
  16. use app\services\activity\promotions\StorePromotionsAuxiliaryServices;
  17. use crmeb\basic\BaseJobs;
  18. use crmeb\traits\QueueTrait;
  19. use think\facade\Log;
  20. /**
  21. * 营销:优惠活动
  22. * Class StorePromotionsJob
  23. * @package app\jobs\activity
  24. */
  25. class StorePromotionsJob extends BaseJobs
  26. {
  27. use QueueTrait;
  28. /**
  29. * 赠送
  30. * @param $orderInfo
  31. * @return bool
  32. */
  33. public function give($orderInfo)
  34. {
  35. $uid = (int)$orderInfo['uid'];
  36. $promotions_give = [];
  37. if (isset($orderInfo['promotions_give']) && $orderInfo['promotions_give']) {
  38. $promotions_give = is_string($orderInfo['promotions_give']) ? json_decode($promotions_give, true) : $orderInfo['promotions_give'];
  39. }
  40. $give_integral = $promotions_give['give_integral'] ?? 0;
  41. if ($give_integral) {
  42. try {
  43. /** @var UserServices $userServices */
  44. $userServices = app()->make(UserServices::class);
  45. $userInfo = $userServices->getUserInfo($uid);
  46. /** @var UserBillServices $userBillServices */
  47. $userBillServices = app()->make(UserBillServices::class);
  48. $balance = bcadd((string)$userInfo['integral'], (string)$give_integral, 0);
  49. $userServices->update(['uid' => $userInfo['uid']], ['integral' => $balance]);
  50. $userBillServices->income('order_promotions_give_integral', $uid, (int)$give_integral, (int)$balance, $orderInfo['id']);
  51. } catch (\Throwable $e) {
  52. Log::error('优惠活动下单赠送积分失败,失败原因:' . $e->getMessage());
  53. }
  54. }
  55. $give_coupon = $promotions_give['give_coupon'] ?? [];
  56. $this->giveCoupon($uid, $give_coupon);
  57. return true;
  58. }
  59. /**
  60. * 赠送优惠券
  61. * @param int $uid
  62. * @param array $give_coupon
  63. * @return bool
  64. */
  65. public function giveCoupon(int $uid, array $give_coupon)
  66. {
  67. if ($give_coupon) {
  68. try {
  69. /** @var StoreCouponIssueServices $storeCoupon */
  70. $storeCoupon = app()->make(StoreCouponIssueServices::class);
  71. $storeCoupon->orderPayGiveCoupon($uid, $give_coupon);
  72. } catch (\Throwable $e) {
  73. Log::error('优惠活动下单赠送优惠券失败,失败原因:' . $e->getMessage());
  74. }
  75. }
  76. return true;
  77. }
  78. /**
  79. * 扣除优惠活动赠品限量
  80. * @param array $promotions_give
  81. * @param bool $isDec
  82. * @rerunt bool
  83. */
  84. public function changeGiveLimit(array $promotions_give, bool $isDec = true)
  85. {
  86. if ($promotions_give){
  87. try {
  88. $promotionsArr = $promotions_give['promotions'] ?? [];
  89. if ($promotionsArr) {
  90. /** @var StorePromotionsAuxiliaryServices $storePromotionsAuxiliaryServices */
  91. $storePromotionsAuxiliaryServices = app()->make(StorePromotionsAuxiliaryServices::class);
  92. $giveCoupon = $promotions_give['give_coupon'] ?? [];
  93. $getPromotionsId = function($id, $key) use ($promotionsArr) {
  94. $pid = 0;
  95. foreach($promotionsArr as $promotions) {
  96. $k = $key == 'coupon_id' ? 'giveCoupon' : 'giveProducts';
  97. $arr = $promotions[$k] ?? [];
  98. $ids = [];
  99. if ($arr) $ids = array_column($arr, $key);
  100. if ($ids && in_array($id, $ids)) $pid = $promotions['id'] ?? $promotionsArr['id'] ?? 0;
  101. }
  102. return $pid;
  103. };
  104. if ($giveCoupon) {
  105. foreach ($giveCoupon as $coupon_id) {
  106. $promotions_id = $getPromotionsId($coupon_id, 'coupon_id');
  107. if ($promotions_id) $storePromotionsAuxiliaryServices->updateLimit([$promotions_id], 2, (int)$coupon_id, $isDec);
  108. }
  109. }
  110. $giveProduct = $promotions_give['give_product'] ?? [];
  111. if ($giveProduct) {
  112. foreach ($giveProduct as $give) {
  113. $promotions_id = (int)$give['promotions_id'] ?? 0;
  114. $product_id = (int)$give['product_id'] ?? 0;
  115. $unique = $give['unique'] ?? '';
  116. $cart_num = (int)$give['cart_num'] ?? 1;
  117. if ($promotions_id && $product_id && $unique) $storePromotionsAuxiliaryServices->updateLimit([$promotions_id], 3, (int)$product_id, $isDec, $unique, $cart_num);
  118. }
  119. }
  120. }
  121. } catch (\Throwable $e) {
  122. Log::error('订单创建优惠活动赠品限量扣除失败,失败原因:' . $e->getMessage());
  123. }
  124. }
  125. return true;
  126. }
  127. /**
  128. * 设置用户购买的标签
  129. * @param $orderInfo
  130. */
  131. public function setUserLabel($orderInfo)
  132. {
  133. try {
  134. $promotions_give = [];
  135. if (isset($orderInfo['promotions_give']) && $orderInfo['promotions_give']) {
  136. $promotions_give = is_string($orderInfo['promotions_give']) ? json_decode($promotions_give, true) : $orderInfo['promotions_give'];
  137. }
  138. $promotions = $promotions_give['promotions'] ?? [];
  139. if (!$promotions) {
  140. return true;
  141. }
  142. $labelIds = [];
  143. foreach ($promotions as $key => $value) {
  144. $label_id = is_string($value['label_id']) ? explode(',', $value['label_id']) : $value['label_id'];
  145. $labelIds = array_merge($labelIds, $label_id);
  146. }
  147. if (!$labelIds) {
  148. return true;
  149. }
  150. $labelIds = array_unique($labelIds);
  151. $store_id = $orderInfo['store_id'] ?? 0;
  152. $type = $store_id ? 1 : 0;
  153. /** @var UserLabelRelationServices $labelServices */
  154. $labelServices = app()->make(UserLabelRelationServices::class);
  155. $labelServices->setUserLable([$orderInfo['uid']], $labelIds, $type, $store_id);
  156. } catch (\Throwable $e) {
  157. Log::error('用户标签添加失败,失败原因:' . $e->getMessage());
  158. }
  159. return true;
  160. }
  161. }