Advertisement
aliGo

get_act_lists

Mar 27th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. create or replace function expert.get_act_lists(aregion_code integer, astatus expert.act_list_status[]
  2. , aold_id character varying[], amedical_organization_codes character varying[], alist_number character varying[]
  3. , anid uuid[], auser_id character varying[], aassigned_expert_id bigint[], alist_name character varying
  4. , create_date_begin timestamp with time zone, create_date_end timestamp with time zone, upload_date_begin timestamp with time zone
  5. , upload_date_end timestamp with time zone, sign_date_begin date, sign_date_end date, send_date_begin date, send_date_end date
  6. , print_date_begin date, print_date_end date, amed_work_type_name character varying[], azpmd_id uuid[]
  7. , order_column text, order_direction text, alimit integer, aoffset integer)
  8. returns TABLE(id uuid, list_number character varying, list_name character varying, date_create timestamp with time zone
  9. , region_code text, medical_organization_name text, medical_organization_code text, user_id character varying
  10. , date_upload timestamp with time zone, zpmd_id uuid, has_cover_letter_date boolean, date_print date
  11. , date_send date, date_sign date, old_id text, theme_category_id uuid, med_work_type_id bigint
  12. , med_work_type_name character varying, assigned_expert_id bigint, status expert.act_list_status
  13. , region expert.region)
  14. parallel safe
  15. language plpgsql
  16. as
  17. $$
  18. declare
  19. _order_by text = coalesce(case when regexp_split_to_array(order_column, ',') <@ array['region_code','medical_organization_code'
  20. ,'id','list_number','status', 'old_id','user_id','list_name','date_create','date_upload','date_sign','date_send','date_print', 'med_work_type_name']
  21. then ' order by ' || order_column || ' ' || order_direction || ' , id '|| format('limit %L offset %L', alimit, aoffset) end, '');
  22. _where text = concat_ws(' and '
  23. , case when cardinality(anid) > 0 then format('al.id = any(%L)', anid) end
  24. , case when cardinality(alist_number) > 0 then format('al.list_number = any(%L)', alist_number) end
  25. , case when cardinality(astatus) > 0 then format('al.status = any(%L)', astatus) end
  26. , case when cardinality(aold_id) > 0 then format('al.old_id = any(%L)', aold_id) end
  27. , case when cardinality(auser_id) > 0 then format('al.user_id = any(%L)', auser_id) end
  28. , case when cardinality(azpmd_id) > 0 then format('al.zpmd_id = any(%L)', azpmd_id) end
  29. , case when cardinality(aassigned_expert_id) > 0 then format('al.assigned_expert_id = any(%L)',aassigned_expert_id) end
  30. , case when alist_name is not null then format('al.list_name like ''%%%s%%''',alist_name) end
  31. , case when create_date_begin is not null then format('al.date_create >= %L',create_date_begin) end
  32. , case when create_date_end is not null then format('al.date_create <= %L', create_date_end) end
  33. , case when upload_date_begin is not null then format('al.date_upload >= %L', upload_date_begin) end
  34. , case when upload_date_end is not null then format('al.date_upload <= %L', upload_date_end) end
  35. , case when sign_date_begin is not null then format('al.date_sign >= %L', sign_date_begin) end
  36. , case when sign_date_end is not null then format('al.date_sign <= %L', sign_date_end) end
  37. , case when send_date_begin is not null then format('al.date_send >= %L', send_date_begin) end
  38. , case when send_date_end is not null then format('al.date_send <= %L', send_date_end) end
  39. , case when print_date_begin is not null then format('al.date_print >= %L', print_date_begin) end
  40. , case when print_date_end is not null then format('al.date_print <= %L', print_date_end) end
  41. , case when cardinality(amed_work_type_name) > 0 then format('v006d.name_type_usl = any(%L)', amed_work_type_name) end);
  42. _sql text = 'select al.id,
  43. al.list_number,
  44. al.list_name,
  45. al.date_create,
  46. f003d.region_code::text as region_code,
  47. case
  48. when f003d.region_code = ''TULA''::expert.region then mo_tula.short_name::text
  49. else f003d.nam_mok::text
  50. end as medical_organization_name,
  51. case
  52. when f003d.region_code = ''TULA''::expert.region then mo_tula.mo_code::text
  53. else f003d.mcode::text
  54. end as medical_organization_code,
  55. al.user_id,
  56. al.date_upload,
  57. al.zpmd_id,
  58. al.has_cover_letter_date,
  59. al.date_print,
  60. al.date_send,
  61. al.date_sign,
  62. al.old_id::text as old_id,
  63. al.theme_category_id,
  64. al.med_work_type_id,
  65. v006d.name_type_usl as med_work_type_name,
  66. al.assigned_expert_id,
  67. al.status,
  68. al.region
  69. from expert.acts_list al
  70. join directory.f003 f003d on f003d.mcode = al.medical_organization_code '|| concat_ws(' and '
  71. , case when aregion_code is not null
  72. then concat(format(' and f003d.region_code = %s ', aregion_code),
  73. case when aregion_code = 71
  74. then 'join directory.mo_tula mo_tula on mo_tula.mo_code = al.medical_organization_code ' end)
  75. end
  76. , case when cardinality(amedical_organization_codes) > 0 then
  77. case when aregion_code = 71
  78. then format('mo_tula.mo_code = any(''%s'')', amedical_organization_codes)
  79. else format('f003d.mcode = any(''%s'')', amedical_organization_codes) end end) || '
  80. join directory.v006 v006d on v006d.code_type_usl = al.med_work_type_id';
  81. begin
  82. return query execute _sql || (case when _where <> '' then ' where '|| _where else '' end) || _order_by;
  83. --return _sql || (case when _where <> '' then ' where '|| _where else '' end) || _order_by; -- debug mode
  84. end
  85. $$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement