KangyangCityEvent.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\event\order;
  3. use app\model\Coupon;
  4. use app\model\CouponDetail;
  5. use app\model\Goods;
  6. use app\model\Member;
  7. use app\model\MemberAccount;
  8. use app\model\SysSerial;
  9. class KangyangCityEvent
  10. {
  11. public function grant($params)
  12. {
  13. // 会员VIP 标识
  14. Member::where('member_id', $params['member_id'])->update(['member_is_vip' => 'Y']);
  15. // 康养城VIP账户
  16. $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])->where('member_account_classify', 'VIP')->first();
  17. if (!$memberAccount) {
  18. $account = [
  19. 'join_account_member_id' => $params['member_id'],
  20. 'member_account_classify' => 'VIP',
  21. 'member_account_status' => 'ACTIVED',
  22. 'member_account_category' => 'NORMAL',
  23. 'member_account_nbr' => $params['member_id'] . '-VIP',
  24. 'member_account_name' => 'VIP账户',
  25. 'member_account_income' => $params['order_amount_pay'],
  26. 'member_account_surplus' => $params['order_amount_pay'],
  27. 'member_account_addtimes' => time(),
  28. ];
  29. MemberAccount::insert($account);
  30. } else {
  31. // 重复购买
  32. $memberAccount->member_account_income = $memberAccount->member_account_income + $params['order_amount_pay'];
  33. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $params['order_amount_pay'];
  34. $memberAccount->save();
  35. }
  36. //发放优惠券
  37. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])->select('goods_attribute_json')->first();
  38. if (!empty($goods) && !empty($goods->goods_attribute_json)) {
  39. $goodsAttributeJson = json_decode($goods->goods_attribute_json, true);
  40. if (!empty($goodsAttributeJson['coupon'])) {
  41. foreach ($goodsAttributeJson['coupon'] as $key => $coupon) {
  42. $couponModel = Coupon::where('coupon_id', $key)->select('coupon_id', 'coupon_validdate_day', 'coupon_validdate_end')->first();
  43. if (empty($couponModel)) {
  44. continue;
  45. }
  46. // 券是否过期
  47. if (!empty($couponModel->coupon_validdate_end) && strtotime($couponModel->coupon_validdate_end) < time()){
  48. continue;
  49. }
  50. $endDate = '';
  51. if (!empty($couponModel->coupon_validdate_end)) {
  52. $endDate = $couponModel->coupon_validdate_end;
  53. } elseif ($couponModel->coupon_validdate_day > 0) {
  54. $endDate = date('Y-m-d H:i:s', time() + ($couponModel->coupon_validdate_day * 24 * 3600));
  55. }
  56. $num = $coupon['num'];
  57. for ($i = 0; $i < $num; $i++) {
  58. CouponDetail::insert([
  59. 'coupon_detail_id' => 'CUDT' . str_pad(SysSerial::getSerial(), 8, "0", STR_PAD_LEFT) . random_string(6, 'up'),
  60. 'join_detail_coupon_id' => $key,
  61. 'join_coupon_detail_member_id' => $params['member_id'],
  62. 'coupon_detail_status' => 'ACTIVED',
  63. 'coupon_detail_gain_datetime' => date('Y-m-d H:i:s'),
  64. 'coupon_detail_deadline_datetime' => $endDate,
  65. 'coupon_detail_json' => '[]',
  66. 'coupon_detail_extend_json' => json_encode(['gettype'=>'VIP']),
  67. 'coupon_detail_addtimes' => time()
  68. ]);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }