84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
import sqlite3
|
|
import random
|
|
import sys
|
|
from datetime import datetime
|
|
|
|
DB_PATH = "production.db"
|
|
|
|
|
|
def simulate(date_str: str):
|
|
random.seed(42)
|
|
|
|
conn = sqlite3.connect(DB_PATH)
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("""
|
|
SELECT s.Id, s.StationName, s.PointId, mp.PointName
|
|
FROM Stations s
|
|
JOIN MeasurementPoints mp ON mp.Id = s.PointId
|
|
ORDER BY s.SortOrder
|
|
""")
|
|
stations = cursor.fetchall()
|
|
|
|
if not stations:
|
|
print("[ERROR] 没有配置工序,请先运行 db_init.py")
|
|
return
|
|
|
|
products = [f"PROD-{i:03d}" for i in range(1, 13)]
|
|
|
|
for pid in products:
|
|
print(f"\n>>> 产品 {pid}")
|
|
|
|
for st_id, st_name, point_id, point_name in stations:
|
|
table = f"MPData_P{point_id}_{date_str}"
|
|
|
|
upper = round(random.uniform(90, 110), 1)
|
|
lower = round(random.uniform(70, 90), 1)
|
|
measured = round(random.uniform(lower - 5, upper + 5), 1)
|
|
qualified = 1 if lower <= measured <= upper else 0
|
|
|
|
cursor.execute(
|
|
f"SELECT InspectionCount FROM {table} WHERE ProductID=?", (pid,)
|
|
)
|
|
row = cursor.fetchone()
|
|
|
|
if row is None:
|
|
cursor.execute(
|
|
f"""INSERT INTO {table}
|
|
(ProductID, RecordTime, IsQualified, InspectionCount,
|
|
UpperLimit, LowerLimit, MeasuredValue)
|
|
VALUES (?, datetime('now'), ?, 1, ?, ?, ?)""",
|
|
(pid, qualified, upper, lower, measured),
|
|
)
|
|
status = (
|
|
f"首次检测, {point_name}={measured}"
|
|
if qualified
|
|
else f"首次检测(不合格), {point_name}={measured}"
|
|
)
|
|
print(f" {st_name} → {status}")
|
|
|
|
if not qualified:
|
|
# 不合格就模拟一次返修
|
|
measured2 = round(random.uniform(lower, upper), 1)
|
|
cursor.execute(
|
|
f"""UPDATE {table}
|
|
SET RecordTime = datetime('now'),
|
|
IsQualified = 1,
|
|
InspectionCount = 2,
|
|
UpperLimit = ?,
|
|
LowerLimit = ?,
|
|
MeasuredValue = ?
|
|
WHERE ProductID = ?""",
|
|
(upper, lower, measured2, pid),
|
|
)
|
|
print(f" └─ 返修, {point_name}={measured2} (合格)")
|
|
|
|
conn.commit()
|
|
conn.close()
|
|
print(f"\n[DONE] 数据模拟完成,共 {len(products)} 个产品")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
date_str = sys.argv[1] if len(sys.argv) > 1 else "2026_06_08"
|
|
simulate(date_str)
|