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.BackupVaults;
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 admin
 *
 */
@Repository("backupVaultsRepository")
public class BackupVaultsRepository extends BasicGenericDao<BackupVaults, Long> {

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

	public List<BackupVaults> list(Long vendorId) {
		String sql = " select a.* from backup_vaults a where a.is_deleted = 0 and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(BackupVaults.class, sql, params);
	}

	public BackupVaults query(Long id) {
		String sql = " select a.*, r.`name` region_name , c.`name` vendor_name , c.type vendor_type from backup_vaults a "
				+ " left join region r on a.region_id = r.region_id and a.vendor_id = r.vendor_id "
				+ " left join cloud_vendor c on a.vendor_id = c.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<BackupVaults> list = this.list(BackupVaults.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

	public boolean delete(Long id) {
		String sql = "update backup_vaults set is_deleted = 1, gmt_modify = :gmtModify where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

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

	public List<BackupVaults> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = " select a.*, r.`name` region_name , c.`name` vendor_name , c.type vendor_type , "
				+ " (select count(*) from volume v where v.is_deleted = 0 and v.vendor_id = a.vendor_id and v.vaults_uuid = a.vaults_uuid) disk_count ,"
				+ " (select count(*) from cloud_server cs where cs.is_deleted = 0 and cs.vendor_id = a.vendor_id and cs.vaults_uuid = a.vaults_uuid) server_count "
				+ " from backup_vaults a "
				+ " left join region r on a.region_id = r.region_id and a.vendor_id = r.vendor_id "
				+ " left join cloud_vendor c on a.vendor_id = c.id and c.is_deleted = 0 where a.is_deleted = 0 ";
		sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.list(BackupVaults.class, sql, param);
	}

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

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

	public List<BackupVaults> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long tenantId,
			List<Long> projectIds) {
		String sql = " select a.* from backup_vaults a where a.is_deleted = 0 ";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (null != tenantId && 0L != tenantId) {
			sql += " and a.tenant_id = :tenantId";
			paramMap.put("tenantId", tenantId);
		}
		if (!ListTool.isEmpty(projectIds)) {
			sql += " and a.project_id in (:projectIds) ";
			paramMap.put("projectIds", projectIds);
		}
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(BackupVaults.class, sql, paramMap);
	}

	public List<BackupVaults> list(Long vendorId, String regionId) {
		String sql = "select a.* from backup_vaults a where a.vendor_id = :vendorId and a.is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		if (!StringUtils.isEmpty(regionId)) {
			sql += " and a.region_id = :regionId ";
			params.put(Common.REGIONID, regionId);
		}
		return this.list(BackupVaults.class, sql, params);
	}

	public BackupVaults getByUuid(String uuid) {
		String sql = "select a.* from backup_vaults a where a.is_deleted = 0 and a.uuid = :uuid";
		Map<String, Object> paramMap = MapTools.simpleMap("uuid", uuid);
		List<BackupVaults> list = this.list(BackupVaults.class, sql, paramMap);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public List<BackupVaults> list(Long vendorId, String regionId, Long tenantId) {
		String sql = "select a.* from backup_vaults a where a.vendor_id = :vendorId and a.is_deleted = 0 and a.region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put(Common.REGIONID, regionId);
		if (null != tenantId && tenantId > 0) {
			sql += " and a.tenant_id= :tenantId ";
			params.put("tenantId", tenantId);
		}
		return this.list(BackupVaults.class, sql, params);
	}

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

	public BackupVaults query(String vaultsUuid, Long vendorId) {
		String sql = " select a.* from backup_vaults a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.vaults_uuid = :vaultsUuid";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("vaultsUuid", vaultsUuid);
		List<BackupVaults> list = this.list(BackupVaults.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByVid(Long vendorId, Long userId) {
		String sql = "update backup_vaults 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(Common.MENDER_ID, userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}
}
