KangyangCityEvent.php 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. use support\Log;
  10. class KangyangCityEvent
  11. {
  12. public function grant($params)
  13. {
  14. // 会员VIP 标识
  15. $member = Member::where('member_id', $params['member_id'])->select('member_mobile', 'member_is_vip', 'member_extend_json')->first();
  16. $memberUpdateData = [
  17. 'member_is_vip' => 'Y'
  18. ];
  19. if (!empty($params['referee'])) {
  20. $memberExtendJson = [];
  21. if (!empty($member->member_extend_json)) {
  22. $memberExtendJson = json_decode($member->member_extend_json, true);
  23. }
  24. $memberExtendJson['referee'][date('Y-m-d H:i:s')] = $params['referee'];
  25. $memberUpdateData['member_extend_json'] = json_encode($memberExtendJson, JSON_UNESCAPED_UNICODE);
  26. }
  27. Member::where('member_id', $params['member_id'])->update($memberUpdateData);
  28. // 康养城VIP账户
  29. $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])->where('member_account_classify', 'VIP')->first();
  30. if (!$memberAccount) {
  31. $account = [
  32. 'join_account_member_id' => $params['member_id'],
  33. 'member_account_classify' => 'VIP',
  34. 'member_account_status' => 'ACTIVED',
  35. 'member_account_category' => 'NORMAL',
  36. 'member_account_nbr' => $params['member_id'] . '-VIP',
  37. 'member_account_name' => 'VIP账户',
  38. 'member_account_income' => $params['order_amount_pay'],
  39. 'member_account_surplus' => $params['order_amount_pay'],
  40. 'member_account_addtimes' => time(),
  41. ];
  42. MemberAccount::insert($account);
  43. } else {
  44. // 重复购买
  45. $memberAccount->member_account_income = $memberAccount->member_account_income + $params['order_amount_pay'];
  46. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $params['order_amount_pay'];
  47. $memberAccount->save();
  48. }
  49. //发放优惠券
  50. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])->select('goods_attribute_json')->first();
  51. if (!empty($goods) && !empty($goods->goods_attribute_json)) {
  52. $goodsAttributeJson = json_decode($goods->goods_attribute_json, true);
  53. if (!empty($goodsAttributeJson['coupon'])) {
  54. foreach ($goodsAttributeJson['coupon'] as $key => $coupon) {
  55. $couponModel = Coupon::where('coupon_id', $key)->select('coupon_id', 'coupon_validdate_day', 'coupon_validdate_end')->first();
  56. if (empty($couponModel)) {
  57. continue;
  58. }
  59. // 券是否过期
  60. if (!empty($couponModel->coupon_validdate_end) && strtotime($couponModel->coupon_validdate_end) < time()) {
  61. continue;
  62. }
  63. $endDate = '';
  64. if (!empty($couponModel->coupon_validdate_end)) {
  65. $endDate = $couponModel->coupon_validdate_end;
  66. } elseif ($couponModel->coupon_validdate_day > 0) {
  67. $endDate = date('Y-m-d H:i:s', time() + ($couponModel->coupon_validdate_day * 24 * 3600) - 1);
  68. }
  69. $num = $coupon['num'];
  70. for ($i = 0; $i < $num; $i++) {
  71. CouponDetail::insert([
  72. 'coupon_detail_id' => 'CUDT' . date("ymdH") . random_string(6, 'up'),
  73. 'join_detail_coupon_id' => $key,
  74. 'join_coupon_detail_member_id' => $params['member_id'],
  75. 'coupon_detail_status' => 'ACTIVED',
  76. 'coupon_detail_gain_datetime' => date('Y-m-d H:i:s'),
  77. 'coupon_detail_deadline_datetime' => $endDate,
  78. 'coupon_detail_json' => '[]',
  79. 'coupon_detail_extend_json' => json_encode(['gettype' => 'VIP']),
  80. 'coupon_detail_addtimes' => time()
  81. ]);
  82. }
  83. }
  84. }
  85. }
  86. // 通知判客
  87. if (!empty($member->member_mobile)) {
  88. $url = getenv("MARKET_NOTICE_DOMAIN") . '/custom/market_custom/update';
  89. $result = http_post($url, ['mobile' => $member->member_mobile]);
  90. Log::info("MARKET_NOTICE_" . $member->member_mobile, json_decode($result, true));
  91. }
  92. }
  93. }