package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.Disk;
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;

/**
 * 磁盘数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("diskRepository")
public class DiskRepository extends BasicGenericDao<Disk, Long> {

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

	public List<Disk> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		// 2019-12-02 : 是否增加字段一定不会影响原有功能稳定性？这里给出了否定答案。
		// 原来的写法是select aa.*, bb.vendor_id...，然后在disk表增加了vendorId，
		// 查询SQL报错：Duplicate column name 'vendor_id'
		String sql = "select a.* from ("
				+ "   select aa.id, aa.storage_id, aa.controller_id, aa.mdisk_id, aa.disk_uuid, aa.key_path, aa.identifier, "
				+ "       aa.block_size, aa.for_use, aa.port1_status, aa.port2_status, aa.size, aa.capacity, aa.rpm, "
				+ "       aa.firmware_level, aa.uid, aa.slot_id, aa.enclosure_id, aa.drive_id, aa.fru_id, aa.fru_part_num, "
				+ "       aa.tech_type, aa.node_id, aa.node_name, aa.provider_id, aa.product_id, aa.fpga_level, aa.member_id, "
				+ "       aa.quorum_id, 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.operational_status, aa.caption_type, aa.encryption, "
				+ "       aa.server_id, aa.is_root, aa.is_cache, aa.mode, aa.data_status, aa.used_capacity, "
				+ "       ifnull(aa.vendor_id, bb.vendor_id) as vendor_id, bb.name as storage_name, dd.name as mdisk_name "
				+ "   from disk aa " + "   left join storage bb on aa.storage_id = bb.id "
				+ "   left join cloud_vendor cc on bb.vendor_id = cc.id "
				+ "   left join mdisk dd on aa.mdisk_id = dd.id " + "   where aa.is_deleted = 0 " + ") a where 1=1";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(Disk.class, sql);
	}

	public int count(List<Param> params) {
		String sql = "select count(1) from ("
				+ "   select aa.id, aa.storage_id, aa.controller_id, aa.mdisk_id, aa.disk_uuid, aa.key_path, aa.identifier, "
				+ "       aa.block_size, aa.for_use, aa.port1_status, aa.port2_status, aa.size, aa.capacity, aa.rpm, "
				+ "       aa.firmware_level, aa.uid, aa.slot_id, aa.enclosure_id, aa.drive_id, aa.fru_id, aa.fru_part_num, "
				+ "       aa.tech_type, aa.node_id, aa.node_name, aa.provider_id, aa.product_id, aa.fpga_level, aa.member_id, "
				+ "       aa.quorum_id, 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.operational_status, aa.caption_type, aa.encryption, "
				+ "       aa.server_id, aa.is_root, aa.is_cache, aa.mode, aa.data_status, aa.used_capacity, "
				+ "       ifnull(aa.vendor_id, bb.vendor_id) as vendor_id, bb.name as storage_name "
				+ "   from disk 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 disk 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 disk set is_deleted = 1 where storage_id = :storageId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("storageId", storageId);
		execute(sql, params);
	}

	public List<Disk> listByVendor(long vendorId) {
		String sql = "select a.* from disk a where a.is_deleted = 0 and a.vendor_id = :vendorId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put(Common.VENDORID, vendorId);
		return this.list(Disk.class, sql, params);
	}

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

	public List<Disk> listByMdisk(long mdiskId) {
		String sql = "select a.* from disk a where a.is_deleted = 0 and a.mdisk_id = :mdiskId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("mdiskId", mdiskId);
		return this.list(Disk.class, sql, params);
	}

	public List<Disk> listByServer(long serverId) {
		String sql = "select a.* from disk a where a.is_deleted = 0 and a.server_id = :serverId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("serverId", serverId);
		return this.list(Disk.class, sql, params);
	}

	public Disk get(long id) {
		String sql = "select a.* from disk a where a.id = :id ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("id", id);
		List<Disk> list = this.list(Disk.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public Disk getByMdiskAndUuid(long mdiskId, String diskUuid) {
		String sql = "select a.* from disk a where a.is_deleted = 0 and a.mdisk_id = :mdiskId and a.disk_uuid = :diskUuid";
		Map<String, Object> params = new HashMap<>(8);
		params.put("mdiskId", mdiskId);
		params.put("diskUuid", diskUuid);
		List<Disk> list = this.list(Disk.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

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

	public Disk getbyVendorIdAndUuid(long vendorId, String diskUuid) {
		String sql = "select a.* from disk a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.disk_uuid = :diskUuid";
		Map<String, Object> params = new HashMap<>(8);
		params.put(Common.VENDORID, vendorId);
		params.put("diskUuid", diskUuid);
		List<Disk> list = this.list(Disk.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

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

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

	public void removeByVendorId(Long vendorId) {
		String sql = "update disk set is_deleted = 1 where vendor_id = :vendorId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("vendorId", vendorId);
		execute(sql, params);
	}

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