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.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.DataCenter;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * 数据中心抽象数据访问实现类
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("dataCenterRepository")
public class DataCenterRepository extends BasicGenericDao<DataCenter, Long> {

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

	public DataCenter query(Long id) throws Exception {
		String sql = "select * from data_center a where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<DataCenter> list = this.list(DataCenter.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<DataCenter> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from data_center where is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(DataCenter.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from data_center 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<DataCenter> centers = this.list(DataCenter.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();
		for (DataCenter center : centers) {
			beans.add(new SimpleBean(center.getId(), center.getName()));
		}
		return beans;
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from data_center 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 data_center 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 isNameRepeat(String name) throws Exception {
		String sql = "select id,name from data_center where name = :name and is_deleted = 0";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		List<DataCenter> list = this.list(DataCenter.class, sql, paramMap);
		if (list.isEmpty()) {
			return false;
		}
		return true;
	}

	public DataCenter queryByName(String name) throws Exception {
		String sql = "select * from data_center a where a.is_deleted = 0 and a.name = :name";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		List<DataCenter> list = this.list(DataCenter.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public DataCenter getDCBean(Long id) throws Exception {
		String sql = "select * from data_center a where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<DataCenter> list = this.list(DataCenter.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}
}
