192 lines
6.4 KiB
PHP
192 lines
6.4 KiB
PHP
<?php
|
|
|
|
|
|
namespace Yunshop\Commission\models;
|
|
|
|
|
|
use app\common\models\MemberShopInfo;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
|
|
|
|
class YzMember extends MemberShopInfo
|
|
{
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public static function getMemberAgent()
|
|
{
|
|
return self::uniacid()
|
|
->select('parent_id')
|
|
->with(['belongsToParent' => function ($query) {
|
|
$query->select('member_id', 'parent_id')
|
|
->with(['belongsToParent' => function ($query) {
|
|
$query->select('member_id', 'parent_id')
|
|
->with(['belongsToParent' => function ($query) {
|
|
$query->select('member_id', 'parent_id');
|
|
}]);
|
|
}])
|
|
->with('hasOneFans');
|
|
}])
|
|
->whereDoesntHave('existsAgent', function ($query) {
|
|
return $query->withTrashed();
|
|
})
|
|
->where('parent_id', '>', '0')
|
|
->groupBy(['parent_id']);
|
|
}
|
|
|
|
/**
|
|
* @param $memberId
|
|
* @param $selfBuy
|
|
* @return mixed
|
|
* 获取上级关系链 (三级)
|
|
*/
|
|
public static function getParentAgents($memberId, $selfBuy)
|
|
{
|
|
$agentModel = self::where('member_id', $memberId)->with('hasOneFans')->first();
|
|
if (!$agentModel) {
|
|
return false;
|
|
}
|
|
$memberAgent = Agents::where('member_id', $agentModel->member_id)->with('agentLevel')->first();
|
|
if (!empty($memberAgent)) {
|
|
$agentModel->setRelation('agent', Agents::where('member_id', $agentModel->member_id)->with('agentLevel')->first());
|
|
}
|
|
if ($agentModel->parent_id) {
|
|
$firstParentModel = self::where('member_id', $agentModel->parent_id)->with('hasOneFans')->first();
|
|
if (!empty($firstParentModel)) {
|
|
$firstParentModel->setRelation('agent', Agents::where('member_id', $agentModel->parent_id)->with('agentLevel')->first());
|
|
}
|
|
if ($firstParentModel->parent_id) {
|
|
$secondParentModel = self::where('member_id', $firstParentModel->parent_id)->with('hasOneFans')->first();
|
|
if (!empty($secondParentModel)) {
|
|
$secondParentModel->setRelation('agent', Agents::where('member_id', $firstParentModel->parent_id)->with('agentLevel')->first());
|
|
}
|
|
if ($secondParentModel->parent_id){
|
|
$thirdParentModel = self::where('member_id', $secondParentModel->parent_id)->with('hasOneFans')->first();
|
|
if (!empty($thirdParentModel)) {
|
|
$thirdParentModel->setRelation('agent', Agents::where('member_id', $secondParentModel->parent_id)->with('agentLevel')->first());
|
|
$secondParentModel->setRelation('belongsToParent', $thirdParentModel);
|
|
}
|
|
}
|
|
}
|
|
if (!empty($firstParentModel)) {
|
|
$firstParentModel->setRelation('belongsToParent', $secondParentModel);
|
|
}
|
|
}
|
|
$agentModel->setRelation('belongsToParent', $firstParentModel);
|
|
|
|
|
|
//上一级
|
|
// $agentModel = self::uniacid()->where('member_id', $memberId);
|
|
// $agentModel->with(['belongsToParent' => function ($query) use ($selfBuy) {
|
|
// //上上级
|
|
// $query->with(['belongsToParent' => function ($query) use ($selfBuy) {
|
|
// if (!$selfBuy) {
|
|
// //上三级
|
|
//// $query->with(['belongsToParent' => function ($query) use ($selfBuy) {
|
|
//// $query->with(['Agent' => function ($query) {
|
|
//// return $query->with('agentLevel');
|
|
//// }])->with('hasOneFans');
|
|
//// return $query;
|
|
//// }])->with('hasOneFans');
|
|
// }
|
|
// $query->with(['Agent' => function ($query) {
|
|
// return $query->with('agentLevel');
|
|
// }])->with('hasOneFans');
|
|
// return $query;
|
|
// }])->with('hasOneFans');
|
|
// $query->with(['Agent' => function ($query) {
|
|
// return $query->with('agentLevel');
|
|
// }]);
|
|
// return $query;
|
|
// }]);
|
|
//
|
|
// $agentModel->with([
|
|
// 'Agent' => function ($query) {
|
|
// return $query->with('agentLevel');
|
|
// },
|
|
// 'hasOneFans']);
|
|
// $agentModel->with('hasOneFans');
|
|
return $agentModel;
|
|
|
|
}
|
|
|
|
/**
|
|
* @param $memberId
|
|
* @return mixed
|
|
*
|
|
*/
|
|
public static function getMemberByMemberId($memberId)
|
|
{
|
|
return self::where('member_id', $memberId)
|
|
->with('hasOneFans');
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $memberParent
|
|
* @return mixed
|
|
* 上三级数据
|
|
*/
|
|
public static function getPraents($memberParent)
|
|
{
|
|
$memberModel = self::uniacid();
|
|
$memberModel->whereIn('member_id', explode(',', $memberParent));
|
|
return $memberModel;
|
|
}
|
|
|
|
/**
|
|
* @param $memberId
|
|
* @param $level
|
|
* @return mixed
|
|
*
|
|
*/
|
|
public static function getLowerData($memberId, $level = 0)
|
|
{
|
|
if ($level) {
|
|
$data = [$memberId, $level];
|
|
} else {
|
|
$data = [$memberId];
|
|
}
|
|
$memberModel = self::uniacid();
|
|
$memberModel->whereRaw('FIND_IN_SET(?,relation)' . ($level != 0 ? ' = ?' : ''), $data);
|
|
return $memberModel;
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function parent()
|
|
{
|
|
return $this->hasOne(get_class($this), $this->getKeyName(), 'parent_id');
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function belongsToParent()
|
|
{
|
|
return $this->belongsTo(self::class, "parent_id", "member_id");
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function Agent()
|
|
{
|
|
return $this->hasOne('Yunshop\Commission\models\Agents', 'member_id', 'member_id');
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function existsAgent()
|
|
{
|
|
return $this->hasOne('Yunshop\Commission\models\Agents', 'member_id', 'parent_id');
|
|
}
|
|
|
|
public function hasOneFans()
|
|
{
|
|
return $this->hasOne('app\common\models\McMappingFans', 'uid', 'member_id');
|
|
}
|
|
|
|
} |