Files

103 lines
2.3 KiB
PHP

<?php
namespace app\common\model\app\agent;
use think\Model;
/**
* 分销关系
*/
class Relation extends Model
{
// 表名,不含前缀
protected $name = 'app_agent_relation';
// 自动写入时间戳字段
protected $autoWriteTimestamp = false;
// 定义时间戳字段名
protected $createTime = false;
protected $updateTime = false;
// 追加属性
protected $append = [
];
/**
* 获取用户的分销关系
* @param $user
* @return void
*/
public static function getUserRelation($userId){
$relation = [
'parent'=>false,//父
'grandpa'=>false//爷
];
$relation['parent'] = self::getParentUser($userId);
if($relation['parent']){
$relation['grandpa'] = self::getParentUser($relation['parent']['parent_id']);
}
return $relation;
}
/**
* 获取上级用户详情
* @param $userId
* @return false
*/
public static function getParentUser($userId){
$data = self::with(['parentUser','member'])->where([
'relation.user_id'=>$userId,
'relation.status'=>1
])->find();
if($data){
return $data->toArray();
}
return false;
}
/**
* 绑定上级
* @param $userId
* @param $parentId
* @return bool
*/
public static function bindParant($userId,$parentId){
$isBind = self::where([
'user_id'=>$userId
])->find();
if($isBind){
return false;
}
$parentInfo = \app\common\model\User::getUserInfo($parentId);
if(!$parentInfo){
return false;
}
self::insert([
'uniacid'=>UNIACID,
'parent_id'=>$parentId,
'user_id'=>$userId,
'createtime'=>time(),
'status'=>1
]);
//检查升级
(new \app\api\model\app\agent\Level())->handle($parentId);
return true;
}
public function parentUser()
{
return $this->belongsTo('app\common\model\User', 'parent_id', 'id', [], 'right')->setEagerlyType(0);
}
public function member()
{
return $this->belongsTo('app\common\model\app\agent\Member', 'parent_id', 'user_id', [], 'right')->setEagerlyType(0);
}
}