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.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.Icon;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 图标数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("resourceIconRepository")
public class ResourceIconRepository extends BasicGenericDao<Icon, Long> {

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

	public List<Icon> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from icon where category = 'IMAGE'";
		sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(Icon.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from icon a where a.category = 'IMAGE' ";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<Icon> list = this.list(Icon.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();
		for (Icon object : list) {
			beans.add(new SimpleBean(object.getId(), object.getName()));
		}
		return beans;
	}

	public Icon query(Long id) throws Exception {
		String sql = "select * from icon where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Icon> icons = this.list(Icon.class, sql, params);
		return ListTool.isEmpty(icons)?null:icons.get(0);
	}


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

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

}
