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

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.azure.network.NicIpConfiguration;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: NicIpConfigurationRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/3/23 17:44
 */
@Repository
public class NicIpConfigurationRepository extends BasicDao {

    public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "select * from nic_ip_configuration where 1=1";
        sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        List<NicIpConfiguration> list = this.queryForList(sql, paramMap, NicIpConfiguration.class).stream().map(o -> (NicIpConfiguration) o).collect(Collectors.toList());
        List<SimpleBean> beans = Lists.newArrayList();
        for (NicIpConfiguration entity : list) {
            beans.add(new SimpleBean(entity.getId(), entity.getUuid()));
        }
        return beans;
    }

    public List<NicIpConfiguration> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "SELECT a.* " +
                "FROM nic_ip_configuration a " +
                "WHERE 1=1 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.queryForList(sql, paramMap, NicIpConfiguration.class).stream().map(o -> (NicIpConfiguration) o).collect(Collectors.toList());
    }

    public int count(List<Param> params) throws BoCloudDBException {
        String sql = "SELECT COUNT(1) " +
                "FROM nic_ip_configuration a " +
                "WHERE 1=1 ";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    public NicIpConfiguration query(Long id) throws BoCloudDBException {
        String sql = "select a.*,n.name as network_interface_name from nic_ip_configuration a "
                + " left join network_interface n on a.network_interface_id = n.id "
                + " where  a.id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<NicIpConfiguration> list = this.queryForList(sql, params, NicIpConfiguration.class).stream().map(o -> (NicIpConfiguration) o).collect(Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public NicIpConfiguration save(NicIpConfiguration ip) throws BoCloudDBException {
        return (NicIpConfiguration) this.saveEntity(ip);
    }

    public Boolean update(NicIpConfiguration ip) throws BoCloudDBException {
        return this.updateEntity(ip);
    }

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

    public Boolean deleteByNetworkInterfaceId(Long networkInterfaceId) throws BoCloudDBException {
        String sql = "delete from nic_ip_configuration where network_interface_id = :networkInterfaceId";
        Map<String, Object> params = MapTools.simpleMap("networkInterfaceId", networkInterfaceId);
        return this.update(sql, params) > 0;
    }

    public NicIpConfiguration getByName(Long networkInterfaceId, String name) throws BoCloudDBException {
        String sql = "select a.* from nic_ip_configuration a where  a.network_interface_id = :networkInterfaceId and a.name = :name  ";
        Map<String, Object> params = MapTools.simpleMap("networkInterfaceId", networkInterfaceId);
        params.put(Common.NAME, name);
        List<NicIpConfiguration> list = this.queryForList(sql, params, NicIpConfiguration.class).stream().map(o -> (NicIpConfiguration) o).collect(Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public List<NicIpConfiguration> listByNetworkInterfaceId(Long networkInterfaceId) throws BoCloudDBException {
        String sql = "select * from nic_ip_configuration where network_interface_id = :networkInterfaceId";
        Map<String, Object> params = MapTools.simpleMap("networkInterfaceId", networkInterfaceId);
        return this.queryForList(sql, params, NicIpConfiguration.class).stream().map(o -> (NicIpConfiguration) o).collect(Collectors.toList());
    }

}