love_php/app/Jobs/MakeShareConsumeCash.php

192 lines
6.2 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\Live\Viewer;
use App\Models\Live\ViewerShare;
use App\Models\Live\Asset;
use App\Models\Live\AssetLog;
class MakeShareConsumeCash implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
protected $order;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($order)
{
$this->order = $order;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
//查找第一个分享人
$sharer = $this->getViewerSharer($this->order);
if (empty($sharer)) {
throw new \Exception("没有一级分享者", 1);
}
//是否该订单已经生成过收益
$result = $this->checkMakeShareCash($this->order, $sharer);
if (empty($result)) {
//生成分享消费效益
$result = $this->makeShareCash($this->order, $sharer, $type='main');
if ($result) {
$this->getShareRoleViewer($this->order, $sharer);
}
}
} catch (\Exception $e) {
\Log::error($e->getMessage().';line-'.$e->getLine());
}
}
public function checkMakeShareCash($order, $viewer)
{
$result = AssetLog::where('viewer_id', $viewer->id)->where('log_id', $order->id)->exists();
return $result;
}
public function getShareRoleViewer($order, $viewer)
{
for ($i=0; $i < 3; $i++) {
$sharer_id = ViewerShare::where('viewer_id', $viewer->id)->where('type', 1)->value('sharer_id');
if ($sharer_id) {
$sharer = Viewer::find($sharer_id);
$result = $this->checkMakeShareCash($order, $sharer);
if (empty($result)) {
$result = $this->makeShareCash($order, $sharer, $type="minor", $viewer);
}
$viewer = $sharer;
}else{
break;
}
}
}
public function makeShareCash($order, $viewer, $type, $previous_viewer=null)
{
try {
//开启事务
\DB::beginTransaction();
//判断用户是否存在
if (empty($viewer)) {
throw new \Exception("分享者不存在", 500);
}
//判断是否有分享角色
$share_role = $viewer->shareRole();
if (empty($share_role)) {
throw new \Exception('分享者没有分享角色', 500);
}
//收益金额
$rule = $share_role->rule();
if ($type == 'main') {
$consume_values = $rule['consume_values'];
$values = ($order->cash) * $consume_values;
}else{
$previous_viewer_role = $previous_viewer->shareRole();
$previous_viewer_role_name = $previous_viewer_role->name;
switch ($previous_viewer_role_name) {
case '普通推广员':
$key = 'pt_consume_values';
break;
case '铜牌推广员':
$key = 'tp_consume_values';
break;
case '银牌推广员':
$key = 'yp_consume_values';
break;
default:
$key = '';
break;
}
if ($key) {
$consume_values = $rule[$key];
$values = ($order->cash) * $consume_values;
}else{
throw new \Exception('分享者的上一级没有对应身份', 500);
}
}
if ($values == 0) {
throw new \Exception('分享者没有分享收益金额', 500);
}
$asset = Asset::firstOrCreate(['viewer_id'=>$viewer->id]);
// $asset->increment('share_cash', $values);
$score = (($asset->share_cash * 100) + ($asset->cash * 100)) / 100;
//生成收益记录
$this->storeAssetLog($viewer->id, $values, $score, $order);
//提交事务
\DB::commit();
return true;
} catch (\Exception $e) {
//回滚事务
\DB::rollBack();
\Log::error($e->getMessage().';line-'.$e->getLine());
return false;
}
}
public function storeAssetLog($viewer_id, $share_values, $score, $order)
{
$log = new AssetLog;
$log->viewer_id = $viewer_id;
$log->num = $share_values;
$log->type = 4;
$log->score = $score;
$log->log_id = $order->trade_no;
$log->save();
return ;
}
public function getViewerSharer($order)
{
try {
$wechat = Wechat::with('user')->where('user_id', $order->user_id)->first();
if (empty($wechat)) {
throw new \Exception("没有小程序微信信息", 1);
}
$unionid = $wechat->unionid;
$mobile = $wechat->user->mobile;
if (empty($unionid)) {
$viewer = Viewer::where('mobile', $mobile)->first();
}else{
$viewer = Viewer::where('unionid', $unionid)->first();
if (empty($viewer)) {
$viewer = Viewer::where('mobile', $mobile)->first();
}
}
if (empty($viewer)) {
throw new \Exception("小程序信息与直播信息关联失败", 1);
}
$sharer = ViewerShare::where('viewer_id', $viewer->id)->where('type', 1)->first();
if (empty($sharer)) {
throw new \Exception("订单没有分享者", 1);
}
return $sharer->sharer;
} catch (\Exception $e) {
\Log::error($e->getMessage().';line-'.$e->getLine());
return false;
}
}
}