package thyyxxk.webserver.dao.his.zhuyuanyisheng;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.zhuyuanyisheng.EmrPatientData;

import java.util.List;

@Mapper
public interface EmrPatientDao {

    @Select("select id, " +
            "       pat_no, " +
            "       times, " +
            "       emr_document_id, " +
            "       emr_category_code, " +
            "       del_flag, " +
            "       emr_name, " +
            "       create_id, " +
            "       create_date, " +
            "       modify_id, " +
            "       modify_date, " +
            "       name " +
            "from emr_patient_data " +
            "where pat_no = #{patNo} " +
            "  and times = #{times}" +
            " and del_flag = 0 ")
    List<EmrPatientData> getPatientData(@Param("patNo") String patNo,
                                        @Param("times") Integer times);

    @Select("select count(1) " +
            "from emr_patient_data " +
            "where pat_no = #{patNo} " +
            "  and times = #{times}" +
            "  and emr_category_code = 'bingchengjiluzhuanyong'" +
            "  and del_flag = 0   ")
    Integer isThereARecordOfTheCourseOfTheDisease(@Param("patNo") String patNo,
                                                  @Param("times") Integer times);

    @Insert("insert into emr_patient_data (pat_no, times, emr_document_id, emr_category_code, emr_name,create_id,name) " +
            "values (#{patNo},#{times},#{emrDocumentId},#{emrCategoryCode},#{emrName},#{createId},#{name})")
    void emrInsertForTheFirstTime(EmrPatientData param);

    @Update("update emr_patient_data " +
            "set name    = #{name}, " +
            "    modify_date = getdate() , " +
            "    modify_id   = #{createId} " +
            "where pat_no = #{patNo} " +
            "  and times = #{times} " +
            "  and emr_document_id = #{emrDocumentId} ")
    void updateCreatedTemplate(EmrPatientData param);

    @Update("update emr_patient_data " +
            "set del_flag = 1," +
            "    modify_id = #{userCode}," +
            "    modify_date = getdate() " +
            "where emr_document_id = #{documentId}")
    void deletePatientEmrByDocumentId(@Param("documentId") String documentId,
                                      @Param("userCode") String userCode);


    @Select("select count(1)\n" +
            "from emr_patient_data\n" +
            "where pat_no = #{patNo}\n" +
            "  and times = #{times}\n" +
            "  and del_flag = 0\n" +
            "  and emr_category_code = #{emrCategoryCode} ")
    Integer queryWhetherThePatientHasASpecifiedMedicalRecord(@Param("patNo") String patNo,
                                                             @Param("times") Integer times,
                                                             @Param("emrCategoryCode") String emrCategoryCode);


}
