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.HmcVirtualDisk;
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("hmcVirtualDisk")
public class HmcVirtualDiskRepository extends EnhancedBaseRepository<HmcVirtualDisk, Long> {

    public List<HmcVirtualDisk> queryByDiskName(Long vgId, String diskName) {
        String sql = "select a.* from hmc_virtual_disk a where a.disk_name = :diskName and a.vg_id = :vgId";
        Map<String, Object> param = MapTools.simpleMap("diskName", diskName);
        param.put("vgId", vgId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualDisk.class);
        List<HmcVirtualDisk> vds = new ArrayList<>();
        for (Object obj : list) {
            vds.add((HmcVirtualDisk) obj);
        }
        return vds;
    }

    public List<HmcVirtualDisk> query(Long viosId, Long assignPartitionId) {
        String sql = "select a.* from hmc_virtual_disk a where a.assign_partition_id = :assignPartitionId and a.vios_id = :viosId";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        param.put("assignPartitionId", assignPartitionId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualDisk.class);
        List<HmcVirtualDisk> vds = new ArrayList<>();
        for (Object obj : list) {
            vds.add((HmcVirtualDisk) obj);
        }
        return vds;
    }

    public List<HmcVirtualDisk> queryByViosId(Long viosId) {
        String sql = "select a.* from hmc_virtual_disk a where a.vios_id = :viosId";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualDisk.class);
        List<HmcVirtualDisk> vds = new ArrayList<>();
        for (Object obj : list) {
            vds.add((HmcVirtualDisk) obj);
        }
        return vds;
    }

    public HmcVirtualDisk query(String diskName, Long viosId, Long vgId) {
        String sql = "SELECT a.* FROM hmc_virtual_disk a, hmc_volume_group b ,	hmc_partition c "
                + "WHERE a.disk_name = :diskName AND a.vg_id = b.id AND b.vios_id = c.id AND c.id = :viosId and a.vg_id = :vgId";
        Map<String, Object> param = MapTools.simpleMap("diskName", diskName);
        param.put("viosId", viosId);
        param.put("vgId", vgId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualDisk.class);
        if (list.isEmpty()) {
            return null;
        }
        return (HmcVirtualDisk) list.get(0);
    }

    public HmcVirtualDisk query(String diskName, Long viosId) {
        String sql = "SELECT a.* FROM hmc_virtual_disk a, hmc_volume_group b ,  hmc_partition c "
                + "WHERE a.disk_name = :diskName AND a.vg_id = b.id AND b.vios_id = c.id AND c.id = :viosId ";
        Map<String, Object> param = MapTools.simpleMap("diskName", diskName);
        param.put("viosId", viosId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualDisk.class);
        if (list.isEmpty()) {
            return null;
        }
        return (HmcVirtualDisk) list.get(0);
    }

    public HmcVirtualDisk query(Long id) {
        String sql = "SELECT a.* FROM hmc_virtual_disk a WHERE a.id = :id ";
        Map<String, Object> param = MapTools.simpleMap("id", id);
        List<Object> list = this.queryForList(sql, param, HmcVirtualDisk.class);
        if (list.isEmpty()) {
            return null;
        }
        return (HmcVirtualDisk) list.get(0);
    }

    public List<HmcVirtualDisk> queryByVgIdViosId(Long vgId, Long viosId) {
        String sql = "SELECT a.* FROM hmc_virtual_disk a where a.vg_id = :vgId and a.vios_id = :viosId";
        Map<String, Object> param = MapTools.simpleMap("vgId", vgId);
        param.put("viosId", viosId);
        List<Object> list = this.queryForList(sql, param, HmcVirtualDisk.class);
        List<HmcVirtualDisk> vgs = new ArrayList<>();
        for (Object obj : list) {
            vgs.add((HmcVirtualDisk) obj);
        }
        return vgs;
    }

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

    public boolean delete(Long viosId) throws BoCloudDBException {
        String sql = "delete from hmc_virtual_disk where vios_id = :viosId";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        return this.update(sql, param) >= 0;
    }
}
