feat: 获取工段全年目标

This commit is contained in:
caoqianming 2023-08-04 14:26:00 +08:00
parent 2be3c43bef
commit 4e5adc6dc3
5 changed files with 29 additions and 10 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2023-08-04 06:24
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('system', '0002_myschedule'),
('mtm', '0006_auto_20230725_1357'),
]
operations = [
migrations.AlterUniqueTogether(
name='goal',
unique_together={('mgroup', 'year', 'goal_cate')},
),
]

View File

@ -82,4 +82,7 @@ class Goal(CommonADModel):
goal_val_9 = models.FloatField('9月份目标值')
goal_val_10 = models.FloatField('10月份目标值')
goal_val_11 = models.FloatField('11月份目标值')
goal_val_12 = models.FloatField('12月份目标值')
goal_val_12 = models.FloatField('12月份目标值')
class Meta:
unique_together = ("mgroup", "year", "goal_cate")

View File

@ -42,7 +42,6 @@ class GoalSerializer(CustomModelSerializer):
fields = '__all__'
read_only_fields = EXCLUDE_FIELDS
class MgroupGoalYearSerializer(serializers.Serializer):
mgroup = serializers.CharField(label='ID或名称')
year = serializers.IntegerField(label='')

View File

@ -1,12 +1,13 @@
from apps.mtm.models import Goal, Mgroup
from django.core.cache import cache
from django.db.models import Q
def get_mgroup_goals(mgroup, year, reload=False):
def get_mgroup_goals(mgroupId, year, reload=False):
"""
获取工段某年的全部目标值, 以字典形式返回, 带缓存
"""
goals = Goal.objects.filter(mgroup=mgroup, year=year)
key = f'mgroup_{mgroup.id}_goals'
goals = Goal.objects.filter(Q(mgroup__id=mgroupId)|Q(mgroup__name=mgroupId), year=year)
key = f'mgroup_{mgroupId}_goals'
if reload is False:
mgroup_goals = cache.get(key, None)
if mgroup_goals is not None:
@ -16,5 +17,5 @@ def get_mgroup_goals(mgroup, year, reload=False):
mgroup_goals[f'{goal.goal_cate.code}_year'] = goal.goal_val
for i in range(12):
mgroup_goals[f'{goal.goal_cate.code}_{i+1}'] = getattr(goal, f'goal_val_{i+1}')
cache.set(key, mgroup_goals)
cache.set(key, mgroup_goals, 10)
return mgroup_goals

View File

@ -77,7 +77,7 @@ class GoalViewSet(CustomModelViewSet):
select_related_fields = ['mgroup', 'goal_cate']
filterset_class = GoalFilter
search_fields = ['name']
ordering = ['mgroup__sort', 'goal_cate__sort', 'goal_cate__create_time']
@action(methods=['post'], detail=False, perms_map={'post': '*'}, serializer_class=MgroupGoalYearSerializer)
def mgroup_goals_year(self, request, pk=None):
@ -89,7 +89,5 @@ class GoalViewSet(CustomModelViewSet):
sr = MgroupGoalYearSerializer(data=request.data)
sr.is_valid(raise_exception=True)
vdata = sr.validated_data
mgroup = Mgroup.objects.get(Q(id=vdata['mgroup'])|Q(name=vdata['mgroup']))
year = vdata['year']
res = get_mgroup_goals(mgroup, year, True)
res = get_mgroup_goals(vdata['mgroup'], vdata['year'], True)
return Response(res)