package com.dashitech.businessdata.dao;

import com.dashitech.businessdata.entity.PatientEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by chi on 2019/9/11.
 */
public interface IPatientDAO extends JpaRepository<PatientEntity, String> {

    @SuppressWarnings("unchecked")
    PatientEntity save(PatientEntity patientEntity);

    //@Query(value = "select id from PatientEntity where patientcode =?1 limit 1 ")
    @Query(value = "select id from itsm_patient where patientcode =?1 limit 1 ",nativeQuery = true)
    Integer selectIdByCode(String code);

    @Query(value = "select * from itsm_patient where bednum is not null and pinyin is null",nativeQuery = true)
    List<PatientEntity> selectPatient();

    @Query(value = "select * from itsm_patient where patientcode =?1 limit 1 ",nativeQuery = true)
    PatientEntity selectPatientByCode(String code);

    @Modifying
    @Transactional
    @Query(value = "UPDATE itsm_patient set bednum = null WHERE patientcode <> ?1 and bednum = ?2 and roomnum = ?3 and department = ?4 ",nativeQuery = true)
    int upPatientBednum(String patientcode,String bednum,String roomnum,Integer department);
}
