NewCustomerEvent.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\event\order;
  3. use app\admin\service\coupon\CouponDetailService;
  4. use app\model\Coupon;
  5. use app\model\CouponDetail;
  6. use app\model\Goods;
  7. use app\model\Member;
  8. use app\model\MemberAccount;
  9. use app\model\PayDetail;
  10. use app\model\SysSerial;
  11. use support\Db;
  12. use support\exception\BusinessException;
  13. use support\Log;
  14. class NewCustomerEvent
  15. {
  16. public function grant($params)
  17. {
  18. Db::beginTransaction();
  19. try {
  20. Log::info("进入新客专享事件", $params);
  21. // 余额账户
  22. $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])->where('member_account_classify', 'CASH')->first();
  23. // 分期付款
  24. if (isset($params['order_amount_paid']) && $params['order_amount_paid'] > 0) {
  25. $params['order_amount_pay'] = $params['order_amount_paid'] + $params['order_amount_pay'];
  26. }
  27. //发放优惠券
  28. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])->select('goods_id', 'goods_name', 'goods_attribute_json')->first();
  29. if (!empty($goods) && !empty($goods->goods_attribute_json)) {
  30. $goodsAttributeJson = json_decode($goods->goods_attribute_json, true);
  31. if (isset($goodsAttributeJson['account'])) {
  32. $memberAccount->member_account_income = $memberAccount->member_account_income + $goodsAttributeJson['account']['amount'];
  33. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $goodsAttributeJson['account']['amount'];
  34. $memberAccount->save();
  35. Log::info("余额账户入账:" . $goodsAttributeJson['account']['amount']);
  36. // PayDetail 里写条充值记录
  37. PayDetail::insert([
  38. 'join_pay_member_id' => $params['member_id'],
  39. 'join_pay_order_id' => 'COMBINE',
  40. 'join_pay_object_json' => json_encode([
  41. 'recharge_order_id' => $params['orderId'],
  42. 'recharge_config' => ['nbr' => 0],
  43. 'recharge_goods_id' => $goods->goods_id,
  44. 'recharge_goods_name' => $goods->goods_name
  45. ]),
  46. 'pay_status' => 'SUCCESS',
  47. 'pay_category' => 'RECHARGE',
  48. 'pay_amount' => $goodsAttributeJson['account']['amount'],
  49. 'pay_paytimes' => date('Y-m-d H:i:s'),
  50. 'pay_prepayid' => $params['member_id'] . '-CASH',
  51. 'pay_json_request' => '[]',
  52. 'pay_json_response' => '[]',
  53. 'pay_extend_json' => '[]',
  54. 'pay_addtimes' => time()
  55. ]);
  56. }
  57. if (!empty($goodsAttributeJson['coupon'])) {
  58. foreach ($goodsAttributeJson['coupon'] as $key => $coupon) {
  59. $couponModel = Coupon::where('coupon_id', $key)->select('coupon_id', 'coupon_validdate_day', 'coupon_validdate_end', 'coupon_is_period', 'coupon_number')->first();
  60. if (empty($couponModel)) {
  61. continue;
  62. }
  63. // 券是否过期
  64. if (!empty($couponModel->coupon_validdate_end) && strtotime($couponModel->coupon_validdate_end) < time()) {
  65. continue;
  66. }
  67. // 发周期券
  68. if ($couponModel->coupon_is_period == 'Y') {
  69. // 发券参数
  70. $couponSendParams = [
  71. 'order_id' => $params['orderId'],
  72. 'gettype' => 'COMBINE',
  73. 'coupon_id' => $key,
  74. 'member_id' => $params['member_id']
  75. ];
  76. Log::info("发周期券参数", $couponSendParams);
  77. CouponDetailService::sendPeriodCoupon($couponSendParams);
  78. continue;
  79. }
  80. $couponResidue = 'all';
  81. if ($couponModel->coupon_number != 0) {
  82. $couponResidue = CouponDetail::where('join_detail_coupon_id', $key)->whereIn('coupon_detail_status', ['INIT', 'PENDING'])->count();
  83. // 超出优惠券设置的数量
  84. if ($couponResidue < $coupon['num']) {
  85. continue;
  86. }
  87. }
  88. $endDate = '';
  89. if (!empty($couponModel->coupon_validdate_end)) {
  90. $endDate = $couponModel->coupon_validdate_end;
  91. } elseif ($couponModel->coupon_validdate_day > 0) {
  92. $endDate = date('Y-m-d H:i:s', time() + ($couponModel->coupon_validdate_day * 24 * 3600) - 1);
  93. }
  94. // 发券参数
  95. $couponSendParams = [
  96. 'gettype' => 'COMBINE',
  97. 'coupon_id' => $key,
  98. 'order_id' => $params['orderId'],
  99. 'chooseCouponNbr' => $coupon['num'],
  100. 'member_id' => $params['member_id'],
  101. 'coupon_detail_gain_datetime' => date('Y-m-d H:i:s'),
  102. 'coupon_detail_deadline_datetime' => $endDate,
  103. ];
  104. Log::info("发普通券参数", $couponSendParams);
  105. if ($couponResidue != 'all' && $couponResidue - $coupon['num'] >= 0) {
  106. CouponDetailService::customSendCouponHave($couponSendParams);
  107. } else {
  108. for ($i = 0; $i < $coupon['num']; $i++) {
  109. CouponDetailService::customSendCoupon($couponSendParams);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. Db::commit();
  116. } catch (\Exception $e) {
  117. Db::rollBack();
  118. _syslog("新客专享", "处理失败");
  119. Log::error("新客专享处理失败", ['msg' => $e->getMessage()]);
  120. }
  121. }
  122. }