from django.conf import settings
from minio import Minio

from backend.models import DocumentKbm, Knowledgebase
from datetime import datetime, timedelta, time
from django.db import transaction
import logging
minio_client = Minio(
    settings.MINIO_ENDPOINT,
    access_key=settings.MINIO_ACCESS_KEY,
    secret_key=settings.MINIO_SECRET_KEY,
    secure=settings.MINIO_SECURE
)

# 配置日志记录
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class minioTimingService:


    @staticmethod
    @transaction.atomic
    def rmMiniInfo():
        three_months_ago = datetime.now() - timedelta(days=90)
        print(three_months_ago)
        results = DocumentKbm.objects.filter(status=4, create_time__lt=three_months_ago)
        print("results",results)
        if results.exists():
            for result in results:
                kb = Knowledgebase.objects.filter(id=result.kb_id).first()
                bucket_name = kb.location
                object_name = result.location

                minio_client.remove_object(bucket_name, object_name)

        logger.info("执行清理任务, 时间: %s", datetime.now())

