883 lines
40 KiB
PHP
883 lines
40 KiB
PHP
<?php
|
||
namespace Yunshop\WeightValue\models;
|
||
|
||
|
||
use app\common\models\BaseModel;
|
||
use app\common\models\Member;
|
||
use app\common\models\member\MemberChildren;
|
||
use app\common\models\member\MemberParent;
|
||
use app\common\models\Order;
|
||
use app\common\models\OrderGoods;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Yunshop\ShareholderDividend\models\ShareholderDividendModel;
|
||
use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
|
||
use Yunshop\TeamDividend\models\TeamDividendLevelModel;
|
||
use Yunshop\TeamDividend\models\TeamDividendModel;
|
||
use Yunshop\TeamDividend\models\Uplog;
|
||
|
||
class WeightValue extends BaseModel{
|
||
|
||
public $table = 'yz_weight_value';
|
||
public $timestamps = false;
|
||
|
||
private static $weightValueLevelTotal = [];// 每个等级的权重值总数列表
|
||
|
||
|
||
/**
|
||
* Common: 购买商品赠送权重值 - 开始处理
|
||
* Author: wu-hui
|
||
* Time: 2023/10/12 15:10
|
||
* @param $model
|
||
*/
|
||
public function giveInit($model){
|
||
\Log::debug('------购买商品赠送权重值 - 开始处理------',$model->id);
|
||
DB::beginTransaction();
|
||
try{
|
||
// 当前订单全部商品
|
||
$goodsList = $this->getGoodsList($model->id);
|
||
// 获取当前用户及上级信息
|
||
$agents = $this->getParent($model->uid);
|
||
$agentAllList = [];
|
||
foreach(array_merge($agents['current_user'],$agents['parents'],$agents['agents']) as $agentSingleInfo){
|
||
$agentAllList[(string)$agentSingleInfo['uid']] = $agentSingleInfo;
|
||
}
|
||
// 循环处理每个商品
|
||
$changeRecord = [];// 变更记录
|
||
foreach($goodsList as $goodsInfo){
|
||
$agentsUseParents = array_values($agents['parents']);
|
||
// 判断:根据赠送方式 是否删除最后一个上级
|
||
if((int)$goodsInfo['lv_type'] == 0) unset($agentsUseParents[1]);
|
||
$useAgentList = [];
|
||
$mergeAgentList = array_merge($agentsUseParents,$agents['agents']);// 默认上级信息
|
||
// 判断:是否开启自购
|
||
if((int)$goodsInfo['is_self_purchase'] == 1) $mergeAgentList = array_merge($agents['current_user'],$agentsUseParents,$agents['agents']);
|
||
foreach($mergeAgentList as $useAgentItem){
|
||
$useAgentList[$useAgentItem['uid']] = $useAgentItem;
|
||
}
|
||
// 循环上级信息 处理每一个用户、每一个商品的赠送情况
|
||
foreach($useAgentList as $agentKey => $agentInfo){
|
||
// 用户当前的权重值
|
||
$currentAgentHasWeightValue = (float)$agentAllList[$agentInfo['uid']]['weight_value']['quantity'] ?? 0;
|
||
// 符合条件 获取当前用户应得的权重值
|
||
$totalWeightValue = $this->getWeightValue($goodsInfo,$agentInfo);// 应得权重值
|
||
// 应得权重值大于0 记录改变信息并且修改持有权重值
|
||
if((float)$totalWeightValue > 0){
|
||
$changeAfter = sprintf("%.2f",($currentAgentHasWeightValue + $totalWeightValue));
|
||
$changeRecord[] = [
|
||
'uniacid' => $model->uniacid,
|
||
'member_id' => $agentInfo['uid'],// 用户id
|
||
'team_dividend_agency_level_id' => $agentInfo['team_dividend_agency_level_id'],// 经销商等级id
|
||
'goods_id' => $goodsInfo['goods_id'],// 商品id
|
||
'order_id' => $model->id,// 订单id
|
||
'order_goods_id' => $goodsInfo['id'],// 订单商品id
|
||
'change_type' => 1,// 变更类型:0=减少,1=增加
|
||
'change_quantity' => $totalWeightValue,// 变更数量
|
||
'change_front' => (float)$currentAgentHasWeightValue,// 变更前拥有的数量
|
||
'change_after' => $changeAfter,// 变更后拥有的数量
|
||
'remark' => "订单{$model->order_sn}赠送",// 备注
|
||
'created_at' => time(),// 变更时间
|
||
];
|
||
$agentAllList[$agentInfo['uid']]['weight_value']['quantity'] = $changeAfter;
|
||
}
|
||
}
|
||
}
|
||
// 修改用户权重值变更信息 | 处理权重值变更记录
|
||
if($changeRecord){
|
||
$updateWeightValueList = array_column($agentAllList,'weight_value');
|
||
self::updateInfo($updateWeightValueList);
|
||
WeightValueLog::insert($changeRecord);
|
||
}
|
||
|
||
DB::commit();
|
||
\Log::debug('------购买商品赠送权重值 - 处理完成------',$model->id);
|
||
}catch(\Exception $e){
|
||
\Log::debug('------购买商品赠送权重值 - 错误抛出------',$e->getMessage());
|
||
DB::rollBack();
|
||
}
|
||
|
||
return;
|
||
}
|
||
/**
|
||
* Common: 购买商品赠送权重值 - 获取相关商品列表
|
||
* Author: wu-hui
|
||
* Time: 2023/10/12 11:26
|
||
* @param $orderId
|
||
* @return array
|
||
*/
|
||
private function getGoodsList($orderId){
|
||
$list = OrderGoods::uniacid()
|
||
->join('yz_goods_weight_value', 'yz_goods_weight_value.goods_id', 'yz_order_goods.goods_id')
|
||
->where('yz_order_goods.order_id',$orderId)
|
||
->where('yz_goods_weight_value.is_open',1)
|
||
->select([
|
||
'yz_order_goods.id',
|
||
'yz_order_goods.goods_id',
|
||
'yz_order_goods.total',
|
||
'yz_order_goods.title',
|
||
'yz_order_goods.payment_amount',
|
||
'yz_goods_weight_value.is_open',
|
||
'yz_goods_weight_value.is_self_purchase',
|
||
'yz_goods_weight_value.gift_type',
|
||
'yz_goods_weight_value.lv_type',
|
||
'yz_goods_weight_value.calculation_type',
|
||
'yz_goods_weight_value.quantity',
|
||
'yz_goods_weight_value.ladder',
|
||
'yz_goods_weight_value.agent_gift_type',
|
||
'yz_goods_weight_value.agent_calculation_type',
|
||
'yz_goods_weight_value.agent_quantity',
|
||
'yz_goods_weight_value.agent_ladder',
|
||
'yz_goods_weight_value.province_gift_type',
|
||
'yz_goods_weight_value.province_calculation_type',
|
||
'yz_goods_weight_value.province_quantity',
|
||
'yz_goods_weight_value.province_ladder',
|
||
])
|
||
->get()
|
||
->makeHidden(['order','after_sales','buttons']);
|
||
|
||
return $list ? $list->toArray() : [];
|
||
}
|
||
/**
|
||
* Common: 赠送权重值 - 获取上级信息
|
||
* Author: wu-hui
|
||
* Time: 2023/10/23 14:51
|
||
* @param $uid
|
||
* @return array
|
||
*/
|
||
private function getParent($uid){
|
||
// 获取上级信息
|
||
$parents = MemberParent::uniacid()
|
||
->where('member_id', $uid)
|
||
->select(['parent_id','level'])
|
||
->orderBy('level', 'asc')
|
||
->get()
|
||
->toArray();
|
||
// 包含购买人
|
||
$parents = array_merge([['parent_id' => $uid,'level' => 0]],$parents);
|
||
$uidS = array_column($parents,'parent_id');
|
||
// 获取经销商信息
|
||
$teamDividend = TeamDividendAgencyModel::uniacid()
|
||
->join('yz_team_dividend_level', 'yz_team_dividend_level.id', '=', 'yz_team_dividend_agency.level')
|
||
->select([
|
||
'yz_team_dividend_agency.uid',
|
||
'yz_team_dividend_agency.level',
|
||
'yz_team_dividend_agency.is_black',
|
||
'yz_team_dividend_level.identity_type',
|
||
])
|
||
->whereIn('uid',$uidS)
|
||
->get();
|
||
$teamDividend = $teamDividend ? $teamDividend->keyBy('uid')->toArray() : [];
|
||
// 获取相关用户的权重值信息
|
||
$weightValueList = WeightValue::uniacid()
|
||
->select(['id','member_id','team_dividend_agency_level_id','quantity'])
|
||
->whereIn('member_id',(array)$uidS)
|
||
->get()
|
||
->toArray();
|
||
$weightValueGroup = [];
|
||
foreach($weightValueList as $weightValueItem){
|
||
$weightValueGroup[$weightValueItem['member_id']][$weightValueItem['team_dividend_agency_level_id']] = $weightValueItem;
|
||
}
|
||
// 获取全部经销商 以此来获取排名信息
|
||
$allTeamDividend = TeamDividendLevelModel::getRanking();
|
||
$agents = [];
|
||
foreach($parents as $memberInfo){
|
||
$teamDividendInfo = $teamDividend[$memberInfo['parent_id']] ?? [];
|
||
if($teamDividendInfo) {
|
||
$currentRank = $allTeamDividend[(int)$teamDividendInfo['level']]['up_log'] ?? [];
|
||
$teamDividendInfo['ranking'] = ($currentRank[$memberInfo['parent_id']] + 1);
|
||
$weightValue = $weightValueGroup[$memberInfo['parent_id']][(int)$teamDividendInfo['level']] ?? [
|
||
'uniacid' => \YunShop::app()->uniacid,
|
||
'member_id' => $memberInfo['parent_id'],
|
||
'team_dividend_agency_level_id' => (int)$teamDividendInfo['level'],
|
||
'quantity' => 0,
|
||
];
|
||
$agents[$memberInfo['parent_id']] = [
|
||
'uid' => $memberInfo['parent_id'],
|
||
'level' => $memberInfo['level'],
|
||
'is_black' => (int)$teamDividendInfo['is_black'] ?? 0,// 是否为黑名单
|
||
'team_dividend_agency_level_id' => (int)$teamDividendInfo['level'],
|
||
'identity_type' => (int)$teamDividendInfo['identity_type'],
|
||
'weight_value' => $weightValue,
|
||
'teamDividend' => $teamDividendInfo
|
||
];
|
||
}
|
||
}
|
||
// 处理上级列表 取前二
|
||
$parentList = $agents;
|
||
unset($parentList[$uid]);
|
||
$parentList = array_splice($parentList,0,2);
|
||
// 获取代理商信息 这里只取第一个代理商
|
||
$agentInfo = collect($agents)
|
||
->where('identity_type',2)
|
||
->where('uid','!=',$uid)
|
||
->first();
|
||
return [
|
||
// 当前用户
|
||
'current_user' => [$uid => $agents[$uid]],
|
||
// 上级列表
|
||
'parents' => array_column($parentList,null,'uid'),
|
||
// 代理商
|
||
'agents' => [$agentInfo['uid'] => $agentInfo],
|
||
];
|
||
}
|
||
/**
|
||
* Common: 赠送权重值 - 获取应得权重值
|
||
* Author: wu-hui
|
||
* Time: 2023/10/23 15:28
|
||
* @param $currentSale
|
||
* @param $agentInfo
|
||
* @return float
|
||
*/
|
||
private function getWeightValue($currentSale,$agentInfo){
|
||
$deservedValue = 0;// 应得权重值(固定值||比例)
|
||
$calculationType = 0;// 计算类型:0=固定值,1=支付比例
|
||
// 获取应得值 特殊身份:0=无特殊身份,1=经纪人,2=代理商,3=省级代理
|
||
switch($agentInfo['identity_type']){
|
||
// 经纪人处理
|
||
case 1:
|
||
$calculationType = (int)$currentSale['calculation_type'];
|
||
if((int)$currentSale['gift_type'] == 0) $deservedValue = $currentSale['quantity'];// 赠送固定值
|
||
else if($agentInfo['teamDividend']) $deservedValue = $this->getRankGive($currentSale['ladder'],(int)$agentInfo['teamDividend']['ranking']);// 阶梯赠送
|
||
else $deservedValue = (float)0;
|
||
break;
|
||
// 代理商处理
|
||
case 2:
|
||
$calculationType = (int)$currentSale['agent_calculation_type'];
|
||
if((int)$currentSale['agent_gift_type'] == 0) $deservedValue = $currentSale['agent_quantity'];
|
||
else if($agentInfo['teamDividend']) $deservedValue = $this->getRankGive($currentSale['agent_ladder'],(int)$agentInfo['teamDividend']['ranking']);
|
||
else $deservedValue = (float)0;
|
||
break;
|
||
}
|
||
// 判断:是固定值还是比例,固定金额需要乘以商品购买数量,比例根据商品实际支付价格的比例
|
||
if((int)$calculationType == 1) return (float)sprintf("%.2f",$currentSale['payment_amount'] * $deservedValue / 100);// 实际支付金额的比例
|
||
else return (float)sprintf("%.2f",$currentSale['total'] * $deservedValue);// 总数量 * 应得权重值 = 总应得权重值
|
||
}
|
||
/**
|
||
* Common: 获取排名奖
|
||
* Author: wu-hui
|
||
* Time: 2023/10/23 15:18
|
||
* @param $ladder
|
||
* @param $ranking
|
||
* @return float
|
||
*/
|
||
private function getRankGive($ladder,$ranking){
|
||
$ladderList = json_decode($ladder,true);
|
||
array_multisort(array_column($ladderList,'where'),SORT_ASC, $ladderList);
|
||
$weightValueNum = 0;
|
||
foreach($ladderList as $ladder){
|
||
if($ranking <= $ladder['where'] && $ranking > 0){
|
||
$weightValueNum = (float)$ladder['num'];
|
||
break;
|
||
}
|
||
}
|
||
|
||
return (float)$weightValueNum;
|
||
}
|
||
|
||
|
||
/**
|
||
* Common: 经销商升级赠送权重值 - 开始处理
|
||
* Author: wu-hui
|
||
* Time: 2023/10/16 18:24
|
||
* @param $uid
|
||
* @param $levelId
|
||
*/
|
||
public function upgradeGiveInit($uid,$levelId){
|
||
\Log::debug('------经销商升级赠送权重值 - 开始处理------',['uid'=>$uid,'level_id'=>$levelId]);
|
||
// 获取当前用户及上级信息
|
||
$agents = $this->getParent($uid);
|
||
// $parents = array_merge($agents['current_user'],$agents['parents']);
|
||
$parents = $agents['current_user'];// 修改:经销商升级仅赠送给本人
|
||
// 等级赠送设置信息
|
||
$levelWvSet = $this->getLevelWeightValue($levelId);
|
||
if(!$levelWvSet){
|
||
\Log::debug('------经销商升级赠送权重值 - 错误 - 当前等级不存在设置信息------',$levelId);
|
||
return;
|
||
}
|
||
$changeRecord = [];// 变更记录
|
||
// 判断:赠送等级为二级时,删除第三个用户 赠送等级:0=二级,1=三级
|
||
// if($levelWvSet['lv_type'] == 1 && count($parents) >= 3) unset($parents[2]);
|
||
// 获取:当前需要操作的所有经销商的领取记录 是否已经领取升级赠送的权重值
|
||
$parentUidS = array_column($parents,'uid');
|
||
$getRecord = WeightValueLog::uniacid()
|
||
->whereIn('member_id',$parentUidS)
|
||
->where('source',1)
|
||
->where('team_dividend_agency_level_id',$levelId)
|
||
->pluck('member_id');
|
||
$getRecord = $getRecord ? $getRecord->toArray() : [];// 已领取记录列表
|
||
// 循环上级信息 处理每一个用户
|
||
foreach($parents as $agentKey => $agentInfo){
|
||
// 判断:当前用户是否已经领取 已领取不可重复领取
|
||
if(in_array($agentInfo['uid'],$getRecord)) {
|
||
\Log::debug('------经销商升级赠送权重值 - 错误 - 当前用户已经领取 ------',$agentInfo['uid']);
|
||
continue;
|
||
}
|
||
// 用户当前持有的权重值
|
||
$currentAgentHasWeightValue = (float)$parents[$agentKey]['weight_value']['quantity'] ?? 0;
|
||
// 获取当前用户应得的权重值
|
||
$weightValue = (float)sprintf("%.2f",$this->getGiveWeightValue($levelWvSet,$agentInfo));
|
||
// 应得权重值大于0 记录改变信息并且修改持有权重值
|
||
if((float)$weightValue > 0){
|
||
$changeAfter = sprintf("%.2f",($currentAgentHasWeightValue + $weightValue));
|
||
$changeRecord[] = [
|
||
'uniacid' => \YunShop::app()->uniacid,
|
||
'member_id' => $agentInfo['uid'],// 用户id
|
||
'team_dividend_agency_level_id' => $agentInfo['team_dividend_agency_level_id'],// 经销商等级id
|
||
'change_type' => 1,// 变更类型:0=减少,1=增加
|
||
'change_quantity' => $weightValue,// 变更数量
|
||
'change_front' => (float)$currentAgentHasWeightValue,// 变更前拥有的数量
|
||
'change_after' => $changeAfter,// 变更后拥有的数量
|
||
'remark' => "升级赠送",// 备注
|
||
'created_at' => time(),// 变更时间
|
||
'source' => 1
|
||
];
|
||
$parents[$agentKey]['weight_value']['quantity'] = $changeAfter;
|
||
}
|
||
}
|
||
|
||
// 修改用户权重值变更信息 | 处理权重值变更记录
|
||
if($changeRecord){
|
||
$updateWeightValueList = array_column($parents,'weight_value');
|
||
self::updateInfo($updateWeightValueList);
|
||
WeightValueLog::insert($changeRecord);
|
||
}
|
||
return;
|
||
}
|
||
/**
|
||
* Common: 经销商升级赠送权重值 - 获取对应等级的设置
|
||
* Author: wu-hui
|
||
* Time: 2023/10/16 17:57
|
||
* @param $levelId
|
||
* @return array
|
||
*/
|
||
private function getLevelWeightValue($levelId){
|
||
$first = GoodsWeightValue::uniacid()
|
||
->where('team_dividend_level_id',$levelId)
|
||
->select(['is_open','gift_type','lv_type','quantity','ladder'])
|
||
->first();
|
||
|
||
return $first ? $first->toArray() : [];
|
||
}
|
||
/**
|
||
* Common: 经销商升级赠送权重值 - 获取应得权重值
|
||
* Author: wu-hui
|
||
* Time: 2023/10/23 15:10
|
||
* @param $currentSale
|
||
* @param $agentInfo
|
||
* @return float|int
|
||
*/
|
||
private function getGiveWeightValue($currentSale,$agentInfo){
|
||
if((int)$currentSale['gift_type'] == 0){
|
||
// 赠送固定值
|
||
return (float)$currentSale['quantity'];
|
||
}else if($agentInfo['teamDividend']){
|
||
// 阶梯赠送 阶梯判断条件必须按照从大到小排序
|
||
return (float)$this->getRankGive($currentSale['ladder'],(int)$agentInfo['teamDividend']['ranking']);
|
||
}else {
|
||
return (float)0;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* Common: 区域代理提成 - 开始处理
|
||
* Author: wu-hui
|
||
* Time: 2023/10/27 11:25
|
||
* @param $uid
|
||
* @param $model
|
||
* @param $weightValueRatio
|
||
*/
|
||
public function areaGiveInit($uid,$model,$weightValueRatio){
|
||
DB::beginTransaction();
|
||
try{
|
||
// 当前订单全部商品
|
||
$goodsList = $this->getGoodsList($model->id);
|
||
// 获取当前用户及上级信息 由于是【区域分红】这里只使用本人的信息进行处理 上级&代理商不进行处理
|
||
$agents = $this->getParent($uid);
|
||
$currentUser = array_values($agents['current_user'])[0];
|
||
// 用户当前持有的权重值
|
||
$currentAgentHasWeightValue = (float)$currentUser['weight_value']['quantity'] ?? 0;
|
||
// 循环处理信息
|
||
foreach($goodsList as $goodsInfo){
|
||
// 获取当前用户应得的权重值
|
||
$goodTotalWeightValue = (float)sprintf("%.2f",$this->getAreaGiveWeightValue($goodsInfo,$currentUser));
|
||
$totalWeightValue = (float)sprintf("%.2f",$goodTotalWeightValue * $weightValueRatio / 100);
|
||
// \Log::debug('------ 省公司权重值处理 - 计算结果------',[
|
||
// 'goods_id' => $goodsInfo['goods_id'],
|
||
// 'order_goods_id' => $goodsInfo['id'],
|
||
// '商品总产生权重值' => $goodTotalWeightValue,
|
||
// '占比例' => $weightValueRatio,
|
||
// '实际获得' => $totalWeightValue
|
||
// ]);
|
||
// 应得权重值大于0 记录改变信息并且修改持有权重值
|
||
if((float)$totalWeightValue > 0){
|
||
$changeAfter = sprintf("%.2f",($currentAgentHasWeightValue + $totalWeightValue));
|
||
$changeRecord[] = [
|
||
'uniacid' => $model->uniacid,
|
||
'member_id' => $currentUser['uid'],// 用户id
|
||
'team_dividend_agency_level_id' => $currentUser['team_dividend_agency_level_id'],// 经销商等级id
|
||
'goods_id' => $goodsInfo['goods_id'],// 商品id
|
||
'order_id' => $model->id,// 订单id
|
||
'order_goods_id' => $goodsInfo['id'],// 订单商品id
|
||
'change_type' => 1,// 变更类型:0=减少,1=增加
|
||
'change_quantity' => $totalWeightValue,// 变更数量
|
||
'change_front' => (float)$currentAgentHasWeightValue,// 变更前拥有的数量
|
||
'change_after' => $changeAfter,// 变更后拥有的数量
|
||
'remark' => "区域提成:订单{$model->order_sn}",// 备注
|
||
'created_at' => time(),// 变更时间
|
||
'source' => 3,
|
||
];
|
||
$currentUser['weight_value']['quantity'] = $currentAgentHasWeightValue = $changeAfter;
|
||
}
|
||
}
|
||
if($changeRecord){
|
||
self::updateInfo($currentUser['weight_value']);
|
||
WeightValueLog::insert($changeRecord);
|
||
}
|
||
|
||
DB::commit();
|
||
}catch(\Exception $e){
|
||
\Log::debug('------ 省公司权重值处理 - 错误抛出------',$e->getMessage());
|
||
DB::rollBack();
|
||
}
|
||
|
||
return;
|
||
}
|
||
/**
|
||
* Common: 区域代理提成 - 获取应得权重值
|
||
* Author: wu-hui
|
||
* Time: 2023/10/24 16:48
|
||
* @param $goodsInfo
|
||
* @param $currentUser
|
||
* @return float
|
||
*/
|
||
private function getAreaGiveWeightValue($goodsInfo,$currentUser){
|
||
$deservedValue = 0;// 应得权重值(固定值||比例)
|
||
if((int)$goodsInfo['province_gift_type'] == 0) $deservedValue = $goodsInfo['province_quantity'];// 赠送固定值
|
||
else if($currentUser['teamDividend']) $deservedValue = $this->getRankGive($goodsInfo['province_ladder'],(int)$currentUser['teamDividend']['ranking']);// 阶梯赠送
|
||
else $deservedValue = (float)0;
|
||
// 判断:是固定值还是比例,固定金额需要乘以商品购买数量,比例根据商品实际支付价格的比例
|
||
if((int)$goodsInfo['province_calculation_type'] == 1) return (float)sprintf("%.2f",$goodsInfo['payment_amount'] * $deservedValue / 100);// 实际支付金额的比例
|
||
else return (float)sprintf("%.2f",$goodsInfo['total'] * $deservedValue);// 总数量 * 应得权重值 = 总应得权重值
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* Common: 修改信息
|
||
* 必须是包含member_id、team_dividend_agency_level_id、quantity的二维数组
|
||
* Author: wu-hui
|
||
* Time: 2023/10/12 15:09
|
||
* @param $updateWeightValueList
|
||
*/
|
||
public static function updateInfo($updateWeightValueList){
|
||
// 判断:是否为二维数组
|
||
if(!is_array($updateWeightValueList[0])) $updateWeightValueList = [$updateWeightValueList];
|
||
// 循环处理 存在修改,不存在添加
|
||
foreach($updateWeightValueList as $wv){
|
||
$weightValueModel = self::uniacid()
|
||
->where('member_id',$wv['member_id'])
|
||
->where('team_dividend_agency_level_id',$wv['team_dividend_agency_level_id'])
|
||
->first();
|
||
if(!$weightValueModel) $weightValueModel = new static();
|
||
// 修改赋值
|
||
if((int)$weightValueModel->id <= 0){
|
||
$weightValueModel->uniacid = \YunShop::app()->uniacid;
|
||
$weightValueModel->member_id = $wv['member_id'];
|
||
$weightValueModel->team_dividend_agency_level_id = $wv['team_dividend_agency_level_id'];
|
||
}
|
||
$weightValueModel->quantity = (float)$wv['quantity'];
|
||
|
||
$weightValueModel->save();
|
||
}
|
||
}
|
||
/**
|
||
* Common: 获取权重值列表
|
||
* Author: wu-hui
|
||
* Time: 2023/10/12 15:35
|
||
* @param $pageSize
|
||
* @param $search
|
||
* @param string[] $field
|
||
* @return array
|
||
*/
|
||
public static function getList($pageSize,$search){
|
||
// 条件生成
|
||
$where = [];
|
||
if($search['member_id'] > 0) $where[] = ['member_id','=',$search['member_id']];
|
||
if($search['team_dividend_agency_level_id'] > 0) $where[] = ['team_dividend_agency_level_id','=',$search['team_dividend_agency_level_id']];
|
||
// 列表获取
|
||
$list = self::uniacid()
|
||
->where($where)
|
||
->with([
|
||
'member' => function($query){
|
||
$query->select(['uid','nickname','realname','avatar']);
|
||
},
|
||
'level' => function($query){
|
||
$query->select(['id','level_name']);
|
||
},
|
||
])
|
||
->orderBy('id','DESC')
|
||
->paginate($pageSize)
|
||
->toArray();
|
||
// 根据经销商等级id分组 根据id大小排序 获取排序序号
|
||
$group = self::uniacid()->groupBy('team_dividend_agency_level_id')->pluck('team_dividend_agency_level_id');
|
||
$group = $group ? $group->toArray() : [];
|
||
sort($group);
|
||
$group = array_flip($group);
|
||
// 循环处理数据
|
||
foreach($list['data'] as &$item){
|
||
// 获取总数及比例
|
||
[$item['total_quantity'],$item['ratio']] = self::getTotalAndRatio((int)$item['team_dividend_agency_level_id'],$item['quantity']);
|
||
// 排序序号
|
||
$item['sort'] = $group[(int)$item['team_dividend_agency_level_id']];
|
||
}
|
||
|
||
return $list;
|
||
}
|
||
/**
|
||
* Common: 根据经销商等级id及持有数量 获取该等级总数及持有数量占比
|
||
* Author: wu-hui
|
||
* Time: 2023/10/12 16:10
|
||
* @param int $levelId
|
||
* @param $quantity
|
||
* @return array
|
||
*/
|
||
public static function getTotalAndRatio(int $levelId,$quantity){
|
||
$name = \YunShop::app()->uniacid.'_'.$levelId;
|
||
// 获取当前等级的总权重值
|
||
if(array_key_exists($name,self::$weightValueLevelTotal)){
|
||
$total = self::$weightValueLevelTotal[$name];
|
||
}else{
|
||
$total = self::uniacid()->where('team_dividend_agency_level_id',$levelId)->sum('quantity');
|
||
self::$weightValueLevelTotal[$name] = $total;
|
||
}
|
||
// 计算比例
|
||
$ratio = sprintf("%.2f",$quantity / $total * 100);
|
||
|
||
return [$total,$ratio];
|
||
}
|
||
/**
|
||
* Common: 获取用户当前持有的权重值总数(支持根据经销商等级id获取某个等级的持有权重值)
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 10:12
|
||
* @param int $uid
|
||
* @param int $teamLevelId
|
||
* @return string
|
||
*/
|
||
public static function getWeightValueQuantity(int $uid,int $teamLevelId = 0){
|
||
$sumQuantity = self::uniacid()
|
||
->where('member_id',$uid)
|
||
->when((int)$teamLevelId > 0,function($query) use ($teamLevelId){
|
||
$query->where('team_dividend_agency_level_id',$teamLevelId);
|
||
})
|
||
->sum('quantity');
|
||
|
||
return sprintf("%.2f",(float)$sumQuantity);
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* Common: 统计信息 - 全部统计信息获取及处理
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 11:11
|
||
* @param $uid
|
||
* @param $teamLevelId
|
||
* @return array[]
|
||
*/
|
||
public function getStatistics($uid,$teamLevelId){
|
||
// 获取相关统计信息
|
||
[$directPush,$recommendAgent] = $this->getCommissionGive($uid);
|
||
// 获取权重收益(当前等级相关的股东分红)
|
||
$weightIncome = ShareholderDividendModel::uniacid()->where('member_id',$uid)->where('team_level',$teamLevelId)->sum('amount');
|
||
// 团队收益
|
||
$teamIncomeInfo = $this->getTeamDividendInfo($uid,$teamLevelId);
|
||
// 权重值
|
||
$weightValue = self::getWeightValueQuantity((int)$uid,(int)$teamLevelId);
|
||
// 团队订单信息
|
||
$teamOrderInfo = self::getTeamOrderInfo($uid);
|
||
// 我的名额
|
||
$giveLimitNum = Uplog::getQuota($uid,$teamLevelId);
|
||
|
||
$list = [
|
||
// 推荐代理 下级购买【升级商品(购买当前商品可以升级到经销商)】的 推广奖励(计算分销商奖励)
|
||
'recommend_agent' => ['key_name' => 'recommend_agent','title' => '推荐代理','num' => (float)$recommendAgent,'sort' => 4],
|
||
// 根据持有权重 在【股东分红】中每周期分红所得金额(仅获取当前等级的权重值分红)
|
||
'weight_income' => ['key_name' => 'weight_income','title' => '权重收益','num' => (float)$weightIncome,'sort' => 2],
|
||
// 本人当前持有的 本等级的权重值
|
||
'weight_value' => ['key_name' => 'weight_value','title' => '权重值','num' => $weightValue,'sort' => 10],
|
||
// 股权收益 todo:目前仅显示,值固定为0,后期功能开发完成后才显示具体统计值(功能暂定,无开发文档、无功能明细、无基本内容)
|
||
'shareholding' => ['key_name' => 'order_money','title' => '股权收益','num' => 0,'sort' => 11],
|
||
// 本团队所有相关订单的实际支付金额(不包括本人)
|
||
'order_money' => ['key_name' => 'order_money','title' => '总订单金额','num' => $teamOrderInfo['total_price'],'sort' => 12],
|
||
// 本团队所有相关订单总数 (不包括本人)
|
||
'order_num' => ['key_name' => 'order_num','title' => '总订单数','num' => $teamOrderInfo['total'],'sort' => 13],
|
||
// 我的赠送升级名额
|
||
'give_limit' => ['key_name' => 'give_limit','title' => '我的名额','num' => (int)$giveLimitNum['surplus_quota'],'sort' => 14],
|
||
];
|
||
// 获取经销商特殊身份 特殊身份:0=无特殊身份,1=经纪人,2=代理商,3=省级代理
|
||
$identityType = (int)TeamDividendLevelModel::uniacid()->where('id',$teamLevelId)->value('identity_type');
|
||
// 非经纪人 追加团队收益
|
||
if((int)$identityType != 1){
|
||
// 本团队提供的所有推广奖励及其他相关奖励 (不包括本人)
|
||
$list['team_income'] = ['key_name' => 'team_income','title' => '团队收益','num' => $teamIncomeInfo['commission'],'sort' => 3];
|
||
}
|
||
// 非省公司 追加平级奖励
|
||
if((int)$identityType != 3){
|
||
// 平级奖励 本人获得的所有平级奖
|
||
$list['peer_level_reward'] = ['key_name' => 'peer_level_reward','title' => '管理奖励','num' => $teamIncomeInfo['peer_level_reward'],'sort' => 5];
|
||
}
|
||
// 存在多个身份 直推奖励 仅在权重最小的经销商等级中显示
|
||
if((boolean)TeamDividendAgencyModel::isMinLevel($uid,$teamLevelId)){
|
||
// 下级购买除【升级商品(购买当前商品可以升级到经销商)】外其他商品的 推广奖励(计算分销商奖励)
|
||
$list['direct_push_give'] = ['key_name' => 'direct_push_give','title' => '直推奖励','num' => (float)$directPush,'sort' => 1];
|
||
}
|
||
|
||
|
||
|
||
// 排序
|
||
$sorts = array_column($list,'sort');
|
||
array_multisort($sorts,SORT_ASC,$list);
|
||
|
||
return $list;
|
||
}
|
||
/**
|
||
* Common: 统计信息 - 分销商相关统计
|
||
* Author: wu-hui
|
||
* Time: 2023/10/18 16:34
|
||
* @param $uid
|
||
* @return array|false|null
|
||
*/
|
||
private function getCommissionGive($uid){
|
||
$info = $this->getCommissionModel($uid)
|
||
->select([
|
||
'yz_commission_order_goods.commission_type',
|
||
DB::raw("sum(commission_money) as total_money")
|
||
])
|
||
->groupBy('yz_commission_order_goods.commission_type')
|
||
->get()
|
||
->toArray();
|
||
$moneyInfo = array_column($info,'total_money','commission_type');
|
||
|
||
return [(float)$moneyInfo[0],(float)$moneyInfo[1]];
|
||
}
|
||
/**
|
||
* Common: 统计信息 - 获取某个经销商提成统计信息(仅统计已结算且结算金额类型为收入的提成信息)
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 10:00
|
||
* @param $uid
|
||
* @param $teamLevelId
|
||
* @return float[]
|
||
*/
|
||
private function getTeamDividendInfo($uid,$teamLevelId){
|
||
// 获取内容
|
||
$info = $this->getTeamDividendModel($uid,$teamLevelId)
|
||
->select(DB::raw('sum(dividend_amount) as total_sum'),'type')
|
||
->groupBy('type')
|
||
->get()
|
||
->toArray();
|
||
$infoTypeGroup = array_flip(array_column($info,'type','total_sum'));
|
||
|
||
// 分红类型 0:分红佣金 1:平级奖
|
||
return [
|
||
'total' => (float)array_sum($infoTypeGroup),// 总计 - 已结算收入提成
|
||
'commission' => (float)$infoTypeGroup[0],// 分红佣金 - 已结算收入提成
|
||
'peer_level_reward' => (float)$infoTypeGroup[1],// 平级奖 - 已结算收入提成
|
||
];
|
||
}
|
||
/**
|
||
* Common: 统计信息 - 获取团队订单统计信息(订单总金额、总订单数)
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 10:38
|
||
* @param $uid
|
||
* @return array
|
||
*/
|
||
private function getTeamOrderInfo($uid){
|
||
$orderStatistics = [
|
||
'total_price' => 0.00,
|
||
'total' => 0,
|
||
];
|
||
$subUserIds = MemberChildren::getTeamModel($uid)->pluck('yz_member_children.child_id')->toArray();
|
||
if(count($subUserIds) > 0){
|
||
$orderInfo = Order::uniacid()
|
||
->select(DB::raw('sum(price) as total_price,count(id) as total'))
|
||
->whereIn('uid',$subUserIds)
|
||
->where('uid','!=',$uid)
|
||
->first();
|
||
$orderStatistics = [
|
||
'total_price' => sprintf("%.2f",(float)$orderInfo->total_price),
|
||
'total' => (int)$orderInfo->total,
|
||
];
|
||
}
|
||
|
||
return $orderStatistics;
|
||
}
|
||
|
||
|
||
/**
|
||
* Common: 直推奖励&推荐代理 奖励明细
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 16:17
|
||
* @param $uid
|
||
* @param int $commissionType
|
||
* @return array
|
||
*/
|
||
public function commissionDetail($uid,$commissionType = 0){
|
||
return $this->getCommissionModel($uid)
|
||
->select([
|
||
'yz_commission_order_goods.id',
|
||
'yz_commission_order_goods.name',
|
||
'yz_commission_order_goods.commission_money',
|
||
DB::raw('FROM_UNIXTIME(created_at) AS created_at'),
|
||
])
|
||
->where('yz_commission_order_goods.commission_type',$commissionType)
|
||
->groupBy('yz_commission_order_goods.id')
|
||
->paginate(20)
|
||
->toArray();
|
||
}
|
||
/**
|
||
* Common: 奖励明细 - 权重收益(股东分红)
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 16:29
|
||
* @param $uid
|
||
* @param $teamLevelId
|
||
* @return mixed
|
||
*/
|
||
public function shareholderDividendDetail($uid,$teamLevelId){
|
||
return ShareholderDividendModel::uniacid()
|
||
->select(['id','amount','created_at','team_level_name'])
|
||
->where('member_id',$uid)
|
||
->where('team_level',$teamLevelId)
|
||
->paginate(20)
|
||
->toArray();
|
||
}
|
||
/**
|
||
* Common: 奖励明细 - 团队收益&平级奖励
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 16:47
|
||
* @param $uid
|
||
* @param $teamLevelId
|
||
* @param int $type
|
||
* @return mixed
|
||
*/
|
||
public function teamDividendDetail($uid,$teamLevelId,$type = 0){
|
||
return $this->getTeamDividendModel($uid,$teamLevelId)
|
||
->select(['id','dividend_amount','agent_level','created_at','type'])
|
||
->where('type',$type)
|
||
->paginate(20)
|
||
->toArray();
|
||
}
|
||
/**
|
||
* Common: 奖励明细 - 团队订单记录
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 17:10
|
||
* @param $uid
|
||
* @return array
|
||
*/
|
||
public function orderDetail($uid){
|
||
$result = [];
|
||
$subUserIds = MemberChildren::getTeamModel($uid)->pluck('yz_member_children.child_id')->toArray();
|
||
if(count($subUserIds) > 0){
|
||
$result = Order::uniacid()
|
||
->select([
|
||
'id',
|
||
'uid',
|
||
'order_sn',
|
||
'create_time',
|
||
'price',
|
||
'status',
|
||
])
|
||
->whereIn('uid',$subUserIds)
|
||
->where('uid','!=',$uid)
|
||
->with([
|
||
'belongsToMember' => function($query){
|
||
$query->select(['uid','nickname','realname','avatar']);
|
||
},
|
||
'orderGoods' => function($query){
|
||
$query->select(['id','title','thumb','payment_amount','total','order_id']);
|
||
}
|
||
])
|
||
->paginate(20)
|
||
->toArray();
|
||
|
||
|
||
$result['data'] = array_map(function($item){
|
||
return [
|
||
'id' => $item['id'],
|
||
'uid' => $item['uid'],
|
||
'order_sn' => $item['order_sn'],
|
||
'create_time' => $item['create_time'],
|
||
'price' => $item['price'],
|
||
'status_name' => $item['status_name'],
|
||
'member' => $item['belongs_to_member'],
|
||
'order_goods' => array_map(function($orderGoodsItem){
|
||
return [
|
||
'id' => $orderGoodsItem['id'],
|
||
'title' => $orderGoodsItem['title'],
|
||
'thumb' => $orderGoodsItem['thumb'],
|
||
'payment_amount' => $orderGoodsItem['payment_amount'],
|
||
'total' => $orderGoodsItem['total'],
|
||
];
|
||
},$item['order_goods'])
|
||
];
|
||
},$result['data']);
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* Common: 分销商查询model
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 15:54
|
||
* @param $uid
|
||
* @return \Illuminate\Database\Query\Builder
|
||
*/
|
||
private function getCommissionModel($uid){
|
||
return DB::table('yz_commission_order_goods')
|
||
->join('yz_commission_order', 'yz_commission_order.id', 'yz_commission_order_goods.commission_order_id')
|
||
->where('yz_commission_order.uniacid',\YunShop::app()->uniacid)
|
||
->where('yz_commission_order.member_id',$uid)
|
||
->where('yz_commission_order.level_type',0)
|
||
->where('yz_commission_order.status',2);
|
||
}
|
||
/**
|
||
* Common: 经销商查询model
|
||
* Author: wu-hui
|
||
* Time: 2023/10/19 16:45
|
||
* @param $uid
|
||
* @param $teamLevelId
|
||
* @return BaseModel
|
||
*/
|
||
private function getTeamDividendModel($uid,$teamLevelId){
|
||
return TeamDividendModel::uniacid()
|
||
->where('member_id',$uid)
|
||
->where('agent_level',$teamLevelId)// 已结算
|
||
->where('status',1)// 已结算
|
||
->where('settle_money_type',1);// 结算金额类型为 - 收入提现
|
||
}
|
||
|
||
|
||
/**
|
||
* Common: 一对一关联 用户信息
|
||
* Author: wu-hui
|
||
* Time: 2023/10/12 15:33
|
||
* @return mixed
|
||
*/
|
||
public function member(){
|
||
return $this->hasOne(Member::class, 'uid', 'member_id');
|
||
}
|
||
/**
|
||
* Common: 一对一关联 经销商等级信息
|
||
* Author: wu-hui
|
||
* Time: 2023/10/12 15:36
|
||
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
||
*/
|
||
public function level(){
|
||
return $this->hasOne(TeamDividendLevelModel::class, 'id', 'team_dividend_agency_level_id');
|
||
}
|
||
|
||
}
|