love_php/app/Jobs/SendEarningMessageV2.php
2026-04-02 09:20:51 +08:00

818 lines
39 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Jobs;
use App\Models\AccessRecord;
use App\Models\CommunityActivity;
use App\Models\MAdvanceEarning;
use App\Models\Server\MarketService;
use App\Models\Match\MRefundOrder;
use App\Models\Server\MEarningGrade;
use App\Models\Server\MEarningGradeUser;
use App\Models\MEarningRules;
use App\Models\MerchantInfo;
use App\Models\Server\MEarningRule;
use App\Models\Server\MerchantEvaluate;
use App\Models\Server\MerchantService;
use App\Models\Server\MerchantShareChannel;
use App\Models\Server\MerchantUser;
use App\Models\Server\TouristOrder;
use App\Models\TouristOrder as Order;
use App\Models\User;
use App\Models\Wechat;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Http\Response\ResponseJson;
use Illuminate\Support\Facades\Log;
class SendEarningMessageV2 implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use ResponseJson;
protected $order_id;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($order_id)
{
$this->order_id = $order_id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$order = TouristOrder::find($this->order_id);
if (empty($order)) return;
if ($order->pay_type == 'partner') return;
//优惠券订单无分成
if ($order->user_coupon_id) return;
// //是否是服务市场订单
// $m_service = MerchantService::where('merchant_id',$order->merchant_id)->where('type', $order->type)->where('type', $order->type_id)->first();
// $order->is_market_service = $m_service ? $m_service->service_type : 0;
// if ($order->is_market_service) return;
Log::info("待收益通知,订单".$order->id);
$order->origin_type = $order->type;
if ($order->type == 'community'){
$class = CommunityActivity::where('id', $order->type_id)->value('class');
if ($class) {
$order->type = ($class == "one") ? 'activity' : 'service';
} else {
Log::info('订单id-' . $order->id . ', 不存在服务或者活动信息');
return ;
}
}
//订单是否是服务市场订单
$market_service = MarketService::where('alliance_id', $order->merchant_id)->where('type', $order->origin_type)->where('type_id', $order->type_id)->whereNull('deleted_at')->first();
if (empty($market_service)){
$market_service = MarketService::whereNull('alliance_id')->where('type', $order->origin_type)->where('type_id', $order->type_id)->whereNull('deleted_at')->first();
$is_market_service = ($market_service && $market_service->merchant_id != $order->merchant_id) ? 1:0;
}else {
$is_market_service = 2;
}
$order->is_market_service = $is_market_service;
$earning_rule = MerchantInfo::where('m_id', $order->merchant_id)->value('earning_rule')?:'system';
if($earning_rule == 'system') {
Log::info("系统分享收益");
$rule = $this->earningRule($order);
// if (empty($rule) || (empty($rule->forzen_time) && $order->type != "activity")) return;
if (empty($rule)){
throw new \Exception('获取rule出错order_id='.$order->id);
}
$system_rule = MEarningRule::where('m_id', $order->merchant_id)->where('name', $order->type)->where('type_id', 0)->first();
//订单购买途径
if ($order->channel == "福恋小程序" || $order->channel == "商家APP") {//小程序或者app
//第一分享人(分享注册)
$first_merchant_user = $this->mpFirstMerchatUser($order);
//中间分享人
$other_merchant_users = $this->mpOtherMerchantUsers($order);
//最后受益人
$last_merchant_user = $this->mpLastMerchantUser($order);
} else {
//首邀分享人(分享进入商户)
$first_merchant_user = $this->saasFirstMerchantUser($order);
//间接分享人
// $other_merchant_users = $this->saasOtherMerchantUsers($order);
$other_merchant_users = $this->saasOtherMerchantUsersV2($order);
//促单分享人
$last_merchant_user = $this->saasLastMerchantUser($order);
}
if ($order->group_id) {
$first_merchant_user = $last_merchant_user = null;
$other_merchant_users = [];
}
//分享人信息
$shares = $this->getShare($first_merchant_user, $other_merchant_users, $last_merchant_user, $system_rule);
//可分配的金额
$price = $this->assignedPrice($order);
//渠道收益
$channel_earnings = $this->channelEarning($order, $rule, $price);
//各分享人的收益
$earnings = $this->earningAmount($shares['first_sharer'], $shares['other_sharers'], $shares['last_sharer'], $rule, $channel_earnings, $order, $price);
Log::info("最终预分成数量:".count($earnings));
//删除已分配的预分成记录
$this->deleteAdvanceEarnings($order);
//创建各分享人收益记录
$this->insertAdvanceEarnings($earnings);
//合并每个用户的收益
$result = $this->combinEarnings($earnings);
//发送通知
$this->messageInfo($order, $result);
}elseif ($earning_rule == 'grade') {
Log::info("等级分享收益");
//获取分享用户
$share = $this->lastMerchantUser($order);
$share = ($order->group_id)?null:$share;
$rule = $this->earningAgentRule($order, $share);
//可分配的金额
$price = $this->assignedPrice($order);
//渠道收益
$channel_earnings = $this->channelAgentEarning($order, $rule?$rule->earning_time:0, $price);
//删除已分配的预分成记录
$this->deleteAdvanceEarnings($order);
//无需冻结
if (empty($rule) || (empty($rule->forzen_time) && $order->type != "activity")) return;
$earnings = $this->earningAgentAmount($order, $rule, $channel_earnings, $price, $share);
$earning_time = $earnings[0]['earning_time']??date("Y-m-d H:i:s");
$this->insertAdvanceEarnings($earnings);
$result = $this->combinEarnings($earnings);
//发送通知
$this->messageInfoV2($order, $result, $earning_time);
}
} catch (\Exception $e) {
$this->getError($e);
return ;
}
}
public function messageInfoV2($order, $result, $earning_time)
{
$name = $order->name;
if (empty($name)) {
$name = MerchantUser::where('id', $order->account_id)->value('nickname');
}
$mobile = $order->mobile;
if (empty($mobile) || $mobile == '未获取') {
$mobile = MerchantUser::where('id', $order->account_id)->value('mobile');
}
$first = '您推荐的用户【'.$name;
if ($mobile) {
$first = $first.','.substr_replace($mobile, '****', 3, 4);
}
if ($order->has_refund) {
$first = $first.'】购买的订单存在退款,您本单收益变动';
}else {
$first = $first.'】已经成功下单,您本单可能产生收益';
}
$this->sendTemplateMessage($result, $first, $order, $earning_time);
}
public function sendTemplateMessage($result, $first, $order, $earning_time)
{
foreach ($result as $res) {
$merchant_user = MerchantUser::find($res['user_id']);
if (empty($merchant_user)) continue;
//收益金额
$amount = $res['amount'];
if ($amount == 0 && empty($order->has_refund)) continue;
if ($merchant_user->openid) {
$data['touser'] = $merchant_user->openid;
$data['template_id'] = config('wechat.tpls.earning_notice');
$data['url'] = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$order->merchant_id.'&anchor_openid=&serve_tab=&url=https%3A%2F%2Flove.ufutx.com%2Fpu%2F%23%2FmyRecommend';
$data['data'] = [
'first' => $first,
'keyword1' => '¥'.$amount,
'keyword2' => $earning_time,
'remark' => '最终到账金额以实际到账金额为准,感谢您的支持!',
];
SendTemplateMsg::dispatch($data)->onQueue('love');
} elseif ($merchant_user->mpUser) {
$mp_user = $merchant_user->mpUser;
//公众号通知
if ($mp_user->wechat && $mp_user->wechat->official_openid) {
//收益金额
$openid = $mp_user->wechat->official_openid;
$data['touser'] = $openid;
$data['template_id'] = config('wechat.tpls.earning_notice');
$data['url'] = env('APP_URL').'/auth/saas/earning/account';
$data['data'] = [
'first' => $first,
'keyword1' => '¥'.$amount,
'keyword2' => $earning_time,
'remark' => '最终到账金额以实际到账金额为准,感谢您的支持!',
];
SendTemplateMsg::dispatch($data)->onQueue('love');
}
}
}
}
public function earningAgentAmount($order, $rule, $channel_earnings, $price, $share)
{
//普通分享用户分成 = 商家提供的总推广收益
$rule_date = $rule->forzen_time;
$rule_time = $rule_date * 24 * 3600;
if ($order->type == "activity") {
$activity = CommunityActivity::find($order->type_id);
//是否到了活动结束时间
$end_time = $activity->end_time;
$earning_time = strtotime($end_time) + $rule_time;
} else {
$earning_time = strtotime($order->created_at) + $rule_time;
}
$earning_time = date('Y-m-d H:i:s', $earning_time);
$array = [];
if (count($channel_earnings)) {
$channel_earnings['earning_time'] = $earning_time;
$array[] = $channel_earnings;
}
if ($order->is_market_service) { //服务市场
if ($order->is_market_service == 1) {//服务市场
$market = MarketService::whereNull('alliance_id')->where('type',$order->origin_type)->where('type_id', $order->type_id)->where('market_type', 1)->whereNull('deleted_at')->first();
}else {//企业联盟
$market = MarketService::where('alliance_id', $order->merchant_id)->where('type',$order->origin_type)->where('type_id', $order->type_id)->where('market_type', 2)->whereNull('deleted_at')->first();
}
//商家提供的总推广收益
$total_earning = number_format($market->ratio * $price, 2, ".", '');
//代理商提供的推广收益
$agent_total_earning = number_format($rule->ratio * $total_earning, 2, ".", '');
//用户收益
$last_ratio = $market->ratio;
$last_sub_ratio = $rule->ratio;
$last_earning = number_format($last_sub_ratio * $total_earning, 2, ".", '');
$last_share_earnins = ['m_id'=>$order->merchant_id, 'm_user_id'=>$share?$share->id:0, 'type'=>$order->type, 'sharer'=>'last_sharer', 'ratio'=>$last_ratio, 'sub_ratio'=>$last_sub_ratio, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time, 'value'=>$last_earning];
$array[] = $last_share_earnins;
//代理商
$agent_sub_ratio = number_format(1 - $last_sub_ratio, '2', '.', '');
$agent_earning = number_format($total_earning - $agent_total_earning,2, '.', '');
$agent_earnings = ['m_id'=>$order->merchant_id, 'm_user_id'=>0, 'type'=>$order->type, 'sharer'=>'merchant', 'ratio'=>number_format($market->ratio, '2', '.', ''), 'sub_ratio'=>$agent_sub_ratio, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time, 'value'=>$agent_earning];
$array[] = $agent_earnings;
//原始商家收益
$merchant_ratio = number_format(1 - $market->ratio - ($channel_earnings['ratio']??0), '2', '.', '');
$merchant_earning = number_format($price - number_format( $total_earning + ($channel_earnings['channel_earning']??0) , 2, '.', ''), 2, '.', '');
$merchant_earnings= ['m_id'=>$market->merchant_id, 'm_user_id'=>0, 'type'=>$order->type, 'sharer'=>'merchant', 'ratio'=>$merchant_ratio, 'sub_ratio'=>1, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time, 'value'=>$merchant_earning];
$array[] = $merchant_earnings;
} else { //普通商家
$last_ratio = $rule->ratio;
$last_earning = number_format($last_ratio * $price, 2, ".", '');
$share_arr = ['m_id'=>$order->merchant_id, 'm_user_id'=>$share?$share->id:0, 'type'=>$order->type, 'sharer'=>'last_sharer', 'ratio'=>$last_ratio, 'sub_ratio'=>1, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time, 'value'=>$last_earning];
$array[] = $share_arr;
//商家分成
$m_ratio = number_format(1 - ($channel_earnings['ratio']??0) - $last_ratio, 2, '.', '');
$m_earning = number_format($price - $last_earning - ($channel_earnings['value']??0), 2, '.', '');
$m_arr = ['m_id'=>$order->merchant_id, 'm_user_id'=>0, 'type'=>$order->type, 'sharer'=>'merchant', 'ratio'=>$m_ratio, 'sub_ratio'=>1, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time, 'value'=>$m_earning];
$array[] = $m_arr;
}
return $array;
}
public function channelAgentEarning($order, $earning_time, $price)
{
$array = [];
//渠道分享分成 分享比例 * 订单金额
if ($order->share_channel_id) {
$channel = MerchantShareChannel::where('id', $order->share_channel_id)->where('m_id', $order->merchant_id)->first();
if ($channel && $channel->ratio) {
if ($channel->m_id == $order->merchant_id) {
//生成渠道分享记录
if ($channel->bind_m_user_id) {
$array['value'] = number_format($price * $channel->ratio, 2, ".", '');
$array['ratio'] = $channel->ratio;
$array['m_id'] = $channel->m_id;
$array['m_user_id'] = $channel->bind_m_user_id;
$array['m_order_id'] = $order->id;
$array['type'] = $order->type;
$array['sharer'] = 'channel';
$array['sub_ratio'] = 1;
$array['earning_time'] = $earning_time;
}
}
}
}
return $array;
}
public function earningAgentRule($order, $last_user)
{
if (empty($last_user)) return null;
//用户等级比例
$grade_user = MEarningGradeUser::with('grade')->where('m_id', $order->merchant_id)->where('user_id', $last_user->id)->first();
if ($grade_user && $grade_user->grade) {
$grade = $grade_user->grade;
}else {
$price = Order::where('merchant_id', $order->merchant_id)->where("from_openid", $last_user->openid)->whereIn('pay_status', [1,4])->where('id', '<', $order->id)->sum('price');
$grade = MEarningGrade::where('m_id', $order->merchant_id)->where('amount', '<=', $price)->orderBy('amount', 'desc')->first();
}
//更新等级
// $this->updateEarningAgent($order, $last_user);
return $grade;
}
public function lastMerchantUser($order)
{
if ($order->channel == 3 || $order->channel == 2) {//小程序或者app
//最后受益人
$last_merchant_user = $this->mpLastMerchantUser($order);
} else {
$last_merchant_user = $this->saasLastMerchantUser($order);
}
return $last_merchant_user;
}
public function deleteAdvanceEarnings($order)
{
MAdvanceEarning::where('m_order_id', $order->id)->forceDelete();
}
/**
* 可分配的金额
* @param $order
*/
public function assignedPrice($order)
{
$price = $order->price;
//如果是测试订单扣除50%
if ($order->type == 'evaluate') {
$evaluate = MerchantEvaluate::with('evaluateList')->where('id', $order->type_id)->first();
if ($evaluate && $evaluate->evaluateList && $evaluate->evaluateList->type == 1) {
$price = number_format($price / 2, 2, '.', '');
}
}
//退款金额
$order->has_refund = 0;
$refund_fee = MRefundOrder::where('trade_no', $order->trade_no)->sum('refund_fee');
if ($refund_fee) {
$order->has_refund = 1;
}
//可分配金额
$price = number_format($price - $refund_fee, 2, '.', '');
return $price;
}
/**
* 创建分享人预收益记录
* @param $earnings
*/
public function insertAdvanceEarnings($earnings)
{
foreach ($earnings as $earning) {
MAdvanceEarning::firstOrCreate(['m_id'=>$earning['m_id'], 'm_user_id'=>$earning['m_user_id'], 'm_order_id'=>$earning['m_order_id'], 'sharer'=>$earning['sharer']], $earning);
}
}
public function orderUserInfo($order)
{
$name = $order->name;
if (empty($name)) {
$name = MerchantUser::where('id', $order->account_id)->value('nickname');
}
$mobile = $order->mobile;
if (empty($mobile) || $mobile == '未获取') {
$mobile = MerchantUser::where('id', $order->account_id)->value('mobile');
}
//到账时间
$rule = MEarningRule::where('m_id', $order->merchant_id)->where('name',$order->type)->where('type_id', $order->type_id)->first();
if (empty($rule)) {
$rule = MEarningRule::where('m_id', $order->merchant_id)->where('name', $order->type)->where('type_id', 0)->first();
}
$rule_date = $rule?$rule->forzen_time:0;
$rule_time = $rule_date * 24 * 3600;
if ($order->type == "activity") {
$activity = CommunityActivity::find($order->type_id);
//是否到了活动结束时间
$end_time = $activity->end_time;
$earning_time = strtotime($end_time) + $rule_time;
} else {
$earning_time = strtotime($order->created_at) + $rule_time;
}
return ['name'=>$name, 'mobile'=>$mobile, 'earning_time'=>date('Y-m-d H:i:s', $earning_time)];
}
public function messageInfo($order, $result){
$res = $this->orderUserInfo($order);
$order_user_name = $res['name'];
$order_user_mobile = $res['mobile'];
$earning_time = $res['earning_time'];
$first = '您推荐的用户【'.$order_user_name;
if ($order_user_mobile) {
$first = $first.','.substr_replace($order_user_mobile, '****', 3, 4);
}
if ($order->has_refund) {
$first = $first.'】购买的订单存在退款,您本单收益变动';
}else {
$first = $first.'】已经成功下单,您本单可能产生收益';
}
$this->sendTemplateMessage($result,$first, $order, $earning_time);
}
/**
* 合并数据
* @param $earnings
*/
public function combinEarnings($earnings)
{
$user_ids = [];
foreach ($earnings as $earning){
$user_ids[] = $earning['m_user_id'];
}
$user_ids = array_unique($user_ids);
$array = [];
foreach ($user_ids as $user_id) {
$amount = 0;
foreach ($earnings as $earning) {
if ($earning['m_user_id'] == $user_id) {
$amount += $earning['value'];
}
}
$array[] = ['user_id'=>$user_id, 'amount'=>$amount];
}
return $array;
}
/**
* 各角色收益金额
* @param $order
* @param $rule
* @param $channel_earning
* @return array
*/
public function earningAmount($first_merchant_user, $other_merchant_users, $last_merchant_user, $rule, $channel_earning, $order, $price)
{
$array = [];
$res = $this->orderUserInfo($order);
$earning_time = $res['earning_time'];
if (count($channel_earning)) {
$channel_earning['earning_time'] = $earning_time;
$array[] = $channel_earning;
}
//默认每分享人收益比例
$first_ratio = $rule->first_sharer;
$other_ratio = $rule->other_sharer;
$last_ratio = $rule->last_sharer;
if ($order->is_market_service) {//服务市场
if ($order->is_market_service == 1) {//服务市场
$market = MarketService::whereNull('alliance_id')->where('type',$order->origin_type)->where('type_id', $order->type_id)->where('market_type', 1)->whereNull('deleted_at')->first();
}else {
$market = MarketService::where('alliance_id', $order->merchant_id)->where('type',$order->origin_type)->where('type_id', $order->type_id)->where('market_type', 2)->whereNull('deleted_at')->first();
}
//商家提供的总推广收益
$total_earning = number_format($market->ratio * $price, 2, ".", '');
//代理商提供的推广收益
$share_total_earning = number_format($rule->ratio * $total_earning, 2, ".", '');
$first_sub_ratio = $market->ratio * $first_ratio;
$other_sub_ratio = $market->ratio * $other_ratio;
$last_sub_ratio = $market->ratio * $last_ratio;
} else { //普通商家
//用户可分配的金额
$share_total_earning = number_format($rule->ratio * $price, 2, ".", '');
$first_sub_ratio = $first_ratio;
$other_sub_ratio = $other_ratio;
$last_sub_ratio = $last_ratio;
}
$first_earning = 0;
$other_earning = 0;
$last_earning = 0;
if ($first_ratio > 0) {
//普通分享用户分成
$first_earning = number_format($first_ratio * $share_total_earning, 2, ".", '');
$array[]= ['m_user_id'=>$first_merchant_user?$first_merchant_user->id:0, 'value'=> $first_earning, 'type'=>$rule->name, 'sharer'=>'first_sharer', 'ratio'=>$rule->ratio, 'sub_ratio'=>$first_sub_ratio, 'm_id'=>$order->merchant_id, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time] ;
}
Log::info("预分成数量:".count($array));
Log::info("间接数量:".count($other_merchant_users));
if ($other_ratio > 0) {
$other_earning = number_format($other_ratio * $share_total_earning, 2, ".", '');
if (count($other_merchant_users) && $other_ratio > 0) { //简介用户
$remain_other_earning = $other_earning;
$index_count = $count = count($other_merchant_users);
$remain_other_ratio = $other_sub_ratio;
$index = 0;
foreach ($other_merchant_users as $other_merchant_user) {
$m_user_id = $other_merchant_user->id;
if ($index_count > 1) {
$sub_earning = number_format($remain_other_earning / $count, 2);
$sub_ratio = number_format($remain_other_ratio / $count, 2);
$remain_other_earning -= $sub_earning;
$remain_other_ratio -= $sub_ratio;
} else {
$sub_earning = $remain_other_earning;
$sub_ratio = $remain_other_ratio;
}
$array[]= ['m_user_id'=>$other_merchant_users[$index]->id, 'value'=> $sub_earning, 'type'=>$rule->name, 'sharer'=>'other_sharer', 'ratio'=>$rule->ratio, 'sub_ratio'=>$sub_ratio, 'm_id'=>$order->merchant_id, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time] ;
$index_count -= 1;
$index += 1;
}
}else {
$array[]= ['m_user_id'=>0, 'value'=> $other_earning, 'type'=>$rule->name, 'sharer'=>'other_sharer', 'ratio'=>$rule->ratio, 'sub_ratio'=>$other_ratio, 'm_id'=>$order->merchant_id, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time] ;
}
}
Log::info("预分成数量:".count($array));
if ($last_ratio > 0) {
//最终分享人
$last_earning = number_format($last_ratio * $share_total_earning, 2, ".", '');
$array[]= ['m_user_id'=>$last_merchant_user?$last_merchant_user->id:0, 'value'=> $last_earning, 'type'=>$rule->name, 'sharer'=>'last_sharer', 'ratio'=>$rule->ratio, 'sub_ratio'=>$last_sub_ratio, 'm_id'=>$order->merchant_id, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time];
}
Log::info("预分成数量:".count($array));
if ($order->is_market_service) {
//代理商家收益
$agent_sub_ratio = number_format(1 - $rule->ratio, 2, '.', '');
$agent_earning = number_format($total_earning - number_format( $first_earning + $other_earning + $last_earning , 2, '.', ''), 2, '.', '');
$array[]= ['m_id'=>$order->merchant_id, 'm_user_id'=>0, 'value'=> $agent_earning, 'type'=>$rule->name, 'sharer'=>'merchant', 'ratio'=>$market->ratio, 'sub_ratio'=>$agent_sub_ratio, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time] ;
//原商家收益
$m_earning = number_format($price - number_format( $total_earning + ($channel_earning['value']??0) , 2, '.', ''), 2, '.', '');
$m_ratio = number_format(1 - ($channel_earning['ratio']??0) - $market->ratio, 2, '.', '');
if ($m_earning < 0) throw new \Exception('商家收益小于0');
$array[]= ['m_id'=>$market->merchant_id, 'm_user_id'=>0, 'value'=> $m_earning, 'type'=>$rule->name, 'sharer'=>'merchant', 'ratio'=>$m_ratio, 'sub_ratio'=>1, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time];
}else {
$m_earning = number_format($price - number_format( $first_earning + $other_earning + $last_earning + ($channel_earning['value']??0) , 2, '.', ''), 2, '.', '');
$m_ratio = number_format(1 - ($channel_earning['ratio']??0) - $rule->ratio, 2, '.', '');
$array[]= ['m_id'=>$order->merchant_id, 'm_user_id'=>0, 'value'=> $m_earning, 'type'=>$rule->name, 'sharer'=>'merchant', 'ratio'=>$m_ratio, 'sub_ratio'=>1, 'm_order_id'=>$order->id, 'earning_time'=>$earning_time] ;
}
Log::info("预分成数量:".count($array));
return $array;
}
/**
* 渠道收益
*/
public function channelEarning($order, $rule, $price)
{
$res = $this->orderUserInfo($order);
$earning_time = $res['earning_time'];
$array = [];
//渠道分享分成 分享比例 * 订单金额
if ($order->share_channel_id) {
$channel = MerchantShareChannel::where('id', $order->share_channel_id)->where('m_id', $order->merchant_id)->first();
if ($channel && $channel->ratio) {
if ($channel->m_id == $order->merchant_id) {
//生成渠道分享记录
if ($channel->bind_m_user_id) {
$array['value'] = number_format($price * $channel->ratio, 2, ".", '');
$array['ratio'] = $channel->ratio;
$array['m_id'] = $channel->m_id;
$array['m_user_id'] = $channel->bind_m_user_id;
$array['m_order_id'] = $order->id;
$array['type'] = $rule->name;
$array['sharer'] = 'channel';
$array['sub_ratio'] = 1;
$array['earning_time'] = $earning_time;
}
}
}
}
return $array;
}
/**
* 渠道规则
* @param $order
* @return mixed
*/
public function earningRule($order)
{
//收益规则没有单个服务设计获取type_id=0
$rule = MEarningRule::where('m_id', $order->merchant_id)->where('name',$order->type)->where('type_id', $order->type_id)->first();
if (empty($rule)) {
$rule = MEarningRule::where('m_id', $order->merchant_id)->where('name', $order->type)->where('type_id', 0)->first();
}
# 收益规则
// $rule = MEarningRule::where('name', $order->type)->where('m_id', $order->merchant_id)->first();
return $rule;
}
/**
* 小程序首邀用户
* @param $order
* @return User|void|null
*/
public function mpFirstMerchatUser($order)
{
$first_merchant_user = null;
$wechat = Wechat::where('openid', $order->open_id)->first();
if ($wechat && $wechat->user) {
$first_wechat = null;
if ($wechat->user->from_user_id) {
$first_wechat = Wechat::where('user_id', $wechat->user->from_user_id)->first();
}
if (empty($first_wechat) && $wechat->user->from_openid) {
$first_wechat = Wechat::where('openid', $wechat->user->from_openid)->first();
}
if ($first_wechat) {
$first_merchant_user = $this->merchantUserByMpOpenid($first_wechat->openid);
}
}
return $first_merchant_user;
}
public function saasFirstMerchantUser($order)
{
$first_merchant_user = null;
//第一分享人(分享注册)
$first_sharer = AccessRecord::where('open_id', $order->open_id)->where('account_id', $order->merchant_id)->orderBy('id', 'asc')->value('recommender');
if ($first_sharer) {
$first_merchant_user = MerchantUser::where('openid', $first_sharer)->first();
}
return $first_merchant_user;
}
/**
* 小程序间接用户
* @param $order
* @return array
*/
public function mpOtherMerchantUsers($order)
{
//上一次成功订单id默认0
if ($order->type == 'service' || $order->type == 'activity') {
$last_order = Order::where('account_id', $order->account_id)->whereIn('channel', [2,3])->where('pay_status', '<>', 0)->where('type', 'community')->where('type_id', $order->type_id)->where('id', '<', $order->id)->orderBy('id', 'desc')->first();
} else {
$last_order = Order::where('account_id', $order->account_id)->whereIn('channel', [2,3])->where('pay_status', '<>', 0)->where('type', $order->type)->where('type_id', $order->type_id)->where('id', '<', $order->id)->orderBy('id', 'desc')->first();
}
$last_order_id = $last_order ? $last_order->id : 0;
//其他分享人
if ($order->type == 'service' || $order->type == 'activity') {
$other_sharers = Order::where('account_id', $order->account_id)->whereIn('channel', [2,3])->where('pay_status', 0)->where('type', 'community')->where('type_id', $order->type_id)->where('id', '>', $last_order_id)->where('id', '<', $order->id)->where('from_openid', '<>', $order->openid)->where('from_openid', '<>', $order->from_openid)->whereNotNull('from_openid')->orderBy('id', 'desc')->pluck('from_openid')->toArray();
} else {
$other_sharers = Order::where('account_id', $order->account_id)->whereIn('channel', [2,3])->where('pay_status', 0)->where('type', $order->type)->where('type_id', $order->type_id)->where('id', '>', $last_order_id)->where('id', '<', $order->id)->where('from_openid', '<>', $order->openid)->where('from_openid', '<>', $order->from_openid)->whereNotNull('from_openid')->orderBy('id', 'desc')->pluck('from_openid')->toArray();
}
$other_sharers = array_unique($other_sharers);
$other_merchant_users = [];
foreach ($other_sharers as $other_sharer) {
$merchant_user = $this->merchantUserByMpOpenid($other_sharer);
if ($merchant_user) $other_merchant_users[] = $merchant_user;
}
return $other_merchant_users;
}
/**
* saas间接用户
*/
public function saasOtherMerchantUsers($order)
{
if ($order->type == 'service' || $order->type == 'activity'){
$last_order = Order::where('open_id', $order->open_id)->where('pay_status','<>', 0)->where('type', 'community')->where('type_id', $order->type_id)->where('id', '<', $order->id)->orderBy('id', 'desc')->first();
}else{
$last_order = Order::where('open_id', $order->open_id)->where('pay_status','<>', 0)->where('type', $order->type)->where('type_id', $order->type_id)->where('id', '<', $order->id)->orderBy('id', 'desc')->first();
}
$last_order_id = $last_order?$last_order->id:0;
//其他分享人
if ($order->type == 'service' || $order->type == 'activity'){
$other_sharers = Order::where('open_id', $order->open_id)->where('type', 'community')->where('type_id', $order->type_id)->where('id', '>', $last_order_id)->where('id', '<', $order->id)->where('pay_status', 0)->where('from_openid', '<>', $order->openid)->where('from_openid', '<>', $order->from_openid)->whereNotNull('from_openid')->orderBy('id', 'desc')->pluck('from_openid')->toArray();
}else{
$other_sharers = Order::where('open_id', $order->open_id)->where('type', $order->type)->where('type_id', $order->type_id)->where('id', '>', $last_order_id)->where('id', '<', $order->id)->where('pay_status', 0)->where('from_openid', '<>', $order->openid)->where('from_openid', '<>', $order->from_openid)->whereNotNull('from_openid')->orderBy('id', 'desc')->pluck('from_openid')->toArray();
}
$other_sharers = array_unique($other_sharers);
$merchant_users = MerchantUser::whereIn('openid', $other_sharers)->whereNotNull('openid')->get();
return $merchant_users;
}
/**
* saas间接用户V2 最多三位
*/
public function saasOtherMerchantUsersV2($order)
{
Log::info("saasOtherMerchantUsersV2");
$type = "";
if ($order->type == 'service' || $order->type == 'activity'){
$type = "App\Models\CommunityActivity";
}elseif ($order->type == "shop") {
$type = "App\Models\MerchantShop";
}elseif ($order->type == "course") {
$type = "App\Models\Course\Course";
}elseif ($order->type == "course") {
$type = "App\Models\Server\MerchantEvaluate";
}
$shares = \DB::table('m_earning_shares')->where('user_id', $order->account_id)->where('type', $type)->where('type_id', $order->type_id)
->groupBy("from_openid")
->orderByDesc("date")
->limit(3)
->selectRaw("MAX(created_at) as date, from_openid")
->get();
$other_sharers = [];
foreach ($shares as $share) {
$other_sharers[] = $share->from_openid;
}
Log::info($other_sharers);
$merchant_users = MerchantUser::whereIn('openid', $other_sharers)->whereNotNull('openid')->get();
return $merchant_users;
}
/**
* 小程序促单用户
* @param $order
* @return User|null
*/
public function mpLastMerchantUser($order)
{
$last_sharer = $order->from_openid;
if ($last_sharer == 'null') return null;
$last_merchant_user = null;
if ($last_sharer && $last_sharer != $order->open_id) {
$last_merchant_user = $this->merchantUserByMpOpenid($order->from_openid);
}
return $last_merchant_user;
}
/**
* saas促单用户
*/
public function saasLastMerchantUser($order)
{
$last_merchant_user = null;
if ($order->from_openid == 'null') return null;
if ( $order->from_openid && $order->from_openid != $order->open_id) {
$last_merchant_user = MerchantUser::where('openid', $order->from_openid)->first();
}
return $last_merchant_user;
}
public function merchantUserByMpOpenid($openid)
{
$merchant_user = null;
$wechat = Wechat::where('openid', $openid)->first();
if ($wechat && $wechat->user){
$merchant_user = MerchantUser::where('user_id', $wechat->user_id)->first();
if (empty($merchant_user)) {
if ($wechat->unionid) {
$other_merchant_user = MerchantUser::where('unionid', $wechat->unionid)->first();
if ($other_merchant_user && $other_merchant_user->user_id) {
$merchant_user = new MerchantUser();
$merchant_user->user_id = $wechat->user_id;
$merchant_user->unionid = $wechat->unionid;
$merchant_user->save();
$other_merchant_user->unionid = null;
$other_merchant_user->save();
}elseif ($other_merchant_user && empty($other_merchant_user->user_od)) {
$other_merchant_user->user_id = $wechat->user_id;
$other_merchant_user->save();
$merchant_user = $other_merchant_user;
}elseif (empty($merchant_user)) {
$merchant_user = new MerchantUser();
$merchant_user->user_id = $wechat->user_id;
$merchant_user->unionid = $wechat->unionid;
$merchant_user->save();
}
}
}
}
return $merchant_user;
}
/**
* 最终用户角色
*/
public function getShare($first_sharer, $other_sharers, $last_sharer, $system_rule)
{
if ($system_rule && $system_rule->first_sharer == 0.3 && $system_rule->other_sharer == 0.2 && $system_rule->last_sharer == 0.5) {
if (empty($first_sharer)) {
if ($last_sharer) {
$first_sharer = $last_sharer;
}elseif (count($other_sharers)) {
$first_sharer = $other_sharers[0];
}
}
if (empty(count($other_sharers))) {
if ($last_sharer) {
$other_sharers = [$last_sharer];
}elseif ($first_sharer) {
$other_sharers = [$first_sharer];
}
}
if (empty($last_sharer)) {
if (count($other_sharers)) {
$last_sharer = $other_sharers[0];
}elseif($first_sharer) {
$last_sharer = $first_sharer;
}
}
}
Log::info("getShare");
Log::info($other_sharers);
return ['first_sharer'=>$first_sharer, 'other_sharers'=>$other_sharers, 'last_sharer'=>$last_sharer];
}
}