package thyyxxk.webserver.dao.his.inpatient.casefrontsheet;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.casefrontsheet.response.DismissCount;
import thyyxxk.webserver.entity.casefrontsheet.SheetQualitySources;
import thyyxxk.webserver.entity.casefrontsheet.TempDismissCount;

import java.util.List;

@Mapper
public interface CaseFrontSheetAnalyzeDao {

    @Select("select rtrim(zk_ward) as deptCode,isnull(file_status,0) as fileStatus, count(1) as dismissCount " +
            "from zy_inactpatient with(nolock) where dis_date>=#{start} and dis_date<=#{end} group by zk_ward, file_status")
    List<TempDismissCount> selectDismissCount(@Param("start") String start,
                                              @Param("end") String end);

    @Select("select isnull(sign_staff,'NONE') as sign_staff, count(1) as dismissCount from t_case_frontsheet_main " +
            "with(nolock) where dismiss_date>=#{start} and dismiss_date<=#{end} " +
            "group by sign_staff")
    List<TempDismissCount> selectDismissCount2(@Param("start") String start,
                                              @Param("end") String end);

    @Select("select a.bah as patNo,a.admiss_times as times,a.sign_staff, " +
            "a.name as name,gender=(case when a.sex='1' then N'男' else N'女' end), " +
            "a.age,a.admiss_date,a.dismiss_date as disDate, " +
            "inHospDays=admiss_days,a.total_cost as total_charge, " +
            "a.trans_dept as disDept,a.clinic_diag_str as admDiag, " +
            "disDiag=(select t.name from t_case_frontsheet_disdiag t where t.bah=a.bah " +
            "and t.times=a.admiss_times and t.no=1), " +
            "blfx=a.case_classification,admiss_dept,a.trans_dept as zkWard, " +
            "a.dept_leader_name as kzy,a.main_doctor_name as zzys, " +
            "a.admiss_doctor_name as zyys,fdcrb=a.statutory_epidemic " +
            "from t_case_frontsheet_main a where a.trans_dept_code like #{dept} and " +
            "a.dismiss_date>=#{begntime} and a.dismiss_date<=#{endtime} " +
            "order by a.trans_dept_code ")
    List<DismissCount> selectDismissDetailCount(@Param("begntime") String begntime,
                                                @Param("endtime") String endtime,
                                                @Param("dept") String dept);


    @Select("select rtrim(a.inpatient_no) as patNo,a.admiss_times as times, " +
            "rtrim(a.name) as name,gender=(case when a.sex='1' then N'男' else N'女' end),  " +
            "age=datediff(year,b.birth_date,getdate()),a.admiss_date,d.start_time as dis_date, " +
            "inHospDays=datediff(day, a.admiss_date, d.start_time),a.total_charge, " +
            "rtrim(a.zk_ward) as disDept,rtrim(a.clinic_diag_str) as admDiag,  " +
            "disDiag=(select dis_diag_comment from ba_first_page1 t where t.inpatient_no=a.inpatient_no  " +
            "and t.admiss_times=a.admiss_times and t.dis_diag_no=1),  " +
            "blfx=isnull(c.blfx,''),rtrim(a.small_dept) as admissDept,rtrim(a.zk_ward) as zkWard,  " +
            "c.kzr,c.zzys,c.zyys,fdcrb=isnull(c.fdcrb,'') " +
            "from zy_inactpatient a, a_patient_mi b, batj_ba2 c, " +
            "(select inpatient_no,admiss_times,start_time from yz_act_order " +
            "where start_time>=#{begntime} and start_time<=#{endtime} " +
            "and order_code in (select order_code from yz_zd_order_item_confirm where item_name=N'死亡') " +
            "and group_no='00' and exec_unit like #{dept} " +
            "union " +
            "select inpatient_no,admiss_times,start_time from yz_inact_order " +
            "where start_time>=#{begntime} and start_time<=#{endtime} " +
            "and order_code in (select order_code from yz_zd_order_item_confirm where item_name=N'死亡') " +
            "and group_no='00' and exec_unit like #{dept}) d " +
            "where a.inpatient_no=b.inpatient_no and a.inpatient_no=c.zyh and " +
            "      a.inpatient_no=d.inpatient_no and a.admiss_times=c.zycs and " +
            "      a.admiss_times=d.admiss_times " +
            "union " +
            "select rtrim(a.inpatient_no) as patNo,a.admiss_times as times, " +
            "rtrim(a.name) as name,gender=(case when a.sex='1' then N'男' else N'女' end), " +
            "age=datediff(year,b.birth_date,getdate()),a.admiss_date,d.start_time as dis_date, " +
            "inHospDays=datediff(day, a.admiss_date, d.start_time),a.total_charge, " +
            "rtrim(a.zk_ward) as disDept,rtrim(a.clinic_diag_str) as admDiag, " +
            "disDiag=(select dis_diag_comment from ba_first_page1 t where t.inpatient_no=a.inpatient_no " +
            "and t.admiss_times=a.admiss_times and t.dis_diag_no=1), " +
            "blfx=isnull(c.blfx,''),rtrim(a.small_dept) as admissDept,rtrim(a.zk_ward) as zkWard, " +
            "c.kzr,c.zzys,c.zyys,fdcrb=isnull(c.fdcrb,'') " +
            "from zy_actpatient a, a_patient_mi b, batj_ba2 c, " +
            "(select inpatient_no,admiss_times,start_time from yz_act_order " +
            "where start_time>=#{begntime} and start_time<=#{endtime} " +
            "and order_code in (select order_code from yz_zd_order_item_confirm where item_name=N'死亡') " +
            "and group_no='00' and exec_unit like #{dept} union " +
            "select inpatient_no,admiss_times,start_time from yz_inact_order " +
            "where start_time>=#{begntime} and start_time<=#{endtime} " +
            "and order_code in (select order_code from yz_zd_order_item_confirm where item_name=N'死亡') " +
            "and group_no='00' and exec_unit like #{dept}) d " +
            "where a.inpatient_no=b.inpatient_no and a.inpatient_no=c.zyh and " +
            "      a.inpatient_no=d.inpatient_no and a.admiss_times=c.zycs and " +
            "      a.admiss_times=d.admiss_times ")
    List<DismissCount> selectDeathCount(@Param("begntime") String begntime,
                                        @Param("endtime") String endtime,
                                        @Param("dept") String dept);


    @Select("select diag_conform2,diag_conform3,diag_conform4,rescue_times,rescue_success_times, " +
            "quality_control_level,admiss_days,${deptIdentity}_code as deptCode,${deptIdentity} as deptName " +
            "from t_case_frontsheet_main where dismiss_date>=#{begndate} and dismiss_date<=#{enddate}")
    List<SheetQualitySources> selectSheetQualitySource(@Param("begndate") String begndate,
                                                       @Param("enddate") String enddate,
                                                       @Param("deptIdentity") String deptIdentity);

    @Select("select heal,deptCode=(select rtrim(dept_code) from a_employee_mi where code=operator) " +
            "from t_case_frontsheet_surgery where date>=#{begndate} and date<=#{enddate} and operator is not null")
    List<SheetQualitySources> selectSheetQualitySurgeries(@Param("begndate") String begndate,
                                                          @Param("enddate") String enddate);

    @Select("select isnull(openbed_num,0) from zy_adtward where dept_code=#{deptCode}")
    Integer selectOpenbedNum(@Param("deptCode") String deptCode);
}
