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 OutpatientCoordinationDao {

    @Select(" select convert(varchar(100), request_day, 112) rq, cast(count(1) as varchar) rs " +
            " from mzy_reqrec with(nolock) where cancel_mark=0 " +
            " and convert(varchar(6), request_day, 112) = #{month} " +
            " and unit_code != '1500010' " +
            " group by convert(varchar(100), request_day, 112) ")
    List<Map<String, String>> selectRegisterPeople(@Param("month") String month);

    @Select(" select cast(count(distinct t.pat_no) as varchar) as rs, convert(varchar(100), t.create_datetime, 112) rq " +
            " from t_si_pat_info t, mzy_reqrec m " +
            " where t.pat_no = m.patient_id " +
            " and t.med_type='11' " +
            " and t.insutype='310' " +
            " and m.cancel_mark=0 " +
            " and convert(varchar(100), t.create_datetime, 112) = convert(varchar(100), m.request_day, 112) " +
            " and convert(varchar(6), t.create_datetime, 112) = #{month} " +
            " group by CONVERT(varchar(100), t.create_datetime, 112) ")
    List<Map<String, String>> selectRegisterOutpatientCoordinationPeople(@Param("month") String month);

    @Select(" select cast(count(1) as varchar) rs, t1.rq from " +
            " (select pat_no, convert(varchar(100), setl_time, 112) rq " +
            " from t_si_setlinfo where med_type='11' and insutype='310' and revoked=0 " +
            " and convert(varchar(6), setl_time, 112) = #{month} ) t1, " +
            " (select distinct patient_id, convert(varchar(100), request_day, 112) rq " +
            " from mzy_reqrec with(nolock) " +
            " where cancel_mark=0 " +
            " and convert(varchar(6), request_day, 112) = #{month} ) t2 " +
            " where t1.pat_no = t2.patient_id " +
            " and t1.rq = t2.rq " +
            " group by t1.rq ")
    List<Map<String, String>> selectUsedOutpatientCoordinationPeople(@Param("month") String month);

    @Select(" select cast(sum(tt.rs) as varchar) rs, tt.rq, tt.code, tt.dept from ( " +
            " select count(1) rs, t1.rq, " +
            " code = (select d.code from a_employee_mi a, zd_unit_code d WHERE a.code = t1.staff_id and d.code = a.dept_code ), " +
            " dept = (select d.name from a_employee_mi a, zd_unit_code d WHERE a.code = t1.staff_id and d.code = a.dept_code ) " +
            " from " +
            " (select pat_no, convert(varchar(100), setl_time, 112) rq, staff_id " +
            " from t_si_setlinfo where med_type='11' and insutype='310' and revoked=0 " +
            " and convert(varchar(6), setl_time, 112) = #{month} ) t1, " +
            " (select distinct patient_id, CONVERT(varchar(100), request_day, 112) rq " +
            " from mzy_reqrec with(nolock) " +
            " where cancel_mark=0 " +
            " and convert(varchar(6), request_day, 112) = #{month} ) t2 " +
            " where t1.pat_no = t2.patient_id " +
            " and t1.rq = t2.rq " +
            " group by t1.rq,t1.staff_id) tt" +
            " group by tt.rq, tt.code, tt.dept ")
    List<Map<String, String>> selectUsedOutpatientCoordinationDeptPeople(@Param("month") String month);

    @Select("<script>" +
            "select tt.* from (\n" +
            "select \n" +
            "t3.doctor_code as doctorId, \n" +
            "(select rtrim(a.name) from a_employee_mi a WHERE a.code = t3.doctor_code) as doctor, \n" +
            "t3.visit_dept_code as deptId, \n" +
            "(select d.name from zd_unit_code d WHERE d.code = t3.visit_dept_code ) as dept, \n" +
            "(select rtrim(a.name) from a_employee_mi a WHERE a.code = t1.staff_id) as czr, \n" +
            "(select d.code from a_employee_mi a, zd_unit_code d WHERE a.code = t1.staff_id and d.code = a.dept_code ) as deptCode, \n" +
            "(select d.name from a_employee_mi a, zd_unit_code d WHERE a.code = t1.staff_id and d.code = a.dept_code ) as deptName, \n" +
            "t1.pat_no as patNo, \n" +
            "t1.times, \n" +
            "t1.setl_time as rq, \n" +
            "isnull((select name from t_si_admdvs z where z.code = t1.insuplc_admdvs), t1.insuplc_admdvs) as cbd, \n" +
            "t1.psn_name as psnName, \n" +
            "t3.icd_text as zd  \n" +
            "from \n" +
            "(select pat_no, convert(varchar(100), setl_time, 112) rq1, setl_time, staff_id, times, psn_name, insuplc_admdvs \n" +
            "from t_si_setlinfo where med_type='11' and insutype='310' and revoked=0 \n" +
            "and convert(varchar(6), setl_time, 112) = #{month} ) t1, \n" +
            "(select distinct patient_id, op_id, CONVERT(varchar(100), request_day, 112) rq2 \n" +
            "from mzy_reqrec with(nolock) \n" +
            "where cancel_mark=0 \n" +
            "and convert(varchar(6), request_day, 112) = #{month} ) t2, \n" +
            "(select distinct patient_id, times, isnull(icd_text_new, icd_text) as icd_text, visit_dept_code, doctor_code from mz_visit_table) t3 \n" +
            "where t1.pat_no = t2.patient_id \n" +
            "and t1.rq1 = t2.rq2 \n" +
            "and t1.pat_no = t3.patient_id \n" +
            "and t1.times = t3.times \n" +
            ") tt\n" +
            "where tt.deptName = #{dept} \n" +
            "order by tt.rq \n" +
            "</script>")
    List<Map<String, String>> selectUsedOutpatientCoordinationDeptPeopleForDept(@Param("month") String month, @Param("dept") String dept);

}
