from django.core.paginator import Paginator
import json

from DCbackend.utils.common import success, fail, pageData
from backend.Form.ProjectCategoryForm import ProjectCategoryForm
from backend.Form.ProjectForm import ProjectForm
from backend.Service.CustomerService import CustomerService
from backend.Service.ProjectCategoryService import ProjectCategoryService
from backend.models import ProjectCategory, DefaultStatus, Project, Customer, ProjectDocument


class ProjectService:

    @staticmethod
    def Create(request):
        form = ProjectForm(request.POST)
        if form.is_valid():
            form.save()
            project_id = form.instance.id
            links = json.loads(request.POST.get("links"))
            for p in links:
                pr = ProjectDocument()
                pr.project_id = project_id
                pr.doc_id = p
                pr.status = 5
                pr.save()
            return success("成功")
        else:
            return fail(form.errors)

    @staticmethod
    def Update(request):
        data = Project.objects.filter(id=request.POST.get("id")).first()
        if data is not None:
            data.name = request.POST.get("name")
            data.intro = request.POST.get("intro")
            data.customer_id = request.POST.get("customer_id")
            data.category_id = request.POST.get("category_id")
            data.status = request.POST.get("status")
            data.save()

            project_id = data.id
            ProjectDocument.objects.filter(project_id=project_id).update(status=4)
            links = json.loads(request.POST.get("links"))
            for p in links:
                pr = ProjectDocument()
                pr.project_id = project_id
                pr.doc_id = p
                pr.status = 5
                pr.save()
            return success("成功")
        else:
            return fail("保存分类失败")

    @staticmethod
    def Info(request):
        info = Project.objects.filter(id=request.POST.get("id")).first()
        return success(ProjectService.getDetail(info))

    @staticmethod
    def Delete(request):
        Project.objects.filter(id=request.POST.get("id")).update(status=4)
        return success("成功")

    @staticmethod
    def getDetail(item: Project):
        category = ProjectCategory.objects.filter(id=item.category_id).first()
        if category:
            categoryInfo = ProjectCategoryService.getDetail(category)
        c = Customer.objects.filter(id=item.customer_id).first()
        if c:
            customerInfo = CustomerService.getDetail(c)

        prList = ProjectDocument.objects.filter(project_id=item.id).exclude(status=4).all()
        linkDoc = []
        for it in prList:
            linkDoc.append(it.doc_id)
        return {
            "id": item.id,
            'name': item.name,
            'intro': item.intro,
            'customerId': item.customer_id,
            'categoryId': item.category_id,
            'createTime': item.create_time,
            'category': categoryInfo,
            'status': item.status,
            'customer': customerInfo,
            'linkDoc':linkDoc,
        }

    @staticmethod
    def Search(request):
        page = request.POST.get("page")
        pageSize = request.POST.get("pageSize")
        if page is None:
            page = 1
        if pageSize is None:
            pageSize = 1

        where = {}

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

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

        paginator = Paginator(Project.objects.filter(**where).exclude(status=4).order_by("-id"), pageSize)  # 每页显示10条数据
        page_obj = paginator.get_page(page)
        dataList = []
        for item in page_obj:
            adminInfo = ProjectService.getDetail(item)
            dataList.append(adminInfo)
        return success(pageData(page, pageSize, paginator.num_pages, paginator.count, dataList))
