package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Pager;
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.HarborTag;
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;

/**
 * @author aile
 * @date 2020/3/25 17:47
 */

@Repository("harborTagRepository")
public class HarborTagRepository extends BasicGenericDao<HarborTag, Long> {

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

	/**
	 * 通过镜像id，获取镜像的所有标签，分页
	 * 
	 * @param imageId
	 * @return
	 */
	public List<HarborTag> tagListImageId(Long imageId, Pager pager) throws BoCloudDBException {
		String sql = "select a.*,b.name image_name from harbor_tag a RIGHT JOIN harbor_image b on a.image_id = b.id \n"
				+ "where a.image_id = :id and a.is_deleted = false and b.is_deleted=false";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, imageId);
		sql = QueryBuilder.buildRaw(sql, pager, "a");
		return this.list(HarborTag.class, sql, params);
	}

	/**
	 * 通过镜像id，获取镜像的所有标签，非分页
	 *
	 * @param imageId
	 * @return
	 */
	public List<HarborTag> tagListImageId(Long imageId) throws BoCloudDBException {
		String sql = "select a.*,b.name image_name from harbor_tag a RIGHT JOIN harbor_image b on a.image_id = b.id \n"
				+ "where a.image_id = :id and a.is_deleted = false and b.is_deleted=false";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, imageId);
		return this.list(HarborTag.class, sql, params);
	}

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

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

	/**
	 *
	 * 通过id查询
	 * 
	 * @param id
	 * @return
	 */
	public HarborTag getById(Long id) {
		String sql = "select a.*, b.name image_name from harbor_tag a left join harbor_image b on a.image_id=b.id \n"
				+ "where a.id = :id and a.is_deleted = false and b.is_deleted=false";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<HarborTag> list = this.list(HarborTag.class, sql, params);
		if (ListTool.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	/**
	 *
	 * 通过harborID,镜像名字，标签的名字查询
	 * 
	 * @param harborId
	 * @param tagName
	 * @param imageName
	 * @return
	 * @throws BoCloudDBException
	 */
	public HarborTag query(Long harborId, String tagName, String imageName) throws BoCloudDBException {
		String sql = "select a.*,b.name imageName from harbor_tag a RIGHT JOIN harbor_image b on a.image_id = b.id \n"
				+ "where a.name=:name and b.name=:imageName and a.harbor_id=:id and a.is_deleted=false and b.is_deleted=false";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, harborId);
		params.put(Common.NAME, tagName);
		params.put("imageName", imageName);
		// master会有sql注入问题,所以先取list，然后挑选master
		if ("master".equals(tagName)) {
			sql = "select a.*,b.name imageName from harbor_tag a RIGHT JOIN harbor_image b on a.image_id = b.id \n"
					+ "where b.name=:imageName and a.harbor_id=:id and a.is_deleted=false and b.is_deleted=false";
			params.remove(Common.NAME);
			List<HarborTag> list = this.list(HarborTag.class, sql, params);
			for (HarborTag harborTag : list) {
				if ("master".equals(harborTag.getName())) {
					return harborTag;
				}
			}
			return null;
		}
		List<HarborTag> list = this.list(HarborTag.class, sql, params);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	/**
	 *
	 * 通过harborId查询所有标签
	 * 
	 * @param harborId
	 * @return
	 * @throws BoCloudDBException
	 */
	public List<HarborTag> tagListByHarborId(Long harborId) throws BoCloudDBException {
		String sql = "select a.* ,b.name image_name from harbor_tag a right JOIN harbor_image b on a.image_id = b.id\n"
				+ "where a.harbor_id = :id and a.is_deleted=false and b.is_deleted=false";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, harborId);
		return this.list(HarborTag.class, sql, params);
	}

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

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

}
