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

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

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

@Deprecated
@Repository("hmcPhysicalFibreChannelRepository")
public class HmcPhysicalFibreChannelRepository extends EnhancedBaseRepository<HmcPhysicalFibreChannel, Long> {

    public List<HmcPhysicalFibreChannel> listByPartitionId(Long partitionId) {
        String sql = "select a.* from hmc_physical_fibre_channel a " +
                "where a.partition_id = :partitionId and ifnull(a.available_ports, 0) > 0";
        Map<String, Object> param = MapTools.simpleMap("partitionId", partitionId);
        List<Object> list = this.queryForList(sql, param, HmcPhysicalFibreChannel.class);
        List<HmcPhysicalFibreChannel> fibreChannels = new ArrayList<>();
        for (Object obj : list) {
            fibreChannels.add((HmcPhysicalFibreChannel) obj);
        }
        return fibreChannels;
    }

    public boolean deleteByViosId(Long viosId) {
        String sql = "delete from hmc_physical_fibre_channel where partition_id  = :partitionId";
        Map<String, Object> params = MapTools.simpleMap("partitionId", viosId);
        return this.update(sql, params) >= 0;
    }

    /**
     * 根据物理光纤通道唯一设备id查询光纤通道信息
     *
     * @param uniqueDeviceId 光纤通道唯一设备id
     * @return 查询结果
     */
    public HmcPhysicalFibreChannel getByUniqueDeviceId(String uniqueDeviceId) {
        String sql = "select * from hmc_physical_fibre_channel where unique_device_id  = :uniqueDeviceId";
        Map<String, Object> params = MapTools.simpleMap("uniqueDeviceId", uniqueDeviceId);
        List<HmcPhysicalFibreChannel> list = list(HmcPhysicalFibreChannel.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

}
