Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- anid value -> inValue
- drop function if exists expert.build_acts_lists_grid_query(boolean, integer, expert.act_list_status[],
- character varying[], character varying[],
- character varying[], uuid, character varying[], bigint[],
- varchar, timestamp with time zone, timestamp with time zone,
- timestamp with time zone, timestamp with time zone, date,
- date, date, date, date, date, character varying[], uuid[],
- uuid[], character varying[], character varying[], text, text,
- text, date, date, expert.act_status[], integer, numeric,
- numeric, numeric, numeric, numeric, numeric, date, date,
- text, text, integer, integer);
- drop function if exists expert.get_act_lists(integer, expert.act_list_status[], character varying[],
- character varying[], character varying[], uuid, character varying[],
- bigint[], varchar, timestamp with time zone, timestamp with time zone,
- timestamp with time zone, timestamp with time zone, date, date, date, date,
- date, date, character varying[], uuid[], text, text, integer, integer);
- create or replace function expert.get_act_lists(aregion_code integer, astatus expert.act_list_status[]
- , aold_id character varying[], amedical_organization_codes character varying[], alist_number character varying[]
- , anid uuid[], auser_id character varying[], aassigned_expert_id bigint[], alist_name character varying
- , create_date_begin timestamp with time zone, create_date_end timestamp with time zone, upload_date_begin timestamp with time zone
- , upload_date_end timestamp with time zone, sign_date_begin date, sign_date_end date, send_date_begin date, send_date_end date
- , print_date_begin date, print_date_end date, amed_work_type_name character varying[], azpmd_id uuid[]
- , order_column text, order_direction text, alimit integer, aoffset integer)
- returns TABLE(id uuid, list_number character varying, list_name character varying, date_create timestamp with time zone
- , region_code text, medical_organization_name text, medical_organization_code text, user_id character varying
- , date_upload timestamp with time zone, zpmd_id uuid, has_cover_letter_date boolean, date_print date
- , date_send date, date_sign date, old_id text, theme_category_id uuid, med_work_type_id bigint
- , med_work_type_name character varying, assigned_expert_id bigint, status expert.act_list_status
- , region expert.region)
- parallel safe
- language plpgsql
- as
- $$
- declare
- _order_by text = coalesce(case when regexp_split_to_array(order_column, ',') <@ array['region_code','medical_organization_code'
- ,'id','list_number','status', 'old_id','user_id','list_name','date_create','date_upload','date_sign','date_send','date_print', 'med_work_type_name']
- then ' order by ' || order_column || ' ' || order_direction || ' , id '|| format('limit %L offset %L', alimit, aoffset) end, '');
- _where text = concat_ws(' and '
- , case when cardinality(anid) > 0 then format('al.id = any(%L)', anid) end
- , case when cardinality(alist_number) > 0 then format('al.list_number = any(%L)', alist_number) end
- , case when cardinality(astatus) > 0 then format('al.status = any(%L)', astatus) end
- , case when cardinality(aold_id) > 0 then format('al.old_id = any(%L)', aold_id) end
- , case when cardinality(auser_id) > 0 then format('al.user_id = any(%L)', auser_id) end
- , case when cardinality(azpmd_id) > 0 then format('al.zpmd_id = any(%L)', azpmd_id) end
- , case when cardinality(aassigned_expert_id) > 0 then format('al.assigned_expert_id = any(%L)',aassigned_expert_id) end
- , case when alist_name is not null then format('al.list_name like ''%%%s%%''',alist_name) end
- , case when create_date_begin is not null then format('al.date_create >= %L',create_date_begin) end
- , case when create_date_end is not null then format('al.date_create <= %L', create_date_end) end
- , case when upload_date_begin is not null then format('al.date_upload >= %L', upload_date_begin) end
- , case when upload_date_end is not null then format('al.date_upload <= %L', upload_date_end) end
- , case when sign_date_begin is not null then format('al.date_sign >= %L', sign_date_begin) end
- , case when sign_date_end is not null then format('al.date_sign <= %L', sign_date_end) end
- , case when send_date_begin is not null then format('al.date_send >= %L', send_date_begin) end
- , case when send_date_end is not null then format('al.date_send <= %L', send_date_end) end
- , case when print_date_begin is not null then format('al.date_print >= %L', print_date_begin) end
- , case when print_date_end is not null then format('al.date_print <= %L', print_date_end) end
- , case when cardinality(amed_work_type_name) > 0 then format('v006d.name_type_usl = any(%L)', amed_work_type_name) end);
- _sql text = 'select al.id,
- al.list_number,
- al.list_name,
- al.date_create,
- case
- when al.region = ''TULA''::expert.region then mo_tula.short_name::text
- else f003d.nam_mok::text
- end
- as medical_organization_name,
- case
- when al.region = ''TULA''::expert.region then mo_tula.mo_code::text
- else f003d.mcode::text
- end as medical_organization_code,
- f003d.mcode::text as medical_organization_code,
- al.user_id,
- al.date_upload,
- al.zpmd_id,
- al.has_cover_letter_date,
- al.date_print,
- al.date_send,
- al.date_sign,
- al.old_id::text as old_id,
- al.theme_category_id,
- al.med_work_type_id,
- v006d.name_type_usl as med_work_type_name,
- al.assigned_expert_id,
- al.status,
- al.region
- from expert.acts_list al
- join directory.f003 f003d on f003d.mcode = al.medical_organization_code '|| concat_ws(' and '
- ,case when aregion_code is not null
- then format(' and f003d.region_code = %s', aregion_code) end
- , case when cardinality(amedical_organization_codes) > 0
- then format('f003d.mcode = any(''%s'')', amedical_organization_codes) end) ||
- 'join directory.v006 v006d on v006d.code_type_usl = al.med_work_type_id' ||
- 'left join directory.mo_tula mo_tula on mo_tula.mo_code = al.medical_organization_code '|| concat_ws(' and '
- ,case when aregion_code is not null
- then format(' and mo_tula.region_code = %s', aregion_code) end
- , case when cardinality(amedical_organization_codes) > 0
- then format('mo_tula.mo_code = any(''%s'')', amedical_organization_codes) end);
- begin
- return query execute _sql || (case when _where <> '' then ' where '|| _where else '' end) || _order_by;
- --return _sql || (case when _where <> '' then ' where '|| _where else '' end) || _order_by; -- debug mode
- end
- $$;
- create or replace function expert.build_acts_lists_grid_query(is_count_query boolean, aregion_code integer
- , astatus expert.act_list_status[], aold_id character varying[], amedical_organization_codes character varying[]
- , alist_number character varying[], anid uuid[], auser_id character varying[], aassigned_expert_id bigint[]
- , alist_name character varying, create_date_begin timestamp with time zone, create_date_end timestamp with time zone
- , upload_date_begin timestamp with time zone, upload_date_end timestamp with time zone, sign_date_begin date
- , sign_date_end date, send_date_begin date, send_date_end date, print_date_begin date, print_date_end date
- , amed_work_type_name character varying[], azpmd_id uuid[], apodrid uuid[], athemename character varying[], acategoryname character varying[]
- , aexpertisepurpose text, aexpertisethemetype text, aexpertisetype text, acoverletterdatebegin date, acoverletterdateend date
- , aactstatus expert.act_status[], acountacts integer, aretentionsum numeric, aretentionsummax numeric, aretentionsummin numeric
- , atotalsumfine numeric, atotalsumfinemax numeric, atotalsumfinemin numeric, aexpertisedatebegin date, aexpertisedateend date
- , order_column text, order_direction text, alimit integer, aoffset integer) returns text
- parallel safe
- language plpgsql
- as
- $$
- declare
- _having_filters text = concat_ws(' and '
- , case when acountActs is not null then format('count(distinct act.id) = %L', acountActs) end
- , case when aretentionSum is not null then format('sum(coalesce(sanction.sum_rem, 0.0)) = %L', aretentionSum) end
- , case when aretentionSumMax is not null then format('sum(coalesce(sanction.sum_rem, 0.0)) < %L', aretentionSumMax) end
- , case when aretentionSumMin is not null then format('sum(coalesce(sanction.sum_rem, 0.0)) > %L', aretentionSumMin) end
- , case when atotalSumFine is not null then format('sum(sanction.sum_fine) = %L', atotalSumFine) end
- , case when atotalSumFineMax is not null then format('sum(sanction.sum_fine) < %L', atotalSumFineMax) end
- , case when atotalSumFineMin is not null then format('sum(sanction.sum_fine) > %L', atotalSumFineMin) end
- , case when aexpertiseDateBegin is not null then format('max(re.expertise_date) >= %L', aexpertiseDateBegin) end
- , case when aexpertiseDateEnd is not null then format('max(re.expertise_date) <= %L', aexpertiseDateEnd) end);
- _where_filters text = concat_ws(' and '
- , case when cardinality(athemeName) > 0 then format('theme.name = any(%L)', athemeName) end
- , case when cardinality(acategoryName) > 0 then format('category.name = any(%L)', acategoryName) end
- , case when aexpertisePurpose is not null then format('ecd.expertise_purpose = %L', aexpertisePurpose) end
- , case when aexpertiseThemeType is not null then format('ecd.expertise_theme_type = %L', aexpertiseThemeType) end
- , case when aexpertiseType is not null then format('ecd.expertise_type = %L', aexpertiseType) end
- , case when acoverLetterDateBegin is not null then format('cv.date >= %L', acoverLetterDateBegin) end
- , case when acoverLetterDateEnd is not null then format('cv.date <= %L', acoverLetterDateEnd) end
- , case when cardinality(aactStatus) > 0 then format('act.status = any(%L)', aactStatus) end
- , case when cardinality(apodrId) > 0 then format('zpmd.podr_id = any(%L)', apodrId) end);
- has_filters bool = _where_filters <> '' or _having_filters <> '';
- _select text = 'select al.id,
- al.list_number,
- al.list_name,
- al.medical_organization_name,
- al.med_work_type_name,
- ecd.expertise_purpose::text as expertise_purpose,
- ecd.expertise_type::text as expertise_type,
- ecd.expertise_theme_type::text as expertise_theme_type,
- al.status as status,
- count(distinct act.id) as count_acts,
- concat_ws('' '', users.last_name, users.first_name) as expert_last_name,
- al.date_create,
- al.region_code,
- al.user_id,
- max(theme.name::text) as theme_name,
- max(category.name::text) as category_name,
- al.date_upload,
- al.medical_organization_code,
- sum(coalesce(sanction.sum_rem, 0.0)) as retention_sum,
- al.zpmd_id,
- theme_category.id as list_theme_category_id,
- zpmd.theme_category_id as zpmd_theme_category_id,
- al.has_cover_letter_date,
- cv.number as cover_letter_number,
- cv.date as cover_letter_date,
- max(re.expertise_date) as expertise_date,
- al.date_print,
- al.date_send,
- al.date_sign,
- concat_ws('' ''::text, exp.family_name, exp.first_name, exp.patronymic) as assigned_expert,
- al.assigned_expert_id,
- sum(sanction.sum_fine) as total_sum_fine,
- zpmd.podr_id,
- max(coalesce(podr_omsk.nam_mop, podr_tula.name)::text) as podr_name,
- max(coalesce(podr_omsk.lpu_1::text, podr_tula.podr_id) ::text) as podr_code,
- al.old_id as old_id';
- _inner_joins text = '
- join expert.act act on act.act_list_id = al.id
- join expert.theme_category theme_category on al.theme_category_id = theme_category.id
- join expert.category on category.id = theme_category.category_id
- join expert.expertise_class_decomposed ecd
- on ecd.expertise_class = theme_category.expertise_class
- join zpmd.zpmd on zpmd.id = al.zpmd_id';
- _left_joins text = '
- left join expert.theme on theme_category.theme_id = theme.id
- left join expert.document cv on al.id = cv.act_list_id and cv.type = ''COVER_LETTER''::expert.document_type
- left join expert.expertise_result re
- on act.id = re.act_id and re.status <> ''DELETED''::expert.expertise_result_status
- left join expert.expertise_sanction sanction on re.id = sanction.expertise_result_id and
- (sanction.sum_fine is not null or sanction.sum_rem is not null)
- left join expert.users users on users.id = al.user_id
- left join directory.expert exp on exp.id = al.assigned_expert_id
- left join directory.podr_omsk podr_omsk on al.region = ''OMSK'' and podr_omsk.id = zpmd.podr_id
- left join directory.t_podr_tula podr_tula on al.region = ''TULA'' and podr_tula.id = zpmd.podr_id';
- _from text = format('
- from expert.get_act_lists(%L, %L, %L, %L, %L, %L , %L, %L, %L, %L, %L, %L, %L, %L, %L, %L, %L, %L, %L
- , %L, %L, %L, %L, %L, %L ) al '
- , aregion_code, astatus, aold_id, amedical_organization_codes, alist_number, anid , auser_id
- , aassigned_expert_id, alist_name, create_date_begin, create_date_end, upload_date_begin, upload_date_end
- , sign_date_begin, sign_date_end, send_date_begin, send_date_end, print_date_begin, print_date_end
- , amed_work_type_name, azpmd_id, order_column, order_direction
- , case when not is_count_query and not has_filters then alimit end
- , case when not is_count_query and not has_filters then aoffset end) ||
- _inner_joins ||
- case when is_count_query and not has_filters then '' else _left_joins end;
- _where text = case when _where_filters <> '' then '
- where '|| _where_filters || ' ' else '' end;
- _group_by text = '
- group by al.id,
- al.list_number,
- al.list_name,
- al.date_create,
- al.region_code,
- al.medical_organization_name,
- al.medical_organization_code,
- al.user_id,
- al.date_upload,
- al.zpmd_id,
- al.has_cover_letter_date,
- al.date_print,
- al.date_send,
- al.date_sign,
- al.old_id,
- al.theme_category_id,
- al.med_work_type_name,
- al.assigned_expert_id,
- al.status,
- al.region,
- (ecd.expertise_purpose::text),(ecd.expertise_type::text), (ecd.expertise_theme_type::text),
- concat_ws('' '', users.last_name, users.first_name), zpmd.theme_category_id,
- theme_category.id, cv.number, cv.date, exp.id, zpmd.podr_id';
- _order_by text = ' order by ' || order_column || ' ' || order_direction || ' , id';
- _limit text = case when not is_count_query and has_filters then format(' limit %L offset %L', alimit, aoffset) else '' end;
- _list_query text = _select ||
- _from ||
- _where ||
- _group_by ||
- (case when _having_filters <> '' then ' having ' || _having_filters else '' end) ||
- _order_by || _limit;
- _count_query text = 'select count(distinct al.id) ' ||
- case when _having_filters = '' then '' else '
- from ( select al.id ' end||
- _from ||
- _where ||
- case when _having_filters = '' then '' else _group_by || ' having '|| _having_filters || ') as al' end;
- begin
- return
- case when is_count_query
- then _count_query
- else _list_query
- end;
- end
- $$;-- anid value -> inValue
- drop function if exists expert.build_acts_lists_grid_query(boolean, integer, expert.act_list_status[],
- character varying[], character varying[],
- character varying[], uuid, character varying[], bigint[],
- varchar, timestamp with time zone, timestamp with time zone,
- timestamp with time zone, timestamp with time zone, date,
- date, date, date, date, date, character varying[], uuid[],
- uuid[], character varying[], character varying[], text, text,
- text, date, date, expert.act_status[], integer, numeric,
- numeric, numeric, numeric, numeric, numeric, date, date,
- text, text, integer, integer);
- drop function if exists expert.get_act_lists(integer, expert.act_list_status[], character varying[],
- character varying[], character varying[], uuid, character varying[],
- bigint[], varchar, timestamp with time zone, timestamp with time zone,
- timestamp with time zone, timestamp with time zone, date, date, date, date,
- date, date, character varying[], uuid[], text, text, integer, integer);
- create or replace function expert.get_act_lists(aregion_code integer, astatus expert.act_list_status[]
- , aold_id character varying[], amedical_organization_codes character varying[], alist_number character varying[]
- , anid uuid[], auser_id character varying[], aassigned_expert_id bigint[], alist_name character varying
- , create_date_begin timestamp with time zone, create_date_end timestamp with time zone, upload_date_begin timestamp with time zone
- , upload_date_end timestamp with time zone, sign_date_begin date, sign_date_end date, send_date_begin date, send_date_end date
- , print_date_begin date, print_date_end date, amed_work_type_name character varying[], azpmd_id uuid[]
- , order_column text, order_direction text, alimit integer, aoffset integer)
- returns TABLE(id uuid, list_number character varying, list_name character varying, date_create timestamp with time zone
- , region_code text, medical_organization_name text, medical_organization_code text, user_id character varying
- , date_upload timestamp with time zone, zpmd_id uuid, has_cover_letter_date boolean, date_print date
- , date_send date, date_sign date, old_id text, theme_category_id uuid, med_work_type_id bigint
- , med_work_type_name character varying, assigned_expert_id bigint, status expert.act_list_status
- , region expert.region)
- parallel safe
- language plpgsql
- as
- $$
- declare
- _order_by text = coalesce(case when regexp_split_to_array(order_column, ',') <@ array['region_code','medical_organization_code'
- ,'id','list_number','status', 'old_id','user_id','list_name','date_create','date_upload','date_sign','date_send','date_print', 'med_work_type_name']
- then ' order by ' || order_column || ' ' || order_direction || ' , id '|| format('limit %L offset %L', alimit, aoffset) end, '');
- _where text = concat_ws(' and '
- , case when cardinality(anid) > 0 then format('al.id = any(%L)', anid) end
- , case when cardinality(alist_number) > 0 then format('al.list_number = any(%L)', alist_number) end
- , case when cardinality(astatus) > 0 then format('al.status = any(%L)', astatus) end
- , case when cardinality(aold_id) > 0 then format('al.old_id = any(%L)', aold_id) end
- , case when cardinality(auser_id) > 0 then format('al.user_id = any(%L)', auser_id) end
- , case when cardinality(azpmd_id) > 0 then format('al.zpmd_id = any(%L)', azpmd_id) end
- , case when cardinality(aassigned_expert_id) > 0 then format('al.assigned_expert_id = any(%L)',aassigned_expert_id) end
- , case when alist_name is not null then format('al.list_name like ''%%%s%%''',alist_name) end
- , case when create_date_begin is not null then format('al.date_create >= %L',create_date_begin) end
- , case when create_date_end is not null then format('al.date_create <= %L', create_date_end) end
- , case when upload_date_begin is not null then format('al.date_upload >= %L', upload_date_begin) end
- , case when upload_date_end is not null then format('al.date_upload <= %L', upload_date_end) end
- , case when sign_date_begin is not null then format('al.date_sign >= %L', sign_date_begin) end
- , case when sign_date_end is not null then format('al.date_sign <= %L', sign_date_end) end
- , case when send_date_begin is not null then format('al.date_send >= %L', send_date_begin) end
- , case when send_date_end is not null then format('al.date_send <= %L', send_date_end) end
- , case when print_date_begin is not null then format('al.date_print >= %L', print_date_begin) end
- , case when print_date_end is not null then format('al.date_print <= %L', print_date_end) end
- , case when cardinality(amed_work_type_name) > 0 then format('v006d.name_type_usl = any(%L)', amed_work_type_name) end);
- _sql text = 'select al.id,
- al.list_number,
- al.list_name,
- al.date_create,
- f003d.region_code::text as region_code,
- f003d.nam_mok::text as medical_organization_name,
- f003d.mcode::text as medical_organization_code,
- al.user_id,
- al.date_upload,
- al.zpmd_id,
- al.has_cover_letter_date,
- al.date_print,
- al.date_send,
- al.date_sign,
- al.old_id::text as old_id,
- al.theme_category_id,
- al.med_work_type_id,
- v006d.name_type_usl as med_work_type_name,
- al.assigned_expert_id,
- al.status,
- al.region
- from expert.acts_list al
- join directory.f003 f003d on f003d.mcode = al.medical_organization_code '|| concat_ws(' and '
- ,case when aregion_code is not null
- then format(' and f003d.region_code = %s', aregion_code) end
- , case when cardinality(amedical_organization_codes) > 0
- then format('f003d.mcode = any(''%s'')', amedical_organization_codes) end) || '
- join directory.v006 v006d on v006d.code_type_usl = al.med_work_type_id';
- begin
- return query execute _sql || (case when _where <> '' then ' where '|| _where else '' end) || _order_by;
- --return _sql || (case when _where <> '' then ' where '|| _where else '' end) || _order_by; -- debug mode
- end
- $$;
- create or replace function expert.build_acts_lists_grid_query(is_count_query boolean, aregion_code integer
- , astatus expert.act_list_status[], aold_id character varying[], amedical_organization_codes character varying[]
- , alist_number character varying[], anid uuid[], auser_id character varying[], aassigned_expert_id bigint[]
- , alist_name character varying, create_date_begin timestamp with time zone, create_date_end timestamp with time zone
- , upload_date_begin timestamp with time zone, upload_date_end timestamp with time zone, sign_date_begin date
- , sign_date_end date, send_date_begin date, send_date_end date, print_date_begin date, print_date_end date
- , amed_work_type_name character varying[], azpmd_id uuid[], apodrid uuid[], athemename character varying[], acategoryname character varying[]
- , aexpertisepurpose text, aexpertisethemetype text, aexpertisetype text, acoverletterdatebegin date, acoverletterdateend date
- , aactstatus expert.act_status[], acountacts integer, aretentionsum numeric, aretentionsummax numeric, aretentionsummin numeric
- , atotalsumfine numeric, atotalsumfinemax numeric, atotalsumfinemin numeric, aexpertisedatebegin date, aexpertisedateend date
- , order_column text, order_direction text, alimit integer, aoffset integer) returns text
- parallel safe
- language plpgsql
- as
- $$
- declare
- _having_filters text = concat_ws(' and '
- , case when acountActs is not null then format('count(distinct act.id) = %L', acountActs) end
- , case when aretentionSum is not null then format('sum(coalesce(sanction.sum_rem, 0.0)) = %L', aretentionSum) end
- , case when aretentionSumMax is not null then format('sum(coalesce(sanction.sum_rem, 0.0)) < %L', aretentionSumMax) end
- , case when aretentionSumMin is not null then format('sum(coalesce(sanction.sum_rem, 0.0)) > %L', aretentionSumMin) end
- , case when atotalSumFine is not null then format('sum(sanction.sum_fine) = %L', atotalSumFine) end
- , case when atotalSumFineMax is not null then format('sum(sanction.sum_fine) < %L', atotalSumFineMax) end
- , case when atotalSumFineMin is not null then format('sum(sanction.sum_fine) > %L', atotalSumFineMin) end
- , case when aexpertiseDateBegin is not null then format('max(re.expertise_date) >= %L', aexpertiseDateBegin) end
- , case when aexpertiseDateEnd is not null then format('max(re.expertise_date) <= %L', aexpertiseDateEnd) end);
- _where_filters text = concat_ws(' and '
- , case when cardinality(athemeName) > 0 then format('theme.name = any(%L)', athemeName) end
- , case when cardinality(acategoryName) > 0 then format('category.name = any(%L)', acategoryName) end
- , case when aexpertisePurpose is not null then format('ecd.expertise_purpose = %L', aexpertisePurpose) end
- , case when aexpertiseThemeType is not null then format('ecd.expertise_theme_type = %L', aexpertiseThemeType) end
- , case when aexpertiseType is not null then format('ecd.expertise_type = %L', aexpertiseType) end
- , case when acoverLetterDateBegin is not null then format('cv.date >= %L', acoverLetterDateBegin) end
- , case when acoverLetterDateEnd is not null then format('cv.date <= %L', acoverLetterDateEnd) end
- , case when cardinality(aactStatus) > 0 then format('act.status = any(%L)', aactStatus) end
- , case when cardinality(apodrId) > 0 then format('zpmd.podr_id = any(%L)', apodrId) end);
- has_filters bool = _where_filters <> '' or _having_filters <> '';
- _select text = 'select al.id,
- al.list_number,
- al.list_name,
- al.medical_organization_name,
- al.med_work_type_name,
- ecd.expertise_purpose::text as expertise_purpose,
- ecd.expertise_type::text as expertise_type,
- ecd.expertise_theme_type::text as expertise_theme_type,
- al.status as status,
- count(distinct act.id) as count_acts,
- concat_ws('' '', users.last_name, users.first_name) as expert_last_name,
- al.date_create,
- al.region_code,
- al.user_id,
- max(theme.name::text) as theme_name,
- max(category.name::text) as category_name,
- al.date_upload,
- al.medical_organization_code,
- sum(coalesce(sanction.sum_rem, 0.0)) as retention_sum,
- al.zpmd_id,
- theme_category.id as list_theme_category_id,
- zpmd.theme_category_id as zpmd_theme_category_id,
- al.has_cover_letter_date,
- cv.number as cover_letter_number,
- cv.date as cover_letter_date,
- max(re.expertise_date) as expertise_date,
- al.date_print,
- al.date_send,
- al.date_sign,
- concat_ws('' ''::text, exp.family_name, exp.first_name, exp.patronymic) as assigned_expert,
- al.assigned_expert_id,
- sum(sanction.sum_fine) as total_sum_fine,
- zpmd.podr_id,
- max(coalesce(podr_omsk.nam_mop, podr_tula.name)::text) as podr_name,
- max(coalesce(podr_omsk.lpu_1::text, podr_tula.podr_id) ::text) as podr_code,
- al.old_id as old_id';
- _inner_joins text = '
- join expert.act act on act.act_list_id = al.id
- join expert.theme_category theme_category on al.theme_category_id = theme_category.id
- join expert.category on category.id = theme_category.category_id
- join expert.expertise_class_decomposed ecd
- on ecd.expertise_class = theme_category.expertise_class
- join zpmd.zpmd on zpmd.id = al.zpmd_id';
- _left_joins text = '
- left join expert.theme on theme_category.theme_id = theme.id
- left join expert.document cv on al.id = cv.act_list_id and cv.type = ''COVER_LETTER''::expert.document_type
- left join expert.expertise_result re
- on act.id = re.act_id and re.status <> ''DELETED''::expert.expertise_result_status
- left join expert.expertise_sanction sanction on re.id = sanction.expertise_result_id and
- (sanction.sum_fine is not null or sanction.sum_rem is not null)
- left join expert.users users on users.id = al.user_id
- left join directory.expert exp on exp.id = al.assigned_expert_id
- left join directory.podr_omsk podr_omsk on al.region = ''OMSK'' and podr_omsk.id = zpmd.podr_id
- left join directory.t_podr_tula podr_tula on al.region = ''TULA'' and podr_tula.id = zpmd.podr_id';
- _from text = format('
- from expert.get_act_lists(%L, %L, %L, %L, %L, %L , %L, %L, %L, %L, %L, %L, %L, %L, %L, %L, %L, %L, %L
- , %L, %L, %L, %L, %L, %L ) al '
- , aregion_code, astatus, aold_id, amedical_organization_codes, alist_number, anid , auser_id
- , aassigned_expert_id, alist_name, create_date_begin, create_date_end, upload_date_begin, upload_date_end
- , sign_date_begin, sign_date_end, send_date_begin, send_date_end, print_date_begin, print_date_end
- , amed_work_type_name, azpmd_id, order_column, order_direction
- , case when not is_count_query and not has_filters then alimit end
- , case when not is_count_query and not has_filters then aoffset end) ||
- _inner_joins ||
- case when is_count_query and not has_filters then '' else _left_joins end;
- _where text = case when _where_filters <> '' then '
- where '|| _where_filters || ' ' else '' end;
- _group_by text = '
- group by al.id,
- al.list_number,
- al.list_name,
- al.date_create,
- al.region_code,
- al.medical_organization_name,
- al.medical_organization_code,
- al.user_id,
- al.date_upload,
- al.zpmd_id,
- al.has_cover_letter_date,
- al.date_print,
- al.date_send,
- al.date_sign,
- al.old_id,
- al.theme_category_id,
- al.med_work_type_name,
- al.assigned_expert_id,
- al.status,
- al.region,
- (ecd.expertise_purpose::text),(ecd.expertise_type::text), (ecd.expertise_theme_type::text),
- concat_ws('' '', users.last_name, users.first_name), zpmd.theme_category_id,
- theme_category.id, cv.number, cv.date, exp.id, zpmd.podr_id';
- _order_by text = ' order by ' || order_column || ' ' || order_direction || ' , id';
- _limit text = case when not is_count_query and has_filters then format(' limit %L offset %L', alimit, aoffset) else '' end;
- _list_query text = _select ||
- _from ||
- _where ||
- _group_by ||
- (case when _having_filters <> '' then ' having ' || _having_filters else '' end) ||
- _order_by || _limit;
- _count_query text = 'select count(distinct al.id) ' ||
- case when _having_filters = '' then '' else '
- from ( select al.id ' end||
- _from ||
- _where ||
- case when _having_filters = '' then '' else _group_by || ' having '|| _having_filters || ') as al' end;
- begin
- return
- case when is_count_query
- then _count_query
- else _list_query
- end;
- end
- $$;-- anid value -> inValue
- drop function if exists expert.build_acts_lists_grid_query(boolean, integer, expert.act_list_status[],
- character varying[], character varying[],
- character varying[], uuid, character varying[], bigint[],
- varchar, timestamp with time zone, timestamp with time zone,
- timestamp with time zone, timestamp with time zone, date,
- date, date, date, date, date, character varying[], uuid[],
- uuid[], character varying[], character varying[], text, text,
- text, date, date, expert.act_status[], integer, numeric,
- numeric, numeric, numeric, numeric, numeric, date, date,
- text, text, integer, integer);
- drop function if exists expert.get_act_lists(integer, expert.act_list_status[], character varying[],
- character varying[], character varying[], uuid, character varying[],
- bigint[], varchar, timestamp with time zone, timestamp with time zone,
- timestamp with time zone, timestamp with time zone, date, date, date, date,
- date, date, character varying[], uuid[], text, text, integer, integer);
- create or replace function expert.get_act_lists(aregion_code integer, astatus expert.act_list_status[]
- , aold_id character varying[], amedical_organization_codes character varying[], alist_number character varying[]
- , anid uuid[], auser_id character varying[], aassigned_expert_id bigint[], alist_name character varying
- , create_date_begin timestamp with time zone, create_date_end timestamp with time zone, upload_date_begin timestamp with time zone
- , upload_date_end timestamp with time zone, sign_date_begin date, sign_date_end date, send_date_begin date, send_date_end date
- , print_date_begin date, print_date_end date, amed_work_type_name character varying[], azpmd_id uuid[]
- , order_column text, order_direction text, alimit integer, aoffset integer)
- returns TABLE(id uuid, list_number character varying, list_name character varying, date_create timestamp with time zone
- , region_code text, medical_organization_name text, medical_organization_code text, user_id character varying
- , date_upload timestamp with time zone, zpmd_id uuid, has_cover_letter_date boolean, date_print date
- , date_send date, date_sign date, old_id text, theme_category_id uuid, med_work_type_id bigint
- , med_work_type_name character varying, assigned_expert_id bigint, status expert.act_list_status
- , region expert.region)
- parallel safe
- language plpgsql
- as
- $$
- declare
- _order_by text = coalesce(case when regexp_split_to_array(order_column, ',') <@ array['region_code','medical_organization_code'
- ,'id','list_number','status', 'old_id','user_id','list_name','date_create','date_upload','date_sign','date_send','date_print', 'med_work_type_name']
- then ' order by ' || order_column || ' ' || order_direction || ' , id '|| format('limit %L offset %L', alimit, aoffset) end, '');
- _where text = concat_ws(' and '
- , case when cardinality(anid) > 0 then format('al.id = any(%L)', anid) end
- , case when cardinality(alist_number) > 0 then format('al.list_number = any(%L)', alist_number) end
- , case when cardinality(astatus) > 0 then format('al.status = any(%L)', astatus) end
- , case when cardinality(aold_id) > 0 then format('al.old_id = any(%L)', aold_id) end
- , case when cardinality(auser_id) > 0 then format('al.user_id = any(%L)', auser_id) end
- , case when cardinality(azpmd_id) > 0 then format('al.zpmd_id = any(%L)', azpmd_id) end
- , case when cardinality(aassigned_expert_id) > 0 then format('al.assigned_expert_id = any(%L)',aassigned_expert_id) end
- , case when alist_name is not null then format('al.list_name like ''%%%s%%''',alist_name) end
- , case when create_date_begin is not null then format('al.date_create >= %L',create_date_begin) end
- , case when create_date_end is not null then format('al.date_create <= %L', create_date_end) end
- , case when upload_date_begin is not null then format('al.date_upload >= %L', upload_date_begin) end
- , case when upload_date_end is not null then format('al.date_upload <= %L', upload_date_end) end
- , case when sign_date_begin is not null then format('al.date_sign >= %L', sign_date_begin) end
- , case when sign_date_end is not null then format('al.date_sign <= %L', sign_date_end) end
- , case when send_date_begin is not null then format('al.date_send >= %L', send_date_begin) end
- , case when send_date_end is not null then format('al.date_send <= %L', send_date_end) end
- , case when print_date_begin is not null then format('al.date_print >= %L', print_date_begin) end
- , case when print_date_end is not null then format('al.date_print <= %L', print_date_end) end
- , case when cardinality(amed_work_type_name) > 0 then format('v006d.name_type_usl = any(%L)', amed_work_type_name) end);
- _sql text = 'select al.id,
- al.list_number,
- al.list_name,
- al.date_create,
- f003d.region_code::text as region_code,
- case
- when al.region = ''TULA''::expert.region then mo_tula.short_name::text
- else f003d.nam_mok::text
- end as medical_organization_name,
- case
- when al.region = ''TULA''::expert.region then mo_tula.mo_code::text
- else f003d.mcode::text
- end as medical_organization_code,
- al.user_id,
- al.date_upload,
- al.zpmd_id,
- al.has_cover_letter_date,
- al.date_print,
- al.date_send,
- al.date_sign,
- al.old_id::text as old_id,
- al.theme_category_id,
- al.med_work_type_id,
- v006d.name_type_usl as med_work_type_name,
- al.assigned_expert_id,
- al.status,
- al.region
- from expert.acts_list al
- join directory.f003 f003d on f003d.mcode = al.medical_organization_code '|| concat_ws(' and '
- ,case when aregion_code is not null
- then format(' and f003d.region_code = %s', aregion_code) end
- , case when cardinality(amedical_organization_codes) > 0
- then format('f003d.mcode = any(''%s'')', amedical_organization_codes) end) || '
- join directory.v006 v006d on v006d.code_type_usl = al.med_work_type_id ' || '
- left join directory.mo_tula mo_tula on mo_tula.mo_code = al.medical_organization_code '|| concat_ws(' and '
- ,case when aregion_code is not null
- then format(' and %s = 71 ', aregion_code) end
- , case when cardinality(amedical_organization_codes) > 0
- then format('mo_tula.mo_code = any(''%s'')', amedical_organization_codes) end);
- begin
- return query execute _sql || (case when _where <> '' then ' where '|| _where else '' end) || _order_by;
- --return _sql || (case when _where <> '' then ' where '|| _where else '' end) || _order_by; -- debug mode
- end
- $$;
- create or replace function expert.build_acts_lists_grid_query(is_count_query boolean, aregion_code integer
- , astatus expert.act_list_status[], aold_id character varying[], amedical_organization_codes character varying[]
- , alist_number character varying[], anid uuid[], auser_id character varying[], aassigned_expert_id bigint[]
- , alist_name character varying, create_date_begin timestamp with time zone, create_date_end timestamp with time zone
- , upload_date_begin timestamp with time zone, upload_date_end timestamp with time zone, sign_date_begin date
- , sign_date_end date, send_date_begin date, send_date_end date, print_date_begin date, print_date_end date
- , amed_work_type_name character varying[], azpmd_id uuid[], apodrid uuid[], athemename character varying[], acategoryname character varying[]
- , aexpertisepurpose text, aexpertisethemetype text, aexpertisetype text, acoverletterdatebegin date, acoverletterdateend date
- , aactstatus expert.act_status[], acountacts integer, aretentionsum numeric, aretentionsummax numeric, aretentionsummin numeric
- , atotalsumfine numeric, atotalsumfinemax numeric, atotalsumfinemin numeric, aexpertisedatebegin date, aexpertisedateend date
- , order_column text, order_direction text, alimit integer, aoffset integer) returns text
- parallel safe
- language plpgsql
- as
- $$
- declare
- _having_filters text = concat_ws(' and '
- , case when acountActs is not null then format('count(distinct act.id) = %L', acountActs) end
- , case when aretentionSum is not null then format('sum(coalesce(sanction.sum_rem, 0.0)) = %L', aretentionSum) end
- , case when aretentionSumMax is not null then format('sum(coalesce(sanction.sum_rem, 0.0)) < %L', aretentionSumMax) end
- , case when aretentionSumMin is not null then format('sum(coalesce(sanction.sum_rem, 0.0)) > %L', aretentionSumMin) end
- , case when atotalSumFine is not null then format('sum(sanction.sum_fine) = %L', atotalSumFine) end
- , case when atotalSumFineMax is not null then format('sum(sanction.sum_fine) < %L', atotalSumFineMax) end
- , case when atotalSumFineMin is not null then format('sum(sanction.sum_fine) > %L', atotalSumFineMin) end
- , case when aexpertiseDateBegin is not null then format('max(re.expertise_date) >= %L', aexpertiseDateBegin) end
- , case when aexpertiseDateEnd is not null then format('max(re.expertise_date) <= %L', aexpertiseDateEnd) end);
- _where_filters text = concat_ws(' and '
- , case when cardinality(athemeName) > 0 then format('theme.name = any(%L)', athemeName) end
- , case when cardinality(acategoryName) > 0 then format('category.name = any(%L)', acategoryName) end
- , case when aexpertisePurpose is not null then format('ecd.expertise_purpose = %L', aexpertisePurpose) end
- , case when aexpertiseThemeType is not null then format('ecd.expertise_theme_type = %L', aexpertiseThemeType) end
- , case when aexpertiseType is not null then format('ecd.expertise_type = %L', aexpertiseType) end
- , case when acoverLetterDateBegin is not null then format('cv.date >= %L', acoverLetterDateBegin) end
- , case when acoverLetterDateEnd is not null then format('cv.date <= %L', acoverLetterDateEnd) end
- , case when cardinality(aactStatus) > 0 then format('act.status = any(%L)', aactStatus) end
- , case when cardinality(apodrId) > 0 then format('zpmd.podr_id = any(%L)', apodrId) end);
- has_filters bool = _where_filters <> '' or _having_filters <> '';
- _select text = 'select al.id,
- al.list_number,
- al.list_name,
- al.medical_organization_name,
- al.med_work_type_name,
- ecd.expertise_purpose::text as expertise_purpose,
- ecd.expertise_type::text as expertise_type,
- ecd.expertise_theme_type::text as expertise_theme_type,
- al.status as status,
- count(distinct act.id) as count_acts,
- concat_ws('' '', users.last_name, users.first_name) as expert_last_name,
- al.date_create,
- al.region_code,
- al.user_id,
- max(theme.name::text) as theme_name,
- max(category.name::text) as category_name,
- al.date_upload,
- al.medical_organization_code,
- sum(coalesce(sanction.sum_rem, 0.0)) as retention_sum,
- al.zpmd_id,
- theme_category.id as list_theme_category_id,
- zpmd.theme_category_id as zpmd_theme_category_id,
- al.has_cover_letter_date,
- cv.number as cover_letter_number,
- cv.date as cover_letter_date,
- max(re.expertise_date) as expertise_date,
- al.date_print,
- al.date_send,
- al.date_sign,
- concat_ws('' ''::text, exp.family_name, exp.first_name, exp.patronymic) as assigned_expert,
- al.assigned_expert_id,
- sum(sanction.sum_fine) as total_sum_fine,
- zpmd.podr_id,
- max(coalesce(podr_omsk.nam_mop, podr_tula.name)::text) as podr_name,
- max(coalesce(podr_omsk.lpu_1::text, podr_tula.podr_id) ::text) as podr_code,
- al.old_id as old_id';
- _inner_joins text = '
- join expert.act act on act.act_list_id = al.id
- join expert.theme_category theme_category on al.theme_category_id = theme_category.id
- join expert.category on category.id = theme_category.category_id
- join expert.expertise_class_decomposed ecd
- on ecd.expertise_class = theme_category.expertise_class
- join zpmd.zpmd on zpmd.id = al.zpmd_id';
- _left_joins text = '
- left join expert.theme on theme_category.theme_id = theme.id
- left join expert.document cv on al.id = cv.act_list_id and cv.type = ''COVER_LETTER''::expert.document_type
- left join expert.expertise_result re
- on act.id = re.act_id and re.status <> ''DELETED''::expert.expertise_result_status
- left join expert.expertise_sanction sanction on re.id = sanction.expertise_result_id and
- (sanction.sum_fine is not null or sanction.sum_rem is not null)
- left join expert.users users on users.id = al.user_id
- left join directory.expert exp on exp.id = al.assigned_expert_id
- left join directory.podr_omsk podr_omsk on al.region = ''OMSK'' and podr_omsk.id = zpmd.podr_id
- left join directory.t_podr_tula podr_tula on al.region = ''TULA'' and podr_tula.id = zpmd.podr_id';
- _from text = format('
- from expert.get_act_lists(%L, %L, %L, %L, %L, %L , %L, %L, %L, %L, %L, %L, %L, %L, %L, %L, %L, %L, %L
- , %L, %L, %L, %L, %L, %L ) al '
- , aregion_code, astatus, aold_id, amedical_organization_codes, alist_number, anid , auser_id
- , aassigned_expert_id, alist_name, create_date_begin, create_date_end, upload_date_begin, upload_date_end
- , sign_date_begin, sign_date_end, send_date_begin, send_date_end, print_date_begin, print_date_end
- , amed_work_type_name, azpmd_id, order_column, order_direction
- , case when not is_count_query and not has_filters then alimit end
- , case when not is_count_query and not has_filters then aoffset end) ||
- _inner_joins ||
- case when is_count_query and not has_filters then '' else _left_joins end;
- _where text = case when _where_filters <> '' then '
- where '|| _where_filters || ' ' else '' end;
- _group_by text = '
- group by al.id,
- al.list_number,
- al.list_name,
- al.date_create,
- al.region_code,
- al.medical_organization_name,
- al.medical_organization_code,
- al.user_id,
- al.date_upload,
- al.zpmd_id,
- al.has_cover_letter_date,
- al.date_print,
- al.date_send,
- al.date_sign,
- al.old_id,
- al.theme_category_id,
- al.med_work_type_name,
- al.assigned_expert_id,
- al.status,
- al.region,
- (ecd.expertise_purpose::text),(ecd.expertise_type::text), (ecd.expertise_theme_type::text),
- concat_ws('' '', users.last_name, users.first_name), zpmd.theme_category_id,
- theme_category.id, cv.number, cv.date, exp.id, zpmd.podr_id';
- _order_by text = ' order by ' || order_column || ' ' || order_direction || ' , id';
- _limit text = case when not is_count_query and has_filters then format(' limit %L offset %L', alimit, aoffset) else '' end;
- _list_query text = _select ||
- _from ||
- _where ||
- _group_by ||
- (case when _having_filters <> '' then ' having ' || _having_filters else '' end) ||
- _order_by || _limit;
- _count_query text = 'select count(distinct al.id) ' ||
- case when _having_filters = '' then '' else '
- from ( select al.id ' end||
- _from ||
- _where ||
- case when _having_filters = '' then '' else _group_by || ' having '|| _having_filters || ') as al' end;
- begin
- return
- case when is_count_query
- then _count_query
- else _list_query
- end;
- end
- $$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement