package com.bocloud.ims.service.resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.secuirty.FirewallDriver;
import com.bocloud.cmp.entity.enums.CloudProvider;
import com.bocloud.cmp.model.FirewallModel;
import com.bocloud.cmp.model.FirewallPolicyModel;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.OperateResult;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.model.Sign;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.model.RequestContext.Catalog;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.Firewall;
import com.bocloud.ims.entity.resource.FirewallPolicy;
import com.bocloud.ims.entity.resource.OSTenant;
import com.bocloud.ims.entity.resource.Router;
import com.bocloud.ims.entity.resource.SecurityGroup;
import com.bocloud.ims.entity.resource.TenantProvider;
import com.bocloud.ims.entity.resource.manageone.ManageOneAccount;
import com.bocloud.ims.interfaces.manageone.ManageOneAccountService;
import com.bocloud.ims.interfaces.resource.FirewallService;
import com.bocloud.ims.interfaces.resource.VendorProjectService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.FirewallPolicyRepository;
import com.bocloud.ims.repository.resource.FirewallRepository;
import com.bocloud.ims.repository.resource.FirewallRuleRepository;
import com.bocloud.ims.repository.resource.OSTenantRepository;
import com.bocloud.ims.repository.resource.RouterRepository;
import com.bocloud.ims.service.utils.ResourceEventPublisher;
import com.bocloud.service.lock.BocloudACLock;
import com.bocloud.service.lock.LockFactory;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 防火墙service实现
 *
 * @author wangyu
 * @version 1.0
 * @since 2017年8月29日
 */
@Service("firewallService")
public class FirewallServiceImpl implements FirewallService {

	private static Logger logger = LoggerFactory.getLogger(FirewallServiceImpl.class);
	@Autowired
	private FirewallRepository firewallRepository;
	@Autowired
	private FirewallPolicyRepository firewallPolicyRepository;
	@Autowired
	private FirewallDriver firewallDriver;
	@Autowired
	private RouterRepository routerRepository;
	@Autowired
	private CloudVendorRepository cloudVendorRepository;
	@Autowired
	private OSTenantRepository osTenantRepository;
	@Autowired
	private VendorProjectService vendorProjectService;
	@Autowired
	private AmqpTemplate amqpTemplate;
	@Autowired
	private ResourceEventPublisher resourceEventPublisher;
	@Autowired
	private FirewallRuleRepository firewallRuleRepository;
	@Autowired
	private LockFactory lockFactory;
	@Autowired
	private ManageOneAccountService accountService;

	@Override
	public BocloudResult<GridBean<Firewall>> list(Pager pager, RequestContext context) {
		GridBean gridBean = null;
		try {
			List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
			Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
			Boolean simple = Optional.ofNullable(pager.getSimple()).orElse(false);
			sorter.put("gmtCreate", Common.ONE);
			Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
			Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
			if (0 != context.getTenant()) {
				params.add(new Param(MapTools.simpleMap(Common.TENANTID, context.getTenant()), Sign.EQ));
			}
			Catalog catalog = context.getCatalog();
			String vendorType = null;
			if (Catalog.Tenant == catalog || Catalog.User == catalog) {
				for (Param param : params) {
					Map<String, Object> paramMap = param.getParam();
					if (paramMap.containsKey("vendorType")) {
						vendorType = String.valueOf(paramMap.get("vendorType"));
						paramMap.remove("vendorType");
					}
				}
			}
			int total = firewallRepository.count(params, vendorType);
			if (simple) {
				if (0 == context.getTenant()) {
					params.add(new Param(MapTools.simpleMap(Common.TENANTID, 0), Sign.EQ));
				}
				List<SimpleBean> beans = firewallRepository.list(params, sorter);
				gridBean = new GridBean(1, 1, total, beans);
			} else {
				List<Firewall> list = this.firewallRepository.list(page, rows, params, sorter, vendorType);
				for (Firewall firewall : list) {
					// 设置路由名称
					List<Router> routers = routerRepository.listByFirewallId(firewall.getId(), firewall.getVendorId());
					if (!routers.isEmpty()) {
						String routerNames = "";
						for (Router router : routers) {
							routerNames += router.getName() + ",";
						}
						firewall.setRouterNames(routerNames.substring(0, routerNames.length() - 1));
					}
				}
				gridBean = GridHelper.getBean(page, rows, total, list);
			}

			return new BocloudResult<>(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("list firewall failure:", e);
			return new BocloudResult<>(false, "查询失败");
		}
	}

	@Override
	public BocloudResult<Firewall> create(Firewall firewall, RequestContext context, String callback) {
		try {
			Long userId = context.getTarget();
			Long vendorId = firewall.getVendorId();
			Firewall exist = firewallRepository.getByName(firewall.getName(), vendorId);
			if (null != exist) {
				return new BocloudResult<Firewall>(false, "防火墙名称已存在");
			}
			CloudVendor vendor = cloudVendorRepository.query(firewall.getVendorId());
			if (null == vendor) {
				return new BocloudResult<Firewall>(false, "云平台不存在");
			}
			if (CloudProvider.MANAGEONE.name().equals(vendor.getType())) {
				return manageOneCreate(firewall, context, vendor);
			}
			FirewallPolicy policy = firewallPolicyRepository.query(firewall.getPolicyId());
			if (null == policy) {
				return new BocloudResult<Firewall>(false, "防火墙策略不存在");
			}
			List<Long> routerIds = firewall.getRouterIds();
			List<String> routerUuids = Lists.newArrayList();
			// 设置路由
			if (!ListTool.isEmpty(routerIds)) {
				for (Long routerId : routerIds) {
					Router router = routerRepository.query(routerId);
					routerUuids.add(router.getRouterUuid());
				}
			}
			FirewallModel model = JSONObject.parseObject(JSONObject.toJSONString(firewall), FirewallModel.class);
			model.setAdminStateUp(true);
			model.setRouterUuids(routerUuids);
			model.setPolicyUuid(policy.getPolicyUuid());
			new Thread(() -> {
				String operate = vendor.getType().toLowerCase() + ".fw";
				BocloudResult result = new BocloudResult<>(false, "创建防火墙失败");
				Firewall fw = null;
				try {
					if (CloudProvider.MANAGEONE.name().equals(vendor.getType()) && null != context.getTenant()
							&& 0L != context.getTenant()) {
						ManageOneAccount account = accountService.queryAccount(vendorId, context.getTenant());
						Assert.notNull(account, "认证失败！");
						result = firewallDriver.create(vendor.getUuid(), account.getUsername(), account.getPassword(),
								account.getProjectId(), account.getDomainName(), model);
					} else {
						TenantProvider provider = vendorProjectService.getAncestorProject(vendor.getId(),
								context.getTenant());
						if (null == provider) {
							result = firewallDriver.create(vendor.getUuid(), firewall.getOsTenantName(), null, model);
						} else {
							result = firewallDriver.create(vendor.getUuid(), provider.getProjectName(), null, model);
						}
					}
					if (result.isSuccess()) {
						fw = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), Firewall.class);
						fw.setPolicyId(policy.getId());// 防火墙策略ID
						fw.preSave(userId, userId, vendorId, context.getTenant(), context.getProject());
						firewallRepository.save(fw);
						if (!ListTool.isEmpty(routerIds)) {
							for (Long routerId : routerIds) {
								// 更新路由防火墙状态
								routerRepository.updateFirewallByRouterId(routerId, fw.getId(), fw.getFirewallUuid());
							}
						}
					}
					resourceEventPublisher.send(new OperateResult(result.isSuccess(), result.getMessage(), operate,
							MapTools.simpleMap("vendorId", vendorId), OperateResult.OperateCategory.ResourceEvent,
							context));
				} catch (Exception e) {
					logger.error("create firewall fail:", e);
					resourceEventPublisher
							.send(new OperateResult(false, "创建防火墙失败", operate, MapTools.simpleMap("vendorId", vendorId),
									OperateResult.OperateCategory.ResourceEvent, context));
				}
				if (!StringUtils.isEmpty(callback)) {
					BocloudResult bocloudResult = new BocloudResult<>(result.isSuccess(), result.getMessage());
					if (result.isSuccess()) {
						bocloudResult.setData(fw);
					}
					amqpTemplate.convertAndSend(callback, JSONObject.toJSON(bocloudResult));
				}
			}).start();
			return new BocloudResult<Firewall>(true, firewall, "创建防火墙任务已下发，正在执行...");
		} catch (Exception e) {
			logger.error("create firewall fail:", e);
			return new BocloudResult<Firewall>(false, "创建防火墙失败：" + e.getMessage());
		}
	}

	private BocloudResult manageOneCreate(Firewall firewall, RequestContext context, CloudVendor vendor) {
		try {
			Long userId = context.getTarget();
			Long vendorId = firewall.getVendorId();
			Long tenant = context.getTenant();
			FirewallModel model = JSONObject.parseObject(JSONObject.toJSONString(firewall), FirewallModel.class);
			model.setAdminStateUp(true);
			new Thread(() -> {
				String operate = vendor.getType().toLowerCase() + ".fw";
				BocloudResult result = new BocloudResult<>(false, "创建防火墙失败");
				Firewall fw = null;
				try {
					if (null != tenant && 0L != tenant) {
						ManageOneAccount account = accountService.queryAccount(vendor.getId(), tenant);
						Assert.notNull(account, "认证失败！");
						result = firewallDriver.create(vendor.getUuid(), account.getUsername(), account.getPassword(),
								account.getProjectId(), account.getDomainName(), model);
					} else {
						result = firewallDriver.create(vendor.getUuid(), null, model);
					}
					if (result.isSuccess()) {
						FirewallModel firewallModel = JSONObject.parseObject(JSONObject.toJSONString(result.getData()),
								FirewallModel.class);
						fw = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), Firewall.class);
						FirewallPolicyModel firewallPolicyModel = firewallModel.getFirewallPolicyModel();
						if (null != firewallPolicyModel) {
							FirewallPolicy firewallPolicy = new FirewallPolicy();
							BeanUtils.copyProperties(firewallPolicyModel, firewallPolicy);
							firewallPolicy.preSave(userId, userId, vendorId, context.getTenant(), context.getProject());
							firewallPolicy.setVendorType(vendor.getType());
							firewallPolicyRepository.save(firewallPolicy);
							firewall.setPolicyId(firewallPolicy.getId());
							firewall.setPolicyUuid(firewallPolicy.getPolicyUuid());
						}
						fw.preSave(userId, userId, vendorId, context.getTenant(), context.getProject());
						firewallRepository.save(fw);
					}
					resourceEventPublisher.send(new OperateResult(result.isSuccess(), result.getMessage(), operate,
							MapTools.simpleMap("vendorId", vendorId), OperateResult.OperateCategory.ResourceEvent,
							context));
				} catch (Exception e) {
					logger.error("create firewall fail:", e);
					resourceEventPublisher
							.send(new OperateResult(false, "创建防火墙失败", operate, MapTools.simpleMap("vendorId", vendorId),
									OperateResult.OperateCategory.ResourceEvent, context));
				}
			}).start();
			return new BocloudResult<>(true, "创建防火墙任务已下发，正在执行...");
		} catch (Exception e) {
			logger.error("create firewall fail:", e);
			return new BocloudResult<>(false, "创建防火墙失败");
		}
	}

	@Override
	public BocloudResult<Firewall> modify(Firewall bean, Long userId) {
		String path = Firewall.class.getSimpleName() + "_" + bean.getId();
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			CloudVendor vendor = cloudVendorRepository.query(bean.getVendorId());
			if (null == vendor) {
				return new BocloudResult<>(false, "云平台不存在");
			}
			Firewall firewall = firewallRepository.query(bean.getId());
			if (null == firewall) {
				return new BocloudResult<Firewall>(false, "防火墙数据不存在");
			}
			// 修改防火墙信息时名称不能与已有名称重复
			if (!bean.getName().equals(firewall.getName())) {
				Firewall exist = firewallRepository.getByName(bean.getName(), firewall.getVendorId());
				if (null != exist) {
					return new BocloudResult<Firewall>(false, "防火墙名称已存在");
				}
			}
			FirewallPolicy policy = null;
			FirewallModel model = JSONObject.parseObject(JSONObject.toJSONString(bean), FirewallModel.class);
			if (!CloudProvider.MANAGEONE.name().equals(vendor.getType())) {
				// 查询防火墙策略
				policy = firewallPolicyRepository.query(bean.getPolicyId());
				if (null == policy) {
					return new BocloudResult<Firewall>(false, "防火墙策略数据不存在");
				}
				model.setPolicyUuid(policy.getPolicyUuid());
			}
			model.setFirewallUuid(firewall.getFirewallUuid());
			model.setAdminStateUp(firewall.getAdminStateUp());
			BocloudResult result = firewallDriver.update(vendor.getUuid(), null, model);
			if (result.isFailed()) {
				return new BocloudResult<Firewall>(false, result.getMessage());
			}
			model = JSONObject.parseObject(JSONObject.toJSONString(result.getData()), FirewallModel.class);
			firewall.setMenderId(userId);
			firewall.setName(model.getName());
			firewall.setRemark(model.getRemark());
			if (null != policy) {
				firewall.setPolicyId(policy.getId());// 防火墙策略ID
				firewall.setPolicyUuid(policy.getPolicyUuid());// 防火墙策略UUID
			}
			firewallRepository.update(firewall);
			return new BocloudResult<Firewall>(true, firewall, "修改成功");
		} catch (Exception e) {
			logger.error("modify firewall fail:", e);
			return new BocloudResult<Firewall>(false, "修改防火墙失败");
		}
	}

	@Override
	public BocloudResult<Firewall> detail(Long id) {
		try {
			Firewall firewall = firewallRepository.query(id);
			if (null != firewall) {
				// 设置路由名称
				List<Router> routers = routerRepository.listByFirewallId(firewall.getId(), firewall.getVendorId());
				if (!routers.isEmpty()) {
					String routerNames = "";
					for (Router router : routers) {
						routerNames += router.getName() + ",";
					}
					firewall.setRouterNames(routerNames.substring(0, routerNames.length() - 1));
				}
				if (!StringUtils.isEmpty(firewall.getTenantUuid())) {
					OSTenant osTenant = osTenantRepository.getByTenantUuid(firewall.getTenantUuid(),
							firewall.getVendorId());
					if (null != osTenant) {
						firewall.setOsTenantName(osTenant.getName());
					}
				}
			}
			return new BocloudResult<Firewall>(true, firewall, "查询成功");
		} catch (Exception e) {
			logger.error("query firewall fail:", e);
			return new BocloudResult<Firewall>(false, "查询防火墙失败");
		}
	}

	@Override
	public BocloudResult relevanceRouter(Long id, FirewallModel fwModel, RequestContext context) {
		String path = Firewall.class.getSimpleName() + "_" + id;
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			Firewall firewall = firewallRepository.query(id);
			if (null == firewall) {
				return new BocloudResult<>(false, "防火墙数据不存在");
			}
			CloudVendor vendor = cloudVendorRepository.query(firewall.getVendorId());
			if (null == vendor) {
				return new BocloudResult<>(false, "云平台不存在");
			}
			String firewallUuid = firewall.getFirewallUuid();// 防火墙UUID
			List<Long> routerIds = fwModel.getRouterIds();// 路由ID
			List<String> routerUuids = Lists.newArrayList();// 路由UUID
			if (!ListTool.isEmpty(routerIds)) {
				for (Long routerId : routerIds) {
					Router router = routerRepository.query(routerId);
					if (null == router) {
						return new BocloudResult<>(false, "ID为【" + routerId + "】的路由不存在");
					}
					routerUuids.add(router.getRouterUuid());
				}
			}
			FirewallModel model = new FirewallModel();
			BeanUtils.copyProperties(firewall, model);
			model.setFirewallUuid(firewallUuid);
			model.setRouterUuids(routerUuids);
			model.setName(firewall.getName());
			model.setRemark(firewall.getRemark());
			model.setShared(firewall.getShared());
			model.setPolicyUuid(firewall.getPolicyUuid());
			model.setOperation(fwModel.getOperation());
			new Thread(() -> {
				String operate = vendor.getType().toLowerCase() + ".fwRouter";
				String message = "关联路由失败";
				try {
					BocloudResult result = firewallDriver.relevanceRouter(vendor.getUuid(), null, model);
					if (result.isSuccess()) {
						message = result.getMessage();
						FirewallModel firewallModel = JSONObject.parseObject(JSONObject.toJSONString(result.getData()),
								FirewallModel.class);
						firewall.setFirewallUuid(firewallModel.getFirewallUuid());
						firewall.setStatus(firewallModel.getStatus());
						// 更新防火墙UUID
						firewallRepository.update(firewall);
						// 重置路由
						routerRepository.updateFirewallIdAndUuid(id);
						List<String> newRouterUuids = firewallModel.getRouterUuids();
						if (!ListTool.isEmpty(newRouterUuids)) {
							for (String routerUuid : newRouterUuids) {
								// 更新路由防火墙状态
								routerRepository.updateFirewallByRouterId(routerUuid, id, firewallUuid);
							}
						}
					} else {
						message += result.getMessage();
					}
					resourceEventPublisher.send(new OperateResult(result.isSuccess(), message, operate,
							MapTools.simpleMap("vendorId", vendor.getId()), OperateResult.OperateCategory.ResourceEvent,
							context));
				} catch (Exception e) {
					logger.error("create firewall fail:", e);
					resourceEventPublisher.send(
							new OperateResult(false, message, operate, MapTools.simpleMap("vendorId", vendor.getId()),
									OperateResult.OperateCategory.ResourceEvent, context));
				}
			}).start();
			return new BocloudResult<>(true, "任务已下发，正在执行...");
		} catch (Exception e) {
			logger.error("update firewall router fail:", e);
			return new BocloudResult<>(false, "VPC关联失败！");
		}
	}

	@Override
	public BocloudResult remove(Long id, RequestContext context) {
		String path = Firewall.class.getSimpleName() + "_" + id;
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			Firewall firewall = firewallRepository.query(id);
			if (null == firewall) {
				return new BocloudResult<>(false, "防火墙数据不存在");
			}
			if (0L == context.getTenant() && 0L != firewall.getTenantId()) {
				return new BocloudResult<>(false, "不能删除租户下的防火墙");
			}
			CloudVendor vendor = cloudVendorRepository.query(firewall.getVendorId());
			if (null == vendor) {
				return new BocloudResult<>(false, "云平台不存在");
			}
			new Thread(new Runnable() {
				@Override
				public void run() {
					String operate = vendor.getType().toLowerCase() + ".fw";
					BocloudResult bocloudResult = new BocloudResult<>(false, "防火墙删除失败！");
					try {
						// 删除防火墙
						bocloudResult = firewallDriver.remove(firewall.getFirewallUuid(), vendor.getUuid(), null);
						if (bocloudResult.isFailed()) {
							resourceEventPublisher.send(new OperateResult(bocloudResult.isSuccess(),
									bocloudResult.getMessage(), operate, MapTools.simpleMap("vendorId", vendor.getId()),
									OperateResult.OperateCategory.ResourceEvent, context));
							return;
						}
						// 删除本地防火墙数据
						firewallRepository.remove(id, context.getTarget());
						// 更新路由绑定防火墙信息
						routerRepository.updateFirewallIdAndUuid(id);
						if (CloudProvider.MANAGEONE.name().equals(vendor.getType())) {
							firewallPolicyRepository.remove(firewall.getPolicyId(), context.getTarget());
							firewallRuleRepository.removeByFirewallId(vendor.getId(), firewall.getId(),
									context.getTarget());
						}
						resourceEventPublisher.send(new OperateResult(true, "删除成功！", operate,
								MapTools.simpleMap("vendorId", vendor.getId()),
								OperateResult.OperateCategory.ResourceEvent, context));
					} catch (Exception e) {
						logger.error("防火墙删除失败：" + e.getMessage());
						resourceEventPublisher.send(new OperateResult(false, "删除失败：" + e.getMessage(), operate,
								MapTools.simpleMap("vendorId", vendor.getId()),
								OperateResult.OperateCategory.ResourceEvent, context));
					}
				}
			}).start();
			return new BocloudResult<>(true, "删除任务已下发，正在执行......");
		} catch (Exception e) {
			logger.error("remove firewall fail:", e);
			return new BocloudResult<>(false, "删除防火墙失败");
		}
	}

	@Override
	public BocloudResult<Map<String, Object>> listRouter(Long id, Long vendorId, String tenantUuid, Long tenantId) {
		try {
			List<Router> list = routerRepository.listRouterByNoBoundFirewall(vendorId, tenantId, tenantUuid);
			Map<String, Object> resultMap = MapTools.simpleMap("routers", list);
			// 查询防火墙绑定的路由
			if (null != id && id != 0L) {
				List<Router> routers = routerRepository.listByFirewallId(id);
				resultMap.put("fwRouters", routers);
			}
			return new BocloudResult<>(true, resultMap, "查询成功");
		} catch (Exception e) {
			logger.error("list router fail:", e);
			return new BocloudResult<>(false, "查询路由失败");
		}
	}

	@Override
	public BocloudResult batchRemove(List<Long> idArray, RequestContext context) {
		if (ListTool.isEmpty(idArray)) {
			return new BocloudResult<>(false, "没有需要删除的防火墙！");
		}
		try {
			List<BocloudResult> succList = Lists.newArrayList();
			List<BocloudResult> failList = Lists.newArrayList();
			Long vendorId = null;
			for (Long id : idArray) {
				Firewall firewall = firewallRepository.query(id);
				if (null != firewall) {
					if (null == vendorId) {
						vendorId = firewall.getVendorId();
					}
					BocloudResult result = remove(id, context);
					if (result.isSuccess()) {
						succList.add(new BocloudResult<>(true, "【" + firewall.getName() + "】删除成功"));
					} else {
						failList.add(
								new BocloudResult<>(false, "【" + firewall.getName() + "】删除失败：" + result.getMessage()));
					}
				}
			}
			CloudVendor vendor = cloudVendorRepository.query(vendorId);
			String operate = vendor.getType().toLowerCase() + ".firewall";
			// 删除成功的数量
			int success = succList.size();
			// 删除失败的数量
			int fail = failList.size();
			String content = null;
			if (fail == idArray.size()) {
				content = "防火墙批量删除失败，详情如下：\n";
				for (BocloudResult bocloudResult : failList) {
					content += bocloudResult.getMessage() + "\n";
				}
				resourceEventPublisher
						.send(new OperateResult(false, content, operate, MapTools.simpleMap("vendorId", vendor.getId()),
								OperateResult.OperateCategory.ResourceEvent, context));
			} else {
				content = "防火墙批量删除成功【" + success + "】个,失败【" + fail + "】个，详情如下：\n";
				for (BocloudResult bocloudResult : succList) {
					content += bocloudResult.getMessage() + "\n";
				}
				for (BocloudResult bocloudResult : failList) {
					content += bocloudResult.getMessage() + "\n";
				}
				resourceEventPublisher
						.send(new OperateResult(true, content, operate, MapTools.simpleMap("vendorId", vendor.getId()),
								OperateResult.OperateCategory.ResourceEvent, context));
			}
		} catch (Exception e) {
			logger.error("batch remove firewall fail:", e);
			return new BocloudResult<>(false, "批量删除任务下发失败");
		}
		return new BocloudResult<>(true, "批量删除任务已下发，正在执行...");
	}
}
