package thyyxxk.webserver.dao.his.examinations;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.examinations.BloodSugar.BgNursingTestSync;
import thyyxxk.webserver.entity.inpatient.patient.Patient;

import java.util.List;

@Mapper
public interface BloodSugarQueryDao {


    @Select("select bed_no, " +
            "       rtrim(a.name) name, " +
            "       birth_date, " +
            "       a.inpatient_no, " +
            "       admiss_date " +
            "from ${tableName} a, " +
            "     a_patient_mi b " +
            "where a.inpatient_no = b.inpatient_no " +
            "  and a.inpatient_no = #{patNo} " +
            "and a.admiss_times = #{times}")
    Patient getPatientInfo(@Param("tableName") String tableName,
                           @Param("patNo") String patNo,
                           @Param("times") Integer times);


    @Select("select id,\n" +
            "       inpatient_no,\n" +
            "       admiss_times,\n" +
            "       nurse_id,\n" +
            "       nurse_id_name = (select rtrim(name) from a_employee_mi where code = nurse_id)," +
            "       test_time,\n" +
            "       bed_no,\n" +
            "       timecode_id,\n" +
            "       timecode_name,\n" +
            "       test_result,\n" +
            "       result_unit\n" +
            "from bg_nursing_test_sync\n" +
            "where inpatient_no = #{patNo}" +
            " and admiss_times = #{times}" +
            " and timecode_name = N'随机血糖'")
    List<BgNursingTestSync> getPatientLoodSugarRandom(@Param("patNo") String patNo,
                                                      @Param("times") Integer times);

    @Select("select id,\n" +
            "       inpatient_no,\n" +
            "       admiss_times,\n" +
            "       nurse_id,\n" +
            "       nurse_id_name = (select rtrim(name) from a_employee_mi where code = nurse_id)," +
            "       test_time,\n" +
            "       bed_no,\n" +
            "       timecode_id,\n" +
            "       timecode_name,\n" +
            "       test_result,\n" +
            "       result_unit\n" +
            "from bg_nursing_test_sync\n" +
            "where inpatient_no = #{patNo}" +
            " and admiss_times = #{times}")
    List<BgNursingTestSync> getPatientLoodSugar(@Param("patNo") String patNo,
                                                @Param("times") Integer times);


    @Select("select admiss_times " +
            "from zy_actpatient " +
            "where inpatient_no = #{patNo} ")
    Integer getPatientTimes(@Param("patNo") String patNo);

    @Update("update bg_nursing_test_sync\n" +
            "set nurse_id  = #{nurseId},\n" +
            "    test_time = #{testTime},\n" +
            "    test_result = #{testResult} \n" +
            "where id = #{id}")
    void modifyPatientBloodGlucoseInfo(BgNursingTestSync param);

}
