feat: 质检统计增加班月

This commit is contained in:
caoqianming 2023-07-24 10:22:06 +08:00
parent 2acd216d33
commit f4f9cb0fae
5 changed files with 42 additions and 5 deletions

View File

@ -0,0 +1,25 @@
# Generated by Django 3.2.12 on 2023-07-24 02:17
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('mtm', '0005_auto_20230706_1032'),
('qm', '0005_auto_20230706_1028'),
]
operations = [
migrations.AddField(
model_name='quastat',
name='shift',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='mtm.shift', verbose_name='关联班组'),
),
migrations.AlterField(
model_name='quastat',
name='type',
field=models.CharField(default='day', help_text='year_s/month_s/day_s/shift/sflog', max_length=50, verbose_name='统计维度'),
),
]

View File

@ -1,7 +1,7 @@
from django.db import models from django.db import models
from apps.system.models import CommonAModel from apps.system.models import CommonAModel
from apps.utils.models import CommonBDModel from apps.utils.models import CommonBDModel
from apps.mtm.models import Material from apps.mtm.models import Material, Shift
from apps.wpm.models import SfLog from apps.wpm.models import SfLog
class TestItem(CommonAModel): class TestItem(CommonAModel):
@ -19,12 +19,13 @@ class QuaStat(CommonBDModel):
""" """
质量数据统计表 需要有belong_dept 质量数据统计表 需要有belong_dept
""" """
type = models.CharField('统计维度', max_length=50, default='day', help_text='year_s/month_s/day_s/sflog') type = models.CharField('统计维度', max_length=50, default='day', help_text='year_s/month_s/day_s/shift/sflog')
year_s = models.PositiveSmallIntegerField('班年', null=True, blank=True) year_s = models.PositiveSmallIntegerField('班年', null=True, blank=True)
month_s = models.PositiveSmallIntegerField('班月', null=True, blank=True) month_s = models.PositiveSmallIntegerField('班月', null=True, blank=True)
day_s = models.PositiveSmallIntegerField('班日', null=True, blank=True) day_s = models.PositiveSmallIntegerField('班日', null=True, blank=True)
material = models.ForeignKey(Material, verbose_name='关联产物', on_delete=models.CASCADE) material = models.ForeignKey(Material, verbose_name='关联产物', on_delete=models.CASCADE)
sflog = models.ForeignKey(SfLog, verbose_name='关联值班记录', on_delete=models.CASCADE, null=True, blank=True) sflog = models.ForeignKey(SfLog, verbose_name='关联值班记录', on_delete=models.CASCADE, null=True, blank=True)
shift = models.ForeignKey(Shift, verbose_name='关联班组', on_delete=models.CASCADE, null=True, blank=True)
testitem = models.ForeignKey(TestItem, verbose_name='质检项目', on_delete=models.CASCADE) testitem = models.ForeignKey(TestItem, verbose_name='质检项目', on_delete=models.CASCADE)
val_avg = models.FloatField('平均值', null=True, blank=True) val_avg = models.FloatField('平均值', null=True, blank=True)
num_test = models.PositiveSmallIntegerField('检测次数', null=True, blank=True) num_test = models.PositiveSmallIntegerField('检测次数', null=True, blank=True)

View File

@ -28,7 +28,7 @@ class QuaStatSerializer(CustomModelSerializer):
attrs['belong_dept'] = attrs['sflog'].belong_dept attrs['belong_dept'] = attrs['sflog'].belong_dept
end_time = attrs['sflog'].end_time end_time = attrs['sflog'].end_time
end_time_local = localtime(end_time) end_time_local = localtime(end_time)
attrs['year_s'], attrs['month_s'], attrs['day_s'] = end_time_local.year, end_time_local.month, end_time_local.day attrs['year_s'], attrs['month_s'], attrs['day_s'], attrs['shift'] = end_time_local.year, end_time_local.month, end_time_local.day, attrs['sflog'].shift
attrs['rate_pass'] = attrs['num_ok']/attrs['num_test'] attrs['rate_pass'] = attrs['num_ok']/attrs['num_test']
return attrs return attrs

View File

@ -6,11 +6,16 @@ from django.db.models import Sum, F, ExpressionWrapper, FloatField, Q
def cal_quastat_type(qs, params_o: dict, type: str): def cal_quastat_type(qs, params_o: dict, type: str):
params = params_o.copy() params = params_o.copy()
v_list = ['belong_dept', 'material', 'testitem', 'year_s', 'month_s', 'day_s'] v_list = ['belong_dept', 'material', 'testitem', 'year_s', 'month_s', 'day_s', 'shift']
if type == 'month_s': if type == 'shift':
params.pop('day_s')
v_list = ['belong_dept', 'material', 'testitem', 'year_s', 'month_s', 'shift']
elif type == 'month_s':
params.pop('shift')
params.pop('day_s') params.pop('day_s')
v_list = ['belong_dept', 'material', 'testitem', 'year_s', 'month_s'] v_list = ['belong_dept', 'material', 'testitem', 'year_s', 'month_s']
elif type == 'year_s': elif type == 'year_s':
params.pop('shift')
params.pop('month_s') params.pop('month_s')
params.pop('day_s') params.pop('day_s')
v_list = ['belong_dept', 'material', 'testitem', 'year_s'] v_list = ['belong_dept', 'material', 'testitem', 'year_s']
@ -50,6 +55,7 @@ def cal_quastat(quastatId: str):
params = { params = {
'testitem': ins.testitem, 'testitem': ins.testitem,
'material': ins.material, 'material': ins.material,
'shift': ins.shift,
'belong_dept': ins.belong_dept, 'belong_dept': ins.belong_dept,
'year_s': ins.year_s, 'year_s': ins.year_s,
'month_s': ins.month_s, 'month_s': ins.month_s,
@ -57,6 +63,10 @@ def cal_quastat(quastatId: str):
} }
# 日统计 # 日统计
cal_quastat_type(qs, params_o=params, type='day_s') cal_quastat_type(qs, params_o=params, type='day_s')
# 班统计
cal_quastat_type(qs, params_o=params, type='shift')
# 班月统计
cal_quastat_type(qs, params_o=params, type='month_sf')
# 月统计 # 月统计
cal_quastat_type(qs, params_o=params, type='month_s') cal_quastat_type(qs, params_o=params, type='month_s')
# 年统计 # 年统计

View File

@ -40,6 +40,7 @@ class QuaStatViewSet(ListModelMixin, BulkUpdateModelMixin, CustomGenericViewSet)
def perform_update(self, serializer): def perform_update(self, serializer):
ins = serializer.save() ins = serializer.save()
cal_quastat.delay(ins.id)
if ins.sflog: # 更新值班记录的质检时间 if ins.sflog: # 更新值班记录的质检时间
ins.sflog.last_test_time = datetime.datetime.now() ins.sflog.last_test_time = datetime.datetime.now()
ins.sflog.save() ins.sflog.save()