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

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.hmc.HmcPhysicalDisk;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Component;

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

/**
 * @author lkx007
 */
@Deprecated
@Component("hmcPhysicalDisk")
public class HmcPhysicalDiskRepository extends EnhancedBaseRepository<HmcPhysicalDisk, Long> {

    public List<HmcPhysicalDisk> query(Long viosId, String volumeName) {
        String sql = "select a.* from hmc_physical_disk a where a.vios_id = :viosId ";
        if (volumeName != null) {
            sql += " and a.volume_name = :volumeName ";
        }
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        if (volumeName != null) {
            param.put("volumeName", volumeName);
        }
        List<Object> list = this.queryForList(sql, param, HmcPhysicalDisk.class);
        List<HmcPhysicalDisk> vgs = new ArrayList<>();
        for (Object obj : list) {
            vgs.add((HmcPhysicalDisk) obj);
        }
        return vgs;
    }

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

    public boolean delete(Long viosId, String diskName) throws BoCloudDBException {
        String sql = "delete from hmc_physical_disk where vios_id = :viosId";
        if (diskName != null) {
            sql += " and disk_name = :diskName";
        }
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        if (diskName != null) {
            param.put("diskName", diskName);
        }
        return this.update(sql, param) >= 0;
    }
}
