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.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.IDFactory;
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.Switcher;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * 交换机数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("switcherRepository")
public class SwitcherRepository extends BasicGenericDao<Switcher, Long> {

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

	@Override
	public void save(Switcher switcher) throws BoCloudDBException {
		switcher.setGuid(IDFactory.instance().uuid());
		super.save(switcher);
	}

	public List<Switcher> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.*,b.name vdc_name,c.name vendor_name from switcher a left join vdc b on a.vdc_id = b.id left join cloud_vendor c on a.vendor_id = c.id where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(Switcher.class, sql);
	}

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

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update switcher set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public Switcher query(Long id) throws Exception {
		String sql = "select a.*,b.name vdc_name,v.name vendor_name from switcher a left join vdc b on a.vdc_id = b.id left join cloud_vendor v on a.vendor_id = v.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Switcher> list = this.list(Switcher.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<Switcher> listByVid(Long vendorId) throws Exception {
		String sql = "select a.*,b.name vdc_name from switcher a left join vdc b on a.vdc_id = b.id where a.is_deleted = 0 and a.vendor_id =:vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(Switcher.class, sql, params);
	}

	public List<Switcher> listByHostId(Long hostId) throws Exception {
		String sql = "select a.* from switcher a where a.is_deleted = 0 and a.host_id = :hostId";
		Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
		return this.list(Switcher.class, sql, params);
	}

	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update switcher set is_deleted = 1, 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("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByHostId(Long hostId, Long userId) throws Exception {
		String sql = "update switcher set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where is_deleted = 0 and host_id = :hostId";
		Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name,a.network_uuid from switcher a where a.is_deleted = 0";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<Switcher> networks = this.list(Switcher.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (Switcher switcher : networks) {
			beans.add(new SimpleBean(switcher.getId(), switcher.getName(), switcher.getSwitcherVal()));
		}
		return beans;
	}

	public List<Switcher> listByVdc(Long vdcId, String type) throws Exception {
		String sql = "select * from switcher where is_deleted = 0 and vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
		if (!StringUtils.isEmpty(type)) {
			sql += " and type = :type";
			params.put("type", type);
		}
		return this.list(Switcher.class, sql, params);
	}

	public boolean removeByVdc(Long vdcId, String type, Long userId) throws Exception {
		String sql = "update switcher set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where is_deleted = 0 and vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		if (!StringUtils.isEmpty(type)) {
			sql += " and type = :type";
			params.put("type", type);
		}
		return this.execute(sql, params) > 0;
	}

	public boolean exists(String name, Long vendorId, String type) throws Exception {
		String sql = "select * from switcher a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.name = :name and a.type = :type";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("name", name);
		params.put("type", type);
		List<Switcher> networks = this.list(Switcher.class, sql, params);
		if (networks.isEmpty()) {
			return false;
		}
		return true;
	}

}
