package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.LbMember;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 负载均衡成员Repository接口实现
 *
 * @author weiwei
 */
@Repository("lbMemberRepository")
public class LbMemberRepository extends BasicGenericDao<LbMember, Long> {

	public LbMemberRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
		super(jdbcTemplate, npJdbcTemplate);
	}

	public List<LbMember> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.*,b.name subnet_name,t.name tenant_name, z.`name` zone_name from lb_member a left join tenant t on a.tenant_id = t.id left join subnet b on a.subnet_id = b.id left join zone z on z.zone_id = b.zone and z.vendor_id = b.vendor_id where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(LbMember.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name,a.member_uuid from lb_member a where a.is_deleted = 0";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<LbMember> members = this.list(LbMember.class, sql, paramMap);
		List<SimpleBean> beans = Lists.newArrayList();
		for (LbMember member : members) {
			beans.add(new SimpleBean(member.getId(), member.getName(), member.getMemberUuid()));
		}
		return beans;
	}

	public List<LbMember> listByVid(Long vendorId) throws Exception {
		String sql = "select a.*,t.name tenant_name from lb_member a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(LbMember.class, sql, params);
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from lb_member a where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update lb_member set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update lb_member set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public LbMember query(Long id) throws Exception {
		String sql = "select a.*,t.name tenant_name from lb_member a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<LbMember> list = this.list(LbMember.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByListenerId(Long listenerId, Long userId) throws Exception {
		String sql = "update lb_member set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and listener_id = :listenerId";
		Map<String, Object> params = MapTools.simpleMap("listenerId", listenerId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<LbMember> listByListenerId(Long listenerId) throws Exception {
		String sql = "select a.*,t.name tenant_name from lb_member a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.listener_id = :listenerId";
		Map<String, Object> params = MapTools.simpleMap("listenerId", listenerId);
		return this.list(LbMember.class, sql, params);
	}

	public boolean tenantCheck(Long tenantId, Long projectId, Long creatorId) throws Exception {
		String sql = "select count(1) from lb_member a where a.is_deleted = 0 ";
		Map<String, Object> paramMap = Maps.newHashMap();
		if (null != tenantId && 0L != tenantId) {
			sql += " and a.tenant_id = :tenantId";
			paramMap.put("tenantId", tenantId);
		}
		if (null != projectId && 0L != projectId) {
			sql += " and a.project_id = :projectId";
			paramMap.put("projectId", projectId);
		}
		if (null != creatorId && 0L != creatorId) {
			sql += " and a.creator_id = :creatorId";
			paramMap.put("creatorId", creatorId);
		}
		return this.countQuery(sql, paramMap).intValue() > 0;
	}

	public boolean remove(String address, Long subnetId, Long vendorId, Long userId) throws Exception {
		String sql = "update lb_member set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and address = :address and subnet_id = :subnetId and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("address", address);
		params.put("subnetId", subnetId);
		params.put("vendorId", vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public LbMember getByUuid(String memberUuid, Long vendorId) {
		String sql = " select a.* from lb_member a  where a.is_deleted = 0 and a.vendor_id = :vendorId and a.member_uuid = :memberUuid ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("memberUuid", memberUuid);
		List<LbMember> list = this.list(LbMember.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByVid(Long vendorId, Long userId, String regionId) throws Exception {
		String sql = "update lb_member set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and region_id = :regionId and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		params.put("regionId", regionId);
		return this.execute(sql, params) > 0;
	}

	public List<LbMember> listByVid(Long vendorId, String regionId) throws Exception {
		String sql = "select a.* from lb_member a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("regionId", regionId);
		return this.list(LbMember.class, sql, params);
	}

	public boolean removeByPoolId(Long poolId, Long userId) throws Exception {
		String sql = "update lb_member set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and pool_id = :poolId";
		Map<String, Object> params = MapTools.simpleMap("poolId", poolId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean updateByPoolId(Long poolId, Long userId, Long listenerId) throws Exception {
		String sql = "update lb_member set gmt_modify = :gmtModify,mender_id = :menderId,listener_id = :listenerId  where is_deleted = 0 and pool_id = :poolId";
		Map<String, Object> params = MapTools.simpleMap("poolId", poolId);
		params.put("listenerId", listenerId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean memberExist(Integer port, Long poolId, String address) throws Exception {
		String sql = "select count(1) from lb_member a where a.is_deleted = 0 and a.pool_id = :poolId and a.port = :port and address = :address ";
		Map<String, Object> paramMap = MapTools.simpleMap("port", port);
		paramMap.put("poolId", poolId);
		paramMap.put("address", address);
		return this.countQuery(sql, paramMap).intValue() > 0;
	}

	public List<LbMember> listByPoolId(Long poolId) throws Exception {
		String sql = "select a.* from lb_member a where a.is_deleted = 0 and a.pool_id = :poolId";
		Map<String, Object> params = MapTools.simpleMap("poolId", poolId);
		return this.list(LbMember.class, sql, params);
	}

	public boolean batchRemove(List<Long> ids, Long userId) throws Exception {
		String sql = "update lb_member set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id in(:ids)";
		Map<String, Object> params = MapTools.simpleMap("ids", ids);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<LbMember> listByInstanceId(String instanceId, Long vendorId) {
		String sql = "select a.* from lb_member a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.instance_id = :instanceId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("instanceId", instanceId);
		return this.list(LbMember.class, sql, params);
	}

}
