package thyyxxk.webserver.dao.his.medicalinsurance.costcontrol;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface BedDaysDao {

    @Select(" select t.ryks, t.small_dept as deptId, sum(t.days) days from " +
            " (select inpatient_no,admiss_times,name,admiss_date,dis_date,small_dept, " +
            " refer_physician, DATEDIFF(day, admiss_date,dis_date) days, " +
            " (select name from zd_unit_code where code=small_dept) ryks, " +
            " (select name from zd_unit_code where code=zk_ward) cyks " +
            " from zy_inactpatient where dis_date< #{endTime} and dis_date >= #{beginTime} " +
            " union all " +
            " select inpatient_no,admiss_times,name,admiss_date,dis_date,small_dept,refer_physician, " +
            " DATEDIFF(day, case when admiss_date < #{beginTime} then #{beginTime} else admiss_date end, " +
            " case when dis_date is null then #{endTime} else dis_date end) days, " +
            " (select name from zd_unit_code where code=small_dept) ryks, " +
            " (select name from zd_unit_code where code=zk_ward) cyks " +
            " from zy_actpatient where admiss_date < #{endTime} and isnull(dis_date,#{beginTime}) >= #{beginTime} " +
            " ) t " +
            " group by t.ryks, t.small_dept " +
            " order by t.ryks ")
    List<Map>  selectBedDays(@Param("beginTime") String beginTime, @Param("endTime") String endTime);

    @Select(" select t.ryks, t.doctor, sum(t.days) days from " +
            " (select inpatient_no,admiss_times,name,admiss_date,dis_date,small_dept, " +
            " refer_physician, DATEDIFF(day, #{beginTime},dis_date) days, " +
            " (select name from zd_unit_code where code=small_dept) ryks, " +
            " (select name from zd_unit_code where code=zk_ward) cyks, " +
            " isnull((select name from a_employee_mi where code=refer_physician), '其他') doctor " +
            " from zy_inactpatient where dis_date< #{endTime} and dis_date >= #{beginTime} and small_dept = #{deptId} " +
            " union all " +
            " select inpatient_no,admiss_times,name,admiss_date,dis_date,small_dept,refer_physician, " +
            " DATEDIFF(day, case when admiss_date < #{beginTime} then #{beginTime} else admiss_date end, " +
            " case when dis_date is null then #{endTime} else dis_date end) days, " +
            " (select name from zd_unit_code where code=small_dept) ryks, " +
            " (select name from zd_unit_code where code=zk_ward) cyks, " +
            " isnull((select name from a_employee_mi where code=refer_physician), '其他') doctor " +
            " from zy_actpatient where admiss_date < #{endTime} and isnull(dis_date,#{beginTime}) >= #{beginTime} and small_dept = #{deptId} " +
            " ) t " +
            " group by t.ryks, t.doctor " +
            " order by t.ryks ")
    List<Map>  selectYsDaysDetails(@Param("beginTime") String beginTime, @Param("endTime") String endTime, @Param("deptId") String deptId);

}
