feat: 增加mlogb表以记录多个产出

This commit is contained in:
caoqianming 2023-11-01 08:42:10 +08:00
parent c2a33de7f7
commit 732a33f1f2
3 changed files with 97 additions and 5 deletions

View File

@ -0,0 +1,41 @@
# Generated by Django 3.2.12 on 2023-10-31 10:47
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('mtm', '0021_material_brothers'),
('wpm', '0022_mlog_shift'),
]
operations = [
migrations.AlterField(
model_name='mlog',
name='material_out',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='mlog_material_out', to='mtm.material', verbose_name='产物'),
),
migrations.CreateModel(
name='Mlogb',
fields=[
('id', models.CharField(editable=False, help_text='主键ID', max_length=20, primary_key=True, serialize=False, verbose_name='主键ID')),
('create_time', models.DateTimeField(default=django.utils.timezone.now, help_text='创建时间', verbose_name='创建时间')),
('update_time', models.DateTimeField(auto_now=True, help_text='修改时间', verbose_name='修改时间')),
('is_deleted', models.BooleanField(default=False, help_text='删除标记', verbose_name='删除标记')),
('count_ok', models.PositiveIntegerField(default=0, verbose_name='合格数量')),
('material_out', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mtm.material', verbose_name='产物')),
('mlog', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='wpm.mlog', verbose_name='关联日志')),
],
options={
'abstract': False,
},
),
migrations.AddField(
model_name='mlog',
name='material_outs',
field=models.ManyToManyField(blank=True, related_name='mlog_material_outs', through='wpm.Mlogb', to='mtm.Material', verbose_name='多个产出'),
),
]

View File

@ -1,5 +1,5 @@
from django.db import models
from apps.utils.models import CommonADModel, CommonBDModel
from apps.utils.models import CommonADModel, CommonBDModel, BaseModel
from apps.mtm.models import Mgroup, Team, Shift, Material
from apps.pm.models import Mtask
from apps.system.models import User
@ -88,7 +88,7 @@ class Mlog(CommonADModel):
mgroup = models.ForeignKey(
Mgroup, verbose_name='工段', on_delete=models.CASCADE, null=True, blank=True)
material_out = models.ForeignKey(
Material, verbose_name='产物', on_delete=models.CASCADE, null=True, blank=True)
Material, verbose_name='产物', on_delete=models.CASCADE, null=True, blank=True, related_name='mlog_material_out')
equipment = models.ForeignKey(
Equipment, verbose_name='生产设备', on_delete=models.CASCADE, null=True, blank=True, related_name='mlog_equipment')
equipment_2 = models.ForeignKey(
@ -125,6 +125,16 @@ class Mlog(CommonADModel):
handle_leader = models.ForeignKey(
User, verbose_name='班长', on_delete=models.CASCADE, null=True, blank=True, related_name='mlog_handle_leader')
note = models.TextField('备注', default='', blank=True)
material_outs = models.ManyToManyField(
Material, verbose_name='多个产出', blank=True, through='wpm.mlogb', related_name='mlog_material_outs')
class Mlogb(BaseModel):
mlog = models.ForeignKey(Mlog, verbose_name='关联日志',
on_delete=models.CASCADE)
material_out = models.ForeignKey(
Material, verbose_name='产物', on_delete=models.CASCADE)
count_ok = models.PositiveIntegerField('合格数量', default=0)
class Handover(CommonADModel):

View File

@ -3,11 +3,12 @@ from apps.utils.serializers import CustomModelSerializer
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from .models import SfLog, StLog, SfLogExp, WMaterial, Mlog, Handover
from .models import SfLog, StLog, SfLogExp, WMaterial, Mlog, Handover, Mlogb
from apps.system.models import Dept, User
from apps.pm.models import Mtask
from apps.wpm.tasks import cal_enstat_when_pcoal_heat_change, cal_enstat_when_team_change
from apps.mtm.serializers import MaterialSimpleSerializer
from django.db import transaction
class StLogSerializer(CustomModelSerializer):
@ -77,6 +78,15 @@ class WMaterialSerializer(CustomModelSerializer):
fields = '__all__'
class MlogbSerializer(CustomModelSerializer):
material_out_ = MaterialSimpleSerializer(
source='material_out', read_only=True)
class Meta:
model = Mlogb
fields = ['id', 'material_out', 'count_ok']
class MlogSerializer(CustomModelSerializer):
mgroup_name = serializers.CharField(
source='mtask.mgroup.name', read_only=True)
@ -101,6 +111,8 @@ class MlogSerializer(CustomModelSerializer):
equipment_2_name = serializers.CharField(
source='equipment_2.name', read_only=True)
shift_name = serializers.CharField(source='shift.name', read_only=True)
material_outs = MlogbSerializer(
label='多产出件信息', many=True, source='mlog_material_outs')
class Meta:
model = Mlog
@ -124,7 +136,19 @@ class MlogSerializer(CustomModelSerializer):
raise ValidationError('缺少工段或产物!')
if Mlog.objects.filter(mtask=mtask, batch=batch, handle_date=handle_date, handle_user=handle_user).exists():
raise ValidationError('存在相同的日志记录')
return super().create(validated_data)
with transaction.atomic():
material_outs = validated_data.pop('material_outs', [])
instance = super().create(validated_data)
brotherId_should_list = material_out.brothers
if brotherId_should_list:
if material_outs:
for item in material_outs:
if item['material_out'].id in brotherId_should_list:
Mlogb.objects.create(
mlog=instance, material_out=item['material_out'], count_ok=item['count_ok'])
else:
raise ValidationError('缺少产出物信息')
return instance
def update(self, instance, validated_data):
validated_data.pop('mtask', None)
@ -132,7 +156,24 @@ class MlogSerializer(CustomModelSerializer):
validated_data.pop('handle_date', None)
validated_data.pop('handle_user', None)
validated_data.pop('mgroup', None)
return super().update(instance, validated_data)
with transaction.atomic():
material_outs = validated_data.pop('material_outs', [])
instance = super().update(instance, validated_data)
brotherId_should_list = instance.material_out.brothers
if brotherId_should_list:
if material_outs:
for item in material_outs:
id = item.get('id', None)
if id:
mlogb = Mlogb.objects.get(id=id)
mlogb.count_ok = item['count_ok']
mlogb.save()
elif item['material_out'].id in brotherId_should_list:
Mlogb.objects.create(
mlog=instance, material_out=item['material_out'], count_ok=item['count_ok'])
else:
raise ValidationError('缺少产出物信息')
return instance
def validate(self, attrs):
mtask = attrs.get('mtask', None)