package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.Zone;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 可用区域Repository实现
 *
 * @author wangyu
 * @version 1.0
 * @since 2018年2月1日
 */
@Repository("zoneRepository")
public class ZoneRepository extends BasicGenericDao<Zone, Long> {

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

	public List<Zone> list(Long vendorId) throws Exception {
		String sql = "select * from zone where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(Zone.class, sql, params);
	}

	public List<Zone> listByZoneId(String zoneId, Long vendorId) throws Exception {
		String sql = "select * from zone where vendor_id = :vendorId and zone_id = :zoneId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("zoneId", zoneId);
		return this.list(Zone.class, sql, params);
	}

	public List<Zone> listByZoneId(String zoneId) throws Exception {
		String sql = "select * from zone where  zone_id = :zoneId ";
		Map<String, Object> params = MapTools.simpleMap("zoneId", zoneId);
		return this.list(Zone.class, sql, params);
	}

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

	public List<Zone> listByRegionId(String regionId, Long vendorId) throws Exception {
		String sql = "select * from zone where vendor_id = :vendorId and region_id = :regionId ORDER BY zone_id ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put(Common.REGIONID, regionId);
		return this.list(Zone.class, sql, params);
	}

	public Boolean batchSave(List<Zone> zoneList) throws Exception {
		return this.batchSave(zoneList, 1000);
	}

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

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

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

	public List<Zone> listByZoneIds(String regionId, List<String> zoneIds, Long vendorId) {
		String sql = "select * from zone where  region_id = :regionId and zone_id in (:zoneIds) ";
		Map<String, Object> params = MapTools.simpleMap("regionId", regionId);
		params.put("zoneIds", zoneIds);
		if (null != vendorId) {
			sql += " and vendor_id = :vendorId ";
			params.put("vendorId", vendorId);
		}
		return this.list(Zone.class, sql, params);
	}

}
