package net.zstong.awump.device.repository;


import net.zstong.awump.device.domain.DeviceInstance;
import net.zstong.awump.device.web.response.CountDeviceData;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;


@Repository
public interface DeviceInsanceRepository extends ReactiveCrudRepository<DeviceInstance, String> {

    @Query("select warehouse_id_ warehouse," +
        "(SELECT name_ FROM aw_warehouse w where w.id=dev_device_instance.warehouse_id_) warehouse_name," +
        "count(*) total " +
        "from dev_device_instance group by warehouse_id_")
    Flux<CountDeviceData> countByWarehouse();

    @Query("select state," +
        "count(*) total " +
        "from dev_device_instance group by state")
    Flux<CountDeviceData> countByState();


    @Query("select state," +
        "count(*) total " +
        "from dev_device_instance where warehouse_id_=:warehouseId  group by state")
    Flux<CountDeviceData> countByWarehouse(String warehouseId);

    @Query("select state," +
        "count(*) total " +
        "from dev_device_instance where warehouse_id_ is null group by state")
    Flux<CountDeviceData> countByNonWarehouse();


}
