Implements the "运输流程" collection from yun8.postman_collection.json as a Python pipeline: driver-side order accept/load/unload -> freight accounting -> payment, with SQL writes, driven by yun8_sync/config.yaml (api/auth/accounting/payment/database/sql/dianyuntong sections).
54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
"""
|
||
数据库操作
|
||
==========
|
||
封装 ntocc.base_gd_mft_file 表的 INSERT 操作。
|
||
"""
|
||
|
||
import pymysql
|
||
|
||
|
||
class Database:
|
||
"""MySQL 数据库写入"""
|
||
|
||
def __init__(self, config: dict):
|
||
db = config["database"]
|
||
self.conn_kwargs = {
|
||
"host": db["host"],
|
||
"port": db.get("port", 3306),
|
||
"user": db["user"],
|
||
"password": db["password"],
|
||
"database": db["database"],
|
||
"charset": db.get("charset", "utf8mb4"),
|
||
}
|
||
sql_cfg = config["sql"]
|
||
self.file_name_template = sql_cfg["file_name_template"]
|
||
self.sql_template = sql_cfg["template"]
|
||
|
||
def build_sql(self, file_prefix: str, pay_no: str) -> str:
|
||
"""拼接完整 SQL(dry-run / 日志用)"""
|
||
file_name = self.file_name_template.format(file_prefix=file_prefix)
|
||
return self.sql_template.format(file_name=file_name, pay_no=pay_no)
|
||
|
||
def insert(self, file_prefix: str, pay_no: str) -> int:
|
||
"""执行 INSERT,返回 affected rows"""
|
||
sql = self.build_sql(file_prefix, pay_no)
|
||
conn = pymysql.connect(**self.conn_kwargs)
|
||
try:
|
||
with conn.cursor() as cur:
|
||
cur.execute(sql)
|
||
conn.commit()
|
||
return cur.rowcount
|
||
finally:
|
||
conn.close()
|
||
|
||
def execute(self, sql: str) -> int:
|
||
"""执行任意 SQL,返回 affected rows"""
|
||
conn = pymysql.connect(**self.conn_kwargs)
|
||
try:
|
||
with conn.cursor() as cur:
|
||
cur.execute(sql)
|
||
conn.commit()
|
||
return cur.rowcount
|
||
finally:
|
||
conn.close()
|