VerifVATInvoice/alibabacloud_sample/dbservice.py

130 lines
5.8 KiB
Python
Raw Normal View History

import sqlite3
from sqlite3 import Error
class DBService:
db_file = "verify.db"
conn = None
def __init__(self):
"""
初始化函数
"""
self.conn = self.get_conn()
print("DBService init")
self.create_file_data_table()
self.create_invoice_table()
self.create_verify_log_table()
pass
def get_conn(self):
"""
创建数据库连接
该函数用于创建数据库连接并返回连接对象
返回值:
连接对象
"""
if self.conn is not None:
return self.conn
try:
self.conn = sqlite3.connect(self.db_file)
except Error as e:
print(e)
return self.conn
def create_verify_log_table(self):
"""
创建验证记录表
该函数用于创建表并返回创建结果
"""
try:
# delete_table_sql = """DROP TABLE IF EXISTS verify_log;"""
# c = self.get_conn().cursor()
# c.execute(delete_table_sql)
create_table_sql = """CREATE TABLE IF NOT EXISTS verify_log (
id integer PRIMARY KEY,
cyjgxx text NOT NULL,
inspectionAmount text NOT NULL,
verify_time text NOT NULL,
file_path text NOT NULL,
desc text
);"""
c = self.get_conn().cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
# 文件数据表
def create_file_data_table(self):
"""
创建文件数据表
该函数用于创建表并返回创建结果
"""
try:
# delete_table_sql = """DROP TABLE IF EXISTS file_data;"""
# c = self.get_conn().cursor()
# c.execute(delete_table_sql)
create_table_sql = """CREATE TABLE IF NOT EXISTS file_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_name text NOT NULL,
file_path text NOT NULL,
file_size integer NOT NULL,
file_type text NOT NULL,
file_time text NOT NULL,
file_hash text NOT NULL,
UNIQUE (file_hash) -- 联合唯一约束
);"""
c = self.get_conn().cursor()
c.execute(create_table_sql)
self.get_conn().commit()
except Error as e:
print(e)
# 发票数据表
def create_invoice_table(self):
# delete_table_sql = """DROP TABLE IF EXISTS invoice;"""
# c = self.get_conn().cursor()
# c.execute(delete_table_sql)
create_table_sql = """
-- 主表存储发票基本信息
CREATE TABLE IF NOT EXISTS invoice (
id INTEGER PRIMARY KEY AUTOINCREMENT,
checkCode TEXT NOT NULL UNIQUE, -- 校验码(唯一)
drawer TEXT, -- 开票人
formType TEXT, -- 表单类型
invoiceAmountPreTax REAL, -- 税前金额
invoiceCode TEXT NOT NULL, -- 发票代码
invoiceDate TEXT, -- 开票日期
invoiceNumber TEXT NOT NULL, -- 发票号码
invoiceTax REAL, -- 发票税额
invoiceType TEXT, -- 发票类型
machineCode TEXT, -- 机器编号
passwordArea TEXT, -- 密码区
printedInvoiceCode TEXT, -- 打印发票代码
printedInvoiceNumber TEXT, -- 打印发票号码
purchaserBankAccountInfo TEXT, -- 购买方银行信息
purchaserContactInfo TEXT, -- 购买方联系信息
purchaserName TEXT, -- 购买方名称
purchaserTaxNumber TEXT, -- 购买方税号
recipient TEXT, -- 收款人
remarks TEXT, -- 备注
reviewer TEXT, -- 复核人
sellerBankAccountInfo TEXT, -- 销售方银行信息
sellerContactInfo TEXT, -- 销售方联系信息
sellerName TEXT, -- 销售方名称
sellerTaxNumber TEXT, -- 销售方税号
specialTag TEXT, -- 特殊标记
title TEXT, -- 发票标题
totalAmount REAL, -- 合计金额
totalAmountInWords TEXT, -- 大写金额
file_path TEXT, -- 文件哈希
verify_time TEXT, -- 验证时间
verify_status TEXT, -- 验证结果
desc TEXT, -- 备注
desc_files TEXT, -- 备注附件
status TEXT, -- 是否报销yes,no
UNIQUE (invoiceCode, invoiceNumber) -- 联合唯一约束
);
"""
try:
c = self.get_conn().cursor()
c.execute(create_table_sql)
except Error as e:
print(e)