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

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

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

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

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

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


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

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


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

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

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


}