package thyyxxk.webserver.dao.his.casefrontsheet;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.casefrontsheet.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("<script>" +
            "select rtrim(a.inpatient_no) as patNo,a.admiss_times as times, " +
            "rtrim(a.name) as name,gender=(case when a.sex='1' then '男' else '女' end), " +
            "age=datediff(year,b.birth_date,getdate()),a.admiss_date,a.dis_date, " +
            "inHospDays=datediff(day, a.admiss_date, a.dis_date),a.total_charge, " +
            "rtrim(a.zk_ward) as disDept, " +
            "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,'')," +
            "deadOrder=(select count(1) from yz_inact_order t where t.inpatient_no=a.inpatient_no and " +
            "t.admiss_times=a.admiss_times and t.order_code='05973') " +
            "from zy_inactpatient a, a_patient_mi b, batj_ba2 c where " +
            "a.inpatient_no not like 'S%' and " +
            "a.inpatient_no not like 'JT%' and " +
            "<if test=\" dept!=null and dept!='' \">" +
            "a.zk_ward=#{dept} and " +
            "</if>" +
            "charindex('$',a.inpatient_no)=0 and " +
            "a.dis_date&gt;=#{begntime} and a.dis_date&lt;=#{endtime} and " +
            "a.inpatient_no=b.inpatient_no and a.inpatient_no=c.zyh and a.admiss_times=c.zycs " +
            "order by a.zk_ward " +
            "</script>")
    List<DismissCount> selectDismissDetailCount(@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);
}
