feat: get_mgroup_goals 可复用去年的数据

This commit is contained in:
caoqianming 2024-12-28 23:22:50 +08:00
parent 28c7cf5375
commit a310185c9d
2 changed files with 16 additions and 5 deletions

View File

@ -117,7 +117,7 @@ class GoalSerializer(CustomModelSerializer):
class MgroupGoalYearSerializer(serializers.Serializer):
mgroup = serializers.CharField(label='ID或名称')
mgroup = serializers.CharField(label='工段ID')
year = serializers.IntegerField(label='')

View File

@ -8,6 +8,7 @@ from datetime import datetime
from apps.wf.models import Ticket
from django.db.models import Sum
from typing import List
from apps.utils.snowflake import idWorker
def cal_material_count(materialId_list: List[str]=None):
"""
@ -37,20 +38,30 @@ def get_mgroup_goals(mgroupId, year, reload=False):
"""
获取工段某年的全部目标值, 以字典形式返回, 带缓存
"""
goals = Goal.objects.filter(
Q(mgroup__id=mgroupId) | Q(mgroup__name=mgroupId), year=year)
key = f'mgroup_{mgroupId}_goals'
goals = Goal.objects.get(mgroup__id=mgroupId, year=year)
key = f'mgroup_{mgroupId}_goals_{year}'
if reload is False:
mgroup_goals = cache.get(key, None)
if mgroup_goals is not None:
return mgroup_goals
mgroup_goals = {}
if not goals.exists():
# 尝试寻找去年的
goals_last_year = Goal.objects.filter(
Q(mgroup__id=mgroupId) | Q(mgroup__name=mgroupId), year=year-1)
if goals_last_year.exists():
for goal in goals_last_year:
# 复用去年的数据创建
goal.id = idWorker.get_id()
goal.year = year + 1
goal.save()
goals = Goal.objects.get(mgroup__id=mgroupId, year=year)
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, 10)
cache.set(key, mgroup_goals)
return mgroup_goals