bztang-admin/app/common/models/weightValue/WeightValue.php

213 lines
8.8 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\common\models\weightValue;
use app\common\models\BaseModel;
use app\common\models\goods\Sale;
use app\common\models\Member;
use app\common\models\OrderGoods;
use Illuminate\Support\Facades\DB;
use Yunshop\TeamDividend\models\MemberParent;
use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
class WeightValue extends BaseModel{
public $table = false;
/**
* Common: 购买商品赠送权重值 - 开始处理
* Author: wu-hui
* Time: 2023/09/27 10:55
* @param $model
*/
public function giveInit($model){
\Log::debug('------购买商品赠送权重值 - 开始处理------',$model->id);
DB::beginTransaction();
try{
// 获取准备信息
$orderGoods = OrderGoods::uniacid()
->where('order_id',$model->id)
->select(['id','goods_id','total','title'])
->get()
->makeHidden(['order','after_sales','buttons'])
->toArray();
$goodsIds = array_column($orderGoods,'goods_id');
$goodsSale = Sale::whereIn('goods_id',$goodsIds)
->select(['id','goods_id','weight_value_user','weight_value_type','weight_value_num','weight_value_ladder'])
->get()
->keyBy('goods_id')
->toArray();
// 获取当前用户及上级信息
$agents = $this->getParent($model->uid);// 全部上级信息(包含购买人)
$uidS = array_column($agents,'uid');
// 获取相关用户的权重值信息
$memberList = Member::uniacid()
->select(['uid','weight_value'])
->whereIn('uid',(array)$uidS)
->get()
->keyBy('uid')
->makeHidden(['avatar_image','username'])
->toArray();
// 循环处理每个商品
$insertData = [];
foreach($orderGoods as $orderGoodItem){
/**
* currentSale参数说明
* weight_value_user受赠用户类型0=全部用户1=仅经销商
* weight_value_type赠送方式
* 0=普通赠送:仅获取三人(购买人、购买人上级、购买人上级的上级)进行处理
* 1=阶梯赠送:仅获取二人(购买人、购买人上级)进行处理
* PS所有赠送权重值数量需要乘以购买的商品数量
*/
$currentSale = $goodsSale[$orderGoodItem['goods_id']];// 当前商品销售设置
$useAgentList = $agents;// 由于涉及到删除 使用的经销商列表重新赋值给新的变量 方便操作
// 判断赠送方式为阶梯赠送时并且经销商个人大于等于3 删除最后一个经销商
if($currentSale['weight_value_type'] == 1 && count($useAgentList) >= 3) unset($useAgentList[2]);
// 循环上级信息 处理每一个用户、每一个商品的赠送情况
foreach($useAgentList as $agentInfo){
$isBlack = (int)$agentInfo['teamDividend']['is_black'] ?? 0;
$currentAgentHasWeightValue = $memberList[$agentInfo['uid']]['weight_value'] ?? 0;
// 判断:当前用户是否可以领取;受赠用户类型为经销商,并且用户存在经销商信息 或者 受赠用户类型为全部用户;如果用户经销商信息为黑名单 则不参与
if((((int)$currentSale['weight_value_user'] == 1 && $agentInfo['teamDividend']) || (int)$currentSale['weight_value_user'] == 0) && $isBlack != 1){
// 符合条件 获取当前用户应得的权重值
$weightValue = $this->getWeightValue($currentSale,$agentInfo);
$totalWeightValue = $orderGoodItem['total'] * $weightValue;// 总数量
// 最终的数据处理 全部数据都在这里进行处理
if((float)$totalWeightValue > 0){
$insertData[] = [
'uniacid' => $model->uniacid,
'member_id' => $agentInfo['uid'],// 用户id
'type' => 0,// 权重值类型0=经纪人(经销商)权重值
'goods_id' => $orderGoodItem['goods_id'],// 商品id
'order_id' => $model->id,// 订单id
'order_goods_id' => $orderGoodItem['id'],// 订单商品id
'change_type' => 1,// 变更类型0=减少1=增加
'change_quantity' => $totalWeightValue,// 变更数量
'change_front' => (float)$currentAgentHasWeightValue,// 变更前拥有的数量
'change_after' => (float)($currentAgentHasWeightValue + $totalWeightValue),// 变更后拥有的数量
'remark' => "订单{$model->order_sn}赠送",// 备注
'created_at' => time(),// 变更时间
];
$memberList[$agentInfo['uid']]['weight_value'] = (float)($currentAgentHasWeightValue + $totalWeightValue);
}
}
}
}
// 修改用户权重值变更信息 | 处理权重值变更记录
if($insertData){
(new Member())->batchUpdate($memberList,'uid','uid');
WeightValueLog::insert($insertData);
}
DB::commit();
}catch(\Exception $e){
\Log::debug('------购买商品赠送权重值 - 错误抛出------',$e->getMessage());
DB::rollBack();
}
return;
}
/**
* Common: 购买商品赠送权重值 - 获取上级信息
* Author: wu-hui
* Time: 2023/09/26 16:59
* @param $uid
* @return array
*/
private function getParent($uid){
// 获取上级信息
$parents = MemberParent::uniacid()
->where('member_id', $uid)
->select(['parent_id','level'])
->orderBy('level', 'asc')
->limit(2)// 由于最多只分三层,这里只获取直推上级及其上级
->get()
->toArray();
// 包含购买人
$parents = array_merge([
[
'parent_id' => $uid,
'level' => 0
]
],$parents);
$parentIds = array_column($parents,'parent_id');
// 获取经销商信息
$teamDividend = TeamDividendAgencyModel::uniacid()
->select(['uid','level','is_black'])
->whereIn('uid',$parentIds)
->get();
$teamDividend = $teamDividend ? $teamDividend->keyBy('uid')->toArray() : [];
// 获取全部经销商 以此来获取排名信息
$allTeamDividend = TeamDividendAgencyModel::uniacid()->pluck('uid')->toArray();
$allTeamDividend = array_flip($allTeamDividend);
$agents = [];
foreach($parents as $memberInfo){
$teamDividendInfo = $teamDividend[$memberInfo['parent_id']] ?? [];
if($teamDividendInfo) $teamDividendInfo['ranking'] = ((int)$allTeamDividend[$memberInfo['parent_id']] + 1);
$agents[] = [
'uid' => $memberInfo['parent_id'],
'level' => $memberInfo['level'],
'teamDividend' => $teamDividendInfo
];
}
return $agents;
}
/**
* Common: 购买商品赠送权重值 - 获取应得权重值
* Author: wu-hui
* Time: 2023/09/27 10:05
* @param $currentSale
* @param $agentInfo
* @return float|int
*/
private function getWeightValue($currentSale,$agentInfo){
if((int)$currentSale['weight_value_type'] == 0){
// 普通赠送
return $currentSale['weight_value_num'];
}else if($agentInfo['teamDividend']){
// 阶梯赠送 阶梯判断条件必须按照从大到小排序
$weightValueLadder = json_decode($currentSale['weight_value_ladder'],true);
array_multisort(array_column($weightValueLadder,'where'),SORT_ASC, $weightValueLadder);
$ranking = $agentInfo['teamDividend']['ranking'];
$weightValueNum = 0;
foreach($weightValueLadder as $ladder){
if($ranking <= $ladder['where']){
$weightValueNum = (float)$ladder['num'];
break;
}
}
return $weightValueNum;
}else {
return (float)0;
}
}
}