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.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.Environment;
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("environmentRepository")
public class EnvironmentRepository extends BasicGenericDao<Environment, Long> {

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

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorterMap) throws Exception {
		String sql = "select a.id,a.name from environment a where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorterMap), "a");
		Map<String, Object> paramsMap = QueryBuilder.getParam(params);
		List<Environment> environmentslist = list(Environment.class, sql, paramsMap);
		List<SimpleBean> listBean = new ArrayList<>();
		for (Environment environment : environmentslist) {
			listBean.add(new SimpleBean(environment.getId(), environment.getName()));
		}
		return listBean;
	}

	public List<Environment> list(Integer page, Integer rows, List<Param> params, Map<String, String> sorterMap)
			throws Exception {
		String sql = "select a.* from environment a where a.is_deleted = 0";
		sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorterMap), "a");
		Map<String, Object> paramsMap = QueryBuilder.getParam(params);
		return this.list(Environment.class, sql, paramsMap);
	}

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

	public boolean existName(String name) throws Exception {
		String sql = "select a.* from environment a where a.is_deleted = 0 and a.name = :name";
		Map<String, Object> paramsMap = MapTools.simpleMap("name", name);
		List<Environment> environmentlist = this.list(Environment.class, sql, paramsMap);
		if (environmentlist.isEmpty()) {
			return false;
		}
		return true;
	}

	public boolean remove(Long id, Long userid) throws Exception {
		String sql = "update environment set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		params.put("gmtModify", new Date());
		params.put("menderId", userid);
		return this.execute(sql, params) > 0;
	}

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

}
