NewCustomerEvent.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. 'gettype' => 'COMBINE',
  72. 'coupon_id' => $key,
  73. 'member_id' => $params['member_id']
  74. ];
  75. Log::info("发周期券参数", $couponSendParams);
  76. CouponDetailService::sendPeriodCoupon($couponSendParams);
  77. continue;
  78. }
  79. $couponResidue = 'all';
  80. if ($couponModel->coupon_number != 0) {
  81. $couponResidue = CouponDetail::where('join_detail_coupon_id', $key)->whereIn('coupon_detail_status', ['INIT', 'PENDING'])->count();
  82. // 超出优惠券设置的数量
  83. if ($couponResidue < $coupon['num']) {
  84. continue;
  85. }
  86. }
  87. $endDate = '';
  88. if (!empty($couponModel->coupon_validdate_end)) {
  89. $endDate = $couponModel->coupon_validdate_end;
  90. } elseif ($couponModel->coupon_validdate_day > 0) {
  91. $endDate = date('Y-m-d H:i:s', time() + ($couponModel->coupon_validdate_day * 24 * 3600) - 1);
  92. }
  93. // 发券参数
  94. $couponSendParams = [
  95. 'gettype' => 'COMBINE',
  96. 'coupon_id' => $key,
  97. 'chooseCouponNbr' => $coupon['num'],
  98. 'member_id' => $params['member_id'],
  99. 'coupon_detail_gain_datetime' => date('Y-m-d H:i:s'),
  100. 'coupon_detail_deadline_datetime' => $endDate,
  101. ];
  102. Log::info("发普通券参数", $couponSendParams);
  103. if ($couponResidue != 'all' && $couponResidue - $coupon['num'] >= 0) {
  104. CouponDetailService::customSendCouponHave($couponSendParams);
  105. } else {
  106. for ($i = 0; $i < $coupon['num']; $i++) {
  107. CouponDetailService::customSendCoupon($couponSendParams);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. Db::commit();
  114. } catch (\Exception $e) {
  115. Db::rollBack();
  116. _syslog("新客专享", "处理失败");
  117. Log::error("新客专享处理失败", ['msg' => $e->getMessage()]);
  118. }
  119. }
  120. }