package com.dashitech.businessdata.dao;

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

import java.util.List;

/**
 * Created by chi on 2019/9/10.
 */
public interface ISpecimenDAO extends JpaRepository<SpecimenEntity, String> {

    @SuppressWarnings("unchecked")
    SpecimenEntity save(SpecimenEntity specimen);

    @Query(value = "select se from SpecimenEntity se where id=:id")
    SpecimenEntity getById(@Param("id") Long id);

    @Query(value = "select id from SpecimenEntity where scode=:scode")
    Long selectIdByScode(@Param("scode")String scode);

    @Query(value = "SELECT id FROM itsm_uc_hospital WHERE _hos_no =?1 AND delete_flag = 0 ", nativeQuery = true)
    Long getHosIdByCode(String gender);

    @Query(value = "select se from SpecimenEntity se where se.orderNumber=:orderNumber")
    List<SpecimenEntity> selectListByorderNumber(@Param("orderNumber")String orderNumber);

    @Modifying
    @Transactional
    @Query(value = "UPDATE itsm_transport_specimen set spe_state = ?1 WHERE patient_no = ?2 and spe_state = 332 and gdid is null",nativeQuery = true)
    int upSpecimenState(Long state,Integer patientNo);

    @Modifying
    @Transactional
    @Query(value = "update itsm_transport_specimen SET spe_state = (SELECT id from itsm_base_dictionary WHERE _key = 'speState' and _value = '110') WHERE spe_state = (SELECT id from itsm_base_dictionary WHERE _key = 'speState' and _value = '1') and print_date <= date_add(now(),interval-1 day)",nativeQuery = true)
    int upSpecimenState();
}
