package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
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.Region;
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接口实现(子区域为zone)
 *
 * @author dongkai
 */
@Repository("regionRepository")
public class RegionRepository extends BasicGenericDao<Region, Long> {

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

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

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

	public boolean modify(Region region) {
		return this.update(region);
	}

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

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

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

	public boolean batchSave(List<Region> regionList) {
		return this.batchSave(regionList, regionList.size());
	}

	@Override
	public boolean update(Region region) {
		String sql = "update region set name = :name ,code = :code ,vendor_id = :vendorId where id = :id";
		Map<String, Object> params = MapTools.simpleMap("name", region.getName());
		params.put(Common.VENDORID, region.getVendorId());
		params.put("code", region.getCode());
		params.put("id", region.getId());
		return this.execute(sql, params) > 0;
	}

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

	public boolean deleteByVid(Long vendorId, String regionId) throws BoCloudDBException {
		String sql = "delete from region 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<Region> listByGroup(Long groupId, Long vendorId) {
		String sql = "select r.* from region r , pool_group g , pool_group_relation p  where g.id = p.pool_group_id and g.vendor_id = r.vendor_id and r.id = p.target and p.pool_group_id = :groupId";
		Map<String, Object> params = MapTools.simpleMap("groupId", groupId);
		if (null != vendorId && vendorId > 0) {
			sql += " and r.vendor_id = :vendorId ";
			params.put(Common.VENDORID, vendorId);
		}
		List<Region> list = this.list(Region.class, sql, params);
		List<Region> regions = new ArrayList<>();
		for (Object obj : list) {
			Region region = (Region) obj;
			regions.add(region);
		}
		return regions;
	}

	public List<Region> listByRegions(Long vendorId, List<String> codes) {
		String sql = " SELECT * FROM region r WHERE r.vendor_id = :vendorId and r.region_id in (:codes)  ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("codes", codes);
		List<Region> list = this.list(Region.class, sql, params);
		List<Region> regions = new ArrayList<>();
		for (Object obj : list) {
			Region region = (Region) obj;
			regions.add(region);
		}
		return regions;
	}
	
	public List<Region> listByVendorId(Long vendorId) {
		String sql = " SELECT * FROM region r WHERE r.vendor_id = :vendorId  ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<Region> list = this.list(Region.class, sql, params);
		List<Region> regions = new ArrayList<>();
		for (Object obj : list) {
			Region region = (Region) obj;
			regions.add(region);
		}
		return regions;
	}

	public Region getByRegionId(String regionId) {
		String sql = "select * from region where region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap(Common.REGIONID, regionId);
		List<Region> list = this.list(Region.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return (Region) list.get(0);
	}
}
