package thyyxxk.webserver.dao.his.zhuyuanyisheng;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.inpatient.patient.Patient;

import java.util.List;

@Mapper
public interface PatientInfoQueryDao {

    @Select("SELECT dept_code    as code, " +
            "       dept_name    as name, " +
            "       dept_py_code as py_code, " +
            "       dept_d_code  as w_code " +
            "FROM zy_adtward " +
            "group by dept_code, dept_name, dept_py_code, dept_d_code " +
            "order by dept_code, dept_name")
    List<GetDropdownBox> getHospWardAndDept();

    @Select("select code, name " +
            "from zd_icd_code_new " +
            "where (code like #{code} or name like #{code})")
    List<GetDropdownBox> selectDiagnsis(String code);

    @Select("select rtrim(yb_code) as code, rtrim(yb_name) as name " +
            "from zd_icd9_cm3 " +
            "where (code like #{code} or name like #{code})")
    List<GetDropdownBox> selectSurgery(String code);


    @Select("select code, name " +
            "from a_employee_mi " +
            "where (py_code like #{code} or name like #{code} or d_code like #{code})")
    List<GetDropdownBox> employeeList(String code);

    @Select("SELECT rtrim(a.inpatient_no) inpatient_no, " +
            "       a.admiss_times, " +
            "       a.name, " +
            "       a.admiss_date, " +
            "       a.dis_date, " +
            "       a.admiss_dept, " +
            "       a.admiss_ward, " +
            "       a.sex, " +
            "       a.ward, " +
            "       a.dept, " +
            "       a.bed_no, " +
            "       move_flag, " +
            "       owe_flag, " +
            "       admiss_diag_str, " +
            "       a.responce_type, " +
            "       responce_name = (select rtrim(name) name from zy_zd_responce_type where code = a.responce_type)," +
            "       balance, " +
            "       total_charge, " +
            "       zk_ward," +
            "       admissWardName = (select rtrim(name) from zd_unit_code where code = a.admiss_ward)," +
            "       zkWardName     = (select rtrim(name) from zd_unit_code where code = zk_ward)," +
            "       birth_date     = (select birth_date from a_patient_mi c where a.inpatient_no = c.inpatient_no)," +
            "       emr_audit       = (select count(1) from emr_audit_detail where pat_no = a.inpatient_no and times = a.admiss_times)," +
            "       act_ipt_days=(datediff(day, a.admiss_date, isnull(a.dis_date,getdate())))," +
            "       admissPhysicianName = (select name from a_employee_mi where code = admiss_physician),\n" +
            "       referPhysicianName = (select name from a_employee_mi where code = refer_physician),\n" +
            "      zyDismissWay =  (select name from zd_zy_dismiss_way where code = zy_dismiss_way),\n" +
            "      disDeptName =  (select name from zd_unit_code where code = dis_dept),\n" +
            "       disMainDiag = (select name\n" +
            "        from t_case_frontsheet_disdiag disdiag\n" +
            "        where disdiag.bah = a.inpatient_no\n" +
            "          and disdiag.times = a.admiss_times\n" +
            "          and no = 1) " +
            "FROM ${tableName} a" +
            "  left join t_case_frontsheet_main b on (a.inpatient_no = b.bah and a.admiss_times = b.admiss_times) " +
            " ${ew.customSqlSegment} ")
    List<Patient> getPatInfo(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                             @Param("tableName") String tableName);

}
