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.HmcPartitionVfcAdapter;
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("hmcPartitionVfcAdapterRepository")
public class HmcPartitionVfcAdapterRepository extends EnhancedBaseRepository<HmcPartitionVfcAdapter, Long> {

    public List<HmcPartitionVfcAdapter> listByViosId(Long viosId) {
        String sql = "select a.*, c.name as server_vios_name, d.name as client_partition_name " +
                "from hmc_partition_vfc_adapter a " +
                "left join hmc_partition c on a.cmp_vios_id = c.id " +
                "left join hmc_partition d on a.cmp_partition_id = d.id " +
                "where a.cmp_vios_id = :viosId";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        List<Object> list = this.queryForList(sql, param, HmcPartitionVfcAdapter.class);
        List<HmcPartitionVfcAdapter> mappings = new ArrayList<>();
        for (Object obj : list) {
            mappings.add((HmcPartitionVfcAdapter) obj);
        }
        return mappings;
    }

    public List<HmcPartitionVfcAdapter> listByPartitionId(long partitionId) {
        String sql = "select a.*, c.name as server_vios_name, d.name as client_partition_name " +
                "from hmc_partition_vfc_adapter a " +
                "left join hmc_partition c on a.cmp_vios_id = c.id " +
                "left join hmc_partition d on a.cmp_partition_id = d.id " +
                "where a.cmp_partition_id = :partitionId";
        Map<String, Object> param = MapTools.simpleMap("partitionId", partitionId);
        List<Object> list = this.queryForList(sql, param, HmcPartitionVfcAdapter.class);
        List<HmcPartitionVfcAdapter> mappings = new ArrayList<>();
        for (Object obj : list) {
            mappings.add((HmcPartitionVfcAdapter) obj);
        }
        return mappings;
    }

    public HmcPartitionVfcAdapter queryMappingByViosIdAndSlot(long viosId, int viosSlot) {
        String sql = "select a.*, c.name as server_vios_name, d.name as client_partition_name " +
                "from hmc_partition_vfc_adapter a " +
                "left join hmc_partition c on a.cmp_vios_id = c.id " +
                "left join hmc_partition d on a.cmp_partition_id = d.id " +
                "where a.cmp_vios_id  = :viosId and a.server_vios_slot = :viosSlot";
        Map<String, Object> params = MapTools.simpleMap("viosId", viosId);
        params.put("viosSlot", viosSlot);
        List<HmcPartitionVfcAdapter> mappingList = list(HmcPartitionVfcAdapter.class, sql, params);
        return mappingList == null || mappingList.size() == 0 ? null : mappingList.get(0);
    }

    public HmcPartitionVfcAdapter queryMappingByPartitionIdAndSlot(long partitionId, int partitionSlot) {
        String sql = "select a.*, c.name as server_vios_name, d.name as client_partition_name " +
                "from hmc_partition_vfc_adapter a " +
                "left join hmc_partition c on a.cmp_vios_id = c.id " +
                "left join hmc_partition d on a.cmp_partition_id = d.id " +
                "where a.cmp_partition_id  = :partitionId and a.client_partition_slot = :partitionSlot";
        Map<String, Object> params = MapTools.simpleMap("partitionId", partitionId);
        params.put("partitionSlot", partitionSlot);
        List<HmcPartitionVfcAdapter> mappingList = list(HmcPartitionVfcAdapter.class, sql, params);
        return mappingList == null || mappingList.size() == 0 ? null : mappingList.get(0);
    }

    public List<HmcPartitionVfcAdapter> listByViosAndPartition(long viosId, long partitionId) {
        String sql = "select a.*, c.name as server_vios_name, d.name as client_partition_name " +
                "from hmc_partition_vfc_adapter a " +
                "left join hmc_partition c on a.cmp_vios_id = c.id " +
                "left join hmc_partition d on a.cmp_partition_id = d.id " +
                "where a.cmp_vios_id = :viosId and a.cmp_partition_id = :partitionId";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        param.put("partitionId", partitionId);
        List<Object> list = this.queryForList(sql, param, HmcPartitionVfcAdapter.class);
        List<HmcPartitionVfcAdapter> mappings = new ArrayList<>();
        for (Object obj : list) {
            mappings.add((HmcPartitionVfcAdapter) obj);
        }
        return mappings;
    }

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

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

    public void deleteMappingByViosIdAndSlot(long viosId, Integer viosSlot) throws BoCloudDBException {
        String sql = "delete from hmc_partition_vfc_adapter " +
                "where cmp_vios_id  = :viosId and server_vios_slot = :viosSlot";
        Map<String, Object> params = MapTools.simpleMap("viosId", viosId);
        params.put("viosSlot", viosSlot);
        update(sql, params);
    }

}
