feat: 获取工段全年目标值
This commit is contained in:
parent
c7b7d9cec4
commit
959fc60e24
|
@ -40,4 +40,9 @@ class GoalSerializer(CustomModelSerializer):
|
|||
class Meta:
|
||||
model = Goal
|
||||
fields = '__all__'
|
||||
read_only_fields = EXCLUDE_FIELDS
|
||||
read_only_fields = EXCLUDE_FIELDS
|
||||
|
||||
|
||||
class MgroupGoalYearSerializer(serializers.Serializer):
|
||||
mgroup = serializers.CharField(label='ID或名称')
|
||||
year = serializers.IntegerField(label='年')
|
|
@ -0,0 +1,20 @@
|
|||
from apps.mtm.models import Goal, Mgroup
|
||||
from django.core.cache import cache
|
||||
|
||||
def get_mgroup_goals(mgroup, year, reload=False):
|
||||
"""
|
||||
获取工段某年的全部目标值, 以字典形式返回, 带缓存
|
||||
"""
|
||||
goals = Goal.objects.filter(mgroup=mgroup, year=year)
|
||||
key = f'mgroup_{mgroup.id}_goals'
|
||||
if reload is False:
|
||||
mgroup_goals = cache.get(key, None)
|
||||
if mgroup_goals is not None:
|
||||
return mgroup_goals
|
||||
mgroup_goals = {}
|
||||
for goal in goals:
|
||||
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)
|
||||
return mgroup_goals
|
|
@ -5,6 +5,13 @@ from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
|
|||
from apps.mtm.models import Material, Mgroup, Shift, Team, Goal
|
||||
from apps.mtm.serializers import MaterialSerializer, MgroupSerializer, ShiftSerializer, TeamSerializer, GoalSerializer
|
||||
from apps.mtm.filters import GoalFilter
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from drf_yasg import openapi
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from django.db.models import Q
|
||||
from apps.mtm.services import get_mgroup_goals
|
||||
from apps.mtm.serializers import MgroupGoalYearSerializer
|
||||
|
||||
# Create your views here.
|
||||
class MaterialViewSet(CustomModelViewSet):
|
||||
|
@ -69,4 +76,20 @@ class GoalViewSet(CustomModelViewSet):
|
|||
serializer_class = GoalSerializer
|
||||
select_related_fields = ['mgroup', 'goal_cate']
|
||||
filterset_class = GoalFilter
|
||||
search_fields = ['name']
|
||||
search_fields = ['name']
|
||||
|
||||
|
||||
@action(methods=['post'], detail=False, perms_map={'post': '*'}, serializer_class=MgroupGoalYearSerializer)
|
||||
def mgroup_goals_year(self, request, pk=None):
|
||||
"""
|
||||
获取工段全年目标
|
||||
|
||||
获取工段全年目标
|
||||
"""
|
||||
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)
|
||||
return Response(res)
|
Loading…
Reference in New Issue