<?php
|
|
namespace app\shop\model\plus\shareholder;
|
|
use app\shop\model\plus\shareholder\Referee as RefereeModel;
|
use app\common\model\plus\shareholder\User as UserModel;
|
use app\shop\model\order\Order as OrderModel;
|
use app\shop\model\order\OrderProduct as OrderProductModel;
|
/**
|
* 股东用户模型
|
* Class User
|
* @package app\shop\model\plus\shareholder
|
*/
|
class User extends UserModel
|
{
|
public static function checkExistByGradeId($grade_id)
|
{
|
self::where('grade_id',$grade_id)->find();
|
}
|
|
/**
|
* 获取股东用户列表
|
*/
|
public function getList($search, $limit = 15)
|
{
|
// 构建查询规则
|
$model = $this->alias('shareholder')
|
->field('shareholder.*, user.nickName, user.avatarUrl')
|
->with(['referee','grade'])
|
->join('user', 'user.user_id = shareholder.user_id')
|
->where('shareholder.is_delete', '=', 0)
|
->order(['shareholder.create_time' => 'desc']);
|
// 查询条件
|
if (!empty($search)) {
|
$model = $model->where('user.nickName|shareholder.real_name|shareholder.mobile', 'like', '%' . $search . '%');
|
}
|
// 获取列表数据
|
$list = $model->paginate($limit);
|
foreach ($list as $user){
|
$user['total_money'] = sprintf('%.2f', $user['money'] + $user['freeze_money'] + $user['total_money']);
|
}
|
return $list;
|
}
|
|
public static function getListAll($grade_id = 0)
|
{
|
$model = new static;
|
if($grade_id > 0) {
|
$model->where('grade_id', '=', $grade_id);
|
}
|
return $model->where('is_delete', '=', 0)->with(['grade'])->select();
|
}
|
|
/**
|
* 获取满足分红条件的股东列表
|
*/
|
public static function getEligibleListAll($bonusData)
|
{
|
// 获取基础设置
|
$setting = Setting::getItem('basic');
|
|
// 先获取所有股东
|
$model = new static;
|
$shareholders = $model->where('is_delete', '=', 0)->with(['grade'])->select();
|
// 如果没有设置分红条件,返回所有股东
|
if (empty($setting['consumption_amount']) && empty($setting['condition_purchase_count'])) {
|
return $shareholders;
|
}
|
|
// 过滤满足条件的股东
|
$eligibleShareholders = [];
|
foreach ($shareholders as $shareholder) {
|
$userId = $shareholder['user_id'];
|
$isEligible = true;
|
|
// 检查消费金额条件
|
if (!empty($setting['consumption_amount'])) {
|
$totalConsumption = (new OrderModel)->getUserTotalConsumption($userId,$bonusData);
|
if ($totalConsumption < $setting['consumption_amount']) {
|
$isEligible = false;
|
}
|
}
|
|
// 检查购买次数条件
|
if (!empty($setting['condition_purchase_count']) && $isEligible) {
|
$purchaseCount = (new OrderProductModel)::getPurchaseCount($userId,$bonusData);
|
if ($purchaseCount < $setting['condition_purchase_count']) {
|
$isEligible = false;
|
}
|
}
|
|
// 如果满足所有条件,添加到结果集中
|
if ($isEligible) {
|
$eligibleShareholders[] = $shareholder;
|
}
|
}
|
|
// 转换为 Collection 对象以保持接口一致性
|
return collect($eligibleShareholders);
|
}
|
|
/**
|
* 新增记录
|
*/
|
public function addUser($data)
|
{
|
$data['app_id'] = self::$app_id;
|
return $this->save($data);
|
}
|
|
/**
|
* 编辑股东用户
|
* @param $data
|
* @return bool
|
*/
|
public function edit($data)
|
{
|
return $this->save($data) !== false;
|
}
|
|
/**
|
* 删除股东用户
|
* @return mixed
|
*/
|
public function setDelete()
|
{
|
return $this->transaction(function () {
|
// 标记当前股东记录为已删除
|
return $this->save([
|
'is_delete' => 1
|
]);
|
});
|
}
|
|
/**
|
* 提现打款成功:累积提现佣金
|
*/
|
public static function totalMoney($user_id, $money)
|
{
|
$model = self::detail($user_id);
|
return $model->save([
|
'freeze_money' => $model['freeze_money'] - $money,
|
'total_money' => $model['total_money'] + $money,
|
]);
|
}
|
|
/**
|
* 提现驳回:解冻股东资金
|
*/
|
public static function backFreezeMoney($user_id, $money)
|
{
|
$model = self::detail($user_id);
|
return $model->save([
|
'money' => $model['money'] + $money,
|
'freeze_money' => $model['freeze_money'] - $money,
|
]);
|
}
|
|
/**
|
* 获取平台的总销售额
|
*/
|
public function getTotalMoney($type = 'all_money')
|
{
|
$model = $this;
|
if($type == 'money'){
|
return $model->sum('money');
|
} else if($type == 'freeze_money'){
|
return $model->sum('freeze_money');
|
} else if($type == 'total_money'){
|
return $model->sum('total_money');
|
} else if($type == 'all_money'){
|
return $model->sum('total_money') + $model->sum('freeze_money') + $model->sum('money');
|
}
|
return 0;
|
}
|
}
|