feat: ofm-views 修改 车辆字段

This commit is contained in:
TianyangZhang 2025-11-17 16:43:02 +08:00
parent c6ec4f6c56
commit 4ba952d705
1 changed files with 10 additions and 10 deletions

View File

@ -144,14 +144,14 @@ class VehicleViewSet(CustomModelViewSet):
filterset_class = VehicleFilterset filterset_class = VehicleFilterset
def add_info_for_list(self, data): def add_info_for_list(self, data):
booking_ids = [d["id"] for d in data] vehicle_ids = [d["id"] for d in data]
slots = VehicleSlot.objects.filter(booking__in=booking_ids).order_by("vehreg", "vehicle", "vdate", "slot") slots = VehicleSlot.objects.filter(vehicle__in=vehicle_ids).order_by("vehreg", "vehicle", "vdate", "slot")
booking_info = {} vehicle_info = {}
for slot in slots: for slot in slots:
booking_id = slot.vehicle.id vehicle_id = slot.vehicle.id
if booking_id not in booking_info: if vehicle_id not in vehicle_info:
booking_info[booking_id] = { vehicle_info[vehicle_id] = {
"vdate": slot.vdate.strftime("%Y-%m-%d"), # 格式化日期 "vdate": slot.vdate.strftime("%Y-%m-%d"), # 格式化日期
"vehreg": slot.vehreg.id, "vehreg": slot.vehreg.id,
"vehreg_name": slot.vehreg.name, # 会议室名称 "vehreg_name": slot.vehreg.name, # 会议室名称
@ -160,7 +160,7 @@ class VehicleViewSet(CustomModelViewSet):
} }
# 检查是否连续当前slot是否紧接上一个slot # 检查是否连续当前slot是否紧接上一个slot
current_slots = booking_info[booking_id]["current_slots"] current_slots = vehicle_info[vehicle_id]["current_slots"]
if not current_slots or slot.slot == current_slots[-1] + 1: if not current_slots or slot.slot == current_slots[-1] + 1:
current_slots.append(slot.slot) current_slots.append(slot.slot)
else: else:
@ -168,12 +168,12 @@ class VehicleViewSet(CustomModelViewSet):
if current_slots: if current_slots:
start_time = self._slot_to_time(current_slots[0]) start_time = self._slot_to_time(current_slots[0])
end_time = self._slot_to_time(current_slots[-1] + 1) end_time = self._slot_to_time(current_slots[-1] + 1)
booking_info[booking_id]["time_ranges"].append(f"{start_time}-{end_time}") vehicle_info[vehicle_id]["time_ranges"].append(f"{start_time}-{end_time}")
current_slots.clear() current_slots.clear()
current_slots.append(slot.slot) current_slots.append(slot.slot)
# 处理最后剩余的连续slot # 处理最后剩余的连续slot
for info in booking_info.values(): for info in vehicle_info.values():
if info["current_slots"]: if info["current_slots"]:
start_time = self._slot_to_time(info["current_slots"][0]) start_time = self._slot_to_time(info["current_slots"][0])
end_time = self._slot_to_time(info["current_slots"][-1] + 1) end_time = self._slot_to_time(info["current_slots"][-1] + 1)
@ -181,7 +181,7 @@ class VehicleViewSet(CustomModelViewSet):
info["slots"] = info.pop("current_slots") # 清理临时数据 info["slots"] = info.pop("current_slots") # 清理临时数据
for item in data: for item in data:
item.update(booking_info.get(item["id"], {})) item.update(vehicle_info.get(item["id"], {}))
return data return data
@staticmethod @staticmethod