KangyangCityEvent.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // 分期付款
  31. if (isset($params['order_amount_paid']) && $params['order_amount_paid'] > 0){
  32. $params['order_amount_pay'] = $params['order_amount_paid'] + $params['order_amount_pay'];
  33. }
  34. if (!$memberAccount) {
  35. $account = [
  36. 'join_account_member_id' => $params['member_id'],
  37. 'member_account_classify' => 'VIP',
  38. 'member_account_status' => 'ACTIVED',
  39. 'member_account_category' => 'NORMAL',
  40. 'member_account_nbr' => $params['member_id'] . '-VIP',
  41. 'member_account_name' => 'VIP账户',
  42. 'member_account_income' => $params['order_amount_pay'],
  43. 'member_account_surplus' => $params['order_amount_pay'],
  44. 'member_account_addtimes' => time(),
  45. ];
  46. MemberAccount::insert($account);
  47. } else {
  48. // 重复购买
  49. $memberAccount->member_account_income = $memberAccount->member_account_income + $params['order_amount_pay'];
  50. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $params['order_amount_pay'];
  51. $memberAccount->save();
  52. }
  53. //发放优惠券
  54. $goods = Goods::where('goods_id', $params['join_sheet_goods_id'])->select('goods_attribute_json')->first();
  55. if (!empty($goods) && !empty($goods->goods_attribute_json)) {
  56. $goodsAttributeJson = json_decode($goods->goods_attribute_json, true);
  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')->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. $endDate = '';
  68. if (!empty($couponModel->coupon_validdate_end)) {
  69. $endDate = $couponModel->coupon_validdate_end;
  70. } elseif ($couponModel->coupon_validdate_day > 0) {
  71. $endDate = date('Y-m-d H:i:s', time() + ($couponModel->coupon_validdate_day * 24 * 3600) - 1);
  72. }
  73. $num = $coupon['num'];
  74. for ($i = 0; $i < $num; $i++) {
  75. CouponDetail::insert([
  76. 'coupon_detail_id' => 'CUDT' . date("ymdH") . random_string(6, 'up'),
  77. 'join_detail_coupon_id' => $key,
  78. 'join_coupon_detail_member_id' => $params['member_id'],
  79. 'coupon_detail_status' => 'ACTIVED',
  80. 'coupon_detail_gain_datetime' => date('Y-m-d H:i:s'),
  81. 'coupon_detail_deadline_datetime' => $endDate,
  82. 'coupon_detail_json' => '[]',
  83. 'coupon_detail_extend_json' => json_encode(['gettype' => 'VIP']),
  84. 'coupon_detail_addtimes' => time()
  85. ]);
  86. }
  87. }
  88. }
  89. }
  90. // 通知判客
  91. if (!empty($member->member_mobile)) {
  92. $url = getenv("MARKET_NOTICE_DOMAIN") . '/custom/market_custom/update';
  93. $result = http_post($url, ['mobile' => $member->member_mobile]);
  94. Log::info("MARKET_NOTICE_" . $member->member_mobile, json_decode($result, true));
  95. }
  96. }
  97. }