class MvsiResult(BaseObject, db.Model): """ 机动车销售发票OCR识别 响应实体 """ __abstract__ = True code = db.Column(db.String(length=20), name='CODE', comment='发票代码') number = db.Column(db.String(length=20), name='NUMBER', comment='发票号码') issue_date = db.Column(db.DateTime, name='ISSUE_DATE', comment='开票日期') machine_printed_code = db.Column(db.String(length=20), name='MACHINE_PRINTED_CODE', comment='机打代码') machine_printed_number = db.Column(db.String(length=20), name='MACHINE_PRINTED_NUMBER', comment='机打号码') machine_number = db.Column(db.String(length=20), name='MACHINE_NUMBER', comment='机器编号') fiscal_code = db.Column(db.String(length=190), name='FISCAL_CODE', comment='税控码') buyer_name = db.Column(db.String(length=20), name='BUYER_NAME', comment='购买方名称') buyer_organization_number = db.Column(db.String(length=30), name='BUYER_ORGANIZATION_NAMBER', comment='购买方身份证号码/组织机构代码') buyer_id = db.Column(db.String(length=20), name='BUYER_ID', comment='购买方纳税人识别号') seller_name = db.Column(db.Text, name='SELLER_NAME', comment='销售方名称') seller_phone = db.Column(db.String(length=20), name='SELLER_PHONE', comment='销售方电话') seller_id = db.Column(db.String(length=20), name='SELLER_ID', comment='销售方纳税人识别号') seller_account = db.Column(db.String(length=64), name='SELLER_ACCOUNT', comment='销售方账号') seller_address = db.Column(db.Text, name='SELLER_ADDRESS', comment='销售方地址') seller_bank = db.Column(db.Text, name='SELLER_BANK', comment='销售方开户行') vehicle_type = db.Column(db.Text, name='VEHICLE_TYPE', comment='多用途乘用车') brand_model = db.Column(db.Text, name='BRAND_MODEL', comment='厂牌型号') manufacturing_location = db.Column(db.String(length=225), name='MANUFACTURING_LOCATION', comment='产地') quality_certificate = db.Column(db.String(length=20), name='QUALITY_CERTIFICATE', comment='合格证号') import_certificate = db.Column(db.String(length=18), name='IMPORT_CERTIFICATE', comment='进口证明书号') inspection_number = db.Column(db.String(length=20), name='INSPECTION_NUMBER', comment='商检单号') engine_number = db.Column(db.String(length=20), name='ENGINE_NUMBER', comment='发动机号码') vehicle_identification_number = db.Column(db.String(length=20), name='VEHICLE_IDENTIFICATION_NUMBER', comment='车架号码') tonnage = db.Column(db.String(length=10), name='TONNAGE', comment='吨位') seating_capacity = db.Column(db.String(length=10), name='SEATING_CAPACITY', comment='限乘人数') tax_authority = db.Column(db.Text, name='TAX_AUTHORITY', comment='主管税务机关') tax_authority_code = db.Column(db.String(length=20), name='TAX_AUTHORITY_CODE', comment='主管税务机关代码') tax_payment_receipt = db.Column(db.String(length=18), name='TAX_PAYMENT_RECEIPT', comment='完税凭证号码') tax_rate = db.Column(db.String(length=6), name='TAX_RATE', comment='增值税税率或征收率') tax = db.Column(db.Numeric(precision=20, scale=2, asdecimal=False), name='TAX', nullable=False, default=0.00, comment='增值税税额') tax_exclusive_price = db.Column(db.Numeric(precision=20, scale=2, asdecimal=False), name='TAX_EXCLUSIVE_PRICE', nullable=False, default=0.00, comment='增值税税额') total = db.Column(db.Numeric(precision=20, scale=2, asdecimal=False), name='TOTAL', nullable=False, default=0.00, comment='价税合计') total_chinese = db.Column(db.Text, name='TOTAL_CHINESE', comment='价税合计大写') def __init__(self, error_code=None, error_msg=None, **kwargs): super(self.__class__, self).__init__(**kwargs) self.error_code = error_code self.error_msg = error_msg
class OrderDetails(db.Model): __tablename__ = 'order_details' orderDetailsID = db.Column(db.Integer(), primary_key=True) item = db.Column(db.String(64)) name = db.Column(db.String(215), index=True) dob = db.Column(db.String(32), index=True) height = db.Column(db.Numeric(scale=2)) weight = db.Column(db.Numeric(scale=2)) message = db.Column(db.String(300)) is_message = db.Column(db.Boolean(), default=False) order_ID = db.Column(db.Integer(), db.ForeignKey('orders.orderID')) product_ID = db.Column(db.Integer(), db.ForeignKey('products.productID')) shipper_ID = db.Column(db.Integer(), db.ForeignKey('shippers.shipperID')) customer_ID = db.Column(db.Integer(), db.ForeignKey('customers.customerID'))
class Product(db.Model): __tablename__ = 'products' productID = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(64), index=True) description = db.Column(db.String(120)) price = db.Column(db.Numeric(scale=2)) is_available = db.Column(db.Boolean(), default=True) supplier_ID = db.Column(db.Integer(), ForeignKey('suppliers.supplierID'))
class blood_resource(db.Model): __tablename__ = 'blood_resource' id = db.Column(db.Integer, primary_key=True) blood_group = db.Column(db.String(3)) bag_type = db.Column(db.String(10)) event_id = db.Column(db.Integer) center_id = db.Column(db.Integer, db.ForeignKey('blood_center.id'), nullable=False) extraction_date = db.Column(db.DateTime) cost = db.Column(db.Numeric(4, 2)) status = db.Column(db.String(1))
class Payment(db.Model): __tablename__ = 'payment' id = db.Column(db.Integer, primary_key=True) amount = db.Column(db.Numeric(10, 2), nullable=False) created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False) currency = db.Column(db.String(3), nullable=False) description = db.Column(db.Text, nullable=True) def __repr__(self): return f'{self.id}:{self.amount}:{self.currency} - {self.description}'
class Order(db.Model): __tablename__ = 'orders' orderID = db.Column(db.Integer(), primary_key=True) wasOrdered = db.Column(db.Boolean(), unique=False, default=False) wasAccepted = db.Column(db.Boolean(), default=False) dateOrdered = db.Column(db.DateTime(), server_default=func.now(), index=True) wasFulfilled = db.Column(db.Boolean(), unique=False, default=False) total = db.Column(db.Numeric(scale=2)) customer_ID = db.Column(db.Integer(), db.ForeignKey('customers.customerID')) payment_ID = db.Column(db.Integer(), db.ForeignKey('payments.paymentID')) shipper_ID = db.Column(db.Integer(), db.ForeignKey('shippers.shipperID')) supplier_ID = db.Column(db.Integer(), db.ForeignKey('suppliers.supplierID')) order_details = db.relationship('OrderDetails', backref='order')
class MenuItem(ModelBase): __tablename__ = 'menu_item' id = db.Column(db.Integer(), primary_key=True, autoincrement=True) menu_id = db.Column(db.Integer(), db.ForeignKey('menu.id', onupdate='CASCADE', ondelete='CASCADE'), nullable=False) translatable_id = db.Column(db.Integer(), db.ForeignKey('translatable.id', onupdate='CASCADE', ondelete='RESTRICT'), nullable=False) external_id = db.Column(db.Integer(), unique=True, nullable=True, server_default=expression.null()) course_type = db.Column(db.Enum(CourseType), nullable=False) course_sub_type = db.Column(db.Enum(CourseSubType), nullable=False) course_attributes = db.Column(db.Text(), nullable=False, default='[]', server_default='[]') course_allergens = db.Column(db.Text(), nullable=False, default='[]', server_default='[]') price_students = db.Column(db.Numeric(4, 2), nullable=False) price_staff = db.Column(db.Numeric(4, 2), nullable=True) data_frozen = db.Column(db.Boolean(), nullable=False, server_default=expression.false()) def __init__(self, menu: Menu, translatable_id: int, course_type: CourseType, course_sub_type: CourseSubType, price_students: Decimal, price_staff: Optional[Decimal]): if not isinstance(menu, Menu): raise expected('menu', menu, Menu) if not isinstance(translatable_id, int): raise expected('translatable_id', translatable_id, int) if not isinstance(course_type, CourseType): raise expected('course_type', course_type, CourseType) if not isinstance(course_sub_type, CourseSubType): raise expected('course_sub_type', course_sub_type, CourseSubType) if not isinstance(price_students, Decimal): raise expected('price_students', price_students, Decimal) if price_staff is not None and not isinstance(price_staff, Decimal): raise expected_or_none('price_staff', price_staff, Decimal) self.menu = menu self.translatable_id = translatable_id self.course_type = course_type self.course_sub_type = course_sub_type self.price_students = price_students self.price_staff = price_staff def get_translation(self, language: str, translator: 'TranslationService') -> 'Translation': return self.translatable.get_translation(language, translator) @staticmethod def format_price(price: Decimal) -> str: if price == 0.0: return '' return locale.currency(price).replace(' ', '') def get_attributes(self) -> List[CourseAttributes]: # Stored as a list of strings or a list of ints (backwards compat) return [CourseAttributes(v) if isinstance(v, int) else CourseAttributes[v] for v in json.loads(self.course_attributes)] def set_attributes(self, attributes: List[CourseAttributes]): self.course_attributes = json.dumps([v.name for v in attributes]) def get_allergens(self) -> List[CourseAllergens]: # Stored as a list of strings return [CourseAllergens[v] for v in json.loads(self.course_allergens)] def set_allergens(self, allergens: List[CourseAllergens]): self.course_allergens = json.dumps([v.name for v in allergens]) def __hash__(self): return hash(self.id)
class FileModel(BasicModel): """ 文件模型 """ __tablename__ = 'FILE' md5_id = db.Column(db.String(length=64), name='MD5_ID', index=True, nullable=False, comment='文件MD5值') file_name = db.Column(db.String(length=64), name='FILE_NAME', nullable=False, comment='文件名') file_format = db.Column(db.String(length=20), name='FILE_FORMAT', nullable=False, comment='文件格式') file_size = db.Column(db.Numeric(precision=10, scale=2, asdecimal=False), name='FILE_SIZE', nullable=False, default=0.00, comment='文件大小') file_path_hash = db.Column(db.String, name='FILE_PATH_HASH', nullable=False, comment='文件路径密文') @property def file_path(self): aes = AESUtil(current_app.config.get('DATA_PATH_KEY')) return aes.decrypt(self.file_path_hash) @file_path.setter def file_path(self, file_path): """ 设置路径 :param file_path: :return: """ # 文件路径加密 aes = AESUtil(current_app.config.get('DATA_PATH_KEY')) self.file_path_hash = aes.encrypt(file_path) def __init__(self, file_base64=None, **kwargs): super(FileModel, self).__init__(**kwargs) self.file_base64 = file_base64 def dao_down_file(self, id, md5_id): """ 文件查询 :param id: 文件ID :param md5_id: :return: """ return self.query.filter(FileModel.id == id, FileModel.md5_id == md5_id).first() def dao_init_file(self, file_path, id=None, file_name=None, subtransactions=False, nested=False): """ 根据路径解析入库 :param nested: :param subtransactions: :param file_name :param file_path: 文件路径 :param id: :return: """ with db.auto_commit_db(subtransactions=subtransactions, nested=nested) as s: super().dao_create(id) # 计算文件 md5 self.md5_id = FileUtil.get_md5_path(file_path) # noinspection PyAttributeOutsideInit self.file_path = file_path _, self.file_name, self.file_format = FileUtil.get_path_name_ext( file_path) if is_not_empty(file_name): self.file_name = file_name self.file_size = FileUtil.get_file_size(file_path) s.add(self)
class Mvsi(BasicModel): """ 机动车销售发票 """ __tablename__ = 'MVSI_OCR' ocr_file_id = db.Column(db.String(length=64), db.ForeignKey('OCR_FILE.ID'), name='OCR_FILE_ID', nullable=False, index=True, comment='OCR流程ID') code = db.Column(db.String(length=20), name='CODE', comment='发票代码') number = db.Column(db.String(length=20), name='NUMBER', comment='发票号码') issue_date = db.Column(db.DateTime, name='ISSUE_DATE', comment='开票日期') machine_printed_code = db.Column(db.String(length=20), name='MACHINE_PRINTED_CODE', comment='机打代码') machine_printed_number = db.Column(db.String(length=20), name='MACHINE_PRINTED_NUMBER', comment='机打号码') machine_number = db.Column(db.String(length=20), name='MACHINE_NUMBER', comment='机器编号') fiscal_code = db.Column(db.String(length=190), name='FISCAL_CODE', comment='税控码') buyer_name = db.Column(db.String(length=20), name='BUYER_NAME', comment='购买方名称') buyer_organization_number = db.Column(db.String(length=30), name='BUYER_ORGANIZATION_NAMBER', comment='购买方身份证号码/组织机构代码') buyer_id = db.Column(db.String(length=20), name='BUYER_ID', comment='购买方纳税人识别号') seller_name = db.Column(db.Text, name='SELLER_NAME', comment='销售方名称') seller_phone = db.Column(db.String(length=20), name='SELLER_PHONE', comment='销售方电话') seller_id = db.Column(db.String(length=20), name='SELLER_ID', comment='销售方纳税人识别号') seller_account = db.Column(db.String(length=64), name='SELLER_ACCOUNT', comment='销售方账号') seller_address = db.Column(db.Text, name='SELLER_ADDRESS', comment='销售方地址') seller_bank = db.Column(db.Text, name='SELLER_BANK', comment='销售方开户行') vehicle_type = db.Column(db.Text, name='VEHICLE_TYPE', comment='多用途乘用车') brand_model = db.Column(db.Text, name='BRAND_MODEL', comment='厂牌型号') manufacturing_location = db.Column(db.String(length=225), name='MANUFACTURING_LOCATION', comment='产地') quality_certificate = db.Column(db.String(length=20), name='QUALITY_CERTIFICATE', comment='合格证号') import_certificate = db.Column(db.String(length=18), name='IMPORT_CERTIFICATE', comment='进口证明书号') inspection_number = db.Column(db.String(length=20), name='INSPECTION_NUMBER', comment='商检单号') engine_number = db.Column(db.String(length=20), name='ENGINE_NUMBER', comment='发动机号码') vehicle_identification_number = db.Column( db.String(length=20), name='VEHICLE_IDENTIFICATION_NUMBER', comment='车架号码') tonnage = db.Column(db.String(length=10), name='TONNAGE', comment='吨位') seating_capacity = db.Column(db.String(length=10), name='SEATING_CAPACITY', comment='限乘人数') tax_authority = db.Column(db.Text, name='TAX_AUTHORITY', comment='主管税务机关') tax_authority_code = db.Column(db.String(length=20), name='TAX_AUTHORITY_CODE', comment='主管税务机关代码') tax_payment_receipt = db.Column(db.String(length=18), name='TAX_PAYMENT_RECEIPT', comment='完税凭证号码') tax_rate = db.Column(db.String(length=6), name='TAX_RATE', comment='增值税税率或征收率') tax = db.Column(db.Numeric(precision=20, scale=2, asdecimal=False), name='TAX', nullable=False, default=0.00, comment='增值税税额') tax_exclusive_price = db.Column(db.Numeric(precision=20, scale=2, asdecimal=False), name='TAX_EXCLUSIVE_PRICE', nullable=False, default=0.00, comment='增值税税额') total = db.Column(db.Numeric(precision=20, scale=2, asdecimal=False), name='TOTAL', nullable=False, default=0.00, comment='价税合计') total_chinese = db.Column(db.Text, name='TOTAL_CHINESE', comment='价税合计大写') def __init__(self, **kwargs): super(self.__class__, self).__init__(**kwargs) def dao_add(self, ocrFile, file_model, file_path): """ 数据入库 :param ocrFile: :param file_model :param file_path: 文件路径 :return: """ super().dao_create() with db.auto_commit_db(False, False, error_call=FileUtil.del_file, file_path=file_path) as s: # 文件入库 file_model.dao_init_file(file_path, id=file_model.id, file_name=ocrFile.file_data.file_name, nested=True) ocrFile.file_id = file_model.id ocrFile.dao_add(nested=True) self.ocr_file_id = ocrFile.id s.add(self)