from django.core.paginator import Paginator

from DCbackend.utils.common import success, fail, pageData
from backend.Form.BrandForm import BrandForm
from backend.Form.ProductCategoryForm import ProductCategoryForm
from backend.Form.ProductForm import ProductForm
from backend.models import ProjectCategory, Brand, Product, ProductCategory


class ProductCategoryService:

    @staticmethod
    def Create(request):
        form = ProductCategoryForm(request.POST)
        if form.is_valid():
            form.save()
            return success("成功")
        else:
            return fail(form.errors)

    @staticmethod
    def Update(request):
        data = ProductCategory.objects.filter(id=request.POST.get("id")).first();
        if data is not None:
            data.name = request.POST.get("name")
            data.parent_id = request.POST.get("parent_id")
            data.attrs = request.POST.get("attrs")
            data.status = request.POST.get("status")
            data.save()
            return success("成功")
        else:
            return fail("更新产品分类失败")

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

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

    @staticmethod
    def getDetail(item: ProductCategory):
        parentData = {}
        if item.parent_id > 0:
            parentItem = ProductCategory.objects.filter(id=item.parent_id).first()
            parentData = ProductCategoryService.getDetail(parentItem)

        return {
            "id": item.id,
            'parent_id': item.parent_id,
            'parentData': parentData,
            'name': item.name,
            'attrs': item.attrs,
            'createTime': item.create_time,
            'children':[],
            'status': item.status
        }

    @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 = {}

        parent_id = request.POST.get("parent_id")
        if parent_id:
            where["parent_id"] = parent_id
        else:
            where['parent_id'] = 0

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

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

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

    @staticmethod
    def getAllCategories():
        top = ProductCategory.objects.filter(status=5, parent_id=0).order_by("id").all()
        dataList = []
        for item in top:
            itemData={
                "id": item.id,
                'children': ProductCategoryService.getSubCategory(item),
                'value': item.id,
                'label': item.name,
                'attrs': item.attrs,
            }
            dataList.append(itemData)
        return success(dataList)

    @staticmethod
    def getSubCategory(topCategory: ProductCategory):
        items = ProductCategory.objects.filter(status=5, parent_id=topCategory.id).order_by("id").all()
        dataList = []
        for item in items:
            itemData = {
                "id": item.id,
                'label': item.name,
                'value': item.id,
                'attrs':item.attrs,
            }

            dataList.append(itemData)
        return dataList
