package com.bocloud.ims.repository.resource;

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

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

/**
 * 镜像仓库Repository接口实现
 *
 */
@Repository("harborRepository")
public class HarborRepository extends BasicGenericDao<Harbor, Long>{

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

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

	public int count(List<Param> params) throws BoCloudDBException {
		String sql = "select count(1) from harbor a where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	/**
	 * 查询数量
	 * 
	 * @return
	 * @throws BoCloudDBException
	 */
	public int count() throws BoCloudDBException {
		String sql = "select count(1) from harbor where is_deleted=false;";
		return this.countQuery(sql, null).intValue();
	}

	public boolean remove(Long id, Long userId) throws BoCloudDBException {
		String sql = "update harbor set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put(Common.MENDER_ID, userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

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

	/**
	 * 根据创建者ID查找
	 * 
	 * @param id
	 * @return
	 */
	public List<Harbor> listByCreateId(Long id, Pager pager) throws BoCloudDBException {
		String sql = "select * from harbor where is_deleted=0 and creator_id = :createrId";
		Map<String, Object> params = MapTools.simpleMap(Common.CREATOR_ID, id);
		sql = QueryBuilder.buildRaw(sql, pager, "");
		List<Harbor> list = this.list(Harbor.class, sql, params);
		return list;
	}

	public List<Harbor> listByVendorId() {
		String sql = " select a.* from harbor a where a.is_deleted=0 and a.id not in ( select c.harbor_id from cloud_vendor c where c.is_deleted = 0 ) ";
		Map<String, Object> params = new HashMap<String, Object>(8);
		return this.list(Harbor.class, sql, params);
	}

	/**
	 * 通过IP查询是否重复
	 * @param IP
	 * @return
	 * @throws BoCloudDBException
	 */
	public Harbor getByIP(String IP) throws BoCloudDBException {
		String sql = "select * from harbor where server_ip = :ip and is_deleted = false";
		Map<String,Object> params = MapTools.simpleMap("ip", IP);
		List<Harbor> list = this.list(Harbor.class, sql, params);
		if (list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}

	/**
	 * 通过名字查询是否重复
	 * @param name
	 * @return
	 * @throws BoCloudDBException
	 */
	public Harbor getByName(String name) throws BoCloudDBException {
		String sql = "select * from harbor where name = :name and is_deleted = false";
		Map<String,Object> params = MapTools.simpleMap(Common.NAME, name);
		List<Harbor> list = this.list(Harbor.class, sql, params);
		if (list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}
}
