six_005
This commit is contained in:
+15
-18
@@ -1,8 +1,5 @@
|
||||
"""注释"""
|
||||
from django.db import models
|
||||
from uuid6 import uuid8
|
||||
|
||||
from wechat_数据库.雪花算法.雪花算法工具 import generator
|
||||
|
||||
"""
|
||||
输入 :当前用户实例(self)
|
||||
@@ -45,13 +42,13 @@ class weiuser用户角色表choicesStatus(models.TextChoices):
|
||||
class weiuser(models.Model):
|
||||
"""用户基础信息表"""
|
||||
objects = models.Manager() #
|
||||
|
||||
|
||||
class ChoicesStatus(models.TextChoices):
|
||||
"""商家转账订单状态"""
|
||||
推广员 = "推广员", gettext_lazy("推广员")
|
||||
企微员工 = "企微员工", gettext_lazy("企微员工")
|
||||
无状态 = "无状态", gettext_lazy("无状态")
|
||||
|
||||
|
||||
unionid = models.CharField(primary_key=True, max_length=120)
|
||||
user_角色权限 = models.CharField(choices=weiuser用户角色表choicesStatus, default=weiuser用户角色表choicesStatus.普通会员)
|
||||
user_企微角色权限 = models.CharField(max_length=128, null=True, blank=True, )
|
||||
@@ -71,6 +68,7 @@ class weiuser(models.Model):
|
||||
user_绑定企微usrID账号 = models.CharField(max_length=128, null=True, blank=True, )
|
||||
user_绑定企微UNIONID = models.CharField(max_length=128, null=True, blank=True, )
|
||||
user_IP地址 = models.GenericIPAddressField(null=True, blank=True, )
|
||||
user_手机设备参数 = models.JSONField(null=True, blank=True, )
|
||||
user_绑定用工关系 = models.CharField(max_length=128, null=True, blank=True, )
|
||||
codeConfigId = models.TextField(blank=True, null=True, ) # 企微的标签ID
|
||||
codeQrCode = models.TextField(blank=True, null=True, ) # 企微的二维码
|
||||
@@ -80,12 +78,12 @@ class weiuser(models.Model):
|
||||
add_time = models.DateTimeField(null=True, blank=True, db_comment="用户添加好友的时间")
|
||||
dateAndTime = models.DateTimeField(auto_now_add=True, db_comment="创建时间") # 数据创建时间
|
||||
updateTime = models.DateTimeField(auto_now=True, db_comment="更新时间") # 更新时间(需要用 save() )
|
||||
|
||||
|
||||
class Meta:
|
||||
db_table = "weiuser" # 给数据表自定义一个名称;默认的会是在 应用名+类名
|
||||
app_label = models_app_name
|
||||
ordering = ["-dateAndTime", "-updateTime"]
|
||||
|
||||
|
||||
def get_all_subordinates(self):
|
||||
"""递归获取所有下级(包括间接下级)"""
|
||||
subs = []
|
||||
@@ -93,7 +91,7 @@ class weiuser(models.Model):
|
||||
subs.append(sub)
|
||||
subs.extend(sub.get_all_subordinates())
|
||||
return subs
|
||||
|
||||
|
||||
# ↓↓↓ 新增方法:获取所有上级(递归实现)↓↓↓
|
||||
def get_all_superiors(self):
|
||||
"""
|
||||
@@ -106,7 +104,7 @@ class weiuser(models.Model):
|
||||
# 递归获取上级的上级
|
||||
superiors.extend(self.parent.get_all_superiors())
|
||||
return superiors
|
||||
|
||||
|
||||
def get_nth_level_superior(self, n):
|
||||
"""
|
||||
获取第n级上级
|
||||
@@ -120,7 +118,7 @@ class weiuser(models.Model):
|
||||
"""
|
||||
if n < 1:
|
||||
raise ValueError("n 必须大于或等于 1")
|
||||
|
||||
|
||||
current = self
|
||||
# 循环向上查询 n 次
|
||||
for _ in range(n):
|
||||
@@ -128,7 +126,7 @@ class weiuser(models.Model):
|
||||
return None # 没有父级时提前终止
|
||||
current = current.parent
|
||||
return current
|
||||
|
||||
|
||||
"""
|
||||
这种实现方式更加简洁,但需要注意:
|
||||
它使用了链式递归调用,可能会增加递归深度
|
||||
@@ -140,7 +138,7 @@ class weiuser(models.Model):
|
||||
内存使用:递归会占用更多栈空间
|
||||
在大多数实际应用中,树结构的深度不会超过100层,因此递归实现通常是可行的。但如果预期树结构会非常深(如超过1000层),建议使用循环实现或混合实现。
|
||||
"""
|
||||
|
||||
|
||||
def find_ancestor_by_field(self, field_name, target_value, include_self=False):
|
||||
"""
|
||||
从当前用户向上查找,找到第一个指定字段等于目标值的祖先
|
||||
@@ -152,11 +150,11 @@ class weiuser(models.Model):
|
||||
# 验证字段名是否有效 - 修复方法
|
||||
if not hasattr(self, field_name):
|
||||
raise ValueError(f"无效字段名: {field_name}")
|
||||
|
||||
|
||||
# 如果包含自身且满足条件,直接返回
|
||||
if include_self and getattr(self, field_name) == target_value:
|
||||
return self
|
||||
|
||||
|
||||
# 向上遍历祖先
|
||||
current = self.parent
|
||||
while current:
|
||||
@@ -164,7 +162,7 @@ class weiuser(models.Model):
|
||||
return current
|
||||
current = current.parent
|
||||
return None
|
||||
|
||||
|
||||
def find查找指定的上级(self, field_name, target_value, include_self=False):
|
||||
"""
|
||||
从当前用户向上查找,找到第一个指定字段等于目标值的祖先
|
||||
@@ -176,11 +174,11 @@ class weiuser(models.Model):
|
||||
# 验证字段名是否有效 - 修复方法
|
||||
if not hasattr(self, field_name):
|
||||
raise ValueError(f"无效字段名: {field_name}")
|
||||
|
||||
|
||||
# 如果包含自身且满足条件,直接返回
|
||||
if include_self and getattr(self, field_name) == target_value:
|
||||
return self
|
||||
|
||||
|
||||
# 向上遍历祖先
|
||||
current = self.parent
|
||||
while current:
|
||||
@@ -189,7 +187,6 @@ class weiuser(models.Model):
|
||||
current = current.parent
|
||||
return None
|
||||
|
||||
|
||||
# class weiuser用户角色表(models.Model):
|
||||
# """城市信息表"""
|
||||
# objects = models.Manager() #
|
||||
|
||||
Reference in New Issue
Block a user