VerifVATInvoice/alibabacloud_sample/dbservice.py
liuxiaoqing 9a004a3133 build(docker): 更新 Docker 配置并修改数据库文件路径
- 修改数据库文件路径为 /data/verify.db
- 在 docker-compose.yml 中添加数据卷映射
- 更新 Dockerfile,包含代码克隆、依赖安装和启动命令
2025-08-20 10:29:18 +08:00

131 lines
5.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sqlite3
from sqlite3 import Error
class DBService:
db_file = "/data/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, -- 验证结果
inspectionAmount 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)