feat: ftest更新ftestwork的count_sampling等字段

This commit is contained in:
caoqianming 2024-11-19 17:52:57 +08:00
parent 1cb64b26af
commit 5730a00742
3 changed files with 28 additions and 9 deletions

View File

@ -109,8 +109,10 @@ class FtestWork(CommonBDModel):
"""
检验工作
"""
TYPE2_SOME = 10
TYPE2_ALL = 20
type = models.CharField('检验类型', max_length=20, choices=FTEST_TYPE_CHOICES, default='prod')
type2 = models.PositiveSmallIntegerField('检验类型2', choices=((10, '抽检'), (20, '全检')), default=10)
type2 = models.PositiveSmallIntegerField('检验类型2', choices=((TYPE2_SOME, '抽检'), (TYPE2_ALL, '全检')), default=10)
shift = models.ForeignKey(Shift, verbose_name='班次', on_delete=models.SET_NULL, null=True, blank=True)
wm = models.ForeignKey(WMaterial, verbose_name='关联车间库存', on_delete=models.SET_NULL, null=True, blank=True)
mb = models.ForeignKey('inm.materialbatch', verbose_name='关联仓库', on_delete=models.SET_NULL, null=True, blank=True)

View File

@ -182,3 +182,8 @@ class PtestSerializer(CustomModelSerializer):
class Meta:
model = Ptest
fields = '__all__'
def create(self, validated_data):
if Ptest.objects.filter(sample_number=validated_data['sample_number']).exists():
raise serializers.ValidationError('该样品编号已存在')
return super().create(validated_data)

View File

@ -83,23 +83,35 @@ class FtestViewSet(CustomModelViewSet):
select_related_fields = ['test_user', 'check_user', 'ftest_work']
filterset_fields = ['type', 'ftest_work']
def count_sampling(self, ftest_work:FtestWork):
qs = Ftest.objects.filter(ftest_work=ftest_work)
all_count = qs.count()
ok_count = qs.filter(is_ok=True).count()
ftest_work.count_sampling = all_count
ftest_work.count_sampling_ok = ok_count
if ftest_work.type2 == FtestWork.TYPE2_ALL: # 如果是全检
ftest_work.count_ok = ok_count
ftest_work.count_notok = all_count - ok_count
ftest_work.save()
@transaction.atomic
def perform_create(self, serializer):
ins = serializer.save()
ins: Ftest = serializer.save()
if ins.ftest_work:
ins.ftest_work.count_sampling = Ftest.objects.filter(
ftest_work=ins.ftest_work).count()
ins.ftest_work.save()
return ins
self.count_sampling(ins.ftest_work)
@transaction.atomic
def perform_update(self, serializer):
ins: Ftest = serializer.save()
if ins.ftest_work:
self.count_sampling(ins.ftest_work)
@transaction.atomic
def perform_destroy(self, instance):
ftest_work = instance.ftest_work
instance.delete()
if ftest_work:
instance.ftest_work.count_sampling = Ftest.objects.filter(
ftest_work=instance.ftest_work).count()
instance.ftest_work.save()
self.count_sampling(ftest_work)
class PtestViewSet(CustomModelViewSet):