追加数据库单例模式

This commit is contained in:
honghuayuan 2025-06-26 02:51:51 +08:00
parent 2093056334
commit d0a9ab4642
5 changed files with 73 additions and 535833 deletions

BIN
data/售价尾端价格.xlsx (Stored with Git LFS)

Binary file not shown.

View File

@ -4,7 +4,7 @@ from pathlib import Path
import pandas as pd import pandas as pd
from utils.gtools import MySQLconnect from utils.gtools import DBconnect
# 根据SPU判断空运或海运1为海运 # 根据SPU判断空运或海运1为海运
@ -62,17 +62,21 @@ def spu_head_type(base_df: pd.DataFrame):
class SellPriceBase: class SellPriceBase:
parent_current_directory = Path(__file__).parent.parent parent_current_directory = Path(__file__).parent.parent
price_path = parent_current_directory.joinpath("data")
_price_files = price_path.joinpath("售价尾端价格.xlsx")
df_2025 = None df_2025 = None
df_2024 = None df_2024 = None
_instance = None
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
"""实现单例模式,只加载一次文件""" """实现单例模式,只加载一次文件"""
if cls._instance is None:
cls._instance = super().__new__(cls)
with DBconnect() as db:
if cls.df_2024 is None: if cls.df_2024 is None:
cls.df_2024 = pd.read_excel(cls._price_files,sheet_name="2025") cls.df_2024 = pd.read_sql("select * from sell_price_ending_2024", db.eng)
if cls.df_2025 is None: if cls.df_2025 is None:
cls.df_2025 = pd.read_excel(cls._price_files,sheet_name="2025") cls.df_2025 = pd.read_sql("select * from sell_price_ending_2025", db.eng)
return super().__new__(cls) return cls._instance
return cls._instance
def __init__(self, packages, purchase_price, shipping_type, ocean_first_cny, ocean_first_usd, air_first_usd, def __init__(self, packages, purchase_price, shipping_type, ocean_first_cny, ocean_first_usd, air_first_usd,
air_cny_type, air_first_fix, exchange_rate, profit_rate, air_rate,tax_rate): air_cny_type, air_first_fix, exchange_rate, profit_rate, air_rate,tax_rate):
self.packages = packages # 单sku包裹数据 self.packages = packages # 单sku包裹数据

View File

@ -30,3 +30,29 @@ class MySQLconnect():
def connect(self): def connect(self):
return pymysql.connect(host=self.host, port=3306, database=self.dbname, user="zhenggantian", password="123456", return pymysql.connect(host=self.host, port=3306, database=self.dbname, user="zhenggantian", password="123456",
charset="utf8") charset="utf8")
class DBconnect():
def __init__(self):
self.host = '192.168.100.33'
def __enter__(self):
self.eng = self.engine()
self.con = self.connect()
self.cur = self.con.cursor()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.eng.dispose()
self.cur.close()
self.con.close()
if exc_val:
raise
def engine(self):
return create_engine("mysql+pymysql://logistics:logistics123@" + self.host + f":3306/logistics",
pool_size=10, max_overflow=5, pool_recycle=3600)
def connect(self):
return pymysql.connect(host=self.host, port=3306, database="logistics", user="logistics", password="logistics123",
charset="utf8")

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff