first commit
This commit is contained in:
64
backend/app/models/__init__.py
Normal file
64
backend/app/models/__init__.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
from app.config import DATABASE_URL
|
||||
import os
|
||||
|
||||
# MySQL 連線引擎設定
|
||||
engine = create_engine(
|
||||
DATABASE_URL,
|
||||
pool_size=10,
|
||||
max_overflow=20,
|
||||
pool_pre_ping=True,
|
||||
pool_recycle=3600,
|
||||
echo=False
|
||||
)
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base()
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
# Import models to register them
|
||||
from app.models.user import User, UserRole
|
||||
from app.models.dit import DitRecord
|
||||
from app.models.sample import SampleRecord
|
||||
from app.models.order import OrderRecord
|
||||
from app.models.match import MatchResult, ReviewLog
|
||||
|
||||
def init_db():
|
||||
"""初始化資料庫並建立預設管理員"""
|
||||
from app.utils.security import get_password_hash
|
||||
|
||||
# 建立所有資料表
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
# 建立預設管理員帳號
|
||||
db = SessionLocal()
|
||||
try:
|
||||
admin_email = os.getenv("ADMIN_EMAIL", "admin@example.com")
|
||||
admin_password = os.getenv("ADMIN_PASSWORD", "admin123")
|
||||
|
||||
existing = db.query(User).filter(User.email == admin_email).first()
|
||||
if not existing:
|
||||
admin = User(
|
||||
email=admin_email,
|
||||
password_hash=get_password_hash(admin_password),
|
||||
display_name="Administrator",
|
||||
language="zh-TW",
|
||||
role=UserRole.admin
|
||||
)
|
||||
db.add(admin)
|
||||
db.commit()
|
||||
print(f"[Init] Admin user created: {admin_email}")
|
||||
else:
|
||||
print(f"[Init] Admin user exists: {admin_email}")
|
||||
except Exception as e:
|
||||
print(f"[Init] Error creating admin: {e}")
|
||||
db.rollback()
|
||||
finally:
|
||||
db.close()
|
||||
22
backend/app/models/dit.py
Normal file
22
backend/app/models/dit.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Float, UniqueConstraint
|
||||
from sqlalchemy.sql import func
|
||||
from app.models import Base
|
||||
from app.config import TABLE_PREFIX
|
||||
|
||||
class DitRecord(Base):
|
||||
__tablename__ = f"{TABLE_PREFIX}DIT_Records"
|
||||
__table_args__ = (
|
||||
UniqueConstraint('op_id', 'pn', name='uix_dit_op_pn'),
|
||||
)
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
op_id = Column(String(255), index=True, nullable=False) # 移除 unique,因為同一 op_id 可有多個 pn
|
||||
erp_account = Column(String(100), index=True) # AQ 欄
|
||||
customer = Column(String(255), nullable=False, index=True)
|
||||
customer_normalized = Column(String(255), index=True)
|
||||
pn = Column(String(100), nullable=False, index=True)
|
||||
eau = Column(Integer, default=0)
|
||||
stage = Column(String(50))
|
||||
date = Column(String(20))
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
49
backend/app/models/match.py
Normal file
49
backend/app/models/match.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Float, Enum, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.sql import func
|
||||
from app.models import Base
|
||||
from app.config import TABLE_PREFIX
|
||||
import enum
|
||||
|
||||
class TargetType(str, enum.Enum):
|
||||
SAMPLE = "SAMPLE"
|
||||
ORDER = "ORDER"
|
||||
|
||||
class MatchStatus(str, enum.Enum):
|
||||
pending = "pending"
|
||||
accepted = "accepted"
|
||||
rejected = "rejected"
|
||||
auto_matched = "auto_matched"
|
||||
|
||||
class ReviewAction(str, enum.Enum):
|
||||
accept = "accept"
|
||||
reject = "reject"
|
||||
|
||||
class MatchResult(Base):
|
||||
__tablename__ = f"{TABLE_PREFIX}Match_Results"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
dit_id = Column(Integer, ForeignKey(f"{TABLE_PREFIX}DIT_Records.id"), nullable=False)
|
||||
target_type = Column(Enum(TargetType), nullable=False)
|
||||
target_id = Column(Integer, nullable=False)
|
||||
score = Column(Float, nullable=False)
|
||||
match_priority = Column(Integer, default=3) # 1: Oppy ID, 2: Account, 3: Name
|
||||
match_source = Column(String(255)) # e.g., "Matched via Opportunity ID: OP12345"
|
||||
reason = Column(String(255))
|
||||
status = Column(Enum(MatchStatus), default=MatchStatus.pending)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
dit = relationship("DitRecord", backref="matches")
|
||||
|
||||
class ReviewLog(Base):
|
||||
__tablename__ = f"{TABLE_PREFIX}Review_Logs"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
match_id = Column(Integer, ForeignKey(f"{TABLE_PREFIX}Match_Results.id"), nullable=False)
|
||||
user_id = Column(Integer, ForeignKey(f"{TABLE_PREFIX}users.id"), nullable=False)
|
||||
action = Column(Enum(ReviewAction), nullable=False)
|
||||
timestamp = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
match_result = relationship("MatchResult", backref="review_logs")
|
||||
user = relationship("User", backref="review_logs")
|
||||
20
backend/app/models/order.py
Normal file
20
backend/app/models/order.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Float
|
||||
from sqlalchemy.sql import func
|
||||
from app.models import Base
|
||||
from app.config import TABLE_PREFIX
|
||||
|
||||
class OrderRecord(Base):
|
||||
__tablename__ = f"{TABLE_PREFIX}Order_Records"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
order_id = Column(String(50), index=True, nullable=False) # 移除 unique,訂單可能有多個項次
|
||||
order_no = Column(String(50), index=True)
|
||||
cust_id = Column(String(100), index=True)
|
||||
customer = Column(String(255), nullable=False, index=True)
|
||||
customer_normalized = Column(String(255), index=True)
|
||||
pn = Column(String(100), nullable=False, index=True)
|
||||
qty = Column(Integer, default=0)
|
||||
status = Column(String(50), default='Backlog') # 改為 String 以支援中文狀態
|
||||
amount = Column(Float, default=0.0)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
20
backend/app/models/sample.py
Normal file
20
backend/app/models/sample.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from sqlalchemy import Column, Integer, String, DateTime
|
||||
from sqlalchemy.sql import func
|
||||
from app.models import Base
|
||||
from app.config import TABLE_PREFIX
|
||||
|
||||
class SampleRecord(Base):
|
||||
__tablename__ = f"{TABLE_PREFIX}Sample_Records"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
sample_id = Column(String(50), unique=True, index=True, nullable=False)
|
||||
order_no = Column(String(50), index=True)
|
||||
oppy_no = Column(String(100), index=True) # AU 欄
|
||||
cust_id = Column(String(100), index=True) # G 欄
|
||||
customer = Column(String(255), nullable=False, index=True)
|
||||
customer_normalized = Column(String(255), index=True)
|
||||
pn = Column(String(100), nullable=False, index=True)
|
||||
qty = Column(Integer, default=0)
|
||||
date = Column(String(20))
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
23
backend/app/models/user.py
Normal file
23
backend/app/models/user.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Enum
|
||||
from sqlalchemy.sql import func
|
||||
from app.models import Base
|
||||
from app.config import TABLE_PREFIX
|
||||
import enum
|
||||
|
||||
class UserRole(str, enum.Enum):
|
||||
admin = "admin"
|
||||
user = "user"
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = f"{TABLE_PREFIX}users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
email = Column(String(200), unique=True, index=True, nullable=False)
|
||||
ad_username = Column(String(100), nullable=True) # Added to satisfy DB constraint
|
||||
department = Column(String(100), nullable=True) # Added to satisfy DB constraint
|
||||
password_hash = Column("local_password", String(255), nullable=True)
|
||||
display_name = Column(String(100), nullable=True)
|
||||
# language = Column(String(10), default="zh-TW") # Not in DB
|
||||
role = Column(String(20), default="user") # Simplified from Enum
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
Reference in New Issue
Block a user