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.NetworkArea;
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("networkAreaRepository")
public class NetworkAreaRepository extends BasicGenericDao<NetworkArea, Long> {

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

	public List<NetworkArea> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.*,b.name dc_name,c.name def_cluster_name from network_area a left join data_center b on b.id = a.dc_id left join cluster c on c.id = a.def_cluster_id where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(NetworkArea.class, sql, paramMap);
	}

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

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from network_area 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 delete(Long id, Long userId) throws Exception {
		String sql = "update network_area 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 NetworkArea query(Long id) throws Exception {
		String sql = "select a.*,b.name dc_name,c.name def_cluster_name from network_area a left join data_center b on b.id = a.dc_id left join cluster c on c.id = a.def_cluster_id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<NetworkArea> list = this.list(NetworkArea.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean exists(String name, Long dcId) throws Exception {
		String sql = "select a.* from network_area a where a.is_deleted = 0 and a.name = :name and a.dc_id = :dcId";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		paramMap.put("dcId", dcId);
		List<NetworkArea> list = this.list(NetworkArea.class, sql, paramMap);
		if (ListTool.isEmpty(list)) {
			return false;
		}
		return true;
	}

	public List<NetworkArea> listByDc(Long dcId) throws Exception {
		String sql = "select a.*,b.name dc_name,c.name def_cluster_name from network_area a left join data_center b on b.id = a.dc_id left join cluster c on c.id = a.def_cluster_id where a.is_deleted = 0 and a.dc_id = :dcId";
		Map<String, Object> params = MapTools.simpleMap("dcId", dcId);
		return this.list(NetworkArea.class, sql, params);
	}

	public List<NetworkArea> listUnbindNetworkArea(Long dcId) throws Exception {
		String sql = "select a.* from network_area a where a.is_deleted = 0 and a.dc_id = :dcId and a.id not in (select b.network_area_id from cloud_vendor b where b.is_deleted = 0 and b.network_area_id is not null)";
		Map<String, Object> params = MapTools.simpleMap("dcId", dcId);
		return this.list(NetworkArea.class, sql, params);
	}

	public boolean reset(Long clusterId, Long userId) throws Exception {
		String sql = "update network_area set def_cluster_id = null, gmt_modify = :gmtModify, mender_id = :menderId where def_cluster_id = :clusterId";
		Map<String, Object> params = MapTools.simpleMap("clusterId", clusterId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}
}
