package com.bocloud.ims.repository.resource;

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.ListTool;
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.NetworkCard;
import com.bocloud.ims.entity.resource.installation.ServerPci;
import com.bocloud.ims.repository.resource.installation.ServerPciRepository;
import com.bocloud.orm.OrmGenericDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * 网卡数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("networkCardRepository")
public class NetworkCardRepository extends OrmGenericDaoImpl<NetworkCard, Long> {
	@Autowired
	private ServerPciRepository serverPciRepository;

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

	public List<NetworkCard> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = " select a.*,r.name region_name,z.`name` zone_name ,v.`name` vpc_name, s.`name` subnet_name,c.`name` server_name from network_card a "
				+ " left join region r on a.region_id = r.region_id and a.vendor_id = r.vendor_id "
				+ " left join zone z on a.zone = z.zone_id and z.vendor_id = r.vendor_id "
				+ " left join vpc v on a.vpc_uuid = v.vpc_id and a.vendor_id = v.vendor_id and v.is_deleted = 0 "
				+ " left join subnet s on a.subnet_uuid = s.subnet_uuid and a.vendor_id = s.vendor_id and s.is_deleted = 0 "
				+ " left join cloud_server c on a.instance_id = c.instance_id and a.vendor_id = c.vendor_id and c.is_deleted = 0 "
				+ " where a.is_deleted = 0 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(NetworkCard.class, sql);
	}

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

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from network_card 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) throws Exception {
		String sql = "update network_card 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 List<NetworkCard> listByVendor(Long vendorId) throws Exception {
		String sql = "select a.* from network_card a where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(NetworkCard.class, sql, params);
	}

	public Boolean remove(Long id, Long userId) throws Exception {
		String sql = "update network_card 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 NetworkCard query(Long id) throws Exception {
		String sql = " select a.*,r.name region_name,z.`name` zone_name ,v.`name` vpc_name, s.`name` subnet_name,c.`name` server_name from network_card a "
				+ " left join region r on a.region_id = r.region_id and a.vendor_id = r.vendor_id "
				+ " left join zone z on a.zone = z.zone_id and z.vendor_id = r.vendor_id "
				+ " left join vpc v on a.vpc_uuid = v.vpc_id and a.vendor_id = v.vendor_id and v.is_deleted = 0 "
				+ " left join subnet s on a.subnet_uuid = s.subnet_uuid and a.vendor_id = s.vendor_id and s.is_deleted = 0 "
				+ " left join cloud_server c on a.instance_id = c.instance_id and a.vendor_id = c.vendor_id and c.is_deleted = 0 "
				+ " where a.is_deleted = 0 and a.id = :id ";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<NetworkCard> list = this.list(NetworkCard.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<NetworkCard> listByHost(Long hostId, String category) throws Exception {
		String sql = "select a.* from network_card a where a.is_deleted = 0 and a.host_id = :hostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
		params.put("category", category);
		List<NetworkCard> list = this.list(NetworkCard.class, sql, params);
		for (NetworkCard networkCard : list) {
			if (null != networkCard && null != networkCard.getPciId()) {
				ServerPci serverPci = serverPciRepository.query(networkCard.getPciId());
				if (serverPci != null) {
					networkCard.setName(serverPci.getProduct() + " " + serverPci.getVendor());
				}
			}
		}
		return list;
	}

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

	public boolean removeByInstanceId(String instanceId, Long userId) throws Exception {
		String sql = "update network_card set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId "
				+ " where instance_id = :instanceId AND is_primary = 1 ";
		Map<String, Object> params = MapTools.simpleMap("instanceId", instanceId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

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

	public boolean release(Long switcherId, Long userId) throws Exception {
		String sql = "update network_card set used = 0, gmt_modify = :gmtModify, mender_id = :menderId, switcher_id = null 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 List<NetworkCard> listBySwitcher(Long vswitcherId, String category) throws Exception {
		String sql = "select a.* from network_card a where a.is_deleted = 0 and a.switcher_id = :vswitcherId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vswitcherId", vswitcherId);
		params.put("category", category);
		return this.list(NetworkCard.class, sql, params);
	}

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

	public List<NetworkCard> listByVendor(Long vendorId, String regionId) throws Exception {
		String sql = "select a.* from network_card 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(Common.REGIONID, regionId);
		return this.list(NetworkCard.class, sql, params);
	}

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

	public NetworkCard getByNetworkCardUuid(String portUuid, Long vendorId) {
		String sql = "select a.* from network_card a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.networkcard_uuid = :portUuid ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("portUuid", portUuid);
		List<NetworkCard> list = this.list(NetworkCard.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

}
