from django.db.models import Count

class TongJiView(View):
    def today(self):
        import datetime
        today=datetime.datetime.now().date()
        customer_list=Customer.objects.filter(deal_date=today)

        # 查询每一个销售的名字以及今天对应的成单量
        ret=UserInfo.objects.filter(depart_id=2,customers__deal_date=today).annotate(c=Count("customers")).values_list("username","c")
        print(ret)
        ret=[[item[0],item[1]] for item in list(ret)]

        return {"customer_list":customer_list,"ret":list(ret)}

    def zuotian(self):
        import datetime
        zuotian = datetime.datetime.now().date()-datetime.timedelta(days=1)
        customer_list = Customer.objects.filter(deal_date=zuotian)

        # 查询每一个销售的名字以及昨天对应的成单量
        ret = UserInfo.objects.filter(depart_id=2, customers__deal_date=zuotian).annotate(
            c=Count("customers")).values_list("username", "c")
        print(ret)
        print(ret)
        ret = [[item[0], item[1]] for item in list(ret)]

        return {"customer_list": customer_list, "ret": list(ret)}



    def week(self):
        import datetime
        today = datetime.datetime.now().date()
        weekdelta = datetime.datetime.now().date()-datetime.timedelta(weeks=1)
        customer_list = Customer.objects.filter(deal_date__gte=weekdelta,deal_date__lte=today)

        # 查询每一个销售的名字以及昨天对应的成单量
        ret = UserInfo.objects.filter(depart_id=2, customers__deal_date__gte=weekdelta,customers__deal_date__lte=today).annotate(
            c=Count("customers")).values_list("username", "c")
        print(ret)

        print(ret)
        ret = [[item[0], item[1]] for item in list(ret)]

        return {"customer_list": customer_list, "ret": list(ret)}


    def recent_month(self):
        import datetime
        today = datetime.datetime.now().date()
        weekdelta = datetime.datetime.now().date()-datetime.timedelta(weeks=4)
        customer_list = Customer.objects.filter(deal_date__gte=weekdelta,deal_date__lte=today)

        # 查询每一个销售的名字以及昨天对应的成单量
        ret = UserInfo.objects.filter(depart_id=2, customers__deal_date__gte=weekdelta,customers__deal_date__lte=today).annotate(
            c=Count("customers")).values_list("username", "c")
        print(ret)

        print(ret)
        ret = [[item[0], item[1]] for item in list(ret)]

        return {"customer_list": customer_list, "ret": list(ret)}

    def get(self,request):

        date=request.GET.get("date","today")

        if hasattr(self,date):
            context=getattr(self,date)()

        return render(request,"customer/tongji.html",context)
from django.views import View
from app01.models import Customer,UserInfo
from django.db.models import Count

class TongJiView2(View):

    def get(self,request):
        date=request.GET.get("date","today")
        # func=getattr(self,date)
        #ret=func()
        import datetime
        now=datetime.datetime.now().date()
        delta1=datetime.timedelta(days=1)
        delta2=datetime.timedelta(weeks=1)
        delta3=datetime.timedelta(weeks=4)

        condition={
             "today":[{"deal_date__date":now},{"customers__deal_date":now}],
             "yesterday":[{"deal_date__date":now-delta1},{"customers__deal_date":now-delta1}],
             "week":[{"deal_date__gte":now-delta2,"deal_date__lte":now},
                     {"customers__deal_date__gte":now-delta2,"customers__deal_date__lte":now}
                     ],
             "recent_month":[{"deal_date__gte":now-delta3,"deal_date__lte":now},
                     {"customers__deal_date__gte":now-delta3,"customers__deal_date__lte":now}
                     ],
              }

        customer_list=Customer.objects.filter(**(condition.get(date)[0]))
        ret=UserInfo.objects.all().filter(**(condition.get(date)[1])).annotate(c=Count("customers")).values_list("username","c")
        ret = [[item[0], item[1]] for item in list(ret)]

        return render(request,"customer/tongji.html",locals())