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.Route;

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;

@Repository("routeRepository")
public class RouteRepository extends BasicGenericDao<Route, Long> {

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

	public List<Route> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.*,r.name region_name from route a left join region r on r.vendor_id = a.vendor_id and r.region_id = a.region_id where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(Route.class, sql, paramMap);
	}

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

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update route 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 Route query(Long id) throws Exception {
		String sql = "select a.* from route a where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Route> list = this.list(Route.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

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

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

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

	public List<Route> listByVid(Long vendorId, String regionId, Long routeTableId) throws Exception {
		String sql = "select a.* from route a  where a.is_deleted = 0 and a.vendor_id = :vendorId and a.region_id = :regionId and route_table_id = :routeTableId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("regionId", regionId);
		params.put("routeTableId", routeTableId);
		return this.list(Route.class, sql, params);
	}

	public Route queryByUuid(String uuid) throws Exception {
		String sql = "select a.* from route a where a.is_deleted = 0 and a.uuid = :uuid";
		Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
		List<Route> list = this.list(Route.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

	public boolean removeByRouteTable(Long vendorId, Long userId, String regionId, String routeTableUuid)
			throws Exception {
		String sql = "update route set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId "
				+ " where is_deleted = 0 and vendor_id = :vendorId  and region_id = :regionId and route_table_uuid = :routeTableUuid ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		params.put("regionId", regionId);
		params.put("routeTableUuid", routeTableUuid);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByDestination(Long vendorId, String destination, Long userId) throws Exception {
		String sql = "update route set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId and destination = :destination";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("destination", destination);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean remove(String routeTableId, Long vendorId, String regionId, String cidr, Long userId) {
		String sql = "update route set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId and route_table_uuid = :routeTableUuid and region_id = :regionId and destination = :destination";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("routeTableUuid", routeTableId);
		params.put("regionId", regionId);
		params.put("destination", cidr);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}
}
