GiveGift.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace addons\shopro\library\activity\traits;
  3. use think\Db;
  4. use addons\shopro\traits\CouponSend;
  5. use app\admin\model\shopro\activity\GiftLog;
  6. use addons\shopro\service\Wallet as WalletService;
  7. /**
  8. * 赠送赠品 (full_gift, groupon_lucky 幸运拼团未拼中)
  9. */
  10. trait GiveGift
  11. {
  12. use CouponSend;
  13. /**
  14. * 按照规则添加赠送日志
  15. * @param array|object $order
  16. * @param array|object $user
  17. * @param array $info {"full":"100","types":"coupon=优惠券|score=积分|money=余额|goods=商品","coupon_ids":"赠优惠券时存在","total":"赠送优惠券总金额","score":"积分","money":"余额","goods_ids":"商品时存在",gift_num:"礼品份数"}
  18. * @return void
  19. */
  20. public function addGiftsLog($order, $user, $info)
  21. {
  22. $rules = $info['discount_rule'];
  23. Db::transaction(function () use ($order, $user, $info, $rules) {
  24. $types = $rules['types'];
  25. foreach ($types as $type) {
  26. extract($this->getTypeGift($rules, $type));
  27. $giftLog = new GiftLog();
  28. $giftLog->activity_id = $info['activity_id'];
  29. $giftLog->order_id = $order->id;
  30. $giftLog->user_id = $user->id;
  31. $giftLog->type = $type;
  32. $giftLog->gift = $gift;
  33. $giftLog->value = $value;
  34. $giftLog->rules = $rules;
  35. $giftLog->status = 'waiting';
  36. $giftLog->save();
  37. }
  38. });
  39. }
  40. /**
  41. * 标记礼品为赠送失败
  42. */
  43. public function checkAndFailGift($order, $fail_msg, $errors = null)
  44. {
  45. // 找到所有没有赠送的礼品,设置为 fail,fail_msg 订单退款
  46. $giftLogs = GiftLog::waiting()->where('order_id', $order->id)->lock(true)->select();
  47. foreach ($giftLogs as $giftLog) {
  48. $giftLog->status = 'fail';
  49. $giftLog->fail_msg = $fail_msg;
  50. $giftLog->errors = $errors;
  51. $giftLog->save();
  52. }
  53. }
  54. /**
  55. * 检查并赠送礼品
  56. *
  57. * @param array|object $order
  58. * @param array|object $user
  59. * @param array $promoInfos
  60. * @param string $event
  61. * @return void
  62. */
  63. public function checkAndGift($order, $user, $promoInfos, $event)
  64. {
  65. foreach ($promoInfos as $info) {
  66. if ($info['activity_type'] == 'full_gift') {
  67. $this->checkPromoAndGift($order, $user, $info, $event);
  68. }
  69. }
  70. }
  71. /**
  72. * 检查并赠送礼品
  73. *
  74. * @param array|object $order
  75. * @param array|object $user
  76. * @param array $infos
  77. * @param string $event
  78. * @return void
  79. */
  80. public function checkPromoAndGift($order, $user, $info, $event)
  81. {
  82. if ($info['event'] == $event) {
  83. // 判断领取次数
  84. $rules = $info['discount_rule'];
  85. $gift_num = $rules['gift_num']; // 礼品数量
  86. // 查询已发放数量
  87. $send_num = GiftLog::where('activity_id', $info['activity_id'])->opered()->group('order_id')->count();
  88. $giftLogs = GiftLog::waiting()
  89. ->where('activity_id', $info['activity_id'])
  90. ->where('order_id', $order->id)
  91. ->select();
  92. if ($send_num >= $gift_num) {
  93. // 礼品已经发放完毕
  94. foreach ($giftLogs as $log) {
  95. $log->status = 'fail';
  96. $log->fail_msg = '礼品已经发完了';
  97. $log->save();
  98. }
  99. return false;
  100. }
  101. // 查询当前用户已领取数量 (只算赠送成功的)
  102. $user_send_num = GiftLog::where('user_id', $order->user_id)->where('activity_id', $info['activity_id'])->finish()->group('order_id')->count();
  103. if ($info['limit_num'] > 0 && $user_send_num >= $info['limit_num']) {
  104. // 已经领取过了
  105. foreach ($giftLogs as $log) {
  106. $log->status = 'fail';
  107. $log->fail_msg = '已经领取过了,每人最多领取 ' . $info['limit_num'] . ' 份';
  108. $log->save();
  109. }
  110. return false;
  111. }
  112. // 赠送礼品
  113. foreach ($giftLogs as $giftLog) {
  114. $this->{'gift' . $giftLog->type}($user, $giftLog);
  115. }
  116. }
  117. }
  118. /**
  119. * 赠送优惠券
  120. *
  121. * @param array|object $user
  122. * @param array|object $giftLog
  123. * @return void
  124. */
  125. public function giftCoupon($user, $giftLog)
  126. {
  127. $couponIds = explode(',', $giftLog->gift);
  128. $result = $this->giveCoupons($user, $couponIds);
  129. $giftLog->status = 'finish';
  130. if ($result['errors']) {
  131. $giftLog->status = 'fail';
  132. $giftLog->fail_msg = $result['success'] ? '优惠券部分发放成功' : '优惠券发放失败';
  133. $giftLog->errors = $result['errors'];
  134. }
  135. $giftLog->save();
  136. }
  137. /**
  138. * 赠送积分
  139. *
  140. * @param array|object $user
  141. * @param array|object $giftLog
  142. * @return void
  143. */
  144. public function giftScore($user, $giftLog)
  145. {
  146. $score = $giftLog->gift;
  147. // 增加用户积分
  148. WalletService::change($user, 'score', $score, 'activity_gift', [
  149. 'activity_id' => $giftLog->activity_id,
  150. 'order_id' => $giftLog->order_id,
  151. 'user_id' => $giftLog->user_id,
  152. 'type' => $giftLog->type,
  153. 'gift' => $giftLog->gift,
  154. 'value' => $giftLog->value,
  155. ]);
  156. $giftLog->status = 'finish';
  157. $giftLog->save();
  158. }
  159. /**
  160. * 赠送余额
  161. *
  162. * @param array|object $user
  163. * @param array|object $giftLog
  164. * @return void
  165. */
  166. public function giftMoney($user, $giftLog)
  167. {
  168. $money = $giftLog->gift;
  169. // 增加用户余额
  170. WalletService::change($user, 'money', $money, 'activity_gift', [
  171. 'activity_id' => $giftLog->activity_id,
  172. 'order_id' => $giftLog->order_id,
  173. 'user_id' => $giftLog->user_id,
  174. 'type' => $giftLog->type,
  175. 'gift' => $giftLog->gift,
  176. 'value' => $giftLog->value,
  177. ]);
  178. $giftLog->status = 'finish';
  179. $giftLog->save();
  180. }
  181. /**
  182. * 赠送商品(暂不开发)
  183. *
  184. * @param array|object $user
  185. * @param array|object $giftLog
  186. * @return void
  187. */
  188. public function giftGoods($user, $giftLog)
  189. {
  190. $goodsIds = explode(',', $giftLog->gift);
  191. // 赠送商品,暂不开发
  192. $giftLog->status = 'finish';
  193. $giftLog->save();
  194. }
  195. /**
  196. * 获取赠送的 gift 和价值
  197. *
  198. * @param array $rules
  199. * @param string $type
  200. * @return array
  201. */
  202. private function getTypeGift($rules, $type)
  203. {
  204. $gift = null;
  205. switch ($type) {
  206. case 'coupon':
  207. $gift = $rules['coupon_ids'];
  208. $value = $rules['total'];
  209. break;
  210. case 'score':
  211. $gift = $rules['score'];
  212. $value = $rules['score'];
  213. break;
  214. case 'money':
  215. $gift = $rules['money'];
  216. $value = $rules['money'];
  217. break;
  218. case 'goods':
  219. $gift = $rules['goods_ids'];
  220. $value = $rules['goods_ids'];
  221. break;
  222. }
  223. return compact('gift', 'value');
  224. }
  225. }