選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

StorePromotionsJob.php 7.0KB

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