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

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.ims.entity.resource.fusionsphere.FusionSphereDeviceHost;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: FusionSphereDeviceHostRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/10/22 18:12
 */
@Repository
public class FusionSphereDeviceHostRepository extends BasicDao {

    public FusionSphereDeviceHost save(FusionSphereDeviceHost entity) throws BoCloudDBException {
        return (FusionSphereDeviceHost) this.saveEntity(entity);
    }

    public Boolean update(FusionSphereDeviceHost entity) throws BoCloudDBException {
        return this.updateEntity(entity);
    }

    public Boolean remove(long id) throws BoCloudDBException {
        String sql = "delete from fusion_sphere_device_host where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.update(sql, params) > 0;
    }

    public Boolean remove(FusionSphereDeviceHost entity) throws BoCloudDBException {
        return this.baseDelete(entity);
    }

    public Boolean removeByDeviceId(Long id) throws BoCloudDBException {
        String sql = "delete from fusion_sphere_device_host where device_id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.update(sql, params) > 0;
    }


    public Boolean removeByHostId(Long id) throws BoCloudDBException {
        String sql = "delete from fusion_sphere_device_host where host_id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.update(sql, params) > 0;
    }


    public List<FusionSphereDeviceHost> listByBackEndId(long id) throws BoCloudDBException {
        String sql = "select * from fusion_sphere_device_host where device_id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.queryForList(sql, params, FusionSphereDeviceHost.class).stream().map(o -> (FusionSphereDeviceHost) o).collect(Collectors.toList());

    }


    public List<FusionSphereDeviceHost> listByVmId(long id) throws BoCloudDBException {
        String sql = "select * from fusion_sphere_device_host where host_id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.queryForList(sql, params, FusionSphereDeviceHost.class).stream().map(o -> (FusionSphereDeviceHost) o).collect(Collectors.toList());
    }


}