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.Label;
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;

@Repository("labelRepository")
public class LabelRepository extends BasicGenericDao<Label, Long> {

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

	public List<Label> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.* from label 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(Label.class, sql, param);
	}

	public List<Label> list(List<Param> params) throws Exception {
		String sql = "select a.* from label 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(Label.class, sql, param);
	}

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

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

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

	public boolean exists(String name) throws Exception {
		String sql = "select * from label where name = :name";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		List<Label> labels = this.list(Label.class, sql, paramMap);
		if (ListTool.isEmpty(labels)) {
			return false;
		}
		return true;
	}

	public boolean exists(String name, String content, String category) throws Exception {
		String sql = "select * from label where name = :name and content = :content and category = :category";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		paramMap.put("content", content);
		paramMap.put("category", category);
		List<Label> labels = this.list(Label.class, sql, paramMap);
		if (ListTool.isEmpty(labels)) {
			return false;
		}
		return true;
	}

	public Label getLabel(String name, String content, String category) throws Exception {
		String sql = "select * from label where name = :name and content = :content and category = :category";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		paramMap.put("content", content);
		paramMap.put("category", category);
		List<Label> labels = this.list(Label.class, sql, paramMap);
		if (ListTool.isEmpty(labels)) {
			return null;
		}
		return labels.get(0);
	}
	
	public Label getLabelByName(String name) throws Exception {
		String sql = "select * from label where name = :name ";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		List<Label> labels = this.list(Label.class, sql, paramMap);
		if (ListTool.isEmpty(labels)) {
			return null;
		}
		return labels.get(0);
	}

}
