Files
pricing_review/CostReview/app/tools/gen_project_mock.py
2026-10-11 10:52:01 +08:00

84 lines
4.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""模拟生成项目批次(列表测试用,2026-10-07)。
与 gen_price_mock.py 同族:Name 以「模拟」开头保证幂等清理,is_mock=1 供
后台「清空模拟数据」整批删除。**只生成 batches 行**(清单/定额/工料机明细不生成
——用途是列表 UI 的排序/分页/滚动压测,点开模拟批次的明细是空表属预期)。
运行:python tools/gen_project_mock.py 3000
"""
import os
import random
import sqlite3
import sys
import time
DB_CANDS = [
os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "data", "history.db")),
os.path.normpath(os.path.join(os.getcwd(), "data", "history.db")),
]
DB = next((c for c in DB_CANDS if os.path.exists(c)), DB_CANDS[0])
STEMS = [
"市政道路改造工程", "供水管网升级工程", "老旧小区环境整治提升工程", "产业园基础设施工程",
"学校改扩建工程", "医院配套设施工程", "公园绿地建设工程", "桥涵结构工程", "隧道结构工程",
"海绵城市建设工程", "污水处理厂扩建工程", "保障房建设项目", "智慧城市基础设施工程",
"河道整治与生态修复工程", "地下综合管廊工程", "LED 路灯节能改造工程",
]
UNITS = ["珠海市供水有限公司", "珠海城建市政建设有限公司", "珠海市政府投资工程项目管理中心",
"北京师范大学珠海校区", "珠海市香洲区三溪科创小镇发展中心", "广东省建筑工程集团"]
REGIONS = [("广东省", "珠海市", "香洲区"), ("广东省", "珠海市", "横琴"),
("广东省", "珠海市", "万山镇"), ("福建省", "福州市", "漳州区"),
("广西", "桂林市", "桂林区")]
KINDS = [1, 2, 3, 4] # 单价类型:预算综合/投标综合/预算全费用/投标全费用
def main(n=3000):
n = int(n)
con = sqlite3.connect(DB)
con.execute("PRAGMA foreign_keys = OFF")
cur = con.cursor()
# 幂等:清掉上一轮项目模拟(Name 以「模拟项目」开头;is_mock=1)
old = [r[0] for r in cur.execute(
"SELECT id FROM batches WHERE kind='project' AND Name LIKE '模拟项目%'").fetchall()]
if old:
ph = ",".join("?" * len(old))
cur.execute("DELETE FROM bills WHERE batch_id IN (%s)" % ph, old)
cur.execute("DELETE FROM batches WHERE id IN (%s)" % ph, old)
print("已清理上一轮项目模拟:%d 份。" % len(old))
# 基于现有真实项目名取词根("基于现有数据")
stems = [r[0] for r in con.execute(
"SELECT DISTINCT Name FROM batches WHERE kind='project' AND Name NOT LIKE '模拟%' LIMIT 30")]
stems = [s for s in stems if s] or STEMS
random.seed(20261007)
now = time.strftime("%Y-%m-%d %H:%M:%S")
t0 = time.time()
for i in range(n):
base = random.choice(stems).strip()
name = "模拟项目%04d-%s" % (i + 1, base[:18])
y, mo = random.choice([2025, 2026]), random.randint(1, 12)
m = y * 12 + mo
prov, city, dist = random.choice(REGIONS)
cur.execute(
"""INSERT INTO batches(kind, Name, FileKind, BulidUnit, x_province, x_city, x_district,
x_period_start_m, x_period_end_m, x_enabled, x_total, x_rows_bill, x_rows_lme,
x_imported_at, x_src_hash, x_parser_ver, is_mock)
VALUES('project',?,?,?,?,?,?,?,?,1,?,?,?,?,?,?,1)""",
(name[:200], random.choice(KINDS), random.choice(UNITS),
prov, city, dist, m, m,
round(random.uniform(3_000_000, 420_000_000), 2),
random.randint(200, 5200), random.randint(300, 6800),
now, "mock://project/%04d" % (i + 1), "2026.10.02-1"))
con.commit()
print("完成:%d 份项目模拟批次,%.1fs。" % (n, time.time() - t0))
print("校验:", cur.execute(
"SELECT COUNT(*) FROM batches WHERE kind='project' AND Name LIKE '模拟项目%'").fetchone()[0])
con.close()
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv) > 1 else 3000)