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.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.PortGroup;
import com.bocloud.orm.OrmGenericDaoImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("portGroupRepository")
public class PortGroupRepository extends OrmGenericDaoImpl<PortGroup, Long> {

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

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

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
		String sql = "select a.id,a.name from port_group 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<PortGroup> alarms = this.list(PortGroup.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (PortGroup port_group : alarms) {
			beans.add(new SimpleBean(port_group.getId(), port_group.getName()));
		}
		return beans;
	}

	public int portCount(List<Param> params, Long vendorId){
		String sql = "select count(1) from port_group a  where a.is_deleted=0";
		if (vendorId !=null){
			sql += " and a.vendor_id = '" + vendorId + "'  ";
		}
		sql = QueryBuilder.build(sql,params,null,"a");
		Map<String,Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql,param).intValue();
	}

	public List<PortGroup> portList(int page, int rows, List<Param> params, Map<String, String> sorter, Long vendorId) throws BoCloudDBException {
		String sql = "select * from port_group a  where a.is_deleted=0";
		if (vendorId !=null){
			sql += " and a.vendor_id = '" + vendorId + "'  ";
		}
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(PortGroup.class, sql);
	}
	public int count(List<Param> params) {
		String sql = "select count(1) from port_group 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 removeByVid(Long vendorId, Long userId) {
		String sql = "update port_group set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where 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 remove(Long id, Long userId) {
		String sql = "update port_group 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 PortGroup query(Long id) {
		String sql = "select a.*,b.name vendor_name from port_group a left join cloud_vendor b on a.vendor_id = b.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<PortGroup> list = this.list(PortGroup.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

	public boolean removeByGroupVal(String groupVal, Long userId) {
		String sql = "update port_group set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId " +
				"where is_deleted = 0 and group_val = :groupVal ";
		Map<String, Object> params = MapTools.simpleMap("groupVal", groupVal);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public List<PortGroup> listBySwitcher(Long switcherId) {
		String sql = "select a.* from port_group a where a.is_deleted = 0 and a.switcher_id = :switcherId";
		Map<String, Object> params = MapTools.simpleMap("switcherId", switcherId);
		return this.list(PortGroup.class, sql, params);
	}

	public boolean exist(String name, Long switcherId) {
		String sql = "select a.* from port_group a where a.is_deleted = 0 and a.name = :name and a.switcher_id = :switcherId";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		params.put("switcherId", switcherId);
		List<PortGroup> list = this.list(PortGroup.class, sql, params);
		if (ListTool.isEmpty(list)) {
			return false;
		}
		return true;
	}

	public List<PortGroup> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = "select a.* from port_group a 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(PortGroup.class, sql, paramMap);
	}

	public List<PortGroup> listByVendorId(Long vendorId) {
		String sql = "select a.*,b.name switch_name from port_group a left join switcher b on b.id = a.switcher_id where a.is_deleted = 0 and a.type in ('VIRTUAL','DVPORT') and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(PortGroup.class, sql, paramMap);
	}

	public List<PortGroup> listByVlanId(Integer vlanId, String type, Long vendorId) {
		String sql = "select a.*,c.id network_id from port_group a left join switcher b on b.id = a.switcher_id left join network c on c.switcher_id = b.id where a.vendor_id = :vendorId and a.is_deleted = 0 and a.type in ('VIRTUAL','DVPORT') and a.vlan_id = :vlanId";
		Map<String, Object> paramMap = MapTools.simpleMap("vlanId", vlanId);
		paramMap.put("vendorId", vendorId);
		if (!StringUtils.isEmpty(type)) {
			sql += " and a.type = :type";
			paramMap.put("type", type);
		}
		return this.list(PortGroup.class, sql, paramMap);
	}

	public List<PortGroup> listByFcVendorId(Long vendorId) throws Exception {
		String sql = "select a.*,b.name switch_name from port_group a left join switcher b on b.id = a.switcher_id where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(PortGroup.class, sql, paramMap);
	}

	public int count(Long switcherId) throws Exception {
		String sql = "select a.* from port_group a where a.is_deleted = 0 and a.switcher_id = :switcherId";
		Map<String, Object> params = MapTools.simpleMap("switcherId", switcherId);
		List<PortGroup> list = this.list(PortGroup.class, sql, params);
		return list.size();
	}

	public PortGroup queryByUuid(String groupUuid,Long vendorId) throws Exception {
		String sql = "select a.* from port_group a  where a.is_deleted = 0 and a.group_val = :groupUuid and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("groupUuid", groupUuid);
		params.put("vendorId",vendorId);
		List<PortGroup> list = this.list(PortGroup.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
