package com.bocloud.ims.repository.resource.installation;

import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.installation.ServerPci;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Server PCI DAO接口实现
 *
 * @author lkx007
 */
@Repository("serverPciRepository")
public class ServerPciRepository extends EnhancedBaseRepository<ServerPci, Long> {

    public ServerPci query(Long id) throws Exception {
        String sql = "select a.* from server_pci a where a.id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<ServerPci> list = this.list(ServerPci.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public List<ServerPci> getBySid(long serverId) throws Exception {
        String sql = "select a.* from server_pci a where a.server_id = :serverId";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        return this.list(ServerPci.class, sql, params);
    }

    public ServerPci get(long serverId, String busInfo) throws Exception {
        String sql = "select a.* from server_pci a where a.server_id = :serverId and a.bus_info = :busInfo";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        params.put("busInfo", busInfo);
        List<ServerPci> list = this.list(ServerPci.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public ServerPci get(String mac, long serverId) throws Exception {
        String sql = "select a.* from server_pci a where a.server_id = :serverId and a.serial = :serial";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        params.put("serial", mac);
        List<ServerPci> list = this.list(ServerPci.class, sql, params);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public void removeBySid(long serverId) throws Exception {
        String sql = "delete from server_pci where server_id = :serverId";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        this.update(sql, params);
    }
}
