love_php/app/Jobs/StoreOrderWorthShare.php

115 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Wechat;
Use App\Models\WorthShare;
use App\Models\User;
class StoreOrderWorthShare implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $order;
protected $pay_order;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($order,$pay_order)
{
$this->order = $order;
$this->pay_order = $pay_order;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//订单绑定价值
$order = $this->order;
$target_user_id = $order->user_id;
$user = User::find($order->user_id);
if($user && ($user->from_user_id || $user->from_openid)){
$bind_user_id = $user->from_user_id?:null;
if(empty($bind_user_id)){
$bind_user_id = Wechat::where('openid', $user->from_openid)->whereNotNull('openid')->value("user_id");
}
if($bind_user_id){
$data = [
'user_id'=>$bind_user_id,
'target_user_id'=>$target_user_id,
'channel'=>$this->getChannel($order),
'sub_channel'=>'bind',
'product_id'=>$order->type_id,
'order_id'=>$order->id,
];
$share = WorthShare::where($data)->first();
if(empty($share)){
WorthShare::addWorthShare($data);
}
}
}
//订单聊天价值
$chat_user_id = $order->chat_user_id;
if($chat_user_id){
$data = [
'user_id'=>$chat_user_id,
'target_user_id'=>$target_user_id,
'channel'=>$this->getChannel($order),
'sub_channel'=>'user',
'product_id'=>$order->type_id,
'order_id'=>$order->id,
];
$share = WorthShare::where($data)->first();
if(empty($share)){
WorthShare::addWorthShare($data);
}
}
//订单直接分享价值
$from_user_id = $order->from_user_id;
$from_openid = $order->from_openid;
if(empty($from_user_id) && $from_openid){
$from_user_id = Wechat::where('openid', $from_openid)->value('user_id');
}
if(!empty($from_user_id)){
$data = [
'user_id'=>$from_user_id,
'target_user_id'=>$target_user_id,
'channel'=>$this->getChannel($order),
'sub_channel'=>'direct',
'product_id'=>$order->type_id,
'order_id'=>$order->id,
];
$share = WorthShare::where($data)->first();
if(empty($share)){
WorthShare::addWorthShare($data);
}
}
return true;
}
public function getChannel($order)
{
switch($order->type){
case 'single_service':
$channel = 'service';
break;
default :
$channel = $order->type;
break;
}
return $channel;
}
}