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

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.fusionsphere.FusionSphereVlanPool;
import org.springframework.stereotype.Repository;

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: FusionSphereVlanPoolRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/10/25 16:46
 */
@Repository
public class FusionSphereVlanPoolRepository extends BasicDao {

    public FusionSphereVlanPool query(Long id) throws BoCloudDBException {
        String sql = "select a.* from fusion_sphere_vlan_pool a where  a.id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<FusionSphereVlanPool> list = this.queryForList(sql, params, FusionSphereVlanPool.class).stream().map(o -> (FusionSphereVlanPool) o).collect(Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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

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

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

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

    public List<FusionSphereVlanPool> listBySwitcherId(Long id) throws BoCloudDBException {
        String sql = "select * from fusion_sphere_vlan_pool where switcher_id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.queryForList(sql, params, FusionSphereVlanPool.class).stream().map(o -> (FusionSphereVlanPool) o).collect(Collectors.toList());
    }

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) from fusion_sphere_vlan_pool a where 1=1";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> param = QueryBuilder.getParam(params);
        return this.countQuery(sql, param).intValue();
    }

    public List<FusionSphereVlanPool> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select * from fusion_sphere_vlan_pool a where 1=1";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.queryForList(sql, FusionSphereVlanPool.class).stream().map(o -> (FusionSphereVlanPool) o).collect(Collectors.toList());
    }
}