package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.SecurityGroupRule;
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("securityGroupRuleRepository")
public class SecurityGroupRuleRepository extends BasicGenericDao<SecurityGroupRule, Long> {

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

	public List<SecurityGroupRule> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.*,t.name tenant_name from security_group_rule a left join tenant t on a.tenant_id = t.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(SecurityGroupRule.class, sql, paramMap);
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from security_group_rule 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 List<SecurityGroupRule> list(String direction, Long groupId, Integer sgrIndex) throws Exception {
		String sql = "select a.*,t.name tenant_name from security_group_rule a "
				+ "left join tenant t on a.tenant_id = t.id "
				+ "where a.is_deleted = 0 and a.direction = :direction and a.group_id = :groupId and a.sgr_index >= :sgrIndex";
		Map<String, Object> params = MapTools.simpleMap("direction", direction);
		params.put("groupId", groupId);
		params.put("sgrIndex", sgrIndex);
		List<SecurityGroupRule> list = this.list(SecurityGroupRule.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list;
	}

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update security_group_rule 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 security_group_rule set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;

	}

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

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

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

	public boolean tenantCheck(Long tenantId, Long projectId, Long creatorId) throws Exception {
		String sql = "select count(1) from security_group_rule 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 List<SecurityGroupRule> listBySgId(Long sgId, String direction) throws Exception {
		String sql = "select a.*,t.name tenant_name from security_group_rule a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.group_id =:groupId and a.direction =:direction";
		Map<String, Object> params = MapTools.simpleMap("groupId", sgId);
		params.put("direction", direction);
		return this.list(SecurityGroupRule.class, sql, params);
	}

    public boolean deleteByVid(Long vendorId, Long userId, List<Long> groupIds) {
		String sql =
				"update security_group_rule set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		if (null != groupIds && groupIds.size() > 0) {
			sql += " and group_id in ( :groupIds ) ";
			params.put("groupIds", groupIds);
		}
		return this.execute(sql, params) > 0;
    }
}
