
                CREATE OR REPLACE FUNCTION temp_iCheckConflictsPlanDisDT(
                    INOUT isConflict int DEFAULT 1::int,
                    INOUT conflictDate date DEFAULT NULL::date,
                    INOUT totalCount int DEFAULT 0::int,
                    IN begDate date DEFAULT null::date,
                    IN endDate date DEFAULT null::date,
                    IN LpuSection_id bigint DEFAULT NULL::int,
                    IN Bed_id bigint DEFAULT NULL::int,
                    IN EvnSection_id bigint DEFAULT NULL::int
                )returns record
                    language plpgsql
                as
                $$
                declare
                    iLpuSection_id bigint :=LpuSection_id;
                    iBedState_id bigint :=null;
                    iBed_id bigint :=Bed_id;
                    iEvnSection_id bigint :=EvnSection_id;
                begin
                    IF iBed_id is not null or iEvnSection_id is not null THEN
                        select bs.Bed_id, bs.BedState_id
                        into iBed_id, iBedState_id
                        from v_BedState bs
                        where
                            (bs.EvnSection_id = iEvnSection_id or bs.Bed_id = iBed_id)
                          and (bs.BedState_begDate is null or cast(bs.BedState_begDate as date) <= begDate)
                          and (bs.BedState_endDate is null or cast(bs.BedState_endDate as date) > begDate);
                    END IF;

                    WHILE begDate <= endDate and isConflict > 0 loop
                            select sum(LpuSectionBedState_Plan) into totalCount
                            from v_LpuSectionBedState LSBS
                                where cast(LSBS.LpuSectionBedState_begDate as date) <= begDate
                                    and (LSBS.LpuSectionBedState_endDate is null or cast(LSBS.LpuSectionBedState_endDate as date) > begDate)
                                    and LSBS.LpuSection_id = iLpuSection_id;

                            select totalCount - count(bs.Bed_id) into isConflict
                            from v_Bed b
                                 inner join v_BedProperties bp
                                        on bp.Bed_id = b.Bed_id
                                            and cast (bp.BedProperties_begDate as date) <= begDate
                                            and (bp.BedProperties_endDate is null or cast(bp.BedProperties_endDate as date ) > begDate)
                                 inner join v_LpuSectionBedState lsbs
                                        on lsbs.LpuSectionBedState_id = bp.LpuSectionBedState_id
                                            and cast (lsbs.LpuSectionBedState_begDate as date) <= begDate
                                            and (lsbs.LpuSectionBedState_endDate is null
                                                or cast (lsbs.LpuSectionBedState_endDate as date) > begDate)

                                 left join v_BedState bs
                                       on b.Bed_id = bs.Bed_id
                                           and (bs.BedState_begDate is null or cast(bs.BedState_begDate as date) <= begDate)
                                           and (bs.BedState_endDate is NULL OR cast(bs.BedState_endDate as date) > begDate)
                            where b.LpuSection_id = iLpuSection_id
                              and (b.Bed_begDate is null or cast(b.Bed_begDate as date) <= begDate)
                              and (b.Bed_endDate is null or cast(b.Bed_endDate as date) > begDate)
                              and bs.Bed_id is not null
                              and (iBedState_id is null or bs.BedState_id != iBedState_id);
                            begDate := begDate + INTERVAL '1 day';
                    end loop;
                    conflictDate := begDate - INTERVAL '1 day';
                end;
                $$;

                select
                    isConflict as "isConflict",
                    to_char(conflictDate, 'YYYY-MM-DD HH24:MI:SS') as "conflictDate",
                    totalCount as "totalCount"
                 from temp_iCheckConflictsPlanDisDT(
                        begDate := '2022-03-21',
                        endDate := '2022-04-04',
                        LpuSection_id := '99560025567'
                    );
        
