424 lines
19 KiB
PHP
424 lines
19 KiB
PHP
<?php
|
|
namespace Yunshop\CulturalSpace\models;
|
|
use app\common\facades\Setting;
|
|
use app\common\models\BaseModel;
|
|
use app\common\models\Member;
|
|
use app\common\models\member\MemberParent;
|
|
use app\common\models\Order;
|
|
use app\common\models\OrderGoods;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yunshop\Commission\models\CommissionOrder;
|
|
use Yunshop\ShareholderDividend\models\ShareholderDividendModel;
|
|
use Yunshop\TeamDividend\admin\models\MemberChild;
|
|
use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
|
|
use Yunshop\TeamDividend\models\TeamDividendLevelModel;
|
|
use Yunshop\TeamDividend\models\TeamDividendModel;
|
|
|
|
class CulturalSpace extends BaseModel
|
|
{
|
|
|
|
public $table = 'yz_cultural_space';
|
|
public $timestamps = false;
|
|
protected $fillable = [
|
|
'uniacid',
|
|
'uid',
|
|
'contribution'
|
|
];
|
|
|
|
|
|
/**
|
|
* Common: 贡献值持有信息列表
|
|
* Author: wu-hui
|
|
* Time: 2023/11/03 9:51
|
|
* @param $search
|
|
* @return array
|
|
*/
|
|
public function getList($search)
|
|
{
|
|
// 条件生成
|
|
$where = [];
|
|
if ($search['uid'] > 0) $where[] = ['uid', '=', $search['uid']];
|
|
// 查询model
|
|
$model = self::uniacid()
|
|
->where($where)
|
|
->with([
|
|
'member' => function ($query) {
|
|
$query->select(['uid', 'nickname', 'realname', 'avatar']);
|
|
}
|
|
]);
|
|
// 信息获取
|
|
$totalContribution = $model->sum('contribution');
|
|
$totalVoucherNumber = $model->sum('voucher_number');
|
|
$list = $model->select(['id', 'uid', 'contribution','voucher_number'])
|
|
->orderBy('id', 'DESC')
|
|
->paginate(10)
|
|
->toArray();
|
|
foreach ($list['data'] as &$item) {
|
|
$item['total_proportion'] = (float)sprintf("%.2f", $totalContribution);
|
|
$item['ratio'] = (float)sprintf("%.2f", $item['contribution'] / $totalContribution * 100);
|
|
|
|
$item['total_voucher_number'] = (float)sprintf("%.6f", $totalVoucherNumber);
|
|
$item['voucher_number_ratio'] = (float)sprintf("%.6f", $item['voucher_number'] / $totalVoucherNumber * 100);
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
|
|
/**
|
|
* Common: 购买商品奖励贡献值
|
|
* Author: wu-hui
|
|
* Time: 2023/11/02 17:59
|
|
* @param $uid
|
|
* @param $orderId
|
|
*/
|
|
public function buyGoodsGiveContribution($uid, $orderId)
|
|
{
|
|
\Log::debug('文创空间 - 购买商品奖励贡献值', ['uid' => $uid, 'order_id' => $orderId]);
|
|
DB::beginTransaction();
|
|
try {
|
|
$set = Setting::get('plugin.cultural_space_set');
|
|
if ($set['is_give_contribution'] != 1 || (float)$set['contribution_ratio'] <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 未开启贡献值奖励或者奖励比例为0');
|
|
// 获取直推上级id && 获取订单商品信息
|
|
$parentUid = (int)MemberParent::getParentId($uid);
|
|
$orderGoodsList = (array)$this->getGoodsModel($orderId)
|
|
->where('yz_goods_cultural_space.is_give_contribution', 1)
|
|
->get()
|
|
->makeHidden(['buttons', 'after_sales', 'order'])
|
|
->toArray();
|
|
if (count($orderGoodsList) <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 无奖励商品信息');
|
|
// 获取文创空间用户信息
|
|
$memberList = $parentUid <= 0 ? $this->getCulturalSpace([$uid]) : $this->getCulturalSpace([$uid, $parentUid]);
|
|
$userName = Member::getMemberById($uid, ['realname', 'nickname'])->username ?? '';
|
|
// 循环商品处理奖励贡献值
|
|
$changeList = [];
|
|
foreach ($orderGoodsList as $goodsInfo) {
|
|
// 计算奖励贡献值
|
|
$contribution = (float)sprintf('%.2f', $goodsInfo['payment_amount'] * (float)$set['contribution_ratio'] / 100);
|
|
if ($contribution > 0) {
|
|
foreach ($memberList as $memberInfo) {
|
|
// 用户当前持有数量
|
|
$changeFront = (float)$memberList[$memberInfo['uid']]['contribution'];
|
|
// 变更后的数量
|
|
$changeAfter = (float)sprintf("%.2f", $changeFront + $contribution);
|
|
// 记录变更信息
|
|
$changeList[] = [
|
|
'uniacid' => $goodsInfo['uniacid'],
|
|
'uid' => $memberInfo['uid'],
|
|
'goods_id' => $goodsInfo['goods_id'],
|
|
'order_id' => $orderId,
|
|
'order_goods_id' => $goodsInfo['id'],
|
|
'change_type' => 1,
|
|
'change_quantity' => $contribution,
|
|
'change_front' => $changeFront,
|
|
'change_after' => $changeAfter,
|
|
'remark' => ($memberInfo['uid'] == $uid ? '' : "【{$userName}】") . "购买商品【{$goodsInfo['title']}】赠送",
|
|
'created_at' => time(),
|
|
];
|
|
// 刷新持有处理
|
|
$memberList[$memberInfo['uid']]['contribution'] = $changeAfter;
|
|
}
|
|
}
|
|
}
|
|
// 数据操作
|
|
|
|
if (count($changeList) > 0) {
|
|
$newMemberList = [];
|
|
foreach($memberList as $key => $val){
|
|
$newMemberList[$key] = [
|
|
'id' => $val['id'],
|
|
'contribution' => $val['contribution']
|
|
];
|
|
}
|
|
$this->batchUpdate($newMemberList);
|
|
ContributionLog::insert($changeList);
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
\Log::debug('文创空间 - 购买商品奖励贡献值 - 错误抛出', $e->getMessage());
|
|
DB::rollBack();
|
|
}
|
|
}
|
|
/***
|
|
* 计算基金比例
|
|
* @param $uid
|
|
* @param $orderId
|
|
* @return void
|
|
*/
|
|
public function GiveGoodsFundMoney($uid, $orderId)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$set = Setting::get('plugin.cultural_space_set');
|
|
if (isset($set['is_fund_open'])&&$set['is_fund_open'] == 1) {
|
|
// 获取直推上级id && 获取订单商品信息
|
|
$orderGoodsList = $this->getGoodsModel($orderId)
|
|
->where('yz_goods_cultural_space.is_fund_open', 1)
|
|
->get()
|
|
->makeHidden(['buttons', 'after_sales', 'order'])
|
|
->toArray();
|
|
if (count($orderGoodsList) <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 无奖励商品信息');
|
|
$current_fee = $set['fund_start_price']; //当前价格
|
|
$last_fee = $current_fee;
|
|
$fund_money = 0;
|
|
$voucher_number = 0;
|
|
$history_number = 0;
|
|
$history_fund_money = 0;
|
|
$fundInfo = CulturalFund::getfund();
|
|
if ($fundInfo) {
|
|
$current_fee = $fundInfo['current_fee'];
|
|
$last_fee = $current_fee;
|
|
$fund_money = $fundInfo['fund_money'];
|
|
$voucher_number = $fundInfo['voucher_number'];
|
|
$history_number = $fundInfo['history_number'];
|
|
$history_fund_money = $fundInfo['history_fund_money'];
|
|
}
|
|
$user_voucher_number = 0;//用户凭证数量
|
|
$user_voucher_total = 0;//用户总计凭证数量
|
|
foreach ($orderGoodsList as $goodsInfo) {
|
|
if($goodsInfo['payment_amount'] <= 0) continue;
|
|
$user_voucher_number = (($goodsInfo['payment_amount'] * $set['user_fund_ratio']) / 100) / $current_fee; //赠送数量
|
|
$user_fund_money = round(($goodsInfo['payment_amount'] * $set['order_fund_ratio']) / 100, 2);//资金数量
|
|
$fund_money += $user_fund_money;
|
|
$user_voucher_total += $user_voucher_number;
|
|
$voucher_number += $user_voucher_number;
|
|
$history_number += $user_voucher_number;
|
|
$history_fund_money += $user_fund_money;
|
|
$data_log = [
|
|
'member_id' => $uid,
|
|
'order_id' => $orderId,
|
|
'goods_id' => $goodsInfo['goods_id'],
|
|
'good_name' => $goodsInfo['title'],
|
|
'pay_money' => $goodsInfo['payment_amount'],
|
|
'fund_money' => $user_fund_money,
|
|
'voucher_number' => $user_voucher_number,
|
|
'current_fee' => $current_fee,
|
|
];
|
|
CulturalOrderFundLog::InsertLog($data_log); //写入日记
|
|
}
|
|
|
|
if($user_voucher_number > 0){
|
|
$this->where('uid', $uid)->increment('voucher_number', $user_voucher_total);//用户增量
|
|
$next_fee = round($fund_money / $voucher_number, 2);
|
|
$FundData = [
|
|
'last_fee' => $last_fee,
|
|
'current_fee' => $next_fee,
|
|
'fund_money' => $fund_money,
|
|
'voucher_number' => $voucher_number,
|
|
'history_number' => $history_number,
|
|
'history_fund_money' => $history_fund_money,
|
|
];
|
|
$culturalFund = new CulturalFund();
|
|
$culturalFund->SaveData($FundData);
|
|
if ($user_voucher_number <= $set['min_number']) {
|
|
$this->capitalIncrease($set);//小于数量增加积分倍数
|
|
}
|
|
}
|
|
|
|
// 获取文创空间用户信息
|
|
// 数据操作
|
|
DB::commit();
|
|
} else {
|
|
DB::rollBack();
|
|
}
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
}
|
|
}
|
|
/***
|
|
* 增加资本
|
|
* @return void
|
|
*/
|
|
public function capitalIncrease($config){
|
|
$uniacid=\YunShop::app()->uniacid;
|
|
$this->where('uniacid', $uniacid) ->update(
|
|
[
|
|
'voucher_number' => DB::raw('voucher_number * '.$config['fund_multiple']),
|
|
]
|
|
); //增加会员资本
|
|
$culturalFund = new CulturalFund();
|
|
$data=$culturalFund->where('uniacid', $uniacid)->first()->toArray();
|
|
$data['up_voucher_number']=$data['voucher_number']*$config['fund_multiple'];
|
|
$data['up_current_fee']=$data['current_fee']/$config['fund_multiple'];
|
|
$data['fund_multiple']=$config['fund_multiple'];
|
|
$data['created_at']=time();
|
|
$data['updated_at']=time();
|
|
unset($data['id']);
|
|
DB::table('yz_cultural_fund_increase_log')->insert($data);
|
|
$culturalFund->where('uniacid', $uniacid)->update( //增加资本数量
|
|
[
|
|
'voucher_number' => DB::raw('voucher_number * '.$config['fund_multiple']),
|
|
'history_number' => DB::raw('history_number * '.$config['fund_multiple']),
|
|
'current_fee' => DB::raw('current_fee /'.$config['fund_multiple']),
|
|
]
|
|
);
|
|
}
|
|
/**
|
|
* Common: 获取订单商品查询model
|
|
* Author: wu-hui
|
|
* Time: 2023/11/02 16:26
|
|
* @param $orderId
|
|
* @return mixed
|
|
*/
|
|
private function getGoodsModel($orderId)
|
|
{
|
|
return OrderGoods::uniacid()
|
|
->select([
|
|
'yz_order_goods.id',
|
|
'yz_order_goods.uniacid',
|
|
'yz_order_goods.goods_id',
|
|
'yz_order_goods.total',
|
|
'yz_order_goods.title',
|
|
'yz_order_goods.payment_amount',
|
|
])
|
|
->leftJoin('yz_goods_cultural_space', 'yz_goods_cultural_space.goods_id', 'yz_order_goods.goods_id')
|
|
->where('yz_order_goods.order_id', $orderId)
|
|
->where('yz_goods_cultural_space.is_open', 1);
|
|
}
|
|
/**
|
|
* Common: 根据用户ids获取用户文创空间相关信息(如果不存在则添加默认信息并且返回默认信息)
|
|
* Author: wu-hui
|
|
* Time: 2023/11/02 17:36
|
|
* @param $ids
|
|
* @return array
|
|
*/
|
|
private function getCulturalSpace($ids)
|
|
{
|
|
// 获取已经存在的信息
|
|
$list = self::uniacid()
|
|
->select(['id', 'uid', 'contribution'])
|
|
->whereIn('uid', $ids)
|
|
->get()
|
|
->keyBy('uid')
|
|
->toArray();
|
|
// 循环处理:不存在则添加,并且赋值默认值;存在则使用已经存在的信息
|
|
$memberList = [];
|
|
foreach ($ids as $userId) {
|
|
if ($list[$userId]) {
|
|
// 存在
|
|
$memberList[$userId] = $list[$userId];
|
|
} else {
|
|
// 不存在
|
|
$id = self::insertGetId([
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'uid' => $userId,
|
|
]);
|
|
$memberList[$userId] = [
|
|
'uid' => $userId,
|
|
'contribution' => 0,
|
|
'id' => $id
|
|
];
|
|
}
|
|
}
|
|
|
|
return $memberList;
|
|
}
|
|
/**
|
|
* Common: 文创空间统计
|
|
* Author: wu-hui
|
|
* Time: 2023/11/22 11:22
|
|
* @param $uid
|
|
* @return array[]
|
|
*/
|
|
public function getStatistics($uid){
|
|
// 服务收益(生态服务) 经销商 生态建设 奖励
|
|
$serviceIncome = TeamDividendModel::getTypeTotalIncome((int)$uid,(int)4)->sum('yz_member_income.amount');
|
|
// 获取用户当前经销商等级对应的文创补贴名称
|
|
// $teamDividendLevel = TeamDividendAgencyModel::where('uid',$uid)->value('cultural_level_id');
|
|
$serviceIncomeTitle = '生态服务';//TeamDividendLevelModel::uniacid()->where('id',$teamDividendLevel)->value('cultural_level_name');
|
|
|
|
// 贡献收益(生态贡献) 经销商 生态贡献 奖励
|
|
$contributionIncome = TeamDividendModel::getTypeTotalIncome((int)$uid,(int)5)->sum('yz_member_income.amount');
|
|
// 贡献值分红
|
|
$contributionBonus = ContributionBonusLog::getIncomeSum($uid)->sum('yz_member_income.amount');
|
|
// 获取文创空间用户信息
|
|
$culturalSpace = CulturalSpace::uniacid()->where('uid',$uid)->select(['uid','contribution','voucher_number'])->first();
|
|
if($culturalSpace) $culturalSpace = $culturalSpace->toArray();
|
|
// 当前文创豆汇率
|
|
$exchangeRate = CulturalFund::uniacid()->value('current_fee');
|
|
// 文创豆收益
|
|
$legumesIncome = sprintf("%.2f",$culturalSpace['voucher_number'] * $exchangeRate);
|
|
// 获取小区业绩(去除最大的线的业绩)和团队总业绩
|
|
$teamAmount = $this->getTeamOrderAmount($uid);
|
|
// 获取加权收益 权证兑换
|
|
$weightIncome = ShareholderDividendModel::getIncome((int)$uid,(int)1)->sum('yz_member_income.amount');
|
|
// 分享奖励
|
|
$shareReward = CommissionOrder::getIncome((int)$uid,(int)1,'undrawn')->sum('yz_member_income.amount');
|
|
|
|
// $isPartner = 0;
|
|
$list = [
|
|
'service_income' => ['key_name' => 'service_income','title' => $serviceIncomeTitle ?? '服务收益','num' => $serviceIncome,'sort' => 0],
|
|
'contribution_income' => ['key_name' => 'contribution_income','title' => '生态贡献','num' => $contributionIncome,'sort' => 1],
|
|
'contribution_bonus' => ['key_name' => 'contribution_bonus','title' => '贡献值分红','num' => $contributionBonus,'sort' => 2],
|
|
'legumes' => ['key_name' => 'legumes','title' => '文创豆','num' => $culturalSpace['voucher_number'],'sort' => 3],
|
|
'legumes_income' => ['key_name' => 'legumes_income','title' => '文创豆收益','num' => $legumesIncome,'sort' => 4],
|
|
'contribution_value' => ['key_name' => 'contribution_value','title' => '贡献值','num' => $culturalSpace['contribution'],'sort' => 5],
|
|
'area_performance' => ['key_name' => 'area_performance','title' => '小区业绩','num' => $teamAmount['area_amount'],'sort' => 7],
|
|
'team_performance' => ['key_name' => 'team_performance','title' => '团队总业绩','num' => $teamAmount['team_amount'],'sort' => 8],
|
|
'weight_income' => ['key_name' => 'weight_income','title' => '权证兑换','num' => $weightIncome,'sort' => 9],
|
|
'shareholding_income' => ['key_name' => 'shareholding_income','title' => '股权收益','num' => 0,'sort' => 10],
|
|
'share_reward' => ['key_name' => 'share_reward','title' => '生态发展','num' => $shareReward,'sort' => 11],
|
|
];
|
|
// 合伙人等级增加显示权证兑换,不显示服务收益、贡献收益、贡献值分红
|
|
// if($isPartner) unset($list['service_income'],$list['contribution_income'],$list['contribution_bonus']);
|
|
// else unset($list['weight_income']);
|
|
// 排序
|
|
$sorts = array_column($list,'sort');
|
|
array_multisort($sorts,SORT_ASC,$list);
|
|
|
|
return $list;
|
|
}
|
|
/**
|
|
* Common: 获取团队业绩
|
|
* Author: wu-hui
|
|
* Time: 2023/11/20 16:18
|
|
* @return float[]
|
|
*/
|
|
public function getTeamOrderAmount($uid){
|
|
// 获取直推下级列表
|
|
$subIds = MemberChild::uniacid()
|
|
->where('level',1)
|
|
->where('member_id',$uid)
|
|
->pluck('child_id')
|
|
->toArray();
|
|
$subPerformance = [];// 每条线的业绩
|
|
foreach($subIds as $subUid){
|
|
$lineAllUid = MemberChild::uniacid()->where('member_id',$subUid)->pluck('child_id')->toArray();
|
|
$lineAllUid[] = $subUid;
|
|
$subPerformance[$subUid] = Order::uniacid()
|
|
->leftJoin('yz_order_goods','yz_order_goods.order_id','yz_order.id')
|
|
->leftJoin('yz_goods_cultural_space','yz_goods_cultural_space.goods_id','yz_order_goods.goods_id')
|
|
->whereIn('yz_order.uid',$lineAllUid)
|
|
->where('yz_order.status','>',0)
|
|
->where('yz_goods_cultural_space.is_open',1)
|
|
->sum('yz_order_goods.payment_amount');
|
|
// $subPerformance[$subUid] = Order::whereIn('uid',$lineAllUid)->where('status','>=',0)->sum('yz_order.price');
|
|
}
|
|
// 计算业绩 小区业绩 && 团队总业绩
|
|
$teamAmount = (float)sprintf("%.2f",array_sum($subPerformance));// 总业绩
|
|
$maxAmount = max($subPerformance);// 最大一条线的业绩
|
|
$areaAmount = (float)sprintf("%.2f",$teamAmount - $maxAmount);// 小区业绩 = 总业绩 - 最大一条线的业绩
|
|
|
|
return [
|
|
'team_amount' => $teamAmount,
|
|
'area_amount' => $areaAmount
|
|
];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* Common: 一对一关联 用户信息
|
|
* Author: wu-hui
|
|
* Time: 2023/11/03 9:35
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function member()
|
|
{
|
|
return $this->hasOne(Member::class, 'uid', 'uid');
|
|
}
|
|
}
|