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

class YearSerializer(serializers.Serializer):
    year = serializers.IntegerField(label='年份')
    dept_name = serializers.CharField(label='单位名称', default='', allow_blank=True)
    dept_type_name = serializers.CharField(label='单位类型', default='2级公司', allow_blank=True)

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

    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_dept(self, request):
        """
        质量目标集团统计

        质量目标集团统计
        """
        vdata = self.is_valid(request)
        sql_str = f"""select task2.year as 年份,
dept.name as 单位,
dict.name as 单位类型,
pgoal.pgoal_1 as 重大事故数基础值,
pdept1.goal_value_b as 重大事故数设定值,
SUM(task2do.num_acc) as 重大事故数,
pgoal.pgoal_2 as 报告证书合格率基础值,
pdept2.goal_value_b as 报告证书合格率设定值,
CAST(SUM(task2do.num_issue)-SUM(task2do.num_error) AS FLOAT)/NULLIF(SUM(task2do.num_issue), 0)*100 AS 报告证书合格率,
pgoal.pgoal_3 as 报告证书及时率基础值,
pdept3.goal_value_b as 报告证书及时率设定值,
CAST(SUM(task2do.num_expect)-SUM(task2do.num_overdue) AS FLOAT)/NULLIF(SUM(task2do.num_expect), 0)*100 AS 报告证书及时率,
pgoal.pgoal_4 as 能力验证满意率基础值,
pdept4.goal_value_b as 能力验证满意率设定值,
CAST(SUM(task2do.num_pt_10) AS FLOAT)/NULLIF(SUM(task2do.num_pt), 0)*100 AS 能力验证满意率,
pgoal.pgoal_5 as 客户投诉处理满意率基础值,
pdept5.goal_value_b as 客户投诉处理满意率设定值,
CAST(SUM(task2do.num_complaint_10) AS FLOAT)/NULLIF(SUM(task2do.num_complaint), 0)*100 AS 客户投诉处理满意率,
SUM(task2do.num_issue) as 已发报告数,
SUM(task2do.num_expect) as 应发报告数,
SUM(task2do.num_error) as 不准确报告数,
SUM(task2do.num_overdue) as 超期报告数,
SUM(task2do.num_complaint) as 客户投诉数,
SUM(task2do.num_complaint_10) as 客户投诉满意数,
SUM(task2do.num_pt) as 能力验证数,
SUM(task2do.num_pt_10) as 能力验证满意数,
SUM(task2do.num_risk) as 风险识别数,
SUM(task2do.num_oinspect) as 外部检查数
from supervision_task2do task2do
LEFT JOIN supervision_task2 task2 on task2.id = task2do.task2_id
left join system_organization dept on dept.id = task2do.belong_dept_id
left join supervision_pgoaldept pdept1 on pdept1.goal_key = 'pgoal_1' and pdept1.year = {vdata['year']} and pdept1.belong_dept_id = task2do.belong_dept_id
left join supervision_pgoaldept pdept2 on pdept2.goal_key = 'pgoal_2' and pdept2.year = {vdata['year']} and pdept2.belong_dept_id = task2do.belong_dept_id
left join supervision_pgoaldept pdept3 on pdept3.goal_key = 'pgoal_3' and pdept3.year = {vdata['year']} and pdept3.belong_dept_id = task2do.belong_dept_id
left join supervision_pgoaldept pdept4 on pdept4.goal_key = 'pgoal_4' and pdept4.year = {vdata['year']} and pdept4.belong_dept_id = task2do.belong_dept_id
left join supervision_pgoaldept pdept5 on pdept5.goal_key = 'pgoal_5' and pdept5.year = {vdata['year']} and pdept5.belong_dept_id = task2do.belong_dept_id
left join supervision_pgoal pgoal on pgoal.year = 2023
left join system_dict dict on dict.id = dept.type_id
where task2.year = {vdata['year']}
and (dept.name = '{vdata['dept_name']}' or '{vdata['dept_name']}'='')
and (dict.name = '{vdata['dept_type_name']}' or '{vdata['dept_type_name']}'='')
GROUP BY task2.year, dept.id, dict.name,
pgoal.pgoal_1,
pgoal.pgoal_2,
pgoal.pgoal_3,
pgoal.pgoal_4,
pgoal.pgoal_5,
pdept1.goal_value_b,
pdept2.goal_value_b,
pdept3.goal_value_b,
pdept4.goal_value_b,
pdept5.goal_value_b
ORDER BY dept.sort
        """
        return Response(query_all_dict(sql_str))