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.Tag;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository("tagRepository")
public class TagRepository extends BasicGenericDao<Tag, Long> {

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

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

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

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

	public List<Tag> listByVid(Long vendorId) throws Exception {
		String sql = "select a.*,v.name vendor_name from tag a left join cloud_vendor v on v.id = a.vendor_id where a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<Tag> list = this.list(Tag.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public List<Tag> listByVid(Long vendorId, String regionId) throws Exception {
		String sql = "select a.*,v.name vendor_name from tag a left join cloud_vendor v on v.id = a.vendor_id where a.vendor_id = :vendorId and a.region_id = :regionId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		paramMap.put("regionId", regionId);
		return this.list(Tag.class, sql, paramMap);
	}

	public List<Tag> listByRid(String resourceId) throws Exception {
		String sql = "select * from tag where resource_id = :resourceId";
		Map<String, Object> paramMap = MapTools.simpleMap("resourceId", resourceId);
		List<Tag> list = this.list(Tag.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public Boolean removeByRid(String resourceId) throws Exception {
		String sql = "delete from tag where resource_id = :resourceId";
		Map<String, Object> params = MapTools.simpleMap("resourceId", resourceId);
		return this.execute(sql, params) > 0;
	}

	public Boolean removeByVid(Long vendorId) throws Exception {
		String sql = "delete from tag where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public Boolean removeByVid(Long vendorId, String regionId) throws Exception {
		String sql = "delete from tag where region_id = :regionId and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("regionId", regionId);
		return this.execute(sql, params) > 0;
	}

	public Boolean remove(Long id) throws Exception {
		String sql = "delete from tag where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.execute(sql, params) > 0;
	}

	public List<Tag> listByTagVal(String tagValue, String resourceType, Long vendorId) throws Exception {
		String sql = "select * from tag where tag_value = :tagValue and resource_type = :resourceType and vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap("tagValue", tagValue);
		paramMap.put("resourceType", resourceType);
		paramMap.put("vendorId", vendorId);
		List<Tag> list = this.list(Tag.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public Boolean removeByRes(Long vendorId, String regionId, String resourceId, String resourceType)
			throws Exception {
		String sql = "delete from tag where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		if (!StringUtils.isEmpty(resourceType)) {
			sql += " and resource_type = :resourceType ";
			params.put("resourceType", resourceType);
		}
		if (!StringUtils.isEmpty(regionId)) {
			sql += " and region_id = :regionId ";
			params.put("regionId", regionId);
		}
		if (!StringUtils.isEmpty(resourceId)) {
			sql += " and resource_id = :resourceId ";
			params.put("resourceId", resourceId);
		}
		params.put("resourceType", resourceType);
		return this.execute(sql, params) > 0;
	}

	public List<Tag> listByRes(Long vendorId, String regionId, String resourceId, String resourceType)
			throws Exception {
		String sql = "select a.*,v.name vendor_name from tag a left join cloud_vendor v on v.id = a.vendor_id "
				+ " where a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		if (!StringUtils.isEmpty(regionId)) {
			sql += " and a.region_id = :regionId ";
			params.put("regionId", regionId);
		}
		if (!StringUtils.isEmpty(resourceId)) {
			sql += " and resource_id = :resourceId ";
			params.put("resourceId", resourceId);
		}
		if (!StringUtils.isEmpty(resourceType)) {
			sql += " and resource_type = :resourceType";
			params.put("resourceType", resourceType);
		}
		return this.list(Tag.class, sql, params);
	}
}
