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.FusionSphereUpLinkPort;
import org.springframework.stereotype.Repository;

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

/**
 * @ClassName: FusionSphereUpLinkPortRepository
 * @Description:上行链路组
 * @author: zhenpingLi/李振平
 * @date: 2021/11/2 15:51
 */
@Repository
public class FusionSphereUpLinkPortRepository extends BasicDao {

    public List<FusionSphereUpLinkPort> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws BoCloudDBException {
        String sql = "select a.* ,s.name host_name ,p.name port_name "
                + " from fusion_sphere_up_link_port a "
                + " left join server s on a.host_id = s.id "
                + " left join fusion_sphere_host_port p on a.port_id = p.id "
                + " 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, FusionSphereUpLinkPort.class).stream().map(o -> (FusionSphereUpLinkPort) o).collect(Collectors.toList());
    }

    public int count(List<Param> params) throws BoCloudDBException {
        String sql = "SELECT COUNT(1) "
                + " from fusion_sphere_up_link_port a "
                + " left join server s on a.host_id = s.id "
                + " left join fusion_sphere_host_port p on a.port_id = p.id "
                + " WHERE 1=1 ";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    public FusionSphereUpLinkPort query(Long id) throws BoCloudDBException {
        String sql = "select a.* ,s.name host_name ,p.name port_name "
                + " from fusion_sphere_up_link_port a "
                + " left join server s on a.host_id = s.id "
                + " left join fusion_sphere_host_port p on a.port_id = p.id "
                + " where  a.id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<FusionSphereUpLinkPort> list = this.queryForList(sql, params, FusionSphereUpLinkPort.class).stream().map(o -> (FusionSphereUpLinkPort) o).collect(Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

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

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

    public Boolean delete(Long id) throws BoCloudDBException {
        String sql = "delete from fusion_sphere_up_link_port 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_up_link_port where switcher_id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        return this.update(sql, params) > 0;
    }

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

}