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.ServerTemplate;
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 ymh
 * @时间 2019年3月9日
 */
@Repository("serverTemplateRepository")
public class ServerTemplateRepository extends BasicGenericDao<ServerTemplate, Long> {

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

	public List<ServerTemplate> list(int page, int rows, List<Param> params, Map<String, String> sorter,
			List<Long> projectIds) throws Exception {
		String sql = " SELECT a.*, r.`name` region_name, p.`name` project_name, p.visibility project_visibility, c.`name` vendor_name, c.type vendor_type FROM server_template a "
				+ " left join region r on a.region = r.region_id and a.vendor_id = r.vendor_id"
				+ " left join project p on a.project_id = p.id" + " left join cloud_vendor c on a.vendor_id = c.id"
				+ " WHERE a.is_deleted = 0  ";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (!ListTool.isEmpty(projectIds)) {
			sql += " and a.project_id in (:projectIds) ";
			paramMap.put("projectIds", projectIds);
		}
		sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(ServerTemplate.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = " SELECT a.* FROM server_template 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<ServerTemplate> serverTemplates = this.list(ServerTemplate.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (ServerTemplate template : serverTemplates) {
			beans.add(new SimpleBean(template.getId(), template.getName()));
		}
		return beans;
	}

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

	public int count(List<Param> params, List<Long> projectIds) throws Exception {
		String sql = "select count(1) from server_template a where a.is_deleted = 0";
		sql = QueryBuilder.build(sql, params, null, "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (!ListTool.isEmpty(projectIds)) {
			sql += " and a.project_id in (:projectIds) ";
			paramMap.put("projectIds", projectIds);
		}
		return this.countQuery(sql, paramMap).intValue();
	}

	public Boolean remove(Long id, Long userId) throws Exception {
		String sql = "update server_template 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 ServerTemplate query(Long id) throws Exception {
		String sql = " SELECT a.*, r.`name` region_name, p.`name` project_name, p.visibility project_visibility, c.`name` vendor_name, c.type vendor_type FROM server_template a "
				+ " left join region r on a.region = r.region_id and a.vendor_id = r.vendor_id"
				+ " left join project p on a.project_id = p.id" + " left join cloud_vendor c on a.vendor_id = c.id"
				+ " WHERE a.id = :id ";
		Map<String, Object> paramMap = MapTools.simpleMap("id", id);
		List<ServerTemplate> list = this.list(ServerTemplate.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public Boolean deleteByVendorId(Long vendorId, String region, Long userId) throws Exception {
		String sql = "update server_template set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId "
				+ " where is_deleted = 0 and vendor_id = :vendorId and region = :region ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("region", region);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<ServerTemplate> listByVendorId(Long vendorId, String region) throws Exception {
		String sql = " SELECT a.* FROM server_template a "
				+ " WHERE a.is_deleted = 0 and a.vendor_id = :vendorId and a.region = :region ";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		paramMap.put("region", region);
		return this.list(ServerTemplate.class, sql, paramMap);
	}

	public ServerTemplate queryByUuid(Long vendorId, String templateUuid) throws Exception {
		String sql = " SELECT a.*, r.`name` region_name, p.`name` project_name, p.visibility project_visibility, c.`name` vendor_name, c.type vendor_type FROM server_template a "
				+ " left join region r on a.region = r.region_id and a.vendor_id = r.vendor_id"
				+ " left join project p on a.project_id = p.id" + " left join cloud_vendor c on a.vendor_id = c.id"
				+ " WHERE a.vendor_id = :vendorId and a.template_uuid = :templateUuid ";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<ServerTemplate> list = this.list(ServerTemplate.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
