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.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.VolumeType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 云硬盘类型Repository实现
 *
 * @author wangyu
 * @version 4.1.0-SNAPSHOT
 * @since 2019/10/17
 */
@Repository("volumeTypeRepository")
public class VolumeTypeRepository extends BasicGenericDao<VolumeType, Long> {

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

	/**
	 * 查询云硬盘类型列表
	 *
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @return
	 * @throws Exception
	 */

	public List<VolumeType> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = "select a.*,b.name vendor_type from volume_type a left join cloud_vendor b on b.id = a.vendor_id where 1 = 1 ";
		sql = QueryBuilder.build(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.list(VolumeType.class, sql, param);
	}

	/**
	 * 查询云硬盘类型总数
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */

	public int count(List<Param> params) {
		String sql = "select count(1) from volume_type where 1 = 1 ";
		sql = QueryBuilder.build(sql, params, null, "");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	/**
	 * 移除云硬盘类型
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */

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

	/**
	 * 查询云硬盘类型详情
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */

	public VolumeType query(Long id) {
		String sql = "select a.*,b.name vendor_type from volume_type a left join cloud_vendor b on b.id = a.vendor_id where a.id = :id";
		Map<String, Object> paramMap = MapTools.simpleMap("id", id);
		List<VolumeType> types = this.list(VolumeType.class, sql, paramMap);
		if (ListTool.isEmpty(types)) {
			return null;
		}
		return types.get(0);
	}

	/**
	 * 根据云平台id查询
	 *
	 * @param vendorId
	 * @return
	 * @throws Exception
	 */
	public List<VolumeType> listByVendorId(Long vendorId) {
		String sql = "select * from volume_type where vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(VolumeType.class, sql, paramMap);
	}

	/**
	 * 根据云平台id删除
	 *
	 * @param vendorId
	 * @return
	 * @throws Exception
	 */
	public boolean deleteByVendorId(Long vendorId) {
		String sql = "delete from volume_type where vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	/**
	 * 根据名称查询
	 *
	 * @param name
	 * @param vendorId
	 * @return
	 * @throws Exception
	 */

	public VolumeType getByName(String name, Long vendorId) {
		String sql = "select a.*,b.name vendor_type from volume_type a left join cloud_vendor b on b.id = a.vendor_id where a.name = :name and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		paramMap.put(Common.VENDORID, vendorId);
		List<VolumeType> types = this.list(VolumeType.class, sql, paramMap);
		if (ListTool.isEmpty(types)) {
			return null;
		}
		return types.get(0);
	}

	/**
	 * 根据UUID查询
	 *
	 * @param volumeTypeUuid
	 * @param vendorId
	 * @return
	 * @throws Exception
	 */

	public VolumeType getByUuid(String volumeTypeUuid, Long vendorId) {
		String sql = "select a.*,b.name vendor_type from volume_type a left join cloud_vendor b on b.id = a.vendor_id where a.volume_type_uuid = :volumeTypeUuid and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap("volumeTypeUuid", volumeTypeUuid);
		paramMap.put(Common.VENDORID, vendorId);
		List<VolumeType> types = this.list(VolumeType.class, sql, paramMap);
		if (ListTool.isEmpty(types)) {
			return null;
		}
		return types.get(0);
	}

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

	public List<VolumeType> listByVendorId(Long vendorId, String regionId) {
		String sql = "select * from volume_type where vendor_id = :vendorId and region_id = :regionId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		paramMap.put("regionId", regionId);
		return this.list(VolumeType.class, sql, paramMap);
	}
	
	public List<VolumeType> listByZone(Long vendorId, String zone) {
		String sql = " select * from volume_type where vendor_id = :vendorId and extra_specs like '%" + zone + "%' ";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(VolumeType.class, sql, paramMap);
	}
	
}
