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

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

@Deprecated
@Repository("hmcProfileFibreAdapterRepository")
public class HmcProfileFibreAdapterRepository extends EnhancedBaseRepository<HmcProfileFibreAdapter, Long> {

    public boolean deleteByPartitionId(Long partitionId) {
        String sql = "update hmc_profile_fibre_adapter set is_delete = 1 where profile_id in (select id from hmc_profile where partition_id = :partitionId)";
        Map<String, Object> params = MapTools.simpleMap("partitionId", partitionId);
        return this.update(sql, params) > 0;
    }

    public boolean deleteByProfileId(Long profileId) {
        String sql = "update hmc_profile_fibre_adapter set is_delete = 1 where profile_id = :profileId";
        Map<String, Object> params = MapTools.simpleMap("profileId", profileId);
        return this.update(sql, params) > 0;
    }

    public List<HmcProfileFibreAdapter> queryByProfileId(Long profileId) {
        String sql = "select a.* from hmc_profile_fibre_adapter a where a.is_delete = 0 and a.profile_id = :profileId";
        Map<String, Object> param = MapTools.simpleMap("profileId", profileId);
        List<Object> list = this.queryForList(sql, param, HmcProfileFibreAdapter.class);
        List<HmcProfileFibreAdapter> adapters = new ArrayList<>();
        for (Object obj : list) {
            adapters.add((HmcProfileFibreAdapter) obj);
        }
        return adapters;
    }

    public List<HmcProfileFibreAdapter> queryByProfileIdIncludeDeleted(Long profileId) {
        String sql = "select a.* from hmc_profile_fibre_adapter a where a.profile_id = :profileId";
        Map<String, Object> param = MapTools.simpleMap("profileId", profileId);
        List<Object> list = this.queryForList(sql, param, HmcProfileFibreAdapter.class);
        List<HmcProfileFibreAdapter> adapters = new ArrayList<>();
        for (Object obj : list) {
            adapters.add((HmcProfileFibreAdapter) obj);
        }
        return adapters;
    }

    public HmcProfileFibreAdapter queryByProfileIdAndAdapterId(Long profileId, int adapterId) {
        String sql = "select a.* from hmc_profile_fibre_adapter a where a.is_delete = 0 and a.profile_id = :profileId and a.adapter_id = :adapterId";
        Map<String, Object> param = MapTools.simpleMap("profileId", profileId);
        param.put("adapterId", adapterId);
        List<Object> list = this.queryForList(sql, param, HmcProfileFibreAdapter.class);
        return list == null || list.size() == 0 ? null : (HmcProfileFibreAdapter) list.get(0);
    }

    @Override
    public HmcProfileFibreAdapter get(Class<HmcProfileFibreAdapter> clazz, Long id) {
        String sql = "select a.* from hmc_profile_fibre_adapter a where a.id = :id ";
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        List<Object> objectList = queryForList(sql, params, HmcProfileFibreAdapter.class);
        return objectList == null || objectList.size() == 0 ? null : (HmcProfileFibreAdapter) objectList.get(0);
    }

    @Override
    public boolean delete(HmcProfileFibreAdapter entity) {
        String sql = "update hmc_profile_fibre_adapter set is_delete = 1 where id = :id ";
        Map<String, Object> params = new HashMap<>();
        params.put("id", entity.getId());
        return update(sql, params) > 0;
    }

    @Override
    public void save(HmcProfileFibreAdapter entity) {
        entity.setIsDelete(false);
        super.save(entity);
    }

    @Override
    public boolean updateEntity(Object bean) throws BoCloudDBException {
        if (bean instanceof HmcProfileFibreAdapter) {
            ((HmcProfileFibreAdapter) bean).setIsDelete(false);
        }
        return super.updateEntity(bean);
    }
}
