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.
96 lines
3.5 KiB
96 lines
3.5 KiB
from tortoise import fields, models
|
|
from tortoise.indexes import Index
|
|
from tortoise.contrib.pydantic import pydantic_model_creator
|
|
from tortoise.signals import post_save
|
|
from typing import Type
|
|
from helper.tools import genRandomStr
|
|
|
|
class Order(models.Model):
|
|
id = fields.UUIDField(pk=True)
|
|
num = fields.CharField(null=True, max_length=255, description='短编号')
|
|
sequence = fields.IntField(default=0, description='管理员手动排序号')
|
|
orderid = fields.CharField(null=True, max_length=255, description='中控系统订单编号')
|
|
owner = fields.ForeignKeyField('models.User', on_delete=fields.CASCADE, null=True, description='订单所有人')
|
|
ordertime = fields.DatetimeField(null=True, description='预约领用时间')
|
|
|
|
tasks = fields.ManyToManyField('models.Task', related_name='ordertasks')
|
|
|
|
is_canceled = fields.BooleanField(default=False, description='是否取消')
|
|
cancel_time = fields.DatetimeField(null=True, description='取消时间')
|
|
|
|
is_failed = fields.BooleanField(default=False, description='是否失败')
|
|
is_done = fields.BooleanField(default=False, description='是否完成')
|
|
|
|
is_finish = 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",)
|
|
allow_cycles = True
|
|
max_recursion = 4
|
|
|
|
class Meta:
|
|
table = 'Orders'
|
|
table_description = '订单表'
|
|
|
|
ordering = ["-sequence", "ordertime"]
|
|
|
|
indexes = [
|
|
Index(fields={"num"}, name="numidx"),
|
|
Index(fields={"orderid"}, name="orderididx"),
|
|
Index(fields={"ordertime"}, name="order_ordertimeidx"),
|
|
Index(fields={"is_canceled"}, name="order_is_canceledidx"),
|
|
Index(fields={"is_done"}, name="is_doneidx"),
|
|
Index(fields={"is_finish"}, name="is_finishidx"),
|
|
Index(fields={"is_failed"}, name="is_failedidx"),
|
|
Index(fields={"is_valid"}, name="is_valididx"),
|
|
Index(fields={"is_active"}, name="is_activeidx"),
|
|
]
|
|
|
|
def __str__(self):
|
|
return f'Order(id={self.id} username={self.owner.username}, name={self.owner.name}, ordertime={self.ordertime} , created_at={self.created_at})'
|
|
|
|
def as_json(self):
|
|
return dict(
|
|
id="{}".format(self.id),
|
|
username=self.owner.username,
|
|
name=self.owner.name,
|
|
ordertime="{}".format(self.ordertime),
|
|
created_at="{}".format(self.created_at)
|
|
)
|
|
|
|
|
|
Order_Pydantic = pydantic_model_creator(Order, name="Order")
|
|
|
|
|
|
@post_save(Order)
|
|
async def order_post_save(
|
|
sender: "Type[Order]",
|
|
instance: Order,
|
|
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 Order.filter(num = num).exists():
|
|
# print("num exist, ", num)
|
|
checkNum()
|
|
else:
|
|
# print("num not exist, ", num)
|
|
instance.num = num
|
|
await instance.save()
|
|
|
|
await checkNum()
|
|
|