order = $order; if (empty($order)) return; //初始化数据 $extend = json_decode($order->extend); $this->facilitate_ratio = $extend->facilitate_ratio ?? 0; //促成人员比例 $this->spread_merchant_ratio = $extend->merchant_ratio ?? 0; //推广商家比例 $this->spread_merchant_id = intval($extend->spread_merchant_id ?? 0); //推广商家id //vip数据 $vip = SaasMemberLevel::where('id', $order->type_id)->first(); $vip->title = $vip->level_title; $this->vip = $vip; //促成人员 if ($order->from_openid && $order->from_openid != $order->open_id) { $this->last_merchant_user = MerchantUser::where('openid', $order->from_openid)->first(); } DB::beginTransaction(); //处理促成人员收益 $this->handleFacilitateEarning(); //处理无促成 发放给推广商家创始人 根据手机号匹配 $this->handleSpreadMerchantFounderEarning(); //处理推广商家收益 $this->handleSpreadMerchantEarning(); //处理天路合一商家收益 $this->handleSelfMerchantEarning(); DB::commit(); //促单人员 未关注公众号 短信通知 $this->sendFacilitateUserSubscribeNotice(); //推广商家 未关注公众号 短信通知 $this->sendSpreadMerchantSubscribeNotice(); //促单人员 收益通知 $this->sendFacilitateUserEarningNotice(); //无促单人员 推广商家创始人收益通知 $this->sendSpreadMerchantFounderEarningNotice(); //推广商家 收益通知 $this->sendSpreadMerchantEarningNotice(); //天路合一 收益通知 $this->sendSelfMerchantEarningNotice(); //saas海报页面渠道购买 短信通知 $this->sendContactTaSms(); } /** * 处理促成人员收益 * @return void */ private function handleFacilitateEarning() { $order = $this->order; $ratio = $this->facilitate_ratio;//促成人员比例 if (!$this->last_merchant_user) { return; } if (!$ratio) { return; } $earning_amount = $ratio * $order->price; if (!$earning_amount) { return; } $this->facilitate_earning = $earning_amount; //更新收益账号 $earning_account = MEarningAccount::firstOrCreate(['m_id' => $order->merchant_id, 'm_user_id' => $this->last_merchant_user->id]); $earning_account->total_value += $earning_amount; $earning_account->balance += $earning_amount; $earning_account->save(); //添加收益记录 $earning = new MEarning; $earning->m_id = $order->merchant_id; $earning->m_user_id = $this->last_merchant_user->id; $earning->m_order_id = $order->id; $earning->type = 'member'; $earning->sharer = 'last_sharer'; $earning->ratio = $ratio; $earning->sub_ratio = 1; $earning->value = $earning_amount; $earning->status = 'finished'; $earning->role_id = $this->last_merchant_user->id; $earning->save(); } /** * 处理无促成 收益发放给推广商家创始人 根据手机号匹配 * @return void */ private function handleSpreadMerchantFounderEarning() { $order = $this->order; if ($this->last_merchant_user) { return; } $ratio = $this->facilitate_ratio;//促成人员比例 if (!$ratio) { return; } if (!$this->spread_merchant_id) { return; } $mobile = MerchantAccount::where('id', $this->spread_merchant_id)->value('mobile'); $spread_merchant_founder = MerchantUser::where('mobile', $mobile)->first(); if (!$spread_merchant_founder) { return; } $earning_amount = $ratio * $order->price; if (!$earning_amount) { return; } $this->spread_merchant_founder = $spread_merchant_founder; $this->spread_merchant_founder_earning = $earning_amount; //更新收益账号 $earning_account = MEarningAccount::firstOrCreate(['m_id' => $order->merchant_id, 'm_user_id' => $spread_merchant_founder->id]); $earning_account->total_value += $earning_amount; $earning_account->balance += $earning_amount; $earning_account->save(); //添加收益记录 $earning = new MEarning; $earning->m_id = $order->merchant_id; $earning->m_user_id = $spread_merchant_founder->id; $earning->m_order_id = $order->id; $earning->type = 'member'; $earning->sharer = 'other_sharer'; $earning->ratio = $ratio; $earning->sub_ratio = 1; $earning->value = $earning_amount; $earning->status = 'finished'; $earning->role_id = $spread_merchant_founder->id; $earning->save(); //记录推广商家创始人id $extend = json_decode($order->extend); $extend->spread_merchant_founder_user_id = $spread_merchant_founder->id; $this->order->extend = json_encode($extend); $this->order->save(); } /** * 处理推广商家收益 * @return void */ private function handleSpreadMerchantEarning() { $order = $this->order; if (!$this->spread_merchant_id) { return; } $ratio = $this->spread_merchant_ratio; //推广商家比例 if (!$ratio) { return; } $earning_amount = $ratio * $order->price; if (!$earning_amount) { return; } $this->spread_merchant_earning = $earning_amount; //更新收益账号 $earning_account = MEarningAccount::firstOrCreate(['m_id' => $this->spread_merchant_id, 'm_user_id' => 0]); $earning_account->total_value += $earning_amount; $earning_account->balance += $earning_amount; $earning_account->save(); //添加收益记录 $earning = new MEarning; $earning->m_id = $this->spread_merchant_id; $earning->m_user_id = 0; $earning->m_order_id = $order->id; $earning->type = 'member'; $earning->sharer = 'merchant'; $earning->ratio = $ratio; $earning->sub_ratio = 1; $earning->value = $earning_amount; $earning->status = 'finished'; $earning->role_id = $this->spread_merchant_id; $earning->save(); } /** * 处理天路合一商家收益 * @return void */ private function handleSelfMerchantEarning() { $order = $this->order; $ratio = 1; if ($this->last_merchant_user) { $ratio -= $this->facilitate_ratio; } if ($this->spread_merchant_founder) { $ratio -= $this->facilitate_ratio; } if ($this->spread_merchant_id) { $ratio -= $this->spread_merchant_ratio; } if (!$ratio) { return; } $earning_amount = $ratio * $order->price; if (!$earning_amount) { return; } $this->self_merchant_earning = $earning_amount; //更新收益账号 $earning_account = MEarningAccount::firstOrCreate(['m_id' => $order->merchant_id, 'm_user_id' => 0]); $earning_account->total_value += $earning_amount; $earning_account->balance += $earning_amount; $earning_account->save(); //添加收益记录 $earning = new MEarning; $earning->m_id = $order->merchant_id; $earning->m_user_id = 0; $earning->m_order_id = $order->id; $earning->type = 'member'; $earning->sharer = 'merchant'; $earning->ratio = $ratio; $earning->sub_ratio = 1; $earning->value = $earning_amount; $earning->status = 'finished'; $earning->role_id = $order->merchant_id; $earning->save(); } /** * 推广商家 未关注公众号 短信通知 * @return void */ private function sendSpreadMerchantSubscribeNotice() { $spread_merchant_account = MerchantAccount::find($this->spread_merchant_id); if (!$spread_merchant_account) { return; } $this->checkSubscribe($spread_merchant_account->openid, $spread_merchant_account->mobile); } /** * 促单人员 未关注公众号 短信通知 * @return void */ private function sendFacilitateUserSubscribeNotice() { $order = $this->order; if (!$this->last_merchant_user) { return; } $this->checkSubscribe($order->from_openid, $this->last_merchant_user->mobile); } /** * 检查是否关注公众号 * @return void */ private function checkSubscribe($openid, $mobile) { if (empty($openid) || empty($mobile)) { return; } $app = \WechatService::officialApp(); $wechat_user = $app->user->get($openid); $subscribe = $wechat_user['subscribe'] ?? 0; if ($subscribe) { return; } $content = "您有一笔收益到账,系统检测到您未关注公众号,请关注下福恋智能公众号哦~"; Message::create([ 'phone' => $mobile, 'message' => $content, 'confirmed' => 1, 'ip' => request() ? request()->ip() : '127.0.0.1', ]); Messengers::sendSMS($mobile, $content); } /** * 促单人员 收益通知 * @return void */ private function sendFacilitateUserEarningNotice() { $order = $this->order; if (!$this->last_merchant_user) { return; } \CommonUtilsService::sendBuySuccessNoticeToBusiness($this->vip, $order->from_openid, '', $order->price, $order->mobile, $order->name); if (!$this->facilitate_earning) { return; } $this->sendTemplateMessage($order->from_openid, $this->facilitate_earning, date('Y-m-d H:i:s'), $order->merchant_id, 'merchant_user'); } /** * 无促单人员 推广商家创始人收益通知 * @return void */ private function sendSpreadMerchantFounderEarningNotice() { $order = $this->order; if ($this->last_merchant_user) { return; } $spread_merchant_founder = $this->spread_merchant_founder; if (!$spread_merchant_founder) { return; } if (!$this->spread_merchant_founder_earning) { return; } \CommonUtilsService::sendBuySuccessNoticeToBusiness($this->vip, $spread_merchant_founder->openid, '', $order->price, $order->mobile, $order->name); $this->sendTemplateMessage($spread_merchant_founder->openid, $this->spread_merchant_founder_earning, date('Y-m-d H:i:s'), $order->merchant_id, 'merchant_user'); } /** * 推广商家 收益通知 * @return void */ private function sendSpreadMerchantEarningNotice() { $order = $this->order; if (!$this->spread_merchant_id) { return; } $merchant = MerchantAccount::where('id', $this->spread_merchant_id)->first(); $merchant_openid = $merchant->openid ?? ''; if (!$merchant_openid) { $merchant_openid = Anchor::where('m_id', $this->spread_merchant_id)->value('openid'); } \CommonUtilsService::sendBuySuccessNoticeToBusiness($this->vip, $merchant_openid, '', $order->price, $order->mobile, $order->name); if (!$this->spread_merchant_earning) { return; } $this->sendTemplateMessage($merchant_openid, $this->spread_merchant_earning, date('Y-m-d H:i:s'), $this->spread_merchant_id, 'merchant'); } /** * 天路合一商家 收益通知 * @return void */ private function sendSelfMerchantEarningNotice() { $order = $this->order; if (!$this->self_merchant_earning) { return; } $merchant = MerchantAccount::where('id', $order->merchant_id)->first(); $merchant_openid = $merchant->openid ?? ''; if (!$merchant_openid) { $merchant_openid = Anchor::where('m_id', $order->merchant_id)->value('openid'); } \CommonUtilsService::sendBuySuccessNoticeToBusiness($this->vip, $merchant_openid, 'merchant', $order->price, $order->mobile, $order->name); $this->sendTemplateMessage($merchant_openid, $this->self_merchant_earning, date('Y-m-d H:i:s'), $order->merchant_id, 'merchant'); } /** * 公众号通知 * @param $openid * @param $amount * @param $earning_time * @param $merchant_id * @param $type * @return void */ private function sendTemplateMessage($openid, $amount, $earning_time, $merchant_id, $type) { switch ($type) { case 'merchant_user': $data['url'] = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $merchant_id . '&anchor_openid=&serve_tab=&url=https%3A%2F%2Flove.ufutx.com%2Fpu%2F%23%2FmyRecommend'; break; case 'merchant': $data['url'] = env('APP_URL') . '/pu_m/#/home?index=2'; break; } $data['touser'] = $openid; $data['template_id'] = config('wechat.tpls.earning_notice'); $data['data'] = [ 'first' => '收益', 'keyword1' => '¥' . $amount, 'keyword2' => $earning_time, 'remark' => '最终到账金额以实际到账金额为准,感谢您的支持!', ]; SendTemplateMsg::dispatch($data)->onQueue('love'); } /** * 推广海报页面 发送联系ta短信 * @return void */ private function sendContactTaSms() { $order = $this->order; $extend = json_decode($order->extend); $vip_user_spread_carrier_id = $extend->vip_user_spread_carrier_id ?? 0; if (!$vip_user_spread_carrier_id) { return; } $saas_vip_service = new SaasVipService(); $send = $saas_vip_service->contactSms($vip_user_spread_carrier_id, $order->mobile); if (!$send) { return; } $model = new SaasVipContactSms; $model->merchant_id = $order->merchant_id; $model->merchant_user_id = $order->account_id; $model->level_id = $order->type_id; $model->vip_user_spread_carrier_id = $vip_user_spread_carrier_id; $model->save(); } }