package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
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.Flavor;

import org.apache.commons.lang3.StringUtils;
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.List;
import java.util.Map;

/**
 * 配置Repository接口实现
 *
 * @author dongkai
 */
@Repository("flavorRepository")
public class FlavorRepository extends BasicGenericDao<Flavor, Long> {

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

	public List<Flavor> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = "select f.* , z.`name` zone_name, r.name region_name from flavor f "
				+ " left join region r on f.region_id = r.region_id and f.vendor_id = r.vendor_id "
				+ " left join zone z on f.zone = z.zone_id and f.vendor_id = z.vendor_id " + " where 1=1 ";
		sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "f");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(Flavor.class, sql, paramMap);
	}

	public List<Flavor> list(List<Param> params, Map<String, String> sorter) {
		String sql = "select * from flavor where 1=1";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(Flavor.class, sql, paramMap);
	}

	public int count(List<Param> params) {
		String sql = "select count(1) from flavor where 1=1";
		sql = QueryBuilder.build(sql, params, null, "");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public Flavor getByFlavorId(String flavorId) {
		String sql = "select * from flavor where flavor_uuid = :flavorId";
		Map<String, Object> params = MapTools.simpleMap("flavorId", flavorId);
		List<Flavor> list = this.list(Flavor.class, sql, params);
		Flavor flavor = null;
		if (null != list && !list.isEmpty()) {
			flavor = (Flavor) list.get(0);
		}
		return flavor;
	}
	
	public Flavor getByFlavorId(String flavorId, Long vendorId, String regionId) {
		String sql = "select * from flavor where flavor_uuid = :flavorId and vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("flavorId", flavorId);
		params.put("vendorId", vendorId);
		if (!StringUtils.isEmpty(regionId)) {
			sql += " and region_id = :regionId ";
			params.put("regionId", regionId);
		}
		List<Flavor> list = this.list(Flavor.class, sql, params);
		Flavor flavor = null;
		if (null != list && !list.isEmpty()) {
			flavor = (Flavor) list.get(0);
		}
		return flavor;
	}

	public List<Flavor> listByVendorId(Long vendorId) {
		String sql = "select * from flavor where vendor_id =:vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<Flavor> list = this.list(Flavor.class, sql, params);
		List<Flavor> flavors = new ArrayList<>();
		Flavor flavor = null;
		for (Object obj : list) {
			flavor = (Flavor) obj;
			flavors.add(flavor);
		}
		return flavors;
	}


	public Flavor query(Long id) {
		String sql = "select a.*,v.name vendor_name, r.`name` region_name,z.`name` zone_name from flavor a "
				+ " left join region r on a.vendor_id = r.vendor_id and a.region_id = r.region_id "
				+ " left join zone z on a.vendor_id = z.vendor_id and a.zone = z.zone_id "
				+ " left join cloud_vendor v on v.id = a.vendor_id where a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Flavor> list = this.list(Flavor.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return (Flavor) list.get(0);
	}

	public Boolean batchSave(List<Flavor> flavor) {
		return this.batchSave(flavor, 1000);
	}

	public Boolean batchUpdate(List<Flavor> flavor) {
		return this.batchUpdate(flavor, flavor.size());
	}

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

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

	public Flavor getByVid(Long vendorId, String name) {
		String sql = "select * from flavor where vendor_id = :vendorId and name = :name";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put(Common.NAME, name);
		List<Flavor> list = this.list(Flavor.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return (Flavor) list.get(0);
	}

	public Flavor getByName(String name) {
		String sql = "select * from flavor where name = :name";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		List<Flavor> list = this.list(Flavor.class, sql, params);
		Flavor flavor = null;
		if (null != list && !list.isEmpty()) {
			flavor = (Flavor) list.get(0);
		}
		return flavor;
	}

	public Flavor getByFlavorUuid(String flavorUuid) {
		String sql = "select * from flavor where flavor_uuid = :flavorUuid";
		Map<String, Object> params = MapTools.simpleMap("flavorUuid", flavorUuid);
		List<Flavor> list = this.list(Flavor.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return (Flavor) list.get(0);
	}

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

	public boolean removeByVid(Long vendorId, String regionId) throws BoCloudDBException {
		String sql = "delete from flavor where vendor_id = :vendorId " + " AND region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put(Common.REGIONID, regionId);
		return this.execute(sql, params) > 0;
	}

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

	public List<Flavor> listByAdvnceVendor(String advnceVendorId) {
		String sql = "select * from flavor where region_id = :regionId and vendors like '%:advnceVendorId%' ";
		Map<String, Object> params = MapTools.simpleMap("advnceVendorId", advnceVendorId);
		return this.list(Flavor.class, sql, params);
	}

}
