package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.StorageSyncedSet;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("storageSyncedSetRepository")
public class StorageSyncedSetRepository extends BasicGenericDao<StorageSyncedSet, Long> {

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

	public List<StorageSyncedSet> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.* from "
				+ "(select aa.id, aa.storage_id ,aa.synced_set_uuid, aa.key_path, aa.type, aa.name, aa.status, "
				+ " aa.gmt_create,aa.creator_id, aa.gmt_modify, aa.mender_id, aa.owner_id, aa.props,"
				+ " aa.remark, aa.is_deleted, aa.snapshot_group_id, "
				+ " ifnull(aa.vendor_id, bb.vendor_id) as vendor_id ,"
				+ " (select count(*) from volume v where is_deleted = 0 and v.synced_set_id = aa.id) volume_count,"
				+ " (select count(*) from snapshot_group a where a.storage_synced_set_id = aa.id and a.is_deleted = 0) snapshot_group_count "
				+ " from storage_synced_set aa "
				+ " left join storage bb on aa.storage_id = bb.id "
				+ " left join cloud_vendor cc on cc.id = aa.vendor_id " + " where aa.is_deleted = 0) " + "a where 1=1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.list(StorageSyncedSet.class, sql, param);
	}

	public int count(List<Param> params) {
		String sql = "select count(1) from "
				+ "(select aa.id, aa.storage_id ,aa.synced_set_uuid, aa.key_path, aa.type, aa.name, aa.status, "
				+ " aa.gmt_create,aa.creator_id, aa.gmt_modify, aa.mender_id, aa.owner_id, aa.props, "
				+ " aa.remark, aa.is_deleted, aa.snapshot_group_id, "
				+ " ifnull(aa.vendor_id, bb.vendor_id) as vendor_id from storage_synced_set aa "
				+ " left join storage bb on aa.storage_id = bb.id "
				+ " left join cloud_vendor cc on cc.id = aa.vendor_id " + " where aa.is_deleted = 0) " + "a where 1=1 ";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	public void remove(long id) {
		String sql = "update storage_synced_set set is_deleted = 1 where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		this.execute(sql, params);
	}

	public void removeByStorage(long storageId) {
		String sql = "update storage_synced_set set is_deleted = 1 where storage_id = :storageId";
		Map<String, Object> params = MapTools.simpleMap("storageId", storageId);
		this.execute(sql, params);
	}

	public List<StorageSyncedSet> listByStorage(long storageId) {
		String sql = "select a.* from storage_synced_set a where a.is_deleted = 0 and a.storage_id = :storageId";
		Map<String, Object> params = MapTools.simpleMap("storageId", storageId);
		return this.list(StorageSyncedSet.class, sql, params);
	}

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

	public StorageSyncedSet get(long id) {
		String sql = "select a.*,"
				+ " (select count(*) from volume v where is_deleted = 0 and v.synced_set_id = a.id) volume_count,"
				+ " (select count(*) from snapshot_group s where s.storage_synced_set_id = a.id and s.is_deleted = 0) snapshot_group_count "
				+ " from storage_synced_set a where a.id = :id and a.is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<StorageSyncedSet> list = this.list(StorageSyncedSet.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public StorageSyncedSet getByVendorAndUuid(long vendorId, String syncedSetUuid) {
		String sql = "select a.* from storage_synced_set a left join storage b on a.storage_id = b.id and b.vendor_id = a.vendor_id "
				+ "where a.is_deleted = 0 and a.vendor_id = :vendorId and a.synced_set_uuid = :syncedSetUuid ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("syncedSetUuid", syncedSetUuid);
		List<StorageSyncedSet> list = this.list(StorageSyncedSet.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public StorageSyncedSet getByNameAndStorage(String name, long storageId) {
		String sql = "select a.* from storage_synced_set a where a.is_deleted = 0 and a.name = :name and a.storage_id = :storageId ";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		params.put("storageId", storageId);
		List<StorageSyncedSet> list = this.list(StorageSyncedSet.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public StorageSyncedSet getByNameAndVendorId(String name, long vendorId) {
		String sql = "select a.* from storage_synced_set a where a.is_deleted = 0 and a.name = :name and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		params.put("vendorId", vendorId);
		List<StorageSyncedSet> list = this.list(StorageSyncedSet.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public void updateStatus(long id, String status) {
		String sql = "update storage_synced_set set status= :status where is_deleted = 0 and id = :id ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("id", id);
		params.put("status", status);
		execute(sql, params);
	}
}
