package com.huimv.eartag.dao.repo;

import com.huimv.eartag.dao.entity.EartagDeviceRegisterEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface EartagDeviceRegisterRepo extends JpaRepository<EartagDeviceRegisterEntity, Integer>, JpaSpecificationExecutor<EartagDeviceRegisterEntity> {
    @Query(nativeQuery = true,value = "SELECT * FROM eartag_device_register WHERE device_code=?1 LIMIT 1")
    EartagDeviceRegisterEntity getDeviceRegister(String deviceCode);

    @Query(nativeQuery = true,value = "SELECT * FROM eartag_device_register WHERE id=?1")
    EartagDeviceRegisterEntity getDeviceRegisterByRowId(String id);

//    @Query(nativeQuery = true,value = "SELECT COUNT(*) total FROM eartag_device_register WHERE farm_id=?1 AND (device_status=0 OR device_status=1 OR device_status=2)")
    @Query(nativeQuery = true,value = "SELECT COUNT(*) total FROM eartag_device_register WHERE farm_id=?1 AND device_status IN(0,1,2)")
    List<Object[]> getDeviceCountByFarmId(String farmId);

    @Query(nativeQuery = true,value = "SELECT COUNT(*) total FROM eartag_device_register WHERE farm_id=?1 AND device_status=?2")
    List<Object[]> getDeviceCountByFarmIdAndLiveStatus(String farmId, Integer liveStatus);

    @Query(nativeQuery = true,value = "SELECT device_code FROM eartag_device_register WHERE device_status=1")
    List<Object[]> getDeviceCodeFromDeviceRegister();

    @Query(nativeQuery = true,value = "SELECT * FROM eartag_device_register WHERE id=?1")
    EartagDeviceRegisterEntity getDeviceRegisterById(Integer id);

    @Query(nativeQuery = true,value = "SELECT * FROM eartag_device_register WHERE chip_id=?1")
    EartagDeviceRegisterEntity getByChipId(String chipId);
}