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

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: ResourcePublicIPAddressRelationRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/1/6 17:25
 */
@Repository("resourcePublicIPAddressRelationRepository")
public class ResourcePublicIPAddressRelationRepository extends BasicDao {


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

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

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


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

    public Boolean removeByIpId(Long id) {
        String sql = "delete from resource_public_ip_address_relation where ip_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_public_ip_address_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 List<ResourcePublicIPAddressRelation> listByIpId(long id) {
        String sql = "select * from resource_public_ip_address_relation where ip_id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.queryForList(sql, params, ResourcePublicIPAddressRelation.class).stream().map(o -> (ResourcePublicIPAddressRelation) o).collect(Collectors.toList());
    }

    public List<ResourcePublicIPAddressRelation> listByResourceId(long id, String type) {
        String sql = "select * from resource_public_ip_address_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, ResourcePublicIPAddressRelation.class).stream().map(o -> (ResourcePublicIPAddressRelation) o).collect(Collectors.toList());
    }


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


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


}