package xn.hxp.ui.plan.room.dao;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Transaction;
import androidx.room.Update;

import java.util.List;

import xn.hxp.ui.plan.room.bean.input_add.InventoryItemBean;

@Dao
public interface HxpInventoryDAO {

    @Insert
    void justInsert(InventoryItemBean inventoryItemBean);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(InventoryItemBean inventoryItemBean);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<InventoryItemBean> inventoryItemBeans);

    @Query("SELECT * FROM inventoryitembean")
    List<InventoryItemBean> getAll();

    @Query("SELECT * FROM inventoryitembean WHERE layer = :layer AND cabinetId =:cabinetId AND doorId =:doorId")
    List<InventoryItemBean> getById(long cabinetId, long doorId, long layer);

    @Update
    void update(InventoryItemBean inventoryitembean);

    @Delete
    void delete(InventoryItemBean inventoryitembean);

    @Query("DELETE FROM inventoryitembean WHERE layer = :layer AND cabinetId =:cabinetId AND doorId =:doorId")
    void delete(long cabinetId, long doorId, long layer);

    @Query("DELETE FROM inventoryitembean WHERE doorId = :doorId")
    void delete(long doorId);

    @Query("DELETE FROM inventoryitembean WHERE id = :id")
    void delete(int id);

    @Query("DELETE FROM inventoryitembean")
    void deleteAll();


    @Query("SELECT COUNT(*) FROM inventoryitembean WHERE waitId = :waitId")
    int checkWaitIdExists(long waitId);

    @Transaction
    default void upsert(InventoryItemBean inventoryItemBean) {
        int exist = checkWaitIdExists(inventoryItemBean.getWaitId());
        if (exist > 0) {
            update(inventoryItemBean);
        } else {
            justInsert(inventoryItemBean);
        }
    }


}
