save code

This commit is contained in:
Your Name
2026-01-27 17:30:17 +08:00
parent a416e2091f
commit 884b9f47a2
17 changed files with 910 additions and 44 deletions
+168
View File
@@ -0,0 +1,168 @@
from app.database.database import engine, Base
from app.models import User, Project
from app.database.database import SessionLocal
import bcrypt
from datetime import datetime
def init_db():
"""初始化数据库"""
print("Creating database tables...")
Base.metadata.create_all(bind=engine)
print("Database tables created successfully!")
def create_initial_users():
"""创建初始用户"""
db = SessionLocal()
try:
# 检查是否已有用户
existing_users = db.query(User).count()
if existing_users > 0:
print(f"Database already has {existing_users} users. Skipping initialization.")
return
# 创建初始用户
password = "123456"
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
users = [
User(
username="admin",
password=hashed_password,
department="IT",
role="admin"
),
User(
username="marketing",
password=hashed_password,
department="Marketing",
role="marketing"
),
User(
username="other",
password=hashed_password,
department="Other",
role="other"
)
]
for user in users:
db.add(user)
db.commit()
print("Initial users created successfully!")
print("Username: admin, Password: 123456")
print("Username: marketing, Password: 123456")
print("Username: other, Password: 123456")
except Exception as e:
db.rollback()
print(f"Error creating initial users: {e}")
finally:
db.close()
def create_sample_projects():
"""创建示例项目数据"""
db = SessionLocal()
try:
# 检查是否已有项目
existing_projects = db.query(Project).count()
if existing_projects > 0:
print(f"Database already has {existing_projects} projects. Skipping initialization.")
return
# 获取marketing用户ID
marketing_user = db.query(User).filter(User.username == "marketing").first()
if not marketing_user:
print("Marketing user not found. Skipping project creation.")
return
# 创建示例项目
projects = [
Project(
project_code="chdj2023178",
name="台江县井洞塘抽水变设备调试",
project_type="分部用户工程",
description="台江县井洞塘抽水变设备调试项目",
department="台江项目部",
contract_amount=0.60,
project_manager="杨如云559714",
client_company="张小平",
client_contact="张小平15908557788",
contract_date=datetime(2023, 6, 8),
start_date=datetime(2023, 6, 8),
planned_end_date=datetime(2023, 6, 13),
actual_end_date=datetime(2023, 6, 13),
progress=1.00,
actual_received_amount=0.60,
payment_completion_rate=1.00,
payment_terms="合同签生效后,5个工作日内,甲方向乙方支付全部工程款,工程竣工后,乙方应出具试验报告及试验资质给甲方,乙方拨付每笔工程款时应向甲方提供等额的增值税发票。",
status="completed",
created_by=marketing_user.id
),
Project(
project_code="chdj2023179",
name="凯里市开怀街道配电工程",
project_type="配电工程",
description="凯里市开怀街道配电工程建设",
department="凯里项目部",
contract_amount=120.50,
project_manager="王小明559715",
client_company="凯里市供电局",
client_contact="李经理13985556677",
contract_date=datetime(2023, 5, 15),
start_date=datetime(2023, 5, 20),
planned_end_date=datetime(2023, 8, 30),
actual_end_date=datetime(2023, 9, 5),
progress=1.00,
actual_received_amount=120.50,
payment_completion_rate=1.00,
payment_terms="合同签订后支付30%预付款,工程进度达到50%时支付30%进度款,工程竣工验收后支付35%工程款,预留5%质保金一年后支付。",
status="completed",
created_by=marketing_user.id
),
Project(
project_code="chdj2023180",
name="雷山县西江镇线路改造工程",
project_type="线路工程",
description="雷山县西江镇10kV线路改造工程",
department="雷山项目部",
contract_amount=85.20,
project_manager="陈小红559716",
client_company="雷山县供电局",
client_contact="张主任13885554433",
contract_date=datetime(2023, 7, 10),
start_date=datetime(2023, 7, 15),
planned_end_date=datetime(2023, 10, 20),
actual_end_date=None,
progress=0.75,
actual_received_amount=63.90,
payment_completion_rate=0.75,
payment_terms="按工程进度付款,每月25日上报进度,次月10日前支付进度款的80%,竣工验收后支付至95%,质保金5%一年后支付。",
status="in_progress",
created_by=marketing_user.id
)
]
for project in projects:
db.add(project)
db.commit()
print("Sample projects created successfully!")
print(f"Created {len(projects)} sample projects.")
except Exception as e:
db.rollback()
print(f"Error creating sample projects: {e}")
finally:
db.close()
if __name__ == "__main__":
init_db()
create_initial_users()
create_sample_projects()