Advertisement
Guest User

Untitled

a guest
May 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION "public"."searchcases8"(_caseid varchar, caseuid varchar, patientuid varchar, lastname varchar, firstname varchar, middlename varchar, birthdate varchar, snils varchar, polis varchar, medicalorganizationogrn varchar, casetypeid varchar, initgoalid varchar, doctorlastname varchar, doctorfirstname varchar, doctormiddlename varchar, doctorsnils varchar, tabelnumber varchar, positionid varchar, positionname varchar, diagnosmkb varchar, datestepfrom varchar, datestepto varchar, createddatefrom varchar, _createddateto varchar)
  2. RETURNS SETOF "pg_catalog"."int4" AS $BODY$
  3. declare
  4. caseid integer;
  5. caseuid character varying;
  6. patientUid character varying;
  7. lastName character varying;
  8. firstName character varying;
  9. middleName character varying;
  10. birthDate date;
  11. snils character varying;
  12. polis character varying;
  13. medicalOrganizationOgrn character varying;
  14. caseTypeId integer;
  15. initGoalId integer;
  16. doctorlastName character varying;
  17. doctorfirstName character varying;
  18. doctormiddleName character varying;
  19. doctorsnils character varying;
  20. TabelNumber character varying;
  21. positionId integer;
  22. positionName character varying;
  23. diagnosMKB character varying;
  24. dateStepFrom date;
  25. dateStepTo date;
  26. createdDateFrom date;
  27. createdDateTo date;
  28.  
  29. patientId integer;
  30.  
  31. begin
  32. if $1 is not null and $1 <> '' then caseid := $1::integer; end if;
  33. caseuid := $2;
  34.  
  35. patientUid := $3;
  36. lastName := $4;
  37. firstName := $5;
  38. middleName := $6;
  39. if $7 is not null and $7 <> '' then birthDate := $7::date; else birthDate := null; end if;
  40. snils := $8;
  41. polis := $9;
  42. medicalOrganizationOgrn :=$10;
  43. if $11 is not null and $11 <> '' then caseTypeId := $11::integer; end if;
  44. if $12 is not null and $12 <> '' then initGoalId := $12::integer; end if;
  45. doctorlastName := $13;
  46. doctorfirstName := $14;
  47. doctormiddleName := $15;
  48. doctorsnils := $16;
  49. TabelNumber := $17;
  50. if $18 is not null and $18 <> '' then positionId := $17::integer; end if;
  51. positionName := $19;
  52. diagnosMKB := $20;
  53. if $21 is not null and $21 <> '' then dateStepFrom := $21::date; else dateStepFrom := (SELECT '-infinity'::date); end if;
  54. if $22 is not null and $22 <> '' then dateStepTo := $22::date; else dateStepTo := (SELECT 'infinity'::date); end if;
  55. if $23 is not null and $23 <> '' then createdDateFrom := $23::date; else createdDateFrom := (SELECT '-infinity'::date); end if;
  56. if $24 is not null and $24 <> '' then createdDateTo := $24::date; else createdDateTo := (SELECT 'infinity'::date); end if;
  57.  
  58.  
  59. if caseid is not null
  60. THEN RETURN QUERY select caseid;
  61. ELSE
  62.  
  63. if caseuid is not null
  64. THEN RETURN QUERY select id from mc_case where uid = caseuid;
  65.  
  66. ELSE
  67.  
  68. if snils is not null
  69. then patientId := (select indiv_id from pim_individual_doc where type_id = 19 and number = snils limit 1);
  70. end if;
  71.  
  72. if polis is not null
  73. then patientId := (select indiv_id from pim_individual_doc where type_id = 26 and number = polis limit 1);
  74. end if;
  75.  
  76. if patientUid is not null and patientId is null
  77. then patientId := (select indiv_id from pim_indiv_code where type_id = 8 and code = patientUid limit 1);
  78. end if;
  79.  
  80. if patientId is null
  81. then patientId:= (select id from pim_individual where surname like coalesce (lastName, '%') and name like coalesce (firstName, '%') and patr_name like coalesce (middleName, '%') and birth_dt::text like coalesce (birthDate::text, '%') limit 1 );
  82. end if;
  83.  
  84. if patientId is not null then
  85.  
  86. RETURN QUERY select distinct (mc.id) from mc_case mc
  87. join mc_step ms on ms.case_id = mc.id
  88. where mc.case_type_id = caseTypeId and mc.patient_id = patientId and mc.init_goal_id::text like coalesce (initGoalId::text, '%%') and ms.id in
  89. (select steps_by_res_group_search3 (medicalOrganizationOgrn, doctorlastName, doctorfirstName, doctormiddleName, doctorsnils, TabelNumber, positionId, positionName, diagnosMKB, dateStepFrom, dateStepTo, patientId))
  90. and mc.open_date >= coalesce (createdDateFrom, '-infinity'::date) and mc.open_date <= coalesce (createdDateTo, 'infinity'::date);
  91.  
  92. else RETURN QUERY select distinct (mc.id) from mc_case mc
  93. join mc_step ms on ms.case_id = mc.id
  94. where mc.case_type_id = caseTypeId and mc.init_goal_id::text like coalesce (initGoalId::text, '%%') and ms.id in
  95. (select steps_by_res_group_search3_without_patient (medicalOrganizationOgrn, doctorlastName, doctorfirstName, doctormiddleName, doctorsnils, TabelNumber, positionId, positionName, diagnosMKB, dateStepFrom, dateStepTo))
  96. and mc.open_date >= coalesce (createdDateFrom, '-infinity'::date) and mc.open_date <= coalesce (createdDateTo, 'infinity'::date);
  97.  
  98. end if;
  99. END IF;
  100. END if;
  101. END;
  102. $BODY$
  103. LANGUAGE 'plpgsql' VOLATILE COST 100
  104. ROWS 1000
  105. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement