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.Backup;
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("backupRepository")
public class BackupRepository extends BasicGenericDao<Backup, Long> {

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

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

	public Backup query(Long id) {
		String sql = " select a.*, vault.name vaults_name , r.name region_name from backup a "
				+ " left join backup_vaults vault on a.vaults_uuid = vault.vaults_uuid and a.vendor_id = vault.vendor_id "
				+ " left join region r on a.region_id = r.region_id and r.vendor_id = a.vendor_id "
				+ " where a.is_deleted = 0 and a.id = :id ";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Backup> list = this.list(Backup.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean delete(Long id, Long userId) {
		String sql = "update backup 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 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 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<Backup> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = " select a.* from backup a 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(Backup.class, sql, param);
	}

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

	public int count(List<Param> params) {
		String sql = " select count(1) from backup 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<Backup> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long tenantId,
			List<Long> projectIds) {
		String sql = " select a.* from backup 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(Backup.class, sql, paramMap);
	}

	public List<Backup> list(Long vendorId, String regionId) {
		String sql = "select a.* from backup 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(Backup.class, sql, params);
	}

	public List<Backup> list(Long vendorId, String regionId, Long tenantId) {
		String sql = "select a.* from backup 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(Backup.class, sql, params);
	}

	public boolean deleteByVendorId(Long vendorId, String regionId, Long userId) {
		String sql = "update backup 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 Backup query(String backupUuid, Long vendorId) {
		String sql = " select a.* from backup a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.backup_uuid = :backupUuid";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("backupUuid", backupUuid);
		List<Backup> list = this.list(Backup.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

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

	public List<Backup> listByResourceUuid(String resourceUuid, Long vendorId, String regionId) {
		String sql = "select a.* from backup a where a.vendor_id = :vendorId and a.is_deleted = 0 and a.region_id = :regionId and a.resource_uuid = :resourceUuid ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put(Common.REGIONID, regionId);
		params.put("resourceUuid", resourceUuid);
		return this.list(Backup.class, sql, params);
	}

	public List<Backup> listByCheckPointId(String resourceUuid, String checkpointUuid, Long vendorId, String regionId,
			String parentBackupUuid) {
		String sql = " select a.* from backup a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.region_id = :regionId and a.checkpoint_uuid = :checkpointUuid  ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		if (!StringUtils.isEmpty(parentBackupUuid)) {
			sql += " and a.parent_resource_uuid = :parentBackupUuid ";
			params.put("parentBackupUuid", parentBackupUuid);
		}
		if (!StringUtils.isEmpty(resourceUuid)) {
			sql += " and a.resource_uuid = :resourceUuid ";
			params.put("resourceUuid", resourceUuid);
		}
		params.put("regionId", regionId);
		params.put("checkpointUuid", checkpointUuid);
		return this.list(Backup.class, sql, params);
	}

	public boolean deleteByCheckPointId(String resourceUuid, String checkpointUuid, Long vendorId, String regionId,
			Long userId, String parentBackupUuid) {
		String sql = "update backup set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId "
				+ " and checkpoint_uuid = :checkpointUuid and region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		if (!StringUtils.isEmpty(parentBackupUuid)) {
			sql += " and parent_resource_uuid = :parentBackupUuid ";
			params.put("parentBackupUuid", parentBackupUuid);
		}
		if (!StringUtils.isEmpty(resourceUuid)) {
			sql += " and resource_uuid = :resourceUuid ";
			params.put("resourceUuid", resourceUuid);
		}
		params.put("checkpointUuid", checkpointUuid);
		params.put("regionId", regionId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVid(Long vendorId, Long userId) {
		String sql = "update backup 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;
	}

}
