fix(854-1): 단가산출 · 일위대가 · 내 재료 번호 — 지운 끝 번호 다시 안 씀 — 묶음 ③
- SK · SC(`M02_Labor_Ref.next_key`) = 층 뿌리 장부 `_번호.json` {종류: 발급한 가장 큰 번호} · 다음 = 파일 · 장부 가운데 큰 번호 + 1
· 만들기 성공 뒤 장부에 적음(store 잠금 안 · 더 클 때만)
- 내 재료 PM(`M02_Material_Own._last`) = 층 파일 `개인자재.json` 의 `마지막` 칸도 셈 · 새로 넣을 때 적음
- 내 기계 EQP 는 이미 `_다음` 카운터라 그대로 · 있는 번호 · 파일은 안 건드림(층마다 같은 SK 번호 = 층 따로라 정상)
- 시험 test_854_1 ⑤ — 끝 번호 지운 뒤 새로 만들기 = 새 번호(옛 코드 = 지운 번호 다시 씀)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -352,11 +352,34 @@ def _no_loop(ref: Ref, trail: list[Ref]) -> None:
|
||||
_no_loop(child, [*trail, child])
|
||||
|
||||
|
||||
#: 층 뿌리 번호 장부 {종류: 발급한 가장 큰 번호} — 지운 끝 번호를 다시 안 씀(854-1 · M01 키대장 규칙과 같은 뜻)
|
||||
ISSUED = "_번호.json"
|
||||
|
||||
|
||||
def _issued() -> dict[str, Any]:
|
||||
try:
|
||||
got = json.loads((store.root() / ISSUED).read_bytes())
|
||||
except (OSError, ValueError):
|
||||
return {}
|
||||
return got if isinstance(got, dict) else {}
|
||||
|
||||
|
||||
def next_key(kind: str) -> str:
|
||||
"""지금 층 그 종류의 다음 키(가장 큰 번호 + 1)."""
|
||||
"""지금 층 그 종류의 다음 키(파일 · 장부 가운데 가장 큰 번호 + 1)."""
|
||||
folder = store.root() / kind
|
||||
have = [int(m.group(2)) for p in folder.glob("*.json") if (m := _KEY.match(p.stem))]
|
||||
return f"{PREFIX[kind]}-{max(have, default=0) + 1:06d}"
|
||||
return f"{PREFIX[kind]}-{max(*have, int(_issued().get(kind) or 0), 0) + 1:06d}"
|
||||
|
||||
|
||||
def _keep_issued(kind: str, key: str) -> None:
|
||||
"""장부에 발급한 번호(더 클 때만)."""
|
||||
from common_util.common_util_json import atomic_write_json
|
||||
|
||||
n = int(_KEY.match(key).group(2))
|
||||
with store._LOCK:
|
||||
got = _issued()
|
||||
if int(got.get(kind) or 0) < n:
|
||||
atomic_write_json(store.root() / ISSUED, {**got, kind: n})
|
||||
|
||||
|
||||
def create(kind: str, doc: dict[str, Any]) -> dict[str, Any]:
|
||||
@@ -364,10 +387,13 @@ def create(kind: str, doc: dict[str, Any]) -> dict[str, Any]:
|
||||
for _ in range(5):
|
||||
key = next_key(kind)
|
||||
try:
|
||||
return store.write(kind, key, "", {**doc, "키": key})
|
||||
got = store.write(kind, key, "", {**doc, "키": key})
|
||||
except store.StoreError as e:
|
||||
if e.status != 409 or not isinstance(e.detail, dict):
|
||||
raise
|
||||
else:
|
||||
_keep_issued(kind, key)
|
||||
return got
|
||||
raise _bad("새 키를 못 잡음 — 다시 누름", 409)
|
||||
|
||||
|
||||
|
||||
@@ -49,14 +49,14 @@ def rows(folder: str | Path) -> list[dict[str, Any]]:
|
||||
|
||||
|
||||
def _last() -> int:
|
||||
"""모든 개인 층 파일의 가장 큰 PM 번호 — 사람 사이 번호 안 겹침."""
|
||||
found = [
|
||||
int(m.group(1))
|
||||
for path in layers.storage_root().glob(f"*/*/{layers.TEMPLATES_DIRNAME}/{FILE}")
|
||||
for row in read(path.parent)["줄"]
|
||||
if (m := KEY.match(str(row.get("키") or "")))
|
||||
]
|
||||
return max(found, default=0)
|
||||
"""모든 개인 층 파일의 가장 큰 PM 번호 — 사람 사이 번호 안 겹침 · 파일마다 `마지막`(발급한 가장 큰 번호)도
|
||||
셈(지운 끝 번호를 다시 안 씀 · 854-1)."""
|
||||
found = [0]
|
||||
for path in layers.storage_root().glob(f"*/*/{layers.TEMPLATES_DIRNAME}/{FILE}"):
|
||||
doc = read(path.parent)
|
||||
found.append(int(doc.get("마지막") or 0))
|
||||
found += [int(m.group(1)) for r in doc["줄"] if (m := KEY.match(str(r.get("키") or "")))]
|
||||
return max(found)
|
||||
|
||||
|
||||
def _text(v: Any) -> str:
|
||||
@@ -114,8 +114,10 @@ def add(folder: str | Path, body: dict[str, Any]) -> dict[str, Any]:
|
||||
with store._LOCK:
|
||||
doc = read(folder)
|
||||
row = _checked(body, doc["줄"])
|
||||
got = {"키": f"{PREFIX}{_last() + 1:06d}", **row}
|
||||
n = _last() + 1
|
||||
got = {"키": f"{PREFIX}{n:06d}", **row}
|
||||
doc["줄"].append(got)
|
||||
doc["마지막"] = n
|
||||
atomic_write_json(Path(folder) / FILE, doc)
|
||||
return got
|
||||
|
||||
|
||||
@@ -96,3 +96,24 @@ def test_brief_cache_follows_index(monkeypatch):
|
||||
assert all(v[0] is other for v in mh._BRIEF.values())
|
||||
monkeypatch.setattr(m01, "loaded", lambda: base)
|
||||
assert mh.brief([key])[0] == first
|
||||
|
||||
|
||||
def test_deleted_last_key_not_reused(world):
|
||||
"""⑤ 단가산출(SK) · 내 재료(PM) — 끝 번호를 지운 뒤 새로 만들면 새 번호(지운 번호 안 다시 씀)."""
|
||||
from M02_MasterTemplete import M02_Labor_Ref as refs
|
||||
from M02_MasterTemplete import M02_Material_Own as own
|
||||
|
||||
me = world["personal"]
|
||||
body = {"단위": "시간", "더한줄": []}
|
||||
made = [
|
||||
_at(me, refs.create, "sangeun", {**body, "이름": f"sub4임시{i}"})["이름"] for i in range(2)
|
||||
]
|
||||
assert made == ["SK-000001", "SK-000002"]
|
||||
_at(me, store.delete, "sangeun", made[-1])
|
||||
assert _at(me, refs.create, "sangeun", {**body, "이름": "sub4임시3"})["이름"] == "SK-000003"
|
||||
|
||||
bolt = {"이름": "sub4임시볼트", "규격": "M16", "단위": "EA", "값": 1200}
|
||||
keys = [own.add(me, {**bolt, "규격": f"M{i}"})["키"] for i in (16, 20)]
|
||||
assert keys == ["PM000001", "PM000002"]
|
||||
own.remove(me, keys[-1])
|
||||
assert own.add(me, {**bolt, "규격": "M24"})["키"] == "PM000003"
|
||||
|
||||
Reference in New Issue
Block a user