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.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.ScsiLun;
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("scsiLunRepository")
public class ScsiLunRepository extends BasicGenericDao<ScsiLun, Long> {

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

	public List<ScsiLun> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.* from scsi_lun a where 1 = 1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(ScsiLun.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from scsi_lun a where 1 = 1 ";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<ScsiLun> groups = this.list(ScsiLun.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (ScsiLun group : groups) {
			beans.add(new SimpleBean(group.getId(), group.getDisplayName()));
		}
		return beans;
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(a.id) from scsi_lun a where 1 = 1";
		sql = QueryBuilder.buildRaw(sql, params, new HashMap<>(8), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public ScsiLun query(Long id) throws Exception {
		String sql = "select a.* from scsi_lun a where a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<ScsiLun> list = this.list(ScsiLun.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<ScsiLun> listByVendorId(Long vendorId) {
		String sql = "select a.* from scsi_lun a where a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<ScsiLun> list = this.list(ScsiLun.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public boolean removeByVendorId(Long vendorId) {
		String sql = "delete from scsi_lun where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public List<ScsiLun> listByHostId(Long hostId) {
		String sql = "select a.* from scsi_lun a where a.host_id = :hostId";
		Map<String, Object> paramMap = MapTools.simpleMap("hostId", hostId);
		List<ScsiLun> list = this.list(ScsiLun.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public boolean removeByHostId(Long hostId) {
		String sql = "delete from scsi_lun where host_id = :hostId";
		Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
		return this.execute(sql, params) > 0;
	}

	public boolean remove(Long id) {
		String sql = "delete from scsi_lun where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.execute(sql, params) > 0;
	}

}
