73 lines
1.6 KiB
PHP
73 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace app\admin\model\app\agent;
|
|
|
|
use think\Model;
|
|
|
|
|
|
class Relation extends Model
|
|
{
|
|
|
|
|
|
// 表名
|
|
protected $name = 'app_agent_relation';
|
|
|
|
// 自动写入时间戳字段
|
|
protected $autoWriteTimestamp = 'integer';
|
|
|
|
// 定义时间戳字段名
|
|
protected $createTime = 'createtime';
|
|
protected $updateTime = false;
|
|
protected $deleteTime = false;
|
|
|
|
// 追加属性
|
|
protected $append = [
|
|
|
|
];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
|
|
}
|
|
|
|
public function parent()
|
|
{
|
|
return $this->belongsTo('app\admin\model\User', 'parent_id', 'id', [], 'right')->setEagerlyType(0);
|
|
}
|
|
|
|
/**
|
|
* 设置上下级关系
|
|
* @param $userId 用户
|
|
* @param $parentId 上级
|
|
* @return void
|
|
*/
|
|
public function setRalation($userId,$parentId)
|
|
{
|
|
if(!$userId){
|
|
return false;
|
|
}
|
|
//判断有没有这条记录,没有添加,有的话判断是否相同,不相同就修改
|
|
$relation = self::where(['user_id'=>$userId])->find();
|
|
|
|
if(!$parentId){
|
|
self::where(['user_id'=>$userId])->delete();
|
|
return true;
|
|
}
|
|
|
|
if(!$relation){
|
|
self::insert([
|
|
'uniacid' => 1,
|
|
'createtime' => time(),
|
|
'user_id' => $userId,
|
|
'parent_id' => $parentId
|
|
]);
|
|
|
|
}else{
|
|
if($relation->parent_id != $parentId){
|
|
$relation->parent_id = $parentId;
|
|
$relation->save();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
} |