feat: 批次统计分析重构

This commit is contained in:
caoqianming 2025-07-16 14:53:40 +08:00
parent bdf69a6635
commit e8eeb14766
5 changed files with 595 additions and 489 deletions

View File

@ -0,0 +1,100 @@
from apps.wpm.models import BatchSt
import logging
from apps.wpm.models import Mlogb, MlogbDefect
from apps.mtm.models import Mgroup
import decimal
from django.db.models import Sum
from apps.wpm.services_2 import get_f_l_date
myLogger = logging.getLogger("log")
def main(batch: str, mgroup_obj:Mgroup=None):
try:
batchst = BatchSt.objects.get(batch=batch, version=1)
except BatchSt.DoesNotExist:
myLogger.error(f"Batch {batch} does not exist")
data = {"批次号": batch}
if mgroup_obj is None:
mgroup_qs = Mgroup.objects.all().order_by("sort")
else:
mgroup_qs = Mgroup.objects.filter(id=mgroup_obj.id)
for mgroup in mgroup_qs:
mgroup_name = mgroup.name
mlogb1_qs = Mlogb.objects.filter(mlog__submit_time__isnull=False,
material_out__isnull=False, mlog__mgroup=mgroup,
mlog__is_fix=False, batch=batch, need_inout=True)
if mlogb1_qs.exists():
data[f"{mgroup_name}_日期"] = []
data[f"{mgroup_name}_操作人"] = []
data[f"{mgroup_name}_count_use"] = 0
data[f"{mgroup_name}_count_real"] = 0
data[f"{mgroup_name}_count_ok"] = 0
data[f"{mgroup_name}_count_notok"] = 0
data[f"{mgroup_name}_count_ok_full"] = 0
data[f"{mgroup_name}_count_pn_jgqbl"] = 0
mlogb_q_ids = []
for item in mlogb1_qs:
# 找到对应的输入
mlogb_from:Mlogb = item.mlogb_from
if mlogb_from:
mlogb_q_ids.append(mlogb_from.id)
data[f"{mgroup_name}_count_use"] += mlogb_from.count_use
data[f"{mgroup_name}_count_pn_jgqbl"] += mlogb_from.count_pn_jgqbl
if item.mlog.handle_user:
data[f"{mgroup_name}_操作人"].append(item.mlog.handle_user)
if item.mlog.handle_date:
data[f"{mgroup_name}_日期"].append(item.mlog.handle_date)
data[f"{mgroup_name}_count_real"] += item.count_real
data[f"{mgroup_name}_count_ok"] += item.count_ok
data[f"{mgroup_name}_count_ok_full"] += item.count_ok_full if item.count_ok_full else 0
data[f"{mgroup_name}_count_notok"] += item.count_notok if item.count_notok else 0
try:
data[f"{mgroup_name}_完全合格率"] = round((data[f"{mgroup_name}_count_ok_full"] / data[f"{mgroup_name}_count_real"])*100, 2)
except decimal.InvalidOperation:
data[f"{mgroup_name}_完全合格率"] = 0
try:
data[f"{mgroup_name}_合格率"] = round((data[f"{mgroup_name}_count_ok"] / data[f"{mgroup_name}_count_real"])*100, 2)
except decimal.InvalidOperation:
data[f"{mgroup_name}_合格率"] = 0
mlogbd1_qs = MlogbDefect.objects.filter(mlogb__in=mlogb1_qs, count__gt=0).values("defect__name").annotate(total=Sum("count"))
mlogbd1_q_qs = MlogbDefect.objects.filter(mlogb__id__in=mlogb_q_ids, count__gt=0).values("defect__name").annotate(total=Sum("count"))
for item in mlogbd1_q_qs:
data[f"{mgroup_name}_加工前_缺陷_{item['defect__name']}"] = item["total"]
data[f"{mgroup_name}_加工前_缺陷_{item['defect__name']}_比例"] = round((item["total"] / data[f"{mgroup_name}_count_use"])*100, 2)
for item in mlogbd1_qs:
data[f"{mgroup_name}_缺陷_{item['defect__name']}"] = item["total"]
data[f"{mgroup_name}_缺陷_{item['defect__name']}_比例"] = round((item["total"] / data[f"{mgroup_name}_count_real"])*100, 2)
data[f"{mgroup_name}_日期"] = list(set(data[f"{mgroup_name}_日期"]))
data[f"{mgroup_name}_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data[f"{mgroup_name}_日期"]])
data[f"{mgroup_name}_操作人"] = list(set(data[f"{mgroup_name}_操作人"]))
data[f"{mgroup_name}_操作人"] = ";".join([item.name for item in data[f"{mgroup_name}_操作人"]])
res = get_f_l_date(data)
old_data = batchst.data
if old_data:
if mgroup_obj is not None:
for item in old_data.keys():
if f'{mgroup_obj.name}_' in item:
del old_data[item]
else:
old_data = {}
old_data.update(data)
batchst.data = old_data
if batchst.first_time is None or (res["first_time"] and res["first_time"] < batchst.first_time):
batchst.first_time = res["first_time"]
if batchst.last_time is None or (res["last_time"] and res["last_time"] > batchst.last_time):
batchst.last_time = res["last_time"]
batchst.save()
if __name__ == '__main__':
pass

View File

@ -9,12 +9,11 @@ from datetime import datetime
from apps.wpm.services_2 import get_f_l_date from apps.wpm.services_2 import get_f_l_date
myLogger = logging.getLogger("log") myLogger = logging.getLogger("log")
def main(batch: str): def main(batch: str, mgroup_obj):
try: try:
batchst = BatchSt.objects.get(batch=batch, version=1) batchst = BatchSt.objects.get(batch=batch, version=1)
except BatchSt.DoesNotExist: except BatchSt.DoesNotExist:
myLogger.error(f"Batch {batch} does not exist") myLogger.error(f"Batch {batch} does not exist")
return None, None
data = {"批次号": batch} data = {"批次号": batch}
@ -135,7 +134,12 @@ def main(batch: str):
res = get_f_l_date(data) res = get_f_l_date(data)
return data, res batchst.data = data
if batchst.first_time is None or (res["first_time"] and res["first_time"] < batchst.first_time):
batchst.first_time = res["first_time"]
if batchst.last_time is None or (res["last_time"] and res["last_time"] > batchst.last_time):
batchst.last_time = res["last_time"]
batchst.save()
if __name__ == '__main__': if __name__ == '__main__':
pass pass

View File

@ -0,0 +1,472 @@
from apps.wpm.models import BatchSt
import logging
from apps.wpm.models import Mlog, Handover
from apps.mtm.models import Mgroup
import decimal
from apps.system.models import Dept
from apps.inm.models import MIOItem
from apps.qm.models import FtestWork
from apps.wpm.services_2 import get_f_l_date
myLogger = logging.getLogger("log")
def main(batch: str, mgroup_obj):
"""
获取某个批次的整体生产数据
"""
batchst, _ = BatchSt.objects.get_or_create(batch=batch, version=1)
data = {"产品批次编号": batch}
dept7 = Dept.objects.get(name='7车间')
dept6 = Dept.objects.get(name='6车间')
dept10 = Dept.objects.get(name='10车间')
mgroup_blcx = Mgroup.objects.get(name="棒料成型")
mlogs_blcx_qs = Mlog.objects.filter(submit_time__isnull=False, mgroup=mgroup_blcx, batch=batch)
mlog_count_fields = Mlog.count_fields()
material_start = None
if mlogs_blcx_qs.exists():
data["产品规格"] = []
data["棒料成型_日期"] = []
data["棒料成型_出料人"] = []
data["棒料成型_切料人"] = []
data["棒料成型_备注"] = ""
for item in mlogs_blcx_qs:
if material_start is None:
material_start = item.material_out
data["产品规格"].append(item.material_out) # 对象
if item.handle_user:
data["棒料成型_出料人"].append(item.handle_user) # 对象
if item.handle_user_2:
data["棒料成型_切料人"].append(item.handle_user_2) # 对象
if item.note:
data["棒料成型_备注"] = ";".join([data["棒料成型_备注"], item.note])
data["棒料成型_日期"].append(item.handle_date)
for field in mlog_count_fields:
if getattr(item, field) > 0 or field in ["count_real", "count_ok"]:
if f'棒料成型_{field}' not in data:
data[f'棒料成型_{field}'] = getattr(item, field)
else:
data[f'棒料成型_{field}'] += getattr(item, field)
# 整理棒料成型数据
data["产品规格"] = list(set(data["产品规格"]))
data["产品规格"] = ";".join([item.specification if item.specification else "" for item in data["产品规格"]])
data["棒料成型_出料人"] = list(set(data["棒料成型_出料人"]))
data["棒料成型_出料人"] = ";".join([item.name for item in data["棒料成型_出料人"]])
data["棒料成型_切料人"] = list(set(data["棒料成型_切料人"]))
data["棒料成型_切料人"] = ";".join([item.name for item in data["棒料成型_切料人"]])
data["棒料成型_日期"] = list(set(data["棒料成型_日期"]))
data["棒料成型_日期"].sort()
data["棒料成型_小日期"] = max(data["棒料成型_日期"]).strftime("%Y-%m-%d")
data["棒料成型_大日期"] = min(data["棒料成型_日期"]).strftime("%Y-%m-%d")
data["棒料成型_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["棒料成型_日期"]])
try:
data["棒料成型_合格率"] = round((data["棒料成型_count_ok"] * 100/ data["棒料成型_count_real"]), 1)
except ZeroDivisionError:
data["棒料成型_合格率"] = 0
except decimal.InvalidOperation:
# myLogger.error(f"棒料成型_合格率计算错误decimal.InvalidOperation-{data}")
data["棒料成型_合格率"] = 0
# 管料成型数据
mgroup_glcx = Mgroup.objects.get(name="管料成型")
mlogs_glcx_qs = Mlog.objects.filter(submit_time__isnull=False, mgroup=mgroup_glcx, batch=batch)
if mlogs_glcx_qs.exists():
data["产品规格"] = []
data["管料成型_备注"] = ""
data["管料成型_日期"] = []
for item in mlogs_glcx_qs:
if material_start is None:
material_start = item.material_out
data["产品规格"].append(item.material_out) # 对象
if item.note:
data["管料成型_备注"] = ";".join([data["管料成型_备注"], item.note])
data["管料成型_日期"].append(item.handle_date)
for field in mlog_count_fields:
if getattr(item, field) > 0 or field in ["count_real", "count_ok"]:
if f'管料成型_{field}' not in data:
data[f'管料成型_{field}'] = getattr(item, field)
else:
data[f'管料成型_{field}'] += getattr(item, field)
data["产品规格"] = list(set(data["产品规格"]))
data["产品规格"] = ";".join([item.specification for item in data["产品规格"]])
data["管料成型_合格率"] = round((data["管料成型_count_ok"] * 100 / data["管料成型_count_real"]), 1)
data["管料成型_日期"] = list(set(data["管料成型_日期"]))
data["管料成型_日期"].sort()
data["管料成型_小日期"] = max(data["管料成型_日期"]).strftime("%Y-%m-%d")
data["管料成型_大日期"] = min(data["管料成型_日期"]).strftime("%Y-%m-%d")
data["管料成型_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["管料成型_日期"]])
# 7车间生产入库数据/ 8车间中检数据
mioitem_count_fields = MIOItem.count_fields()
mioitem_qs = MIOItem.objects.filter(mio__belong_dept=dept7, mio__type="do_in", batch=batch, mio__submit_time__isnull=False)
if mioitem_qs.exists():
data["七车间入库_日期"] = []
data["七车间入库_车间执行人"] = []
data["七车间入库_仓库执行人"] = []
data["七车间入库_检验备注"] = ""
for item in mioitem_qs:
if material_start is None:
material_start = item.material
data["七车间入库_日期"].append(item.mio.inout_date)
if item.test_note:
data["七车间入库_检验备注"] = ";".join([data["七车间入库_检验备注"], item.test_note])
if item.mio.do_user:
data["七车间入库_车间执行人"].append(item.mio.do_user)
if item.mio.mio_user:
data["七车间入库_仓库执行人"].append(item.mio.mio_user)
for field in mioitem_count_fields:
f_v = getattr(item, field)
if f_v is not None and (f_v > 0 or field in ["count", "count_notok"]):
if f'七车间入库_{field}' not in data:
data[f'七车间入库_{field}'] = f_v
else:
data[f'七车间入库_{field}'] += f_v
data["七车间入库_合格率"] = round((data["七车间入库_count"] - data["七车间入库_count_notok"]) * 100/ data["七车间入库_count"], 1)
data["七车间入库_日期"] = list(set(data["七车间入库_日期"]))
data["七车间入库_日期"].sort()
data["七车间入库_小日期"] = max(data["七车间入库_日期"]).strftime("%Y-%m-%d")
data["七车间入库_大日期"] = min(data["七车间入库_日期"]).strftime("%Y-%m-%d")
data["七车间入库_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["七车间入库_日期"]])
data["七车间入库_车间执行人"] = list(set(data["七车间入库_车间执行人"]))
data["七车间入库_车间执行人"] = ";".join([item.name for item in data["七车间入库_车间执行人"]])
data["七车间入库_仓库执行人"] = list(set(data["七车间入库_仓库执行人"]))
data["七车间入库_仓库执行人"] = ";".join([item.name for item in data["七车间入库_仓库执行人"]])
# 十车间入库检验
mioitem10_qs = MIOItem.objects.filter(mio__belong_dept=dept10, mio__type="do_in", batch=batch, mio__submit_time__isnull=False)
if mioitem10_qs.exists():
data["十车间入库_抽检人"] = []
data["十车间入库_仓库执行人"] = []
data["十车间入库_日期"] = []
data["十车间入库_检验备注"] = ""
for item in mioitem10_qs:
if material_start is None:
material_start = item.material
if item.test_note:
data["十车间入库_检验备注"] = ";".join([data["十车间入库_检验备注"], item.test_note])
if item.mio.mio_user:
data["十车间入库_仓库执行人"].append(item.mio.mio_user)
if item.test_user:
data["十车间入库_抽检人"].append(item.test_user)
data["十车间入库_日期"].append(item.mio.inout_date)
for field in mioitem_count_fields:
if getattr(item, field) is not None and (getattr(item, field) > 0 or field in ["count", "count_notok", "count_sampling"]):
if f'十车间入库_{field}' not in data:
data[f'十车间入库_{field}'] = getattr(item, field)
else:
data[f'十车间入库_{field}'] += getattr(item, field)
data["十车间入库_抽检人"] = list(set(data["十车间入库_抽检人"]))
data["十车间入库_抽检人"] = ";".join([item.name for item in data["十车间入库_抽检人"]])
if data["十车间入库_count_sampling"] > 0:
data["十车间入库_抽检合格数"] = data["十车间入库_count_sampling"] - data["十车间入库_count_notok"]
data["十车间入库_抽检合格率"] = round(data["十车间入库_抽检合格数"] * 100/ data["十车间入库_count_sampling"], 1)
data["十车间入库_仓库执行人"] = list(set(data["十车间入库_仓库执行人"]))
data["十车间入库_仓库执行人"] = ";".join([item.name for item in data["十车间入库_仓库执行人"]])
data["十车间入库_日期"] = list(set(data["十车间入库_日期"]))
data["十车间入库_日期"].sort()
data["十车间入库_小日期"] = max(data["十车间入库_日期"]).strftime("%Y-%m-%d")
data["十车间入库_大日期"] = min(data["十车间入库_日期"]).strftime("%Y-%m-%d")
data["十车间入库_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["十车间入库_日期"]])
data["十车间入库_合格数"] = data["十车间入库_count"] - data["十车间入库_count_notok"]
data["十车间入库_合格率"] = round((data["十车间入库_count"] - data["十车间入库_count_notok"]) * 100/ data["十车间入库_count"], 1)
# 其他入库数据
mioitem_qt_qs = MIOItem.objects.filter(mio__type="other_in", batch=batch, mio__submit_time__isnull=False)
if mioitem_qt_qs.exists():
data["其他入库_仓库执行人"] = []
data["其他入库_日期"] = []
for item in mioitem_qt_qs:
if material_start is None:
material_start = item.material
data["其他入库_日期"].append(item.mio.inout_date)
if item.mio.mio_user:
data["其他入库_仓库执行人"].append(item.mio.mio_user)
for field in mioitem_count_fields:
if getattr(item, field) is not None and (getattr(item, field) > 0 or field in ["count", "count_notok", "count_sampling"]):
if f'其他入库_{field}' not in data:
data[f'其他入库_{field}'] = getattr(item, field)
else:
data[f'其他入库_{field}'] += getattr(item, field)
data["其他入库_仓库执行人"] = list(set(data["其他入库_仓库执行人"]))
data["其他入库_仓库执行人"] = ";".join([item.name for item in data["其他入库_仓库执行人"]])
data["其他入库_日期"] = list(set(data["其他入库_日期"]))
data["其他入库_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["其他入库_日期"]])
# 管料退火生产数据
mgroup_gltx = Mgroup.objects.get(name="管料退火")
mlogs_glth_qs = Mlog.objects.filter(submit_time__isnull=False, mgroup=mgroup_gltx, batch=batch)
if mlogs_glth_qs.exists():
data["管料退火_日期"] = []
data["管料退火_操作人"] = []
data["管料退火_备注"] = ""
data["产品规格"] = []
for item in mlogs_glth_qs:
data["产品规格"].append(item.material_out)
if material_start is None:
material_start = item.material_out
if item.note:
data["管料退火_备注"] = ";".join([data["管料退火_备注"], item.note])
if item.handle_date:
data["管料退火_日期"].append(item.handle_date)
if item.handle_user:
data["管料退火_操作人"].append(item.handle_user)
for field in mlog_count_fields:
if getattr(item, field) is not None and (getattr(item, field) > 0 or field in ["count", "count_notok"]):
if f'管料退火_{field}' not in data:
data[f'管料退火_{field}'] = getattr(item, field)
else:
data[f'管料退火_{field}'] += getattr(item, field)
data["管料退火_日期"] = list(set(data["管料退火_日期"]))
data["管料退火_日期"].sort()
data["管料退火_小日期"] = max(data["管料退火_日期"]).strftime("%Y-%m-%d")
data["管料退火_大日期"] = min(data["管料退火_日期"]).strftime("%Y-%m-%d")
data["管料退火_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["管料退火_日期"]])
data["管料退火_操作人"] = list(set(data["管料退火_操作人"]))
data["管料退火_操作人"] = ";".join([item.name for item in data["管料退火_操作人"]])
data["产品规格"] = list(set(data["产品规格"]))
data["产品规格"] = ";".join([item.specification for item in data["产品规格"]])
# 六车间领料数据
mioitem6_qs = MIOItem.objects.filter(mio__belong_dept=dept6, mio__type="do_out",
batch=batch,
mio__submit_time__isnull=False)
if mioitem6_qs.exists():
data["六车间领料_日期"] = []
data["六车间领料_车间执行人"] = []
data["六车间领料_仓库执行人"] = []
data["产品规格"] = []
for item in mioitem6_qs:
data["产品规格"].append(item.material)
if material_start is None:
material_start = item.material
data["六车间领料_日期"].append(item.mio.inout_date)
if item.mio.mio_user:
data["六车间领料_仓库执行人"].append(item.mio.mio_user)
if item.mio.do_user:
data["六车间领料_车间执行人"].append(item.mio.do_user)
for field in mioitem_count_fields:
if getattr(item, field) is not None and getattr(item, field) > 0:
if f'六车间领料_{field}' not in data:
data[f'六车间领料_{field}'] = getattr(item, field)
else:
data[f'六车间领料_{field}'] += getattr(item, field)
data["六车间领料_日期"] = list(set(data["六车间领料_日期"]))
data["六车间领料_日期"].sort()
data["六车间领料_小日期"] = max(data["六车间领料_日期"]).strftime("%Y-%m-%d")
data["六车间领料_大日期"] = min(data["六车间领料_日期"]).strftime("%Y-%m-%d")
data["六车间领料_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间领料_日期"]])
data["六车间领料_仓库执行人"] = list(set(data["六车间领料_仓库执行人"]))
data["六车间领料_仓库执行人"] = ";".join([item.name for item in data["六车间领料_仓库执行人"]])
data["六车间领料_车间执行人"] = list(set(data["六车间领料_车间执行人"]))
data["六车间领料_车间执行人"] = ";".join([item.name for item in data["六车间领料_车间执行人"]])
data["产品规格"] = list(set(data["产品规格"]))
data["产品规格"] = ";".join([item.specification for item in data["产品规格"]])
# 六车间通过交接记录的领料数据
handover6_qs = Handover.objects.filter(recive_dept=dept6, submit_time__isnull=False, batch=batch)
if handover6_qs.exists():
data["六车间交接领料_日期"] = []
data["六车间交接领料_送料人"] = []
data["六车间交接领料_接料人"] = []
data["六车间交接领料_count"] = 0
for item in handover6_qs:
data["六车间交接领料_count"] += item.count
data["六车间交接领料_日期"].append(item.send_date)
if item.send_user:
data["六车间交接领料_送料人"].append(item.send_user)
if item.recive_user:
data["六车间交接领料_接料人"].append(item.recive_user)
data["六车间交接领料_日期"] = list(set(data["六车间交接领料_日期"]))
data["六车间交接领料_日期"].sort()
data["六车间交接领料_小日期"] = max(data["六车间交接领料_日期"]).strftime("%Y-%m-%d")
data["六车间交接领料_大日期"] = min(data["六车间交接领料_日期"]).strftime("%Y-%m-%d")
data["六车间交接领料_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间交接领料_日期"]])
data["六车间交接领料_送料人"] = list(set(data["六车间交接领料_送料人"]))
data["六车间交接领料_送料人"] = ";".join([item.name for item in data["六车间交接领料_送料人"]])
data["六车间交接领料_接料人"] = list(set(data["六车间交接领料_接料人"]))
data["六车间交接领料_接料人"] = ";".join([item.name for item in data["六车间交接领料_接料人"]])
# 六车间工段生产数据
mgroup_list = ["平头", "粘铁头", "粗中细磨", "平磨", "掏管", "抛光", "开槽", "倒角"]
for mgroup_name in mgroup_list:
if mgroup_name == '粗中细磨':
mgroups = Mgroup.objects.filter(name__in=['粗磨', '粗中磨', '粗中细磨'])
else:
mgroups = Mgroup.objects.filter(name=mgroup_name)
mlogs_qs = Mlog.objects.filter(submit_time__isnull=False, mgroup__in=mgroups, batch=batch)
if mlogs_qs.exists():
data[f'六车间_{mgroup_name}_日期'] = []
data[f'六车间_{mgroup_name}_操作人'] = []
data[f'六车间_{mgroup_name}_备注'] = ""
for item in mlogs_qs:
if material_start is None:
material_start = item.material_out
if item.note:
data[f'六车间_{mgroup_name}_备注'] = ";".join([data[f'六车间_{mgroup_name}_备注'], item.note])
if item.handle_date:
data[f'六车间_{mgroup_name}_日期'].append(item.handle_date)
if item.handle_user:
data[f'六车间_{mgroup_name}_操作人'].append(item.handle_user)
for field in mlog_count_fields:
if getattr(item, field) > 0 or field in ["count_ok", "count_real"]:
if f'六车间_{mgroup_name}_{field}' not in data:
data[f'六车间_{mgroup_name}_{field}'] = getattr(item, field)
else:
data[f'六车间_{mgroup_name}_{field}'] += getattr(item, field)
data[f'六车间_{mgroup_name}_日期'] = list(set(data[f'六车间_{mgroup_name}_日期']))
data[f'六车间_{mgroup_name}_日期'].sort()
data[f'六车间_{mgroup_name}_小日期'] = max(data[f'六车间_{mgroup_name}_日期']).strftime("%Y-%m-%d")
data[f'六车间_{mgroup_name}_大日期'] = min(data[f'六车间_{mgroup_name}_日期']).strftime("%Y-%m-%d")
data[f'六车间_{mgroup_name}_日期'] = ";".join([item.strftime("%Y-%m-%d") for item in data[f'六车间_{mgroup_name}_日期']])
data[f'六车间_{mgroup_name}_操作人'] = list(set(data[f'六车间_{mgroup_name}_操作人']))
data[f'六车间_{mgroup_name}_操作人'] = ";".join([item.name for item in data[f'六车间_{mgroup_name}_操作人']])
try:
data[f'六车间_{mgroup_name}_合格率'] = round(data[f'六车间_{mgroup_name}_count_ok'] * 100/ data[f'六车间_{mgroup_name}_count_real'], 1)
except decimal.InvalidOperation:
# myLogger.error(f"六车间_{mgroup_name}_合格率decimal.InvalidOperation-{data}")
data[f'六车间_{mgroup_name}_合格率'] = 0
ftestwork_count_fields = FtestWork.count_fields()
# 六车间中检数据
ftestwork_qs = FtestWork.objects.filter(batch=batch, type="process")
if ftestwork_qs.exists():
data["六车间中检_日期"] = []
data['六车间中检_检验人'] = []
for item in ftestwork_qs:
if material_start is None:
material_start = item.material
if item.test_date:
data["六车间中检_日期"].append(item.test_date)
if item.test_user:
data['六车间中检_检验人'].append(item.test_user)
for field in ftestwork_count_fields:
if field == 'count_notok_json':
for k, v in getattr(item, field).items():
if f'六车间中检_{k}' not in data:
data[f'六车间中检_{k}'] = v
else:
data[f'六车间中检_{k}'] += v
else:
if getattr(item, field) > 0 or field in ["count", "count_ok"]:
if f'六车间中检_{field}' not in data:
data[f'六车间中检_{field}'] = getattr(item, field)
else:
data[f'六车间中检_{field}'] += getattr(item, field)
data["六车间中检_日期"] = list(set(data["六车间中检_日期"]))
data["六车间中检_日期"].sort()
data["六车间中检_小日期"] = max(data["六车间中检_日期"]).strftime("%Y-%m-%d")
data["六车间中检_大日期"] = min(data["六车间中检_日期"]).strftime("%Y-%m-%d")
data["六车间中检_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间中检_日期"]])
data['六车间中检_检验人'] = list(set(data['六车间中检_检验人']))
data['六车间中检_检验人'] = ";".join([item.name for item in data['六车间中检_检验人']])
# 六车间入库/检验数据
mioitem6_qs2 = MIOItem.objects.filter(mio__belong_dept=dept6, mio__type="do_in",
batch=batch,
mio__submit_time__isnull=False)
if mioitem6_qs2.exists():
data["六车间生产入库_日期"] = []
data["六车间生产入库_检验日期"] = []
data["六车间生产入库_检验人"] = []
for item in mioitem6_qs2:
if material_start is None:
material_start = item.material
data["六车间生产入库_日期"].append(item.mio.inout_date)
if item.test_date:
data["六车间生产入库_检验日期"].append(item.test_date)
for field in mioitem_count_fields:
if getattr(item, field) is not None and (getattr(item, field) > 0 or field in ["count", "count_notok", "count_sampling"]):
if f'六车间生产入库_{field}' not in data:
data[f'六车间生产入库_{field}'] = getattr(item, field)
else:
data[f'六车间生产入库_{field}'] += getattr(item, field)
data["六车间生产入库_日期"] = list(set(data["六车间生产入库_日期"]))
data["六车间生产入库_日期"].sort()
data["六车间生产入库_小日期"] = max(data["六车间生产入库_日期"]).strftime("%Y-%m-%d")
data["六车间生产入库_大日期"] = min(data["六车间生产入库_日期"]).strftime("%Y-%m-%d")
data["六车间生产入库_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间生产入库_日期"]])
data["六车间生产入库_检验日期"] = list(set(data["六车间生产入库_检验日期"]))
data["六车间生产入库_检验日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间生产入库_检验日期"]])
data['六车间生产入库_检验人'] = list(set(data['六车间生产入库_检验人']))
data['六车间生产入库_检验人'] = ";".join([item.name for item in data['六车间生产入库_检验人']])
try:
data['六车间生产入库_合格率'] = round((data['六车间生产入库_count'] - data['六车间生产入库_count_notok']) * 100/ data['六车间生产入库_count'], 1)
except decimal.InvalidOperation:
# myLogger.error("六车间生产入库_合格率decimal.InvalidOperation-{data}")
data['六车间生产入库_合格率'] = 0
# 成品检验数据
ftestwork_qs = FtestWork.objects.filter(batch=batch, type="prod")
if ftestwork_qs.exists():
data["成品检验_日期"] = []
data['成品检验_检验人'] = []
for item in ftestwork_qs:
if material_start is None:
material_start = item.material
if item.test_date:
data["成品检验_日期"].append(item.test_date)
if item.test_user:
data['成品检验_检验人'].append(item.test_user)
for field in ftestwork_count_fields:
if field == 'count_notok_json':
for k, v in getattr(item, field).items():
if f'成品检验_{k}' not in data:
data[f'成品检验_{k}'] = v
else:
data[f'成品检验_{k}'] += v
else:
if getattr(item, field) > 0 or field in ["count", "count_ok"]:
if f'成品检验_{field}' not in data:
data[f'成品检验_{field}'] = getattr(item, field)
else:
data[f'成品检验_{field}'] += getattr(item, field)
data["成品检验_日期"] = list(set(data["成品检验_日期"]))
data["成品检验_日期"].sort()
data["成品检验_小日期"] = max(data["成品检验_日期"]).strftime("%Y-%m-%d")
data["成品检验_大日期"] = min(data["成品检验_日期"]).strftime("%Y-%m-%d")
data["成品检验_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["成品检验_日期"]])
data['成品检验_检验人'] = list(set(data['成品检验_检验人']))
data['成品检验_检验人'] = ";".join([item.name for item in data['成品检验_检验人']])
data['成品检验_合格率'] = round(data['成品检验_count_ok'] * 100/ data['成品检验_count'], 1)
if getattr(data, "六车间领料_count", 0) > 0:
data["六车间_批次生产合格率"] = round(data["成品检验_count_ok"] * 100/ data["六车间领料_count"], 1)
if getattr(data, "棒料成型_count_real", 0) > 0:
data["七车间_批次应出合格率"] = round(data["成品检验_count_ok"] * 100/ data["棒料成型_count_real"], 1)
# 销售发货数据
mioitem_qs = MIOItem.objects.filter(batch=batch, mio__type="sale_out", mio__submit_time__isnull=False)
if mioitem_qs.exists():
data["销售发货_日期"] = []
data['销售发货_仓库执行人'] = []
data['销售发货_count'] = 0
for item in mioitem_qs:
if material_start is None:
material_start = item.material
if item.mio.inout_date:
data["销售发货_日期"].append(item.mio.inout_date)
if item.mio.mio_user:
data['销售发货_仓库执行人'].append(item.mio.mio_user)
data['销售发货_count']+= item.count
if getattr(data, "棒料成型_count_real", 0) > 0:
data["七车间_批次发货合格率"] = round(data["销售发货_count"] * 100/ data["棒料成型_count_real"], 1)
if getattr(data, "六车间领料_count", 0) > 0:
data["六车间_批次发货合格率"] = round(data["销售发货_count"] * 100/ data["六车间领料_count"], 1)
data['销售发货_仓库执行人'] = list(set(data['销售发货_仓库执行人']))
data['销售发货_仓库执行人'] = ";".join([item.name for item in data['销售发货_仓库执行人']])
data["销售发货_日期"] = list(set(data["销售发货_日期"]))
data["销售发货_日期"].sort()
data["销售发货_小日期"] = max(data["销售发货_日期"]).strftime("%Y-%m-%d")
data["销售发货_大日期"] = min(data["销售发货_日期"]).strftime("%Y-%m-%d")
data["销售发货_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["销售发货_日期"]])
res = get_f_l_date(data)
batchst.data = data
batchst.material_start = material_start
if batchst.first_time is None or (res["first_time"] and res["first_time"] < batchst.first_time):
batchst.first_time = res["first_time"]
if batchst.last_time is None or (res["last_time"] and res["last_time"] > batchst.last_time):
batchst.last_time = res["last_time"]
batchst.save()

View File

@ -19,20 +19,27 @@ tz_shanghai = ZoneInfo("Asia/Shanghai")
# 批次统计分析 # 批次统计分析
def ana_batch_thread(xbatchs: list): def ana_batch_thread(xbatchs: list, mgroup=None):
MyThread(target=ana_batch, args=(xbatchs,)).start() MyThread(target=ana_batch, args=(xbatchs, mgroup)).start()
def ana_wpr_thread(numbers: list, mgroup): def ana_wpr_thread(numbers: list, mgroup):
MyThread(target=ana_wpr, args=(numbers, mgroup)).start() MyThread(target=ana_wpr, args=(numbers, mgroup)).start()
def ana_batch(xbatchs: list): def ana_batch(xbatchs: list, mgroup):
""" """
批次统计分析 批次统计分析
""" """
time.sleep(10) time.sleep(10)
xbatchs = list(set(xbatchs)) xbatchs = list(set(xbatchs))
BASE_PROJECT_CODE = getattr(settings, "BASE_PROJECT_CODE", None)
try:
m = importlib.import_module(f"apps.wpm.scripts.batch_{BASE_PROJECT_CODE}")
except ModuleNotFoundError:
# myLogger.error(f"ana_batch {BASE_PROJECT_CODE} not found")
return
f = getattr(m, "main")
for xbatch in xbatchs: for xbatch in xbatchs:
get_alldata_with_batch_and_store(xbatch) f(xbatch, mgroup)
def ana_wpr(numbers: list, mgroup): def ana_wpr(numbers: list, mgroup):
""" """
@ -50,487 +57,6 @@ def ana_wpr(numbers: list, mgroup):
f(number, mgroup) f(number, mgroup)
def get_alldata_with_batch_and_store(batch: str):
"""
获取某个批次的整体生产数据并保存
"""
data = None
action = "get"
BASE_PROJECT_CODE = getattr(settings, "BASE_PROJECT_CODE", None)
if BASE_PROJECT_CODE == "gzerp":
action = "get_or_create"
data, extra = get_alldata_with_batch(batch)
elif BASE_PROJECT_CODE == "gxerp":
from apps.wpm.scripts.batch_gxerp import main
data, extra = main(batch)
if data:
if action == "get_or_create":
bobj, _ = BatchSt.objects.get_or_create(batch=batch, version=1)
elif action == "get":
bobj = BatchSt.objects.get(batch=batch, version=1)
bobj.data = json.loads(json.dumps(data, cls=MyJSONEncoder))
for k, v in extra.items():
if hasattr(bobj, k):
setattr(bobj, k, v)
bobj.save()
def get_alldata_with_batch(batch: str):
"""
获取某个批次的整体生产数据
"""
data = {"产品批次编号": batch}
dept7 = Dept.objects.get(name='7车间')
dept6 = Dept.objects.get(name='6车间')
dept10 = Dept.objects.get(name='10车间')
mgroup_blcx = Mgroup.objects.get(name="棒料成型")
mlogs_blcx_qs = Mlog.objects.filter(submit_time__isnull=False, mgroup=mgroup_blcx, batch=batch)
mlog_count_fields = Mlog.count_fields()
material_start = None
if mlogs_blcx_qs.exists():
data["产品规格"] = []
data["棒料成型_日期"] = []
data["棒料成型_出料人"] = []
data["棒料成型_切料人"] = []
data["棒料成型_备注"] = ""
for item in mlogs_blcx_qs:
if material_start is None:
material_start = item.material_out
data["产品规格"].append(item.material_out) # 对象
if item.handle_user:
data["棒料成型_出料人"].append(item.handle_user) # 对象
if item.handle_user_2:
data["棒料成型_切料人"].append(item.handle_user_2) # 对象
if item.note:
data["棒料成型_备注"] = ";".join([data["棒料成型_备注"], item.note])
data["棒料成型_日期"].append(item.handle_date)
for field in mlog_count_fields:
if getattr(item, field) > 0 or field in ["count_real", "count_ok"]:
if f'棒料成型_{field}' not in data:
data[f'棒料成型_{field}'] = getattr(item, field)
else:
data[f'棒料成型_{field}'] += getattr(item, field)
# 整理棒料成型数据
data["产品规格"] = list(set(data["产品规格"]))
data["产品规格"] = ";".join([item.specification if item.specification else "" for item in data["产品规格"]])
data["棒料成型_出料人"] = list(set(data["棒料成型_出料人"]))
data["棒料成型_出料人"] = ";".join([item.name for item in data["棒料成型_出料人"]])
data["棒料成型_切料人"] = list(set(data["棒料成型_切料人"]))
data["棒料成型_切料人"] = ";".join([item.name for item in data["棒料成型_切料人"]])
data["棒料成型_日期"] = list(set(data["棒料成型_日期"]))
data["棒料成型_日期"].sort()
data["棒料成型_小日期"] = max(data["棒料成型_日期"]).strftime("%Y-%m-%d")
data["棒料成型_大日期"] = min(data["棒料成型_日期"]).strftime("%Y-%m-%d")
data["棒料成型_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["棒料成型_日期"]])
try:
data["棒料成型_合格率"] = round((data["棒料成型_count_ok"] * 100/ data["棒料成型_count_real"]), 1)
except ZeroDivisionError:
data["棒料成型_合格率"] = 0
except decimal.InvalidOperation:
# myLogger.error(f"棒料成型_合格率计算错误decimal.InvalidOperation-{data}")
data["棒料成型_合格率"] = 0
# 管料成型数据
mgroup_glcx = Mgroup.objects.get(name="管料成型")
mlogs_glcx_qs = Mlog.objects.filter(submit_time__isnull=False, mgroup=mgroup_glcx, batch=batch)
if mlogs_glcx_qs.exists():
data["产品规格"] = []
data["管料成型_备注"] = ""
data["管料成型_日期"] = []
for item in mlogs_glcx_qs:
if material_start is None:
material_start = item.material_out
data["产品规格"].append(item.material_out) # 对象
if item.note:
data["管料成型_备注"] = ";".join([data["管料成型_备注"], item.note])
data["管料成型_日期"].append(item.handle_date)
for field in mlog_count_fields:
if getattr(item, field) > 0 or field in ["count_real", "count_ok"]:
if f'管料成型_{field}' not in data:
data[f'管料成型_{field}'] = getattr(item, field)
else:
data[f'管料成型_{field}'] += getattr(item, field)
data["产品规格"] = list(set(data["产品规格"]))
data["产品规格"] = ";".join([item.specification for item in data["产品规格"]])
data["管料成型_合格率"] = round((data["管料成型_count_ok"] * 100 / data["管料成型_count_real"]), 1)
data["管料成型_日期"] = list(set(data["管料成型_日期"]))
data["管料成型_日期"].sort()
data["管料成型_小日期"] = max(data["管料成型_日期"]).strftime("%Y-%m-%d")
data["管料成型_大日期"] = min(data["管料成型_日期"]).strftime("%Y-%m-%d")
data["管料成型_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["管料成型_日期"]])
# 7车间生产入库数据/ 8车间中检数据
mioitem_count_fields = MIOItem.count_fields()
mioitem_qs = MIOItem.objects.filter(mio__belong_dept=dept7, mio__type="do_in", batch=batch, mio__submit_time__isnull=False)
if mioitem_qs.exists():
data["七车间入库_日期"] = []
data["七车间入库_车间执行人"] = []
data["七车间入库_仓库执行人"] = []
data["七车间入库_检验备注"] = ""
for item in mioitem_qs:
if material_start is None:
material_start = item.material
data["七车间入库_日期"].append(item.mio.inout_date)
if item.test_note:
data["七车间入库_检验备注"] = ";".join([data["七车间入库_检验备注"], item.test_note])
if item.mio.do_user:
data["七车间入库_车间执行人"].append(item.mio.do_user)
if item.mio.mio_user:
data["七车间入库_仓库执行人"].append(item.mio.mio_user)
for field in mioitem_count_fields:
f_v = getattr(item, field)
if f_v is not None and (f_v > 0 or field in ["count", "count_notok"]):
if f'七车间入库_{field}' not in data:
data[f'七车间入库_{field}'] = f_v
else:
data[f'七车间入库_{field}'] += f_v
data["七车间入库_合格率"] = round((data["七车间入库_count"] - data["七车间入库_count_notok"]) * 100/ data["七车间入库_count"], 1)
data["七车间入库_日期"] = list(set(data["七车间入库_日期"]))
data["七车间入库_日期"].sort()
data["七车间入库_小日期"] = max(data["七车间入库_日期"]).strftime("%Y-%m-%d")
data["七车间入库_大日期"] = min(data["七车间入库_日期"]).strftime("%Y-%m-%d")
data["七车间入库_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["七车间入库_日期"]])
data["七车间入库_车间执行人"] = list(set(data["七车间入库_车间执行人"]))
data["七车间入库_车间执行人"] = ";".join([item.name for item in data["七车间入库_车间执行人"]])
data["七车间入库_仓库执行人"] = list(set(data["七车间入库_仓库执行人"]))
data["七车间入库_仓库执行人"] = ";".join([item.name for item in data["七车间入库_仓库执行人"]])
# 十车间入库检验
mioitem10_qs = MIOItem.objects.filter(mio__belong_dept=dept10, mio__type="do_in", batch=batch, mio__submit_time__isnull=False)
if mioitem10_qs.exists():
data["十车间入库_抽检人"] = []
data["十车间入库_仓库执行人"] = []
data["十车间入库_日期"] = []
data["十车间入库_检验备注"] = ""
for item in mioitem10_qs:
if material_start is None:
material_start = item.material
if item.test_note:
data["十车间入库_检验备注"] = ";".join([data["十车间入库_检验备注"], item.test_note])
if item.mio.mio_user:
data["十车间入库_仓库执行人"].append(item.mio.mio_user)
if item.test_user:
data["十车间入库_抽检人"].append(item.test_user)
data["十车间入库_日期"].append(item.mio.inout_date)
for field in mioitem_count_fields:
if getattr(item, field) is not None and (getattr(item, field) > 0 or field in ["count", "count_notok", "count_sampling"]):
if f'十车间入库_{field}' not in data:
data[f'十车间入库_{field}'] = getattr(item, field)
else:
data[f'十车间入库_{field}'] += getattr(item, field)
data["十车间入库_抽检人"] = list(set(data["十车间入库_抽检人"]))
data["十车间入库_抽检人"] = ";".join([item.name for item in data["十车间入库_抽检人"]])
if data["十车间入库_count_sampling"] > 0:
data["十车间入库_抽检合格数"] = data["十车间入库_count_sampling"] - data["十车间入库_count_notok"]
data["十车间入库_抽检合格率"] = round(data["十车间入库_抽检合格数"] * 100/ data["十车间入库_count_sampling"], 1)
data["十车间入库_仓库执行人"] = list(set(data["十车间入库_仓库执行人"]))
data["十车间入库_仓库执行人"] = ";".join([item.name for item in data["十车间入库_仓库执行人"]])
data["十车间入库_日期"] = list(set(data["十车间入库_日期"]))
data["十车间入库_日期"].sort()
data["十车间入库_小日期"] = max(data["十车间入库_日期"]).strftime("%Y-%m-%d")
data["十车间入库_大日期"] = min(data["十车间入库_日期"]).strftime("%Y-%m-%d")
data["十车间入库_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["十车间入库_日期"]])
data["十车间入库_合格数"] = data["十车间入库_count"] - data["十车间入库_count_notok"]
data["十车间入库_合格率"] = round((data["十车间入库_count"] - data["十车间入库_count_notok"]) * 100/ data["十车间入库_count"], 1)
# 其他入库数据
mioitem_qt_qs = MIOItem.objects.filter(mio__type="other_in", batch=batch, mio__submit_time__isnull=False)
if mioitem_qt_qs.exists():
data["其他入库_仓库执行人"] = []
data["其他入库_日期"] = []
for item in mioitem_qt_qs:
if material_start is None:
material_start = item.material
data["其他入库_日期"].append(item.mio.inout_date)
if item.mio.mio_user:
data["其他入库_仓库执行人"].append(item.mio.mio_user)
for field in mioitem_count_fields:
if getattr(item, field) is not None and (getattr(item, field) > 0 or field in ["count", "count_notok", "count_sampling"]):
if f'其他入库_{field}' not in data:
data[f'其他入库_{field}'] = getattr(item, field)
else:
data[f'其他入库_{field}'] += getattr(item, field)
data["其他入库_仓库执行人"] = list(set(data["其他入库_仓库执行人"]))
data["其他入库_仓库执行人"] = ";".join([item.name for item in data["其他入库_仓库执行人"]])
data["其他入库_日期"] = list(set(data["其他入库_日期"]))
data["其他入库_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["其他入库_日期"]])
# 管料退火生产数据
mgroup_gltx = Mgroup.objects.get(name="管料退火")
mlogs_glth_qs = Mlog.objects.filter(submit_time__isnull=False, mgroup=mgroup_gltx, batch=batch)
if mlogs_glth_qs.exists():
data["管料退火_日期"] = []
data["管料退火_操作人"] = []
data["管料退火_备注"] = ""
data["产品规格"] = []
for item in mlogs_glth_qs:
data["产品规格"].append(item.material_out)
if material_start is None:
material_start = item.material_out
if item.note:
data["管料退火_备注"] = ";".join([data["管料退火_备注"], item.note])
if item.handle_date:
data["管料退火_日期"].append(item.handle_date)
if item.handle_user:
data["管料退火_操作人"].append(item.handle_user)
for field in mlog_count_fields:
if getattr(item, field) is not None and (getattr(item, field) > 0 or field in ["count", "count_notok"]):
if f'管料退火_{field}' not in data:
data[f'管料退火_{field}'] = getattr(item, field)
else:
data[f'管料退火_{field}'] += getattr(item, field)
data["管料退火_日期"] = list(set(data["管料退火_日期"]))
data["管料退火_日期"].sort()
data["管料退火_小日期"] = max(data["管料退火_日期"]).strftime("%Y-%m-%d")
data["管料退火_大日期"] = min(data["管料退火_日期"]).strftime("%Y-%m-%d")
data["管料退火_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["管料退火_日期"]])
data["管料退火_操作人"] = list(set(data["管料退火_操作人"]))
data["管料退火_操作人"] = ";".join([item.name for item in data["管料退火_操作人"]])
data["产品规格"] = list(set(data["产品规格"]))
data["产品规格"] = ";".join([item.specification for item in data["产品规格"]])
# 六车间领料数据
mioitem6_qs = MIOItem.objects.filter(mio__belong_dept=dept6, mio__type="do_out",
batch=batch,
mio__submit_time__isnull=False)
if mioitem6_qs.exists():
data["六车间领料_日期"] = []
data["六车间领料_车间执行人"] = []
data["六车间领料_仓库执行人"] = []
data["产品规格"] = []
for item in mioitem6_qs:
data["产品规格"].append(item.material)
if material_start is None:
material_start = item.material
data["六车间领料_日期"].append(item.mio.inout_date)
if item.mio.mio_user:
data["六车间领料_仓库执行人"].append(item.mio.mio_user)
if item.mio.do_user:
data["六车间领料_车间执行人"].append(item.mio.do_user)
for field in mioitem_count_fields:
if getattr(item, field) is not None and getattr(item, field) > 0:
if f'六车间领料_{field}' not in data:
data[f'六车间领料_{field}'] = getattr(item, field)
else:
data[f'六车间领料_{field}'] += getattr(item, field)
data["六车间领料_日期"] = list(set(data["六车间领料_日期"]))
data["六车间领料_日期"].sort()
data["六车间领料_小日期"] = max(data["六车间领料_日期"]).strftime("%Y-%m-%d")
data["六车间领料_大日期"] = min(data["六车间领料_日期"]).strftime("%Y-%m-%d")
data["六车间领料_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间领料_日期"]])
data["六车间领料_仓库执行人"] = list(set(data["六车间领料_仓库执行人"]))
data["六车间领料_仓库执行人"] = ";".join([item.name for item in data["六车间领料_仓库执行人"]])
data["六车间领料_车间执行人"] = list(set(data["六车间领料_车间执行人"]))
data["六车间领料_车间执行人"] = ";".join([item.name for item in data["六车间领料_车间执行人"]])
data["产品规格"] = list(set(data["产品规格"]))
data["产品规格"] = ";".join([item.specification for item in data["产品规格"]])
# 六车间通过交接记录的领料数据
handover6_qs = Handover.objects.filter(recive_dept=dept6, submit_time__isnull=False, batch=batch)
if handover6_qs.exists():
data["六车间交接领料_日期"] = []
data["六车间交接领料_送料人"] = []
data["六车间交接领料_接料人"] = []
data["六车间交接领料_count"] = 0
for item in handover6_qs:
data["六车间交接领料_count"] += item.count
data["六车间交接领料_日期"].append(item.send_date)
if item.send_user:
data["六车间交接领料_送料人"].append(item.send_user)
if item.recive_user:
data["六车间交接领料_接料人"].append(item.recive_user)
data["六车间交接领料_日期"] = list(set(data["六车间交接领料_日期"]))
data["六车间交接领料_日期"].sort()
data["六车间交接领料_小日期"] = max(data["六车间交接领料_日期"]).strftime("%Y-%m-%d")
data["六车间交接领料_大日期"] = min(data["六车间交接领料_日期"]).strftime("%Y-%m-%d")
data["六车间交接领料_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间交接领料_日期"]])
data["六车间交接领料_送料人"] = list(set(data["六车间交接领料_送料人"]))
data["六车间交接领料_送料人"] = ";".join([item.name for item in data["六车间交接领料_送料人"]])
data["六车间交接领料_接料人"] = list(set(data["六车间交接领料_接料人"]))
data["六车间交接领料_接料人"] = ";".join([item.name for item in data["六车间交接领料_接料人"]])
# 六车间工段生产数据
mgroup_list = ["平头", "粘铁头", "粗中细磨", "平磨", "掏管", "抛光", "开槽", "倒角"]
for mgroup_name in mgroup_list:
if mgroup_name == '粗中细磨':
mgroups = Mgroup.objects.filter(name__in=['粗磨', '粗中磨', '粗中细磨'])
else:
mgroups = Mgroup.objects.filter(name=mgroup_name)
mlogs_qs = Mlog.objects.filter(submit_time__isnull=False, mgroup__in=mgroups, batch=batch)
if mlogs_qs.exists():
data[f'六车间_{mgroup_name}_日期'] = []
data[f'六车间_{mgroup_name}_操作人'] = []
data[f'六车间_{mgroup_name}_备注'] = ""
for item in mlogs_qs:
if material_start is None:
material_start = item.material_out
if item.note:
data[f'六车间_{mgroup_name}_备注'] = ";".join([data[f'六车间_{mgroup_name}_备注'], item.note])
if item.handle_date:
data[f'六车间_{mgroup_name}_日期'].append(item.handle_date)
if item.handle_user:
data[f'六车间_{mgroup_name}_操作人'].append(item.handle_user)
for field in mlog_count_fields:
if getattr(item, field) > 0 or field in ["count_ok", "count_real"]:
if f'六车间_{mgroup_name}_{field}' not in data:
data[f'六车间_{mgroup_name}_{field}'] = getattr(item, field)
else:
data[f'六车间_{mgroup_name}_{field}'] += getattr(item, field)
data[f'六车间_{mgroup_name}_日期'] = list(set(data[f'六车间_{mgroup_name}_日期']))
data[f'六车间_{mgroup_name}_日期'].sort()
data[f'六车间_{mgroup_name}_小日期'] = max(data[f'六车间_{mgroup_name}_日期']).strftime("%Y-%m-%d")
data[f'六车间_{mgroup_name}_大日期'] = min(data[f'六车间_{mgroup_name}_日期']).strftime("%Y-%m-%d")
data[f'六车间_{mgroup_name}_日期'] = ";".join([item.strftime("%Y-%m-%d") for item in data[f'六车间_{mgroup_name}_日期']])
data[f'六车间_{mgroup_name}_操作人'] = list(set(data[f'六车间_{mgroup_name}_操作人']))
data[f'六车间_{mgroup_name}_操作人'] = ";".join([item.name for item in data[f'六车间_{mgroup_name}_操作人']])
try:
data[f'六车间_{mgroup_name}_合格率'] = round(data[f'六车间_{mgroup_name}_count_ok'] * 100/ data[f'六车间_{mgroup_name}_count_real'], 1)
except decimal.InvalidOperation:
# myLogger.error(f"六车间_{mgroup_name}_合格率decimal.InvalidOperation-{data}")
data[f'六车间_{mgroup_name}_合格率'] = 0
ftestwork_count_fields = FtestWork.count_fields()
# 六车间中检数据
ftestwork_qs = FtestWork.objects.filter(batch=batch, type="process")
if ftestwork_qs.exists():
data["六车间中检_日期"] = []
data['六车间中检_检验人'] = []
for item in ftestwork_qs:
if material_start is None:
material_start = item.material
if item.test_date:
data["六车间中检_日期"].append(item.test_date)
if item.test_user:
data['六车间中检_检验人'].append(item.test_user)
for field in ftestwork_count_fields:
if field == 'count_notok_json':
for k, v in getattr(item, field).items():
if f'六车间中检_{k}' not in data:
data[f'六车间中检_{k}'] = v
else:
data[f'六车间中检_{k}'] += v
else:
if getattr(item, field) > 0 or field in ["count", "count_ok"]:
if f'六车间中检_{field}' not in data:
data[f'六车间中检_{field}'] = getattr(item, field)
else:
data[f'六车间中检_{field}'] += getattr(item, field)
data["六车间中检_日期"] = list(set(data["六车间中检_日期"]))
data["六车间中检_日期"].sort()
data["六车间中检_小日期"] = max(data["六车间中检_日期"]).strftime("%Y-%m-%d")
data["六车间中检_大日期"] = min(data["六车间中检_日期"]).strftime("%Y-%m-%d")
data["六车间中检_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间中检_日期"]])
data['六车间中检_检验人'] = list(set(data['六车间中检_检验人']))
data['六车间中检_检验人'] = ";".join([item.name for item in data['六车间中检_检验人']])
# 六车间入库/检验数据
mioitem6_qs2 = MIOItem.objects.filter(mio__belong_dept=dept6, mio__type="do_in",
batch=batch,
mio__submit_time__isnull=False)
if mioitem6_qs2.exists():
data["六车间生产入库_日期"] = []
data["六车间生产入库_检验日期"] = []
data["六车间生产入库_检验人"] = []
for item in mioitem6_qs2:
if material_start is None:
material_start = item.material
data["六车间生产入库_日期"].append(item.mio.inout_date)
if item.test_date:
data["六车间生产入库_检验日期"].append(item.test_date)
for field in mioitem_count_fields:
if getattr(item, field) is not None and (getattr(item, field) > 0 or field in ["count", "count_notok", "count_sampling"]):
if f'六车间生产入库_{field}' not in data:
data[f'六车间生产入库_{field}'] = getattr(item, field)
else:
data[f'六车间生产入库_{field}'] += getattr(item, field)
data["六车间生产入库_日期"] = list(set(data["六车间生产入库_日期"]))
data["六车间生产入库_日期"].sort()
data["六车间生产入库_小日期"] = max(data["六车间生产入库_日期"]).strftime("%Y-%m-%d")
data["六车间生产入库_大日期"] = min(data["六车间生产入库_日期"]).strftime("%Y-%m-%d")
data["六车间生产入库_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间生产入库_日期"]])
data["六车间生产入库_检验日期"] = list(set(data["六车间生产入库_检验日期"]))
data["六车间生产入库_检验日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["六车间生产入库_检验日期"]])
data['六车间生产入库_检验人'] = list(set(data['六车间生产入库_检验人']))
data['六车间生产入库_检验人'] = ";".join([item.name for item in data['六车间生产入库_检验人']])
try:
data['六车间生产入库_合格率'] = round((data['六车间生产入库_count'] - data['六车间生产入库_count_notok']) * 100/ data['六车间生产入库_count'], 1)
except decimal.InvalidOperation:
# myLogger.error("六车间生产入库_合格率decimal.InvalidOperation-{data}")
data['六车间生产入库_合格率'] = 0
# 成品检验数据
ftestwork_qs = FtestWork.objects.filter(batch=batch, type="prod")
if ftestwork_qs.exists():
data["成品检验_日期"] = []
data['成品检验_检验人'] = []
for item in ftestwork_qs:
if material_start is None:
material_start = item.material
if item.test_date:
data["成品检验_日期"].append(item.test_date)
if item.test_user:
data['成品检验_检验人'].append(item.test_user)
for field in ftestwork_count_fields:
if field == 'count_notok_json':
for k, v in getattr(item, field).items():
if f'成品检验_{k}' not in data:
data[f'成品检验_{k}'] = v
else:
data[f'成品检验_{k}'] += v
else:
if getattr(item, field) > 0 or field in ["count", "count_ok"]:
if f'成品检验_{field}' not in data:
data[f'成品检验_{field}'] = getattr(item, field)
else:
data[f'成品检验_{field}'] += getattr(item, field)
data["成品检验_日期"] = list(set(data["成品检验_日期"]))
data["成品检验_日期"].sort()
data["成品检验_小日期"] = max(data["成品检验_日期"]).strftime("%Y-%m-%d")
data["成品检验_大日期"] = min(data["成品检验_日期"]).strftime("%Y-%m-%d")
data["成品检验_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["成品检验_日期"]])
data['成品检验_检验人'] = list(set(data['成品检验_检验人']))
data['成品检验_检验人'] = ";".join([item.name for item in data['成品检验_检验人']])
data['成品检验_合格率'] = round(data['成品检验_count_ok'] * 100/ data['成品检验_count'], 1)
if getattr(data, "六车间领料_count", 0) > 0:
data["六车间_批次生产合格率"] = round(data["成品检验_count_ok"] * 100/ data["六车间领料_count"], 1)
if getattr(data, "棒料成型_count_real", 0) > 0:
data["七车间_批次应出合格率"] = round(data["成品检验_count_ok"] * 100/ data["棒料成型_count_real"], 1)
# 销售发货数据
mioitem_qs = MIOItem.objects.filter(batch=batch, mio__type="sale_out", mio__submit_time__isnull=False)
if mioitem_qs.exists():
data["销售发货_日期"] = []
data['销售发货_仓库执行人'] = []
data['销售发货_count'] = 0
for item in mioitem_qs:
if material_start is None:
material_start = item.material
if item.mio.inout_date:
data["销售发货_日期"].append(item.mio.inout_date)
if item.mio.mio_user:
data['销售发货_仓库执行人'].append(item.mio.mio_user)
data['销售发货_count']+= item.count
if getattr(data, "棒料成型_count_real", 0) > 0:
data["七车间_批次发货合格率"] = round(data["销售发货_count"] * 100/ data["棒料成型_count_real"], 1)
if getattr(data, "六车间领料_count", 0) > 0:
data["六车间_批次发货合格率"] = round(data["销售发货_count"] * 100/ data["六车间领料_count"], 1)
data['销售发货_仓库执行人'] = list(set(data['销售发货_仓库执行人']))
data['销售发货_仓库执行人'] = ";".join([item.name for item in data['销售发货_仓库执行人']])
data["销售发货_日期"] = list(set(data["销售发货_日期"]))
data["销售发货_日期"].sort()
data["销售发货_小日期"] = max(data["销售发货_日期"]).strftime("%Y-%m-%d")
data["销售发货_大日期"] = min(data["销售发货_日期"]).strftime("%Y-%m-%d")
data["销售发货_日期"] = ";".join([item.strftime("%Y-%m-%d") for item in data["销售发货_日期"]])
res = get_f_l_date(data)
return data, {"material_start": material_start, **res}
def get_f_l_date(data): def get_f_l_date(data):
first_date = None first_date = None
last_date = None last_date = None

View File

@ -53,3 +53,7 @@ class WprViewSet(CustomListModelMixin, RetrieveModelMixin, ComplexQueryMixin, Cu
last_number_count = int(last_number.split("-")[-1].lstrip('0')) last_number_count = int(last_number.split("-")[-1].lstrip('0'))
mat = Material.objects.get(id=material_start) mat = Material.objects.get(id=material_start)
return Response({"count": count, "last_number": last_number, "material_model":mat.model, "last_number_count": last_number_count}) return Response({"count": count, "last_number": last_number, "material_model":mat.model, "last_number_count": last_number_count})
def assgin_number_out(self, request, *args, **kwargs):
"""分配对外编号"""
pass