我在postgres的下面功能中遇到了问题,我试过通过查看这样的许多例子,已经被许多其他人上传但我仍然找到解决方案。请帮帮我。
我知道下面的功能很长。谢谢
CREATE OR REPLACE FUNCTION adt_transfer_patient(in_hospital_id integer, in_reg_no character varying(50), in_admt_id numeric, in_transfer_type character varying(25), in_release_cause character varying(1), in_user_id character varying(50), in_new_dept_id integer, in_new_unit_id integer, in_new_ward_id integer, in_consultant integer, in_icd character varying(50), in_icd_desc character varying(50))
RETURNS integer AS
$BODY$
DECLARE
temp_dept_id integer;
temp_unit_id integer;
temp_ward_id integer;
return_int integer;
temp_bed_id integer;
temp_present_position character varying;
temp_is_blocked integer;
max_diagnos_id integer;
tmp_bedno text;
tmp_ward text;
temp_node_ward_id integer;
tmp_tot_record integer;
tmp_int_var integer;
icdcode character varying(25);
disease_remark character varying(200);
BEGIN
temp_is_blocked=0;
--Present Department,Unit,Ward and Bed
SELECT dept_id INTO temp_dept_id FROM department_admission WHERE hospital_id=in_hospital_id AND ipd_admission_id=in_admt_id AND release_date IS NULL ORDER BY admission_date DESC limit 1;
SELECT unit_id INTO temp_unit_id FROM unit_admission WHERE hospital_id=in_hospital_id AND ipd_admission_id=in_admt_id AND release_date IS NULL;
SELECT ward_id INTO temp_ward_id FROM ward_admission WHERE hospital_id=in_hospital_id AND ipd_admission_id=in_admt_id AND release_date IS NULL
ORDER BY admission_date DESC limit 1;
SELECT COALESCE(bedid,'0') INTO temp_bed_id FROM bed_occupancy WHERE hospital_id=in_hospital_id AND ipd_admission_id=in_admt_id AND releasedate IS NULL;
--Tranfer Department
IF in_transfer_type = 'D' THEN
UPDATE department_admission SET release_date=current_timestamp,release_cause=in_release_cause,userid=in_user_id
WHERE reg_no=in_reg_no AND ipd_admission_id=in_admt_id AND hospital_id=in_hospital_id AND release_date IS NULL;
INSERT INTO department_admission(hospital_id,reg_no,ipd_admission_id,dept_id,admission_date,admission_time,userid)
VALUES (in_hospital_id,in_reg_no,in_admt_id,in_new_dept_id,current_timestamp,current_time,in_user_id);
END IF;
--Transfer Unit
IF in_transfer_type = 'D' OR in_transfer_type = 'U' THEN
UPDATE unit_admission SET release_date=current_timestamp,release_cause=in_release_cause,userid=in_user_id
WHERE reg_no=in_reg_no AND ipd_admission_id=in_admt_id AND hospital_id=in_hospital_id AND release_date IS NULL;
INSERT INTO unit_admission(hospital_id,reg_no,ipd_admission_id,unit_id,admission_date,admission_time,userid)
VALUES (in_hospital_id,in_reg_no,in_admt_id,in_new_unit_id,current_timestamp,current_time,in_user_id);
END IF;
--Transfer Ward
IF in_transfer_type = 'D' OR in_transfer_type = 'U' OR in_transfer_type = 'W' THEN
UPDATE ward_admission SET release_date=current_timestamp,release_cause=in_release_cause,userid=in_user_id
WHERE reg_no=in_reg_no AND ipd_admission_id=in_admt_id AND hospital_id=in_hospital_id AND release_date IS NULL;
IF in_transfer_type = 'W' THEN
INSERT INTO ward_admission(hospital_id,reg_no,ipd_admission_id,ward_id,admission_date,admission_time,userid,dept_id,unit_id)
VALUES (in_hospital_id,in_reg_no,in_admt_id,in_new_ward_id,current_timestamp,current_time,in_user_id,temp_dept_id,temp_unit_id);
ELSIF in_transfer_type = 'U' THEN
INSERT INTO ward_admission(hospital_id,reg_no,ipd_admission_id,ward_id,admission_date,admission_time,userid,dept_id,unit_id)
VALUES (in_hospital_id,in_reg_no,in_admt_id,in_new_ward_id,current_timestamp,current_time,in_user_id,temp_dept_id,in_new_unit_id);
ELSIF in_transfer_type = 'D' THEN
INSERT INTO ward_admission(hospital_id,reg_no,ipd_admission_id,ward_id,admission_date,admission_time,userid,dept_id,unit_id)
VALUES (in_hospital_id,in_reg_no,in_admt_id,in_new_ward_id,current_timestamp,current_time,in_user_id,in_new_dept_id,in_new_unit_id);
END IF;
END IF;
--Tranfer Bed
UPDATE bed_occupancy SET releasedate=current_timestamp,userid=in_user_id
WHERE reg_no=in_reg_no AND ipd_admission_id=in_admt_id AND hospital_id=in_hospital_id AND releasedate IS NULL;
--SELECT COUNT(*) into temp_is_blocked FROM mas_link_ward,ward_admission where mas_link_ward.parent_ward_id=ward_admission.ward_id
--and mas_link_ward.node_ward_id=temp_ward_id and ward_admission.release_date is null and abs(extract(epoch from current_timestamp - admission_date)/20000)>48;
-- SELECT COUNT(*) into temp_is_blocked FROM mas_link_ward,ward_admission where mas_link_ward.node_ward_id=ward_admission.ward_id and mas_link_ward.parent_ward_id=temp_ward_id and mas_link_ward.parent_dept_id=temp_dept_id and ward_admission.release_date is null and abs(extract(epoch from current_timestamp - admission_date)/20000)>48;
--SELECT node_ward_id into temp_node_ward_id FROM mas_link_ward,ward_admission where mas_link_ward.node_ward_id=ward_admission.ward_id and mas_link_ward.parent_ward_id=temp_ward_id and mas_link_ward.parent_dept_id=temp_dept_id and ward_admission.release_date is null and abs(extract(epoch from current_timestamp - admission_date)/20000)>48;
if temp_is_blocked is null then
temp_is_blocked=0;
end if;
IF temp_is_blocked>0 THEN
UPDATE mas_bed SET status='B' WHERE bedid=temp_bed_id AND hospital_id=in_hospital_id;
IF in_hospital_id=4 THEN
--*** insert record into notification table ***
select bedno into tmp_bedno from mas_bed where bedid=temp_bed_id;
select ward_name into tmp_ward from mas_ward where ward_id=temp_node_ward_id;
INSERT INTO table_notification(sl_no, notification, group_type, ward_id, dept_id, unit_id, employee_id,seen)
SELECT (SELECT COALESCE(max(sl_no),0) FROM table_notification)+row_number() OVER(),'Bed No'||tmp_bedno||' of ward '||tmp_ward||' is blocked','W',temp_node_ward_id,0,0,uid,0 FROM config.user_ward_mapping
WHERE ward_id=temp_node_ward_id;
END IF;
ELSE
UPDATE mas_bed SET status='A' WHERE bedid=temp_bed_id AND hospital_id=in_hospital_id;
END IF;
IF in_new_bed_id != 0 THEN
INSERT INTO bed_occupancy(hospital_id,reg_no,ipd_admission_id,bedid,occupancydate,occupied,userid,entrytime)
VALUES (in_hospital_id,in_reg_no,in_admt_id,in_new_bed_id,current_timestamp,'O',in_user_id,current_time);
UPDATE mas_bed SET status='O' WHERE bedid=in_new_bed_id AND hospital_id=in_hospital_id;
-- Set present Position
temp_present_position='~IPD^'||get_department_name(in_hospital_id,in_new_dept_id)||'^'||get_unit_name(in_hospital_id,in_new_unit_id)||'^'||get_ward_name(in_hospital_id,in_new_ward_id)||'^'||get_bed_no(in_hospital_id,in_new_bed_id);
END IF;
IF in_new_bed_id = 0 THEN
INSERT INTO bed_occupancy(hospital_id,reg_no,ipd_admission_id,bedid,occupancydate,occupied,userid,entrytime,transfer_to_floor)
VALUES (in_hospital_id,in_reg_no,in_admt_id,-1,current_timestamp,'O',in_user_id,current_time,'Y');
--UPDATE bed_occupancy SET transfer_to_floor='Y' WHERE reg_no=in_reg_no AND ipd_admission_id=in_admt_id AND hospital_id=in_hospital_id;
END IF;
--Entry in Patient transfer log
--NOTE: Valid Entry Against Trasnfer Type only
INSERT INTO patient_transfer_log(hospital_id, reg_no, ipd_admission_id, transfer_type, transfer_date,transfer_time, from_department_id, to_department_id, from_unit_id,
to_unit_id, from_ward_id, to_ward_id, from_bed_id, to_bed_id,consultant_id)
VALUES (in_hospital_id,in_reg_no,in_admt_id,in_transfer_type,current_timestamp,current_time,temp_dept_id,in_new_dept_id,temp_unit_id,
in_new_unit_id,temp_ward_id,in_new_ward_id,temp_bed_id,in_new_bed_id,in_consultant);
IF in_transfer_type = 'D' THEN
-- Set present Position
temp_present_position='~IPD^'||get_department_name(in_hospital_id,in_new_dept_id)||'^'||get_unit_name(in_hospital_id,in_new_unit_id)||'^'||get_ward_name(in_hospital_id,in_new_ward_id)||'^'||get_bed_no(in_hospital_id,in_new_bed_id);
ELSIF in_transfer_type = 'U' THEN
-- Set present Position
temp_present_position='~IPD^'||get_department_name(in_hospital_id,temp_dept_id)||'^'||get_unit_name(in_hospital_id,in_new_unit_id)||'^'||get_ward_name(in_hospital_id,in_new_ward_id)||'^'||get_bed_no(in_hospital_id,in_new_bed_id);
ELSIF in_transfer_type = 'W' THEN
-- Set present Position
temp_present_position='~IPD^'||get_department_name(in_hospital_id,temp_dept_id)||'^'||get_unit_name(in_hospital_id,temp_unit_id)||'^'||get_ward_name(in_hospital_id,in_new_ward_id)||'^'||get_bed_no(in_hospital_id,in_new_bed_id);
ELSIF in_transfer_type = 'B' THEN
-- Set present Position
temp_present_position='~IPD^'||get_department_name(in_hospital_id,temp_dept_id)||'^'||get_unit_name(in_hospital_id,temp_unit_id)||'^'||get_ward_name(in_hospital_id,temp_ward_id)||'^'||get_bed_no(in_hospital_id,in_new_bed_id);
END IF;
UPDATE patient_detail SET present_position=COALESCE(present_position,' ^ ^ ^ ^')||temp_present_position,ward_id=in_new_ward_id,ward_entry_date=current_timestamp,bedid=in_new_bed_id,occupancydate=current_timestamp
WHERE reg_no=in_reg_no AND
hospital_id=in_hospital_id AND ipd_admission_id=in_admt_id;
IF in_hospital_id=4 and in_icd<>'' THEN
select count(*) into tmp_tot_record from (select unnest(string_to_array(in_icd, ',')))tbl;
tmp_int_var = 1;
WHILE tmp_int_var <= tmp_tot_record LOOP
SELECT split_part(in_icd, ',', tmp_int_var) INTO icdcode;
SELECT split_part(in_icd_desc, ',', tmp_int_var) INTO disease_remark;
IF icdcode IS NOT NULL THEN
Select coalesce(max(slno),0)+1 into max_diagnos_id FROM tran_diagnosis_log where reg_no=in_reg_no;
INSERT INTO tran_diagnosis(
reg_no, slno,icd_o,remarks,
provisional_final, doctor_id, hospital_id, ipd_admission_no, entry_source,entry_date)
VALUES (in_reg_no, max_diagnos_id,icdcode,disease_remark,'Final', in_user_id::integer, in_hospital_id, in_admt_id,'TRANSFER',current_timestamp);
INSERT INTO tran_diagnosis_log(
reg_no, slno,icd_o,remarks,
provisional_final, doctor_id, hospital_id, ipd_admission_no, entry_source,entry_date)
VALUES (in_reg_no, max_diagnos_id,icdcode,disease_remark,'Final', in_user_id::integer, in_hospital_id, in_admt_id,'TRANSFER',current_timestamp);
tmp_int_var = tmp_int_var+1;
END IF;
END LOOP;
/**--update ipd_patient_admission_detail set disease=in_pro_diagnos where hospital_id=in_hospital_id and reg_no=in_reg_no and ipd_admission_id=in_admt_id;
Select coalesce(max(slno),0)+1 into max_diagnos_id FROM tran_diagnosis_log where reg_no=in_reg_no;
if max_diagnos_id=1 THEN
INSERT INTO tran_diagnosis(
reg_no, slno,
provisional_final, doctor_id, remarks, hospital_id, ipd_admission_no, entry_source)
VALUES (in_reg_no, max_diagnos_id,'Provisional', in_user_id::integer, in_pro_diagnos, in_hospital_id, in_admt_id,'TRANSFER');
INSERT INTO tran_diagnosis_log(
reg_no, slno,
provisional_final, doctor_id, remarks, hospital_id, ipd_admission_no, entry_source)
VALUES (in_reg_no, max_diagnos_id,'Provisional', in_user_id::integer, in_pro_diagnos, in_hospital_id, in_admt_id,'TRANSFER');
END IF;
if max_diagnos_id>1 THEN
INSERT INTO tran_diagnosis_log(
reg_no, slno,
provisional_final, doctor_id, remarks, hospital_id, ipd_admission_no, entry_source)
VALUES (in_reg_no, max_diagnos_id,'Provisional', in_user_id::integer, in_pro_diagnos, in_hospital_id, in_admt_id,'TRANSFER');
END IF; **/
END IF;
RETURN 1;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;