Files
zpark-ems/scripts/seed_maintenance.py

68 lines
4.1 KiB
Python
Raw Normal View History

"""Seed maintenance data: asset categories and sample spare parts."""
import asyncio
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "core", "backend"))
from app.core.database import async_session_factory
from app.models.maintenance import AssetCategory, SparePart
ASSET_CATEGORIES = [
{"name": "光伏组件", "description": "太阳能电池板及组件"},
{"name": "逆变器", "description": "光伏逆变器设备"},
{"name": "汇流箱", "description": "直流汇流箱"},
{"name": "变压器", "description": "升压变压器"},
{"name": "电缆线路", "description": "直流/交流电缆"},
{"name": "监控设备", "description": "采集器、通信模块等"},
{"name": "配电设备", "description": "开关柜、断路器等"},
{"name": "支架结构", "description": "光伏支架及基础"},
{"name": "防雷接地", "description": "防雷及接地装置"},
{"name": "其他", "description": "其他辅助设备"},
]
SPARE_PARTS = [
{"name": "MC4连接器(公)", "part_code": "SP-MC4-M", "category": "光伏组件", "specification": "1000VDC/30A", "unit": "", "current_stock": 50, "min_stock": 20, "unit_price": 3.5, "supplier": "正泰"},
{"name": "MC4连接器(母)", "part_code": "SP-MC4-F", "category": "光伏组件", "specification": "1000VDC/30A", "unit": "", "current_stock": 50, "min_stock": 20, "unit_price": 3.5, "supplier": "正泰"},
{"name": "光伏直流熔断器", "part_code": "SP-FUSE-DC", "category": "汇流箱", "specification": "15A/1000VDC", "unit": "", "current_stock": 30, "min_stock": 10, "unit_price": 12.0, "supplier": "正泰"},
{"name": "防雷器SPD", "part_code": "SP-SPD-DC", "category": "防雷接地", "specification": "T2级/40kA", "unit": "", "current_stock": 10, "min_stock": 5, "unit_price": 180.0, "supplier": "德力西"},
{"name": "光伏电缆PV1-F", "part_code": "SP-CABLE-4", "category": "电缆线路", "specification": "4mm²/黑色", "unit": "", "current_stock": 200, "min_stock": 50, "unit_price": 4.8, "supplier": "远东电缆"},
{"name": "4G通信模块", "part_code": "SP-4G-MOD", "category": "监控设备", "specification": "全网通/LORA", "unit": "", "current_stock": 5, "min_stock": 2, "unit_price": 320.0, "supplier": "协合智能"},
{"name": "SIM卡", "part_code": "SP-SIM", "category": "监控设备", "specification": "物联网卡/3年", "unit": "", "current_stock": 10, "min_stock": 3, "unit_price": 80.0, "supplier": "中国移动"},
{"name": "交流断路器", "part_code": "SP-ACB-63", "category": "配电设备", "specification": "63A/3P", "unit": "", "current_stock": 5, "min_stock": 2, "unit_price": 85.0, "supplier": "正泰"},
{"name": "电能表", "part_code": "SP-METER-3P", "category": "监控设备", "specification": "三相/RS485", "unit": "", "current_stock": 3, "min_stock": 1, "unit_price": 450.0, "supplier": "安科瑞"},
{"name": "不锈钢扎带", "part_code": "SP-TIE-SS", "category": "其他", "specification": "4.6×300mm", "unit": "", "current_stock": 20, "min_stock": 5, "unit_price": 15.0, "supplier": "华达"},
]
async def seed():
async with async_session_factory() as session:
# Seed categories
from sqlalchemy import select
existing = (await session.execute(select(AssetCategory))).scalars().all()
if existing:
print(f"Asset categories already exist ({len(existing)} found), skipping...")
else:
for cat in ASSET_CATEGORIES:
session.add(AssetCategory(**cat))
await session.flush()
print(f"Seeded {len(ASSET_CATEGORIES)} asset categories")
# Seed spare parts
existing_parts = (await session.execute(select(SparePart))).scalars().all()
if existing_parts:
print(f"Spare parts already exist ({len(existing_parts)} found), skipping...")
else:
for part in SPARE_PARTS:
session.add(SparePart(**part))
await session.flush()
print(f"Seeded {len(SPARE_PARTS)} spare parts")
await session.commit()
print("Done!")
if __name__ == "__main__":
asyncio.run(seed())