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

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

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: ResourceSubnetRelationRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/5/29 16:02
 */
@Repository
public class ResourceSubnetRelationRepository extends BasicDao {

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

    public Boolean update(ResourceSubnetRelation entity) {
        return this.updateEntity(entity);
    }

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

    public Boolean remove(ResourceSubnetRelation entity) throws Exception {
        return this.baseDelete(entity);
    }

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

    public Boolean removeByResourceId(long id, String type) {
        String sql = "delete from resource_subnet_relation where resource_id = :id and resource_type = :type ";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        params.put("type", type);
        return this.update(sql, params) > 0;
    }

    public Boolean removeBySubnetIdAndResourceId(Long subnetId, Long resourceId, String resourceType) {
        String sql = "delete from resource_subnet_relation where subnet_id = :subnetId and resource_id = :resourceId and resource_type = :resourceType ";
        Map<String, Object> params = MapTools.simpleMap("subnetId", subnetId);
        params.put("resourceId", resourceId);
        params.put("resourceType", resourceType);
        return this.update(sql, params) > 0;
    }

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

    public List<ResourceSubnetRelation> listByResourceId(long id, String type) {
        String sql = "select * from resource_subnet_relation where resource_id = :id and resource_type = :type ";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        params.put(Common.TYPE, type);
        return this.queryForList(sql, params, ResourceSubnetRelation.class).stream().map(o -> (ResourceSubnetRelation) o).collect(Collectors.toList());
    }

    public ResourceSubnetRelation queryBySubnetId(long id) {
        String sql = "select * from resource_subnet_relation where subnet_id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        List<ResourceSubnetRelation> list = this.queryForList(sql, params, ResourceSubnetRelation.class).stream().map(o -> (ResourceSubnetRelation) o).collect(Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public ResourceSubnetRelation queryByResourceId(long id, String type) {
        String sql = "select * from resource_subnet_relation where resource_id = :id and resource_type = :type ";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        params.put(Common.TYPE, type);
        List<ResourceSubnetRelation> list = this.queryForList(sql, params, ResourceSubnetRelation.class).stream().map(o -> (ResourceSubnetRelation) o).collect(Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }


}