You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.5 KiB

from tortoise import fields, models
from tortoise.indexes import Index
from tortoise.contrib.pydantic import pydantic_model_creator, pydantic_queryset_creator
from tortoise.signals import post_save
from typing import Type
from helper.tools import genRandomStr
class Vacancy(models.Model):
id = fields.UUIDField(pk=True)
number = fields.CharField(max_length=255, null=True, description='空位编号')
traynum = fields.CharField(max_length=255, null=True, description='托盘编号')
orderids = fields.JSONField(null=True, description='托盘编号对应的订单号列表')
memo = fields.JSONField(null=True, description='备忘录')
is_shelf = fields.BooleanField(default=False, description='是货架库存')
is_connect = fields.BooleanField(default=False, description='是接驳区库存')
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)
is_valid = fields.BooleanField(default=True, description='是否取消')
is_active = fields.BooleanField(default=True, description='是否删除')
class PydanticMeta:
# Let's exclude the created timestamp
exclude = ("created_at",)
class Meta:
table = 'Vacancys'
table_description = '暂存货架或接驳机空位编号表'
ordering = ["number", "created_at"]
indexes = [
Index(fields={"number"}, name="numberidx"),
Index(fields={"traynum"}, name="traynumidx"),
Index(fields={"is_shelf"}, name="is_shelfidx"),
Index(fields={"is_connect"}, name="is_connectidx"),
Index(fields={"is_valid"}, name="is_valididx"),
Index(fields={"is_active"}, name="is_activeidx"),
]
def __str__(self):
return f'Vacancy(id={self.id} number={self.number}, traynum={self.traynum}, created_at={self.created_at})'
def as_json(self):
return dict(id="{}".format(self.id), number=self.number, traynum=self.traynum, created_at="{}".format(self.created_at))
Vacancy_Pydantic = pydantic_model_creator(Vacancy, name="Vacancy")
Vacancy_Pydantic_List = pydantic_queryset_creator(Vacancy)
class Schelve(models.Model):
id = fields.UUIDField(pk=True)
num = fields.CharField(null=True, max_length=255, description='短编号')
name = fields.CharField(null=True, max_length=255, description='暂存货架编号')
vacancies = fields.ManyToManyField('models.Vacancy', related_name='schelvevacancies')
memo = fields.JSONField(null=True, description='备忘录')
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)
is_valid = fields.BooleanField(default=True, description='是否取消')
is_active = fields.BooleanField(default=True, description='是否删除')
class PydanticMeta:
# Let's exclude the created timestamp
exclude = ("created_at",)
allow_cycles = True
max_recursion = 4
class Meta:
table = 'Schelves'
table_description = '暂存货架库存表'
ordering = ["name", "created_at"]
indexes = [
Index(fields={"name"}, name="nameidx"),
Index(fields={"num"}, name="numidx"),
Index(fields={"is_valid"}, name="is_valididx"),
Index(fields={"is_active"}, name="is_activeidx"),
]
def __str__(self):
return f'Schelve(id={self.id} num={self.num}, created_at={self.created_at})'
def as_json(self):
return dict(
id="{}".format(self.id),
num=self.num,
name =self.name,
memo =self.memo,
is_valid =self.is_valid,
is_active =self.is_active,
updated_at="{}".format(self.updated_at),
created_at="{}".format(self.created_at))
Schelve_Pydantic = pydantic_model_creator(Schelve, name="Schelve")
@post_save(Schelve)
async def schelve_post_save(
sender: "Type[Schelve]",
instance: Schelve,
created,
using_db,
update_fields,
) -> None:
if not instance.num or "null" == instance.num:
# print("instance.num: ", instance.num)
# check duplicated num
async def checkNum():
num = genRandomStr(4)
# print("new num: ", num)
if await Schelve.filter(num = num).exists():
# print("num exist, ", num)
checkNum()
else:
# print("num not exist, ", num)
instance.num = num
await instance.save()
await checkNum()