package thyyxxk.webserver.dao.his.dashboard;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.dashboard.InpatientBrief;

import java.util.List;

@Mapper
public interface DashboardDao {

    @Select("select                 rtrim(bed_no)       as bedNo,\n" +
            "                       rtrim(name)         as name,\n" +
            "                       sex                 as gender,\n" +
            "                       rtrim(inpatient_no) as patNo,\n" +
            "    birthDate=(select t.birth_date from a_patient_mi t where t.inpatient_no = a.inpatient_no),\n" +
            "                       admiss_date,\n" +
            "    convertAdmissDate=admiss_date,\n" +
            "    physician=(select rtrim(t.name) from a_employee_mi t where t.code = a.refer_physician),\n" +
            "                       a.med_type,\n" +
            "    surgery=(select top 1 rtrim(op_scale) + N'级'\n" +
            "             from op_record t\n" +
            "             where t.inpatient_no = a.inpatient_no\n" +
            "               and t.admiss_times = a.admiss_times\n" +
            "               and t.status != 'd'\n" +
            "               and datediff(day, getdate(), t.op_datetime) = 0),\n" +
            "    nursingLevel=(select top 1 rtrim(order_name)\n" +
            "                  from yz_act_order t\n" +
            "                  where t.order_code in ('05298','05299','05300','05301','08879','08880','08881','08882')\n" +
            "                    and t.group_no = '00'\n" +
            "                    and t.status_flag in ('3', '4')\n" +
            "                    and t.inpatient_no = a.inpatient_no\n" +
            "                  order by t.start_time desc),\n" +
            "    sickLevelOrderName=(select top 1 rtrim(order_name)\n" +
            "                        from yz_act_order t\n" +
            "                        where t.order_code in ('05887', '05888')\n" +
            "                          and t.group_no = '00'\n" +
            "                          and t.status_flag in ('3', '4')\n" +
            "                          and t.inpatient_no = a.inpatient_no\n" +
            "                        order by t.start_time desc)\n" +
            "from zy_actpatient a where (zk_ward=#{dept} or zk_ward in " +
            "(select small_dept from zd_dept_all t where t.dept=#{dept})) " +
            "order by cast(bed_no as int)")
    List<InpatientBrief> selectInpatientBrief(@Param("dept") String dept);  

    @Select("select                 rtrim(bed_no)       as bedNo,\n" +
            "                       rtrim(name)         as name,\n" +
            "                       sex                 as gender,\n" +
            "                       rtrim(inpatient_no) as patNo,\n" +
            "    birthDate=(select t.birth_date\n" +
            "               from a_patient_mi t\n" +
            "               where t.inpatient_no = a.inpatient_no),\n" +
            "                       admiss_date,\n" +
            "    convertAdmissDate=admiss_date,\n" +
            "    physician=(select rtrim(t.name)\n" +
            "               from a_employee_mi t\n" +
            "               where t.code = a.refer_physician),\n" +
            "                       a.med_type,\n" +
            "    surgery=(select top 1 rtrim(op_scale) + N'级'\n" +
            "             from op_record t\n" +
            "             where t.inpatient_no = a.inpatient_no\n" +
            "               and t.admiss_times = a.admiss_times\n" +
            "               and t.status != 'd'\n" +
            "               and datediff(day, getdate(), t.op_datetime) = 0),\n" +
            "    nursingLevel=(select top 1 rtrim(order_name)\n" +
            "                  from yz_act_order t\n" +
            "                  where t.order_code in (\n" +
            "                                         '05298', '05299', '05300', '05301', '08879', '08880', '08881', '08882'\n" +
            "                      )\n" +
            "                    and t.group_no = '00'\n" +
            "                    and t.status_flag in (\n" +
            "                                          '3', '4'\n" +
            "                      )\n" +
            "                    and t.inpatient_no = a.inpatient_no\n" +
            "                  order by t.start_time desc),\n" +
            "    sickLevelOrderName=(select top 1 rtrim(order_name)\n" +
            "                        from yz_act_order t\n" +
            "                        where t.order_code in (\n" +
            "                                               '05887', '05888'\n" +
            "                            )\n" +
            "                          and t.group_no = '00'\n" +
            "                          and t.status_flag in (\n" +
            "                                                '3', '4'\n" +
            "                            )\n" +
            "                          and t.inpatient_no = a.inpatient_no\n" +
            "                        order by t.start_time desc)\n" +
            "from zy_actpatient a\n" +
            "where (\n" +
            "    ward = #{dept}\n" +
            "        or dept = #{dept}\n" +
            "    )\n" +
            "  and (select top 1 count(1)\n" +
            "       from yz_act_order c\n" +
            "       where a.inpatient_no = c.inpatient_no\n" +
            "         and a.admiss_times = c.admiss_times\n" +
            "         and c.serial = '00'\n" +
            "         and c.order_code in\n" +
            "             (select i.order_code from yz_zd_order_item_confirm i where i.item_name = N'出院')) = 0\n" +
            "order by cast(bed_no as int);")
    List<InpatientBrief> selectInpatientBriefV2(@Param("dept") String dept);


    @Select("select count(*)\n" +
            "from zy_bed_mi\n" +
            "where ward_code = '${dept}'")
    Integer bedCount(@Param("dept") String dept);


    @Select("select count(*) from zy_actpatient " +
            "where CONVERT(varchar(100), admiss_times, 23) = CONVERT(varchar(100), GETDATE(), 23)  and admiss_dept='${dept}'")
    Integer admissCount(@Param("dept") String dept);

    @Select("select (select count(*)\n" +
            "        from zy_actpatient\n" +
            "        where CONVERT(varchar(100), dis_date, 23) = CONVERT(varchar(100), GETDATE(), 23)  and admiss_dept = '${dept}')\n" +
            "           +\n" +
            "       (select count(*)\n" +
            "        from zy_inactpatient\n" +
            "        where CONVERT(varchar(100), dis_date, 23) = CONVERT(varchar(100), GETDATE(), 23) and admiss_dept = '${dept}')")
    Integer dischargeCount(String dept);

    @Select("select count(*)\n" +
            "from yz_act_order a,zy_order_zk b\n" +
            "where  CONVERT(varchar(100), a.start_time, 23) = CONVERT(varchar(100), GETDATE(), 23) \n" +
            "and a.act_order_no=b.act_order_no\n" +
            "and old_ward='${dept}'")
    Integer outCount(String dept);

    @Select("select count(*)\n" +
            "from yz_act_order a,zy_order_zk b\n" +
            "where CONVERT(varchar(100), a.start_time, 23) = CONVERT(varchar(100), GETDATE(), 23)\n" +
            "and a.act_order_no=b.act_order_no\n" +
            "and new_ward='${dept}'")
    Integer inCount(String dept);

    @Select("select count(*)\n" +
            "from yz_act_order \n" +
            "where ward_code='${dept}'\n" +
            "and end_time is null and group_no ='00'  \n" +
            "and order_name like '%病危%'")
    Integer criticallyIllCount(String dept);

    @Select("select count(*)\n" +
            "from yz_act_order \n" +
            "where ward_code='${dept}'\n" +
            "and end_time is null and group_no ='00' \n" +
            "and order_name like '%病重%'\n ")
    Integer seriouslyIllCount(String dept);

    @Select("select count(*)\n" +
            "from yz_act_order \n" +
            "where ward_code='${dept}'\n" +
            "and end_time is null  \n" +
            "and order_code in ('01296') and group_no ='00'")
    Integer primaryCare(String dept);

    @Select("select count(*)\n" +
            "from yz_act_order \n" +
            "where ward_code='${dept}'\n" +
            "and end_time is null  \n" +
            "and order_code in ('05300','01297') and group_no ='00'")
    Integer secondaryCare(String dept);
}
