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.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.resource.HarborImage;
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.List;
import java.util.Map;

@Repository("harborImageRepository")
public class HarborImageRepository extends BasicGenericDao<HarborImage, Long>{

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

	/**
	 * 查询某项目下的所有镜像 重载：分页查询
	 * 
	 * @param projectId
	 * @return
	 */
	public List<HarborImage> imageListByProjectId(Long projectId, Pager pager) throws BoCloudDBException {
		String sql = "select * from harbor_image where is_deleted = false and project_id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, projectId);
		sql = QueryBuilder.buildRaw(sql, pager, "");
		return this.list(HarborImage.class, sql, params);
	}

	/**
	 * 查询某项目下的所有镜像 重载：非分页查询
	 * 
	 * @param projectId
	 * @return
	 */
	public List<HarborImage> imageListByProjectId(Long projectId) throws BoCloudDBException {
		String sql = "select * from harbor_image where is_deleted = false and project_id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, projectId);
		return this.list(HarborImage.class, sql, params);
	}

	/**
	 * 删除某项目下面的所有镜像
	 * 
	 * @param projectId
	 * @return
	 * @throws BoCloudDBException
	 */
	public boolean deleteByProjectId(Long projectId, Long menderId) throws BoCloudDBException {
		String sql = "update harbor_image set is_deleted = true,mender_id=:menderId where project_id = :id and is_deleted=false";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, projectId);
        params.put(Common.MENDER_ID, menderId);
		return this.execute(sql, params) > 0;
	}

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

	/**
	 * 通过harborId直接查询镜像，不经过项目
	 *
	 * @param id
	 * @return
	 * @throws BoCloudDBException
	 */
	public List<HarborImage> imagesByHarborId(Long id, Pager pager) throws BoCloudDBException {
		String sql = "select * from harbor_image where harbor_id=:id and is_deleted=false";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		sql = QueryBuilder.buildRaw(sql, pager, "");
		return this.list(HarborImage.class, sql, params);
	}

	/**
	 * 总数
	 *
	 * @param harborId
	 * @return
	 * @throws BoCloudDBException
	 */
	public int count(Long harborId, List<Param> params) throws BoCloudDBException {
		String sql = "select count(1) from harbor_image where harbor_id=:id and is_deleted=false";
		Map<String, Object> map = MapTools.simpleMap(Common.ID, harborId);
		sql = QueryBuilder.buildRaw(sql, params, null, "");
		return this.countQuery(sql, map).intValue();
	}

	public HarborImage getByName(Long harborId, Long projectId, String name) {
		String sql = "select a.* from harbor_image a where a.is_deleted = 0 and a.harbor_id = :harborId and a.project_id = :projectId and a.name = :name ";
		Map<String, Object> params = MapTools.simpleMap("projectId", projectId);
		params.put(Common.NAME, name);
		params.put("harborId", harborId);
		List<HarborImage> list = this.list(HarborImage.class, sql, params);
		if (ListTool.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	/**
	 *
	 * 通过harborId与镜像在harbor仓库中的image_id查询
	 * @param harborImageId
	 * @param harborId
	 * @return
	 * @throws BoCloudDBException
	 */
	public HarborImage query(Long harborImageId, Long harborId) throws BoCloudDBException {
		String sql = "select * from harbor_image where harbor_id=:harborId and harbor_image_id=:imageId and is_deleted=false";
		Map<String, Object> params = MapTools.simpleMap("harborId", harborId);
		params.put("imageId", harborImageId);
		List<HarborImage> list = this.list(HarborImage.class, sql, params);
		if (list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}

	/**
	 * 通过vendor——id获取镜像
	 * @param vendorId
	 * @param pager
	 * @return
	 * @throws BoCloudDBException
	 */
	public List<HarborImage> getByVendorId(Long vendorId, Pager pager) throws BoCloudDBException {
		String sql = "SELECT b.*, h.`name` harbor_name FROM cloud_vendor a " +
				"left join harbor_image b on a.harbor_id = b.harbor_id "
				+ " left join harbor h on b.harbor_id = h.id and h.is_deleted = 0 " +
				"where a.id = :id and a.is_deleted = false and b.is_deleted = false";
		sql = QueryBuilder.buildRaw(sql, pager, "a");
		return this.list(HarborImage.class, sql, MapTools.simpleMap(Common.ID, vendorId));
	}

	/**
	 *
	 * 通过vendor_id查询镜像总数
	 * @param vendorId
	 * @return
	 * @throws BoCloudDBException
	 */
	public int countByVendorId(Long vendorId, List<Param> params) throws BoCloudDBException {
		String sql = "SELECT count(1) FROM cloud_vendor a\n" +
				"left join harbor_image b on a.harbor_id = b.harbor_id\n" +
				"where a.id = :id and a.is_deleted = false and b.is_deleted = false";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		return this.countQuery(sql, MapTools.simpleMap(Common.ID, vendorId)).intValue();
	}

	/**
	 *
	 * 通过harborId获取所有镜像
	 * @param harborId
	 * @return
	 * @throws BoCloudDBException
	 */
	public List<HarborImage> imageListByHarborId(Long harborId) throws BoCloudDBException {
		String sql = "select * from harbor_image where harbor_id =:id and is_deleted=false";
		return this.list(HarborImage.class, sql, MapTools.simpleMap(Common.ID, harborId));
	}

	/**
	 *
	 * 通过镜像名与harborId更新
	 * @param harborId
	 * @param imageName
	 * @param description
	 * @return
	 * @throws BoCloudDBException
	 */
	public boolean updateByNameAndHarborId(Long harborId, String imageName, Long menderId, String description) throws BoCloudDBException {
		String sql = "update harbor_image set mender_id=:menderId,description=:description,gmt_modify=:gmtModify where " +
				"harbor_id = :harborId and name = :name and is_deleted = false";
		Map<String, Object> params = MapTools.simpleMap(Common.MENDER_ID, menderId);
		params.put(Common.NAME, imageName);
		params.put("description", description);
		params.put("harborId", harborId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}
}
