feat:修改自动生成班组

This commit is contained in:
zty 2024-11-25 17:45:08 +08:00
parent 7c85d3df65
commit f0a748e304
1 changed files with 27 additions and 1 deletions

View File

@ -52,6 +52,23 @@ def get_sflog(mgroup: Mgroup, happen_time: datetime):
start_time__lte=happen_time, end_time__gt=happen_time, mgroup=mgroup).order_by('-start_time').first()
return sflog
def get_team(mgroup: Mgroup, team_ids: list):
from apps.mtm.models import Srule
# 获取 r_list 列表
teams_ins = Srule.objects.get(mgroup=mgroup)
# 提取team_id
teams = [team['team'] for team in teams_ins.r_list]
# 找到 team_ids 匹配段的结束位置
start_idx = -1
for i in range(len(teams)-len(team_ids)+1):
if teams[i:i+len(team_ids)] == team_ids:
start_idx = i
break
if start_idx == -1:
raise ParseError("无法找到匹配的段")
# 获取匹配段的结束位置
new_team = teams[start_idx+len(team_ids)] if start_idx != -1 and start_idx+len(team_ids) < len(teams) else None
return new_team
def make_sflogs(mgroup: Mgroup, start_date: datetime.date, end_date: datetime.date, create_by=None):
shift_rule = mgroup.shift_rule
@ -70,6 +87,14 @@ def make_sflogs(mgroup: Mgroup, start_date: datetime.date, end_date: datetime.da
else:
start_time -= datetime.timedelta(days=1)
total_sec = (end_time - start_time).total_seconds()
# 创建之前查询最后三条记录
all_teams = SfLog.objects.filter(mgroup=mgroup, start_time__lt=start_time).order_by('-start_time').values_list('team', flat=True)
# 如果最后一条记录的team_id为空则继续向前取三条
from itertools import islice
# 获取前三个非空值
last_teams = list(islice(all_teams, 3))
new_team = get_team(mgroup=mgroup, team_ids=last_teams)
# 创建SfLog记录
SfLog.objects.get_or_create(mgroup=mgroup, shift=shift, start_time=start_time, defaults={
"mgroup": mgroup,
"shift": shift,
@ -78,7 +103,8 @@ def make_sflogs(mgroup: Mgroup, start_date: datetime.date, end_date: datetime.da
"end_time": end_time,
"total_sec_now": total_sec,
"total_sec": total_sec,
"create_by": create_by
"create_by": create_by,
"team": new_team
})
current_date = current_date + datetime.timedelta(days=1)