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.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.Mdisk;
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.HashMap;
import java.util.List;
import java.util.Map;

@Repository("mdiskRepository")
public class MdiskRepository extends BasicGenericDao<Mdisk, Long> {

	@Autowired
	private DiskRepository diskRepository;

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

	public List<Mdisk> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = "select a.* from "
				+ " (select aa.*, bb.name as storage_name, cc.name as pool_name, bb.vendor_id from mdisk aa "
				+ "  left join storage bb on aa.storage_id = bb.id "
				+ "  left join volume_pool cc on aa.pool_id = cc.id " + "  where aa.is_deleted = 0) a " + "where 1=1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(Mdisk.class, sql);
	}

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

	public int count(List<Param> params) {
		String sql = "select count(1) from " + " (select aa.*, bb.vendor_id from mdisk aa "
				+ "  left join storage bb on aa.storage_id = bb.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 mdisk set is_deleted = 1 where id = :id ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("id", id);
		execute(sql, params);
	}

	public void removeByStorage(long storageId) {
		String sql = "update mdisk set is_deleted = 1 where storage_id = :storageId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("storageId", storageId);
		execute(sql, params);
	}

	public List<Mdisk> listByStorage(long storageId) {
		String sql = "select a.* from mdisk a where is_deleted = 0 and storage_id = :storageId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("storageId", storageId);
		return this.list(Mdisk.class, sql, params);
	}

	public List<Mdisk> listByPool(long poolId) {
		String sql = "select a.* from mdisk a where is_deleted = 0 and pool_id = :poolId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("poolId", poolId);
		return this.list(Mdisk.class, sql, params);
	}

	public Mdisk get(long id) {
		String sql = "select a.*, b.name as pool_name, c.name as storage_name from mdisk a "
				+ "left join volume_pool b on a.pool_id = b.id " + "left join storage c on a.storage_id = c.id "
				+ "where a.id = :id ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("id", id);
		List<Mdisk> list = this.list(Mdisk.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public Mdisk getByStorageIdAndUuid(long storageId, String mdiskUuid) {
		String sql = "select a.* from mdisk a where is_deleted = 0 and storage_id = :storageId and mdisk_uuid = :mdiskUuid ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("mdiskUuid", mdiskUuid);
		params.put("storageId", storageId);
		List<Mdisk> list = this.list(Mdisk.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public Mdisk getByControllerIdAndUuid(long controllerId, String mdiskUuid) {
		String sql = "select a.* from mdisk a where a.is_deleted = 0 and a.controller_id = :controllerId and a.mdisk_uuid = :mdiskUuid ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("controllerId", controllerId);
		params.put("mdiskUuid", mdiskUuid);
		List<Mdisk> list = this.list(Mdisk.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public List<Mdisk> getByControllerId(long controllerId) {
		String sql = "select a.* from mdisk a where a.is_deleted = 0 and a.controller_id = :controllerId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("controllerId", controllerId);
		List<Mdisk> list = this.list(Mdisk.class, sql, params);
		for (Mdisk mdisk : list) {
			mdisk.setDisks(diskRepository.listByMdisk(mdisk.getId()));
		}
		return list;
	}

	public Mdisk getByMdiskUuid(String mdiskUuid) {
		String sql = "select a.* from mdisk a where a.is_deleted = 0 and a.mdisk_uuid = :mdiskUuid ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("mdiskUuid", mdiskUuid);
		List<Mdisk> list = this.list(Mdisk.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public Mdisk getByPoolId(long poolId) {
		String sql = "select b.* from volume_pool a join mdisk b on a.mdisk_id = b.id where a.id = :poolId";
		Map<String, Object> params = new HashMap<>(8);
		params.put("poolId", poolId);
		List<Mdisk> list = this.list(Mdisk.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

    public void removeByControllerId(Long controllerId) {
		String sql = "update mdisk set is_deleted = 1 where controller_id = :controllerId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("controllerId", controllerId);
		execute(sql, params);
	}
}
