from rest_framework.viewsets import GenericViewSet
from apps.vod.serializers import DatetimeSerializer
from rest_framework.decorators import action
from rest_framework.response import Response
from utils.sql import query_all_dict

class AnalyseViewSet(GenericViewSet):
    perms_map = {'post': '*'}
    serializer_class = DatetimeSerializer

    def is_valid(self, request):
        data = request.data
        sr = self.get_serializer(data=data)
        sr.is_valid(raise_exception=True)
        vdata = sr.validated_data
        return vdata

    @action(methods=['post'], detail=False)
    def group_by_video_category_big(self, request):
        """
        视频大类播放量统计

        视频大类播放量统计
        """
        vdata = self.is_valid(request)
        sql_str = """select 
        d.name as "视频大类", 
        count(v.id) as "视频数量",
        count(vi.id) as "观看总次数", 
        count(distinct vi.create_by_id) as "观看总人数"
        from vod_video v
        left join system_dict d on d.id = v.category_big_id
        left join vod_viewitem vi on vi.video_id = v.id
        and vi.create_time >= %s
        and vi.create_time <= %s
        group by d.id
        order by "视频数量" desc, d.sort
        limit %s
        """
        return Response(query_all_dict(sql_str, [vdata['start_time'], vdata['end_time'], vdata['limit']]))

    @action(methods=['post'], detail=False)
    def group_by_user_view(self, request):
        """
        个人观看量统计

        个人观看量统计
        """
        vdata = self.is_valid(request)
        sql_str = """select 
        u.name as "姓名",  
        u.username as "账号", 
        count(v2.is_completed is true) as "观看完成视频总数",
        count(distinct vi.video_id) as "观看视频总数", 
        count(vi.id) as "观看总次数" , 
        sum(vi.total_seconds/60) as "观看总时间"
        from vod_viewitem vi
        left join vod_video v on v.id = vi.video_id
        left join system_user u on u.id = vi.create_by_id
        left join vod_view2 v2 on v2.create_by_id =  vi.create_by_id and v2.video_id = vi.video_id
        where vi.create_time >= %s and vi.create_time <= %s
        group by u.id
        order by "观看完成视频总数" desc, "账号"
        limit %s
        """
        return Response(query_all_dict(sql_str, [vdata['start_time'], vdata['end_time'], vdata['limit']]))

    
    @action(methods=['post'], detail=False)
    def group_by_org_view(self, request):
        """
        单位观看量统计

        单位观看量统计
        """
        vdata = self.is_valid(request)
        sql_str = """select 
        o.name as "单位名称", 
        count(v2.is_completed is true) as "观看完成视频总数",
        count(distinct vi.video_id) as "观看视频总数", 
        count(vi.id) as "观看总次数" , 
        sum(vi.total_seconds/60) as "观看总时间"
        from vod_viewitem vi
        left join vod_video v on v.id = vi.video_id
        left join system_user u on u.id = vi.create_by_id
        left join system_organization o on o.id = u.dept_id
        left join vod_view2 v2 on v2.create_by_id =  vi.create_by_id and v2.video_id = vi.video_id
        where vi.create_time >= %s and vi.create_time <= %s
        group by o.id
        order by "观看完成视频总数" desc, o.sort
        limit %s
        """
        return Response(query_all_dict(sql_str, [vdata['start_time'], vdata['end_time'], vdata['limit']]))