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

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

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

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

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

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

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

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

    public int count(List<Param> params) throws Exception {
        String sql = "select count(1) "
                + " from fusion_sphere_up_link a "
                + " left join server s on a.host_id = s.id "
                + " left join fusion_sphere_host_port p on a.port_id = p.id "
                + " left join switcher w on a.switcher_id = w.id "
                + " 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<FusionSphereUpLink> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
        String sql = "select a.*, s.name host_name,p.name port_name ,w.name switcher_name,p.port_uuid"
                + " from fusion_sphere_up_link a "
                + " left join server s on a.host_id = s.id "
                + " left join fusion_sphere_host_port p on a.port_id = p.id "
                + " left join switcher w on a.switcher_id = w.id "
                + " where 1=1";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.queryForList(sql, FusionSphereUpLink.class).stream().map(o -> (FusionSphereUpLink) o).collect(Collectors.toList());
    }
}