from backend.Form.KbmDocumentTypeForm import KbmDocumentTypeForm
from backend.models import KbmDocumentType, DocumentKbm
from base import logger
from django.core.paginator import Paginator
from django.db import transaction

from DCbackend import settings
from DCbackend.utils.common import success, fail, pageData
#知识库文件分类
class KbmDocumentTypeService:
    @staticmethod
    @transaction.atomic
    def insertType(request):
        kb_id = request.POST.get("kb_id")
        if not kb_id:
            return fail("知识库id为空")
        form = KbmDocumentTypeForm(request.POST)
        if form.is_valid():
            form.save()
            return success("保存成功")
        else:
            return fail(form.errors)

    @staticmethod
    @transaction.atomic
    def updateType(request):
        data = KbmDocumentType.objects.filter(id=request.POST.get("id")).first();
        if data is not None:
            data.name = request.POST.get("name")
            data.kb_id = request.POST.get("kb_id")
            data.status = request.POST.get("status")
            data.parent_id = request.POST.get("parent_id")
            data.save()
            return success("更新成功")
        else:
            return fail("更新参数信息失败")

    @staticmethod
    def selectTypeList(request):
        page = request.POST.get("page",1)
        pageSize = request.POST.get("pageSize",10)
        kb_id = request.POST.get("kb_id")
        if not kb_id:
            return fail("知识库id为空")

        where = {}
        name = request.POST.get("name", '')
        if name:
            where["name__icontains"] = name

        status = request.POST.get("status")
        if status:
            where['status'] = status

        where['kb_id'] = kb_id

        paginator = Paginator(KbmDocumentType.objects.filter(**where).exclude(status=4).order_by("-id"),
                              pageSize)  # 每页显示10条数据
        page_obj = paginator.get_page(page)

        dataList = []
        for item in page_obj:
            count = DocumentKbm.objects.filter(doc_type_id=item.id).count()
            info = KbmDocumentTypeService.getDetail(item, count)
            dataList.append(info)
        return success(pageData(page, pageSize, paginator.num_pages, paginator.count, dataList))

    @staticmethod
    def getDetail(item, count=None):
        info = {
            "id": item.id,
            'name': item.name,
            'kb_id': item.kb_id,
            'create_time': item.create_time,
            'status': item.status
        }
        if count is not None:
            info['document_count'] = count
        return info

    @staticmethod
    @transaction.atomic
    def delete(request):
        id = request.POST.get("id")
        if not id:
            return fail("id为空")
        count = DocumentKbm.objects.filter(doc_type_id=id).exclude(status=4).count()
        if count > 0:
            return fail("该分类下存在文件，请删除后重试")

        KbmDocumentType.objects.filter(id=id).update(status=4)

        return success("删除成功")

    @staticmethod
    def Info(request):
        info = KbmDocumentType.objects.filter(id=request.POST.get("id")).first()
        return success(KbmDocumentTypeService.getDetail(info),"查询成功")


    @staticmethod
    def selectType0(request):
        type_id = request.POST.get("type_id")
        kb_id = request.POST.get("kb_id")
        if not type_id:
            return fail("type_id为空")

        count = DocumentKbm.objects.filter(doc_type_id=type_id,kb_id=kb_id).exclude(status=4).count()

        return success(count)





