fix: 撤回 is_fix 日志时兜底清理误带入的继承缺陷

- sync_inherited_defect 早返回路径删除继承记录后补算 mlogb 数量
- mlog_revert 末尾针对 is_fix 已残留继承缺陷的产出 mlogb 触发 sync 并刷新 mlog 汇总

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
caoqianming 2026-04-30 11:44:29 +08:00
parent d2581d8500
commit 1f7e17b6c1
2 changed files with 17 additions and 0 deletions

View File

@ -553,13 +553,18 @@ class Mlogb(BaseModel):
def sync_inherited_defect(self, cal_count=True):
inherited_qs = MlogbDefect.objects.filter(mlogb=self, is_inherited=True)
had_inherited = inherited_qs.exists()
if MlogbDefect.objects.filter(mlogb=self, is_inherited=False).exists() or self.has_legacy_defect_count():
inherited_qs.delete()
if had_inherited and cal_count:
self.cal_count_notok(cal_mlog=False)
return
defect = self.get_default_inherited_defect()
if defect is None:
inherited_qs.delete()
if had_inherited and cal_count:
self.cal_count_notok(cal_mlog=False)
return
count = self.count_real

View File

@ -610,6 +610,18 @@ def mlog_revert(mlog: Mlog, user: User, now: Union[datetime.datetime, None]):
mlog.submit_user = None
mlog.save()
# 兜底清理历史 is_fix 错误带入的继承缺陷
if is_fix:
legacy_mlogbouts = list(Mlogb.objects.filter(
mlog=mlog,
material_out__isnull=False,
mlogbdefect__is_inherited=True,
).distinct())
if legacy_mlogbouts:
for mlogbout in legacy_mlogbouts:
mlogbout.sync_inherited_defect(cal_count=True)
mlog.cal_mlog_count_from_mlogb()
# mtask变更状态
update_mtaskIds = []
if mlog.mtask: