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.HmcVirtualCd;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Component;

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

@Deprecated
@Component("hmcVirtualCdRepository")
public class HmcVirtualCdRepository extends EnhancedBaseRepository<HmcVirtualCd, Long> {

    public HmcVirtualCd query(Long id) {
        String sql = "select a.* from hmc_virtual_cd a where a.id = :id";
        Map<String, Object> param = MapTools.simpleMap("id", id);
        return this.list(HmcVirtualCd.class, sql, param).stream().findFirst().orElse(null);
    }

    public HmcVirtualCd queryByUuid(Long viosId, String uuid) {
        String sql = "select a.* from hmc_virtual_cd a where a.vios_id = :viosId and a.uuid = :uuid";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        param.put("uuid", uuid);
        return this.list(HmcVirtualCd.class, sql, param).stream().findFirst().orElse(null);
    }

    public HmcVirtualCd queryByName(Long viosId, String name) {
        String sql = "select a.* from hmc_virtual_cd a where a.vios_id = :viosId and a.cd_name = :name";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        param.put("name", name);
        return this.list(HmcVirtualCd.class, sql, param).stream().findFirst().orElse(null);
    }

    public List<HmcVirtualCd> queryByViosSlot(Long viosId, Integer viosSlot) {
        String sql = "select a.* from hmc_virtual_cd a where a.vios_id = :viosId";
        Map<String, Object> param = MapTools.simpleMap("viosId", viosId);
        if (viosSlot != null) {
            sql += " and a.vios_slot = :viosSlot";
            param.put("viosSlot", viosSlot);
        }
        return this.list(HmcVirtualCd.class, sql, param);
    }

    public List<HmcVirtualCd> queryByAssignPartitionSlot(Long partitionId, Integer partitionSlot) {
        String sql = "select a.* from hmc_virtual_cd a " +
                "where a.assign_partition_id = :partitionId and a.assign_partition_slot = :partitionSlot";
        Map<String, Object> param = MapTools.simpleMap("partitionId", partitionId);
        param.put("partitionSlot", partitionSlot);
        return this.list(HmcVirtualCd.class, sql, param);
    }

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