package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.ListTool;
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.NameRule;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 命名规则数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("nameRuleRepository")
public class NameRuleRepository extends BasicGenericDao<NameRule, Long> {

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

	public List<NameRule> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.* from name_rule a where 1 = 1 ";
		sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.list(NameRule.class, sql, param);
	}

	public List<NameRule> list(List<Param> params) throws Exception {
		String sql = "select a.* from name_rule a  where 1 = 1 ";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, new HashMap<>(8)), "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.list(NameRule.class, sql, param);
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from name_rule where 1 = 1 ";
		sql = QueryBuilder.build(sql, params, null, "");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	public boolean remove(Long id) throws Exception {
		String sql = "delete from name_rule where id = :id ";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		return this.execute(sql, params) > 0;
	}

	public NameRule query(Long id) throws Exception {
		String sql = "select * from name_rule where id = :id";
		Map<String, Object> paramMap = MapTools.simpleMap("id", id);
		List<NameRule> nameRules = this.list(NameRule.class, sql, paramMap);
		return ListTool.isEmpty(nameRules) ? null : nameRules.get(0);
	}

	public boolean exists(String name, Long tenantId) throws Exception {
		String sql = "select * from name_rule where name = :name and tenant_id = :tenantId";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		paramMap.put("tenantId", tenantId);
		List<NameRule> nameRules = this.list(NameRule.class, sql, paramMap);
		if (ListTool.isEmpty(nameRules)) {
			return false;
		}
		return true;
	}

	/**
	 * 判断命名规则是否已存在
	 *
	 * @param hasTenant
	 * @param hasUser
	 * @param customize
	 * @param suffixType
	 * @param tenantId
	 * @return
	 * @throws Exception
	 */
	public boolean exists(Boolean hasTenant, Boolean hasUser, Boolean hasBusiness, Boolean hasProject, String customize,
			String suffixType, Long tenantId) throws Exception {
		String sql = "select * from name_rule where has_tenant = :hasTenant and has_user = :hasUser and has_business = :hasBusiness and has_project = :hasProject and suffix_type = :suffixType and tenant_id = :tenantId";
		Map<String, Object> paramMap = MapTools.simpleMap("hasTenant", hasTenant);
		paramMap.put("hasUser", hasUser);
		paramMap.put("hasBusiness", hasBusiness);
		paramMap.put("hasProject", hasProject);
		if (null == customize) {
			sql += " and customize is null";
		} else {
			sql += " and customize = :customize";
			paramMap.put("customize", customize);
		}
		paramMap.put("suffixType", suffixType);
		paramMap.put("tenantId", tenantId);
		List<NameRule> nameRules = this.list(NameRule.class, sql, paramMap);
		if (ListTool.isEmpty(nameRules)) {
			return false;
		}
		return true;
	}

}
