package com.bocloud.ims.service.resource;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.loadbalance.LbDriver;
import com.bocloud.cmp.entity.enums.CloudProvider;
import com.bocloud.cmp.model.HealthMonitorModel;
import com.bocloud.cmp.model.LbMemberModel;
import com.bocloud.cmp.model.LbPoolModel;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.OperateResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.model.SimpleBean;
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.CloudServer;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.ElasticIp;
import com.bocloud.ims.entity.resource.HealthMonitor;
import com.bocloud.ims.entity.resource.LbListener;
import com.bocloud.ims.entity.resource.LbMember;
import com.bocloud.ims.entity.resource.LbPool;
import com.bocloud.ims.entity.resource.LoadBalancer;
import com.bocloud.ims.entity.resource.ResourceEvent;
import com.bocloud.ims.entity.resource.Subnet;
import com.bocloud.ims.interfaces.resource.LbPoolService;
import com.bocloud.ims.repository.resource.CloudServerRepository;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.HealthMonitorRepository;
import com.bocloud.ims.repository.resource.LbListenerRepository;
import com.bocloud.ims.repository.resource.LbMemberRepository;
import com.bocloud.ims.repository.resource.LbPoolRepository;
import com.bocloud.ims.repository.resource.LoadBalancerRepository;
import com.bocloud.ims.repository.resource.SubnetRepository;
import com.bocloud.ims.service.utils.ResourceEventPublisher;
import com.bocloud.service.lock.BocloudACLock;
import com.bocloud.service.lock.LockFactory;
import com.google.api.client.util.Lists;
import com.google.common.collect.Maps;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

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

/**
 * 负载均衡器成员池service实现
 *
 * @author lyy
 * @Version 1.0
 * @since 2017年9月4日
 */

@Service("lbPoolService")
public class LbPoolServiceImpl implements LbPoolService {

	private static Logger logger = LoggerFactory.getLogger(LbPoolServiceImpl.class);
	@Autowired
	private LbPoolRepository lbPoolRepository;
	@Autowired
	private CloudVendorRepository cloudVendorRepository;
	@Autowired
	private LbDriver lbDriver;
	@Autowired
	private LockFactory lockFactory;
	@Autowired
	private CloudServerRepository cloudServerRepository;
	@Autowired
	private LoadBalancerRepository loadBalancerRepository;
	@Autowired
	private LbMemberRepository lbMemberRepository;
	@Autowired
	private LbListenerRepository lbListenerRepository;
	@Autowired
	private HealthMonitorRepository healthMonitorRepository;
	@Autowired
	private SubnetRepository subnetRepository;
	@Autowired
	private ThreadPoolTaskExecutor taskExecutor;
	@Autowired
	private ResourceEventPublisher resourceEventPublisher;

	@Override
	public BocloudResult list(Integer page, Integer rows, List<Param> params, Map<String, String> sorter,
			boolean simple) {
		GridBean gridBean = null;
		List<SimpleBean> beans = null;
		try {
			if (null == params) {
				params = new ArrayList<>();
			}
			if (null == sorter) {
				sorter = Maps.newHashMap();
			}
			sorter.put("gmtCreate", Common.ONE);
			int total = lbPoolRepository.count(params);
			if (simple) {
				beans = lbPoolRepository.list(params, sorter);
				gridBean = new GridBean(1, 1, total, beans);
			} else {
				List<LbPool> list = lbPoolRepository.list(page, rows, params, sorter);
				processData(list);
				gridBean = GridHelper.getBean(page, rows, total, list);
			}
			return new BocloudResult<>(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("list lb pool failure:", e);
			return new BocloudResult<>(false, "查询失败");
		}
	}

	private void processData(List<LbPool> list) {
		try {
			if (ListTool.isEmpty(list)) {
				return;
			}
			LbPool lbPool = list.get(0);
			CloudVendor cloudVendor = cloudVendorRepository.query(lbPool.getVendorId());
			Assert.notNull(cloudVendor, "云平台不存在");
			if (CloudProvider.valueOf(cloudVendor.getType()) == CloudProvider.ALIYUN) {
				for (LbPool pool : list) {
					List<LbListener> listeners = lbListenerRepository.listByPoolUuid(cloudVendor.getId(),
							pool.getRegionId(), pool.getPoolUuid());
					if (!ListTool.isEmpty(listeners)) {
						String listenerData = "";
						for (LbListener lbListener : listeners) {
							listenerData += lbListener.getProtocol() + ":" + lbListener.getPort() + ";";
						}
						pool.setListeners(listenerData);
					}
				}
			}
		} catch (Exception e) {
			logger.error("阿里云查询服务器组异常：" + e.getMessage());
		}
	}

	@Override
	public BocloudResult create(LbPool lbPool, RequestContext context, Long vendorId) {
		try {
			CloudVendor vendor = cloudVendorRepository.query(vendorId);
			if (null == vendor) {
				return new BocloudResult<>(false, "云平台不存在");
			}
			BocloudResult bocloudResult;
			LbPoolModel plModel = new LbPoolModel();
			switch (CloudProvider.valueOf(vendor.getType())) {
			case ALIYUN:
				List<JSONObject> members = lbPool.getMembers().toJavaList(JSONObject.class);
				List<LbMemberModel> lbMembers = Lists.newArrayList();
				for (JSONObject member : members) {
					LbMemberModel lbMemberModel = new LbMemberModel();
					CloudServer cloudServer = cloudServerRepository.query(member.getLong("serverId"));
					lbMemberModel.setMemberUuid(cloudServer.getInstanceId());
					lbMemberModel.setWeight(member.getInteger("weight"));
					lbMemberModel.setType(member.getString("type"));
					lbMemberModel.setPort(member.getInteger("port"));
					lbMemberModel.setName(member.getString("name"));
					lbMembers.add(lbMemberModel);
				}
				LoadBalancer lb = loadBalancerRepository.queryByUuid(lbPool.getLbUuid());
				BeanUtils.copyProperties(lbPool, plModel);
				bocloudResult = lbDriver.createPool(vendor.getUuid(), lb.getRegion(), plModel, lbMembers);
				if (bocloudResult.isFailed()) {
					return new BocloudResult(false, bocloudResult.getMessage());
				}
				plModel = JSONObject.parseObject(JSONObject.toJSONString(bocloudResult.getData()), LbPoolModel.class);
				lbPool.setOwnerId(context.getTarget());
				lbPool.setCreatorId(context.getTarget());
				lbPool.setMenderId(context.getTarget());
				lbPool.setTenantId(context.getTenant());
				lbPool.setProjectId(context.getProject());
				lbPool.setPoolUuid(plModel.getPoolUuid());
				lbPool.setRegionId(lb.getRegion());
				lbPool.setLbId(lb.getId());
				lbPoolRepository.save(lbPool);
				// 保存member信息
				for (JSONObject member : members) {
					CloudServer cloudServer = cloudServerRepository.query(member.getLong("serverId"));
					LbMember lbMember = new LbMember();
					lbMember.setName(cloudServer.getName());
					lbMember.setMemberUuid(cloudServer.getInstanceId());
					lbMember.setPort(member.getInteger("port"));
					lbMember.setWeight(member.getInteger("weight"));
					lbMember.setPoolUuid(plModel.getPoolUuid());
					lbMember.setVendorId(vendorId);
					lbMember.setGmtCreate(new Date());
					String subnetUuid = cloudServer.getSubnetUuid();
					if (!StringUtils.isEmpty(subnetUuid)) {
						Subnet subnet = subnetRepository.query(subnetUuid, vendorId);
						if (null != subnet) {
							lbMember.setSubnetUuid(subnetUuid);
							lbMember.setSubnetId(subnet.getId());
						}
					}
					lbMember.setAddress(cloudServer.getPrivateIps());
					lbMember.setCreatorId(context.getTarget());
					lbMember.setRegionId(lbPool.getRegionId());
					lbMember.setPoolId(lbPool.getId());
					lbMember.setStatus("RUNNING");
					lbMemberRepository.save(lbMember);
				}
				return new BocloudResult(true, "创建成功");
			case HUAWEI:
				HealthMonitor healthMonitor = lbPool.getMonitor();
				plModel = new LbPoolModel();
				Long listenerId = lbPool.getListenerId();
				Long lbId = lbPool.getLbId();

				LbListener listener = lbListenerRepository.query(listenerId);
				LoadBalancer loadBalancer = loadBalancerRepository.query(lbId);
				if (null == listener && null == loadBalancer) {
					return new BocloudResult(false, "参数设置有误");
				}
				String regionId = "";
				BeanUtils.copyProperties(lbPool, plModel);
				if (null != loadBalancer) {
					plModel.setLbUuid(loadBalancer.getLbUuid());
					regionId = loadBalancer.getRegion();
				}

				if (null != listener) {
					plModel.setListenerUuid(listener.getListenerUuid());
					plModel.setLbUuid(listener.getLbUuid());
					regionId = listener.getRegionId();
				}

				String path = LbPool.class.getSimpleName() + "_" + listenerId;
				try (BocloudACLock lock = lockFactory.getACLock(path)) {
					Assert.isTrue(lock.acquire(600, TimeUnit.SECONDS), "请求超时");
					bocloudResult = lbDriver.createPool(vendor.getUuid(), regionId, plModel);
					if (bocloudResult.isFailed()) {
						return new BocloudResult(false, bocloudResult.getMessage());
					}
					plModel = JSONObject.parseObject(JSONObject.toJSONString(bocloudResult.getData()),
							LbPoolModel.class);
					BeanUtils.copyProperties(plModel, lbPool);

					if (null != healthMonitor) {
						// 创建健康检查
						HealthMonitorModel hmModel = new HealthMonitorModel();
						BeanUtils.copyProperties(healthMonitor, hmModel);
						hmModel.setPoolUuid(lbPool.getPoolUuid());
						bocloudResult = lbDriver.createHealMonitor(vendor.getUuid(), regionId, hmModel);
						if (bocloudResult.isFailed()) {
							return new BocloudResult(false, bocloudResult.getMessage());
						}
						hmModel = JSONObject.parseObject(JSONObject.toJSONString(bocloudResult.getData()),
								HealthMonitorModel.class);
						BeanUtils.copyProperties(hmModel, healthMonitor);

					}

					Long userId = context.getTarget();
					lbPool.setOwnerId(1L);
					lbPool.setCreatorId(userId);
					lbPool.setMenderId(userId);
					lbPool.setProjectId(context.getProject());
					lbPool.setVendorId(vendorId);
					lbPool.setRegionId(regionId);

					if (null != loadBalancer) {
						lbPool.setLbUuid(loadBalancer.getLbUuid());
						lbPool.setLbId(loadBalancer.getId());
					}

					if (null != listener) {
						lbPool.setListenerUuid(listener.getListenerUuid());
						lbPool.setLbUuid(listener.getLbUuid());
						lbPool.setListenerId(listener.getId());
						lbPool.setLbId(listener.getPoolId());
					}
					if (null != healthMonitor) {
						lbPool.setHmUuid(healthMonitor.getHealthUuid());
					}
					lbPoolRepository.save(lbPool);
					if (null != healthMonitor) {
						healthMonitor.setVendorId(vendorId);
						healthMonitor.setRegionId(regionId);
						healthMonitor.setCreatorId(userId);
						healthMonitor.setMenderId(userId);
						healthMonitor.setListenerId(lbPool.getListenerId());
						healthMonitor.setPoolId(lbPool.getId());
						healthMonitorRepository.save(healthMonitor);
					}

				} catch (Exception e) {
					return new BocloudResult(false, "创建失败");
				}
				return new BocloudResult(true, "创建成功");

			default:
				BeanUtils.copyProperties(lbPool, plModel);
				bocloudResult = lbDriver.createPool(vendor.getUuid(), null, plModel);
				if (bocloudResult.isFailed()) {
					return new BocloudResult(false, bocloudResult.getMessage());
				}
				plModel = JSONObject.parseObject(JSONObject.toJSONString(bocloudResult.getData()), LbPoolModel.class);
				BeanUtils.copyProperties(plModel, lbPool);
				lbPool.setOwnerId(1L);
				lbPool.setCreatorId(context.getTarget());
				lbPool.setMenderId(context.getTarget());
				lbPool.setTenantId(context.getTenant());
				lbPool.setProjectId(context.getProject());
				lbPoolRepository.save(lbPool);
				return new BocloudResult(true, "创建成功");
			}
		} catch (Exception e) {
			logger.error("create lb pool fail:", e);
			return new BocloudResult<>(false, "创建失败");
		}
	}

	@Override
	public BocloudResult modify(LbPool lbPool, RequestContext context, Long vendorId) {
		String path = LbPool.class.getSimpleName() + "_" + lbPool.getId();
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			CloudVendor vendor = cloudVendorRepository.query(vendorId);
			if (null == vendor) {
				return new BocloudResult<>(false, "云平台不存在");
			}
			LbPool pool = lbPoolRepository.query(lbPool.getId());
			if (null == pool) {
				return new BocloudResult<>(false, "数据不存在");
			}
			Long userId = context.getTarget();
			taskExecutor.execute(() -> {
				String operate = vendor.getType().toLowerCase() + ".lbPool";
				String content = "挂载硬盘失败";
				try {
					LbPoolModel plModel = new LbPoolModel();
					BeanUtils.copyProperties(lbPool, plModel);
					plModel.setPoolUuid(pool.getPoolUuid());
					BocloudResult bocloudResult;
					switch (CloudProvider.valueOf(vendor.getType())) {
					case HUAWEI:
						bocloudResult = lbDriver.updatePool(vendor.getUuid(), pool.getRegionId(), plModel);
						break;
					case ALIYUN:
						List<JSONObject> members = lbPool.getMembers().toJavaList(JSONObject.class);
						List<LbMemberModel> lbMembers = Lists.newArrayList();
						for (JSONObject member : members) {
							LbMemberModel lbMemberModel = new LbMemberModel();
							CloudServer cloudServer = cloudServerRepository.get(member.getString("memberUuid"),
									vendorId);
							lbMemberModel.setMemberUuid(cloudServer.getInstanceId());
							lbMemberModel.setWeight(member.getInteger("weight"));
							lbMemberModel.setType(member.getString("type"));
							lbMemberModel.setPort(member.getInteger("port"));
							lbMemberModel.setName(member.getString("name"));
							lbMembers.add(lbMemberModel);
						}
						bocloudResult = lbDriver.updatePool(vendor.getUuid(), pool.getRegionId(), plModel, lbMembers);
						break;
					default:
						bocloudResult = lbDriver.updatePool(vendor.getUuid(), null, plModel);
						break;
					}
					if (bocloudResult.isFailed()) {
						resourceEventPublisher.send(new OperateResult(false, bocloudResult.getMessage(), operate,
								MapTools.simpleMap("vendorId", vendor.getId()),
								OperateResult.OperateCategory.ResourceEvent, context));
						return;
					}
					plModel = JSONObject.parseObject(JSONObject.toJSONString(bocloudResult.getData()),
							LbPoolModel.class);
					switch (CloudProvider.valueOf(vendor.getType())) {
					case HUAWEI:
						pool.setName(plModel.getName());
						pool.setRemark(plModel.getRemark());
						pool.setLbMethod(plModel.getLbMethod());
						pool.setType(plModel.getType());
						pool.setCookieName(plModel.getCookieName());
						pool.setPersistenceTimeout(plModel.getPersistenceTimeout());
						break;
					case ALIYUN:
						pool.setName(plModel.getName());
						List<LbMember> lbMembers = lbMemberRepository.listByPoolId(pool.getId());
						if (null != lbMembers) {
							for (LbMember lbMember : lbMembers) {
								lbMemberRepository.remove(lbMember.getId(), userId);
							}
						}
						List<JSONObject> members = lbPool.getMembers().toJavaList(JSONObject.class);
						// 保存member信息
						for (JSONObject member : members) {
							CloudServer cloudServer = cloudServerRepository.get(member.getString("memberUuid"),
									vendorId);
							LbMember lbMember = new LbMember();
							lbMember.setName(cloudServer.getName());
							lbMember.setMemberUuid(cloudServer.getInstanceId());
							lbMember.setPort(member.getInteger("port"));
							lbMember.setWeight(member.getInteger("weight"));
							lbMember.setPoolUuid(plModel.getPoolUuid());
							lbMember.setVendorId(vendorId);
							lbMember.setRegionId(pool.getRegionId());
							lbMember.setGmtCreate(new Date());
							String subnetUuid = cloudServer.getSubnetUuid();
							if (!StringUtils.isEmpty(subnetUuid)) {
								Subnet subnet = subnetRepository.query(subnetUuid, vendorId);
								if (null != subnet) {
									lbMember.setSubnetUuid(subnetUuid);
									lbMember.setSubnetId(subnet.getId());
								}
							}
							lbMember.setAddress(cloudServer.getPrivateIps());
							lbMember.setCreatorId(userId);
							lbMember.setPoolId(lbPool.getId());
							lbMember.setStatus("RUNNING");
							lbMemberRepository.save(lbMember);
						}
						break;
					default:
						BeanUtils.copyProperties(plModel, pool);
						break;
					}
					pool.setMenderId(userId);
					lbPoolRepository.update(pool);
					resourceEventPublisher.send(
							new OperateResult(true, "编辑成功", operate, MapTools.simpleMap("vendorId", vendor.getId()),
									OperateResult.OperateCategory.ResourceEvent, context));
				} catch (Exception e) {
					logger.error("attach volume to cloud server fail:", e);
					resourceEventPublisher.send(new OperateResult(false, e.getMessage(), operate,
							MapTools.simpleMap("vendorId", vendor.getId()), OperateResult.OperateCategory.ResourceEvent,
							context));
				}
			});
			return new BocloudResult<>(true, "任务已下发，正在执行...");
		} catch (Exception e) {
			logger.error("update lb pool fail:", e);
			return new BocloudResult<>(false, "修改失败");
		}
	}

	@Override
	public BocloudResult detail(Long id) {
		try {
			LbPool lbPool = lbPoolRepository.query(id);
			return new BocloudResult<>(true, lbPool, "查询成功");
		} catch (Exception e) {
			logger.error("query lb pool fail:", e);
			return new BocloudResult<>(false, "查询失败");
		}
	}

	@Override
	public BocloudResult remove(Long id, Long userId) {
		try {
			LbPool pool = lbPoolRepository.query(id);
			if (null == pool) {
				return new BocloudResult(false, "数据不存在");
			}
			CloudVendor vendor = cloudVendorRepository.query(pool.getVendorId());
			if (null == vendor) {
				return new BocloudResult(false, "云平台不存在");
			}
			BocloudResult bocloudResult;
			switch (CloudProvider.valueOf(vendor.getType())) {
			case HUAWEI:
				bocloudResult = removeHUAWEI(id, userId);
				break;
			case ALIYUN:
				bocloudResult = removeALIYUN(id, userId);
				break;
			default:
				bocloudResult = removeNormal(id, userId);
				break;
			}
			return bocloudResult;
		} catch (Exception e) {
			logger.error("remove lb pool fail:", e);
			return new BocloudResult(false, "删除失败");
		}
	}

	private BocloudResult removeALIYUN(Long id, Long userId) {
		String path = LbPool.class.getSimpleName() + "_" + id;
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			LbPool pool = lbPoolRepository.query(id);
			if (null == pool) {
				return new BocloudResult(false, "数据不存在");
			}
			CloudVendor vendor = cloudVendorRepository.query(pool.getVendorId());
			if (null == vendor) {
				return new BocloudResult(false, "云平台不存在");
			}
			List<LbListener> listeners = lbListenerRepository.listByPoolUuid(vendor.getId(),
					pool.getRegionId(), pool.getPoolUuid());
			if (!ListTool.isEmpty(listeners)) {
				return new BocloudResult<>(false, "存在关联监听或关联转发策略，需要先解除监听或转发策略的关联关系，才可以删除该虚拟服务器组");
			}
			// 删除后端服务器组
			BocloudResult bocloudResult = lbDriver.removePool(vendor.getUuid(), pool.getRegionId(), pool.getPoolUuid());

			if (bocloudResult.isFailed()) {
				return new BocloudResult(false, bocloudResult.getMessage());
			}
			lbMemberRepository.removeByPoolId(pool.getId(), userId);
			// 删除本地成员记录
			lbPoolRepository.remove(id, userId);
			return new BocloudResult(true, "删除成功");
		} catch (Exception e) {
			logger.error("remove lb pool fail:", e);
			return new BocloudResult(false, "删除失败 ： " + e.getMessage());
		}
	}

	public BocloudResult removeHUAWEI(Long id, Long userId) {
		String path = LbPool.class.getSimpleName() + "_" + id;
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			LbPool pool = lbPoolRepository.query(id);
			if (null == pool) {
				return new BocloudResult(false, "数据不存在");
			}
			CloudVendor vendor = cloudVendorRepository.query(pool.getVendorId());
			if (null == vendor) {
				return new BocloudResult(false, "云平台不存在");
			}

			List<LbMember> members = lbMemberRepository.listByPoolId(id);

			if (CollectionUtils.isNotEmpty(members)) {
				return new BocloudResult(false, "当前后端服务器组中包含后端服务器，不能执行删除操作");
			}
			BocloudResult bocloudResult = new BocloudResult<>(false, "删除失败");
			HealthMonitor healthMonitor = healthMonitorRepository.getByPoolId(pool.getId());
			if (null != healthMonitor) {
				// 删除健康检查
				bocloudResult = lbDriver.removeHealMonitor(vendor.getUuid(), healthMonitor.getRegionId(),
						healthMonitor.getHealthUuid());
				if (bocloudResult.isFailed()) {
					return new BocloudResult(false, "删除失败");
				}
			}
			// 删除后端服务器组
			bocloudResult = lbDriver.removePool(vendor.getUuid(), pool.getRegionId(), pool.getPoolUuid());

			if (bocloudResult.isFailed()) {
				return new BocloudResult(false, bocloudResult.getMessage());
			}
			healthMonitorRepository.removeByPoolId(pool.getId(), userId);
			// lbMemberRepository.removeByPoolId(pool.getId(), userId);
			// 删除本地成员记录
			lbPoolRepository.remove(id, userId);
			return new BocloudResult(true, "删除成功");
		} catch (Exception e) {
			logger.error("remove lb pool fail:", e);
			return new BocloudResult(false, "删除失败");
		}
	}

	public BocloudResult removeNormal(Long id, Long userId) {
		String path = LbPool.class.getSimpleName() + "_" + id;
		try (BocloudACLock lock = lockFactory.getACLock(path)) {
			Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时");
			LbPool pool = lbPoolRepository.query(id);
			if (null == pool) {
				return new BocloudResult(false, "数据不存在");
			}
			CloudVendor vendor = cloudVendorRepository.query(pool.getVendorId());
			if (null == vendor) {
				return new BocloudResult(false, "云平台不存在");
			}
			BocloudResult bocloudResult = lbDriver.removePool(vendor.getUuid(), null, pool.getPoolUuid());
			if (bocloudResult.isFailed()) {
				return new BocloudResult(false, bocloudResult.getMessage());
			}
			// 删除本地成员记录
			lbPoolRepository.remove(id, userId);
			return new BocloudResult(true, "删除成功");
		} catch (Exception e) {
			logger.error("remove lb pool fail:", e);
			return new BocloudResult(false, "删除失败");
		}
	}

	@Override
	public BocloudResult listByListenerId(Long listenerId) {
		try {
			List<LbPool> lbPools = lbPoolRepository.listByListenerId(listenerId);
			return new BocloudResult<>(true, lbPools, "查询成功");
		} catch (Exception e) {
			logger.error("list lb pool fail:", e);
			return new BocloudResult<>(false, "查询失败");
		}
	}

}
