from datetime import datetime, timedelta from tortoise import fields, models from fastapi import HTTPException # from models.archive import Archive from models.template import Template class StorageFormItem(models.Model): id = fields.UUIDField(pk = True) storage_form = fields.ForeignKeyField('rms.StorageForm', to_field = 'id', description = '入库单ID') template = fields.ForeignKeyField('rms.Template', to_field = 'id', description = '药剂模板ID') k1 = fields.CharField(null = True, max_length = 255, description = '关键字段1') k2 = fields.CharField(null = True, max_length = 255, description = '关键字段2') k3 = fields.CharField(null = True, max_length = 255, description = '关键字段3') k4 = fields.CharField(null = True, max_length = 255, description = '关键字段4') k5 = fields.CharField(null = True, max_length = 255, description = '关键字段5') k6 = fields.CharField(null = True, max_length = 255, description = '关键字段6') fill_json_content = fields.JSONField(null = True, description = '扩展字段') created_at = fields.DatetimeField(auto_now_add = True) updated_at = fields.DatetimeField(auto_now = True) class Meta: table = 'storage_form_items' table_description = '入库单详细表' async def calculate_expire_date(self, template_obj): """ 计算过期日期 - 如果用户定义了生产日期与保质期,计算过期日期.如果用户定义了过期日期返回过期日期 """ if self.fill_json_content.get("expire_date"): return self.fill_json_content.get("expire_date") if not self.fill_json_content.get("produce_date") or not self.fill_json_content.get("shelf_life"): return "" else: produce_date = self.fill_json_content.get("produce_date") shelf_life = self.fill_json_content.get("shelf_life") production_date = datetime.strptime(produce_date, '%Y-%m-%d') shelf_life_date = timedelta(days = int(shelf_life)) expiration_date = production_date + shelf_life_date return expiration_date.strftime('%Y-%m-%d')