feat: group_by_dept优化

This commit is contained in:
caoqianming 2023-08-01 11:23:34 +08:00
parent e4d0b56a0b
commit 39e98f4e69
1 changed files with 8 additions and 2 deletions

View File

@ -6,6 +6,8 @@ from utils.sql import query_all_dict
class YearSerializer(serializers.Serializer): class YearSerializer(serializers.Serializer):
year = serializers.IntegerField(label='年份') year = serializers.IntegerField(label='年份')
dept_name = serializers.CharField(label='单位名称', default='')
dept_type_name = serializers.CharField(label='单位类型', default='2级公司')
class AnalyseViewSet(GenericViewSet): class AnalyseViewSet(GenericViewSet):
perms_map = {'post': '*'} perms_map = {'post': '*'}
@ -28,6 +30,7 @@ class AnalyseViewSet(GenericViewSet):
vdata = self.is_valid(request) vdata = self.is_valid(request)
sql_str = f"""select task2.year as 年份, sql_str = f"""select task2.year as 年份,
dept.name as 单位, dept.name as 单位,
dict.name as 单位类型,
pgoal.pgoal_1 as 重大事故数基础值, pgoal.pgoal_1 as 重大事故数基础值,
pdept1.goal_value_b as 重大事故数设定值, pdept1.goal_value_b as 重大事故数设定值,
SUM(task2do.num_acc) as 重大事故数, SUM(task2do.num_acc) as 重大事故数,
@ -61,9 +64,12 @@ left join supervision_pgoaldept pdept2 on pdept2.goal_key = 'pgoal_2' and pdept2
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 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 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_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 = {vdata['year']} 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']} where task2.year = {vdata['year']}
GROUP BY task2.year, dept.id, and (dept.name = '{vdata['dept_name']}' or '{vdata['dept_name']}'='')
and (dic.name = '{vdata['dept_type_name']}' or '{vdata['dept_type_name']}'='')
GROUP BY task2.year, dept.id, dict.name,
pgoal.pgoal_1, pgoal.pgoal_1,
pgoal.pgoal_2, pgoal.pgoal_2,
pgoal.pgoal_3, pgoal.pgoal_3,