package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.ResourceGroup;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: AzureResourceGroupsRepositoryImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2019/8/7 10:55
 */
@Repository("resourceGroupRepository")
public class ResourceGroupRepository extends BasicGenericDao<ResourceGroup, Long> {

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

	public ResourceGroup query(Long id)  {
		String sql = "select a.*,r.name region_name ,t.name tenant_name, p.name project_name, c.name vendor_name from resource_group a "
				+ " left join tenant t on a.tenant_id = t.id "
				+ " left join project p on a.project_id = p.id "
				+ " left join cloud_vendor c on a.vendor_id = c.id "
				+ " left join region r on a.region_id = r.region_id and a.vendor_id = r.vendor_id "
				+ " where a.is_deleted = 0 and a.id = :id ";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<ResourceGroup> list = this.list(ResourceGroup.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<ResourceGroup> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select v.* ,r.name region_name ,t.name tenant_name, p.name project_name, c.name vendor_name from resource_group v "
				+ " left join tenant t on v.tenant_id = t.id "
				+ " left join project p on v.project_id = p.id "
				+ " left join cloud_vendor c on v.vendor_id = c.id "
				+ " left join region r on v.region_id = r.region_id and r.vendor_id = c.id "
				+ " where v.is_deleted = 0 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "v");
		return this.list(ResourceGroup.class, sql);
	}

	public List<ResourceGroup> list() {
		return null;
	}

	public List<ResourceGroup> listByVendorId(Long vendorId) {
		String sql = "select * from resource_group where is_deleted = 0 and vendor_id =:vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(ResourceGroup.class, sql, params);
	}

	public List<ResourceGroup> listByVendorId(Long vendorId, String regionId) {
		String sql = "select * from resource_group where is_deleted = 0 and vendor_id =:vendorId and region_id = :regionId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put(Common.REGIONID, regionId);
		return this.list(ResourceGroup.class, sql, params);
	}

	public List<ResourceGroup> listByVendorId(Long vendorId, Long projectId) {
		String sql = "select * from resource_group where is_deleted = 0 and vendor_id =:vendorId and project_id = :projectId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("projectId", projectId);
		return this.list(ResourceGroup.class, sql, params);
	}

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

	public boolean remove(Long id, Long userId) {
		String sql = "update resource_group 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 removeByVid(Long vendorId, Long userId) {
		String sql = "update resource_group set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVid(Long vendorId, Long userId, String regionId) {
		String sql = "update resource_group set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId and region_id = :regionId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put(Common.REGIONID, regionId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public ResourceGroup getByName(Long vendorId, String name) {
		String sql = "select a.* from resource_group a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.name = :name";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		paramMap.put("name", name);
		List<ResourceGroup> list = this.list(ResourceGroup.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public ResourceGroup getByGroupUuid(Long vendorId, String groupUuid) {
		String sql = "select a.* from resource_group a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.resource_group_uuid = :groupUuid";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		paramMap.put("groupUuid", groupUuid);
		List<ResourceGroup> list = this.list(ResourceGroup.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public ResourceGroup getByUuid(String uuid, Long vendorId) throws Exception {
		String sql = "select a.* from resource_group a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.resource_group_uuid = :uuid";
		Map<String, Object> paramMap = MapTools.simpleMap("uuid", uuid);
		paramMap.put(Common.VENDORID, vendorId);
		List<ResourceGroup> list = this.list(ResourceGroup.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}
}
