from django.core.management.base import BaseCommand

from api.models import TtStaff, TtLocation, TtStudentSet, TtActivityTemplate, TtActivity, TtModule
from api.utils import convert_to_redis_week, bulk_sync_to_redis
from backend.redis_client import redis_client

class Command(BaseCommand):
    def handle(self, *args, **options):
        redis_data={
            "update":{},
        }
        redis_table = "student_set"

        models = (
            TtStudentSet.objects
            .prefetch_related("student")
        )
        total = 0
        for val in models:
            total += 1
            planned_size = len([s.id for s in val.student.all()])
            val.planned_size = planned_size
            redis_data["update"].setdefault(redis_table, []).append({
                "id": val.id,
                "planned_size": planned_size,
            })

        TtStudentSet.objects.bulk_update(models, ["planned_size"])

        bulk_sync_to_redis(redis_client, redis_data)
        print("activity count:"+str(total))

