package com.bocloud.ims.repository.resource.azure.slb;

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.azure.slb.BackEndVirtualMachine;
import org.springframework.stereotype.Repository;

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

/**
 * @ClassName: BackEndVirtualMachineRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2020/10/16 10:40
 */
@Repository("backEndVirtualMachineRepository")
public class BackEndVirtualMachineRepository extends BasicDao {


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

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


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


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

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


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


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

    }


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


}