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.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.HarborProject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

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

/**
 * @author aile
 * @date 2020/3/26 10:47
 */

@Service("harborProjectRepository")
public class HarborProjectRepository extends BasicGenericDao<HarborProject, Long> {

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

	/**
	 * 通过仓库ID获得所有的项目 重载：分页查询
	 * 
	 * @param id
	 * @return
	 */
	public List<HarborProject> listByHarborId(Long id, Pager pager) throws BoCloudDBException {
		String sql = "select * from harbor_project where is_deleted = false and harbor_id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		sql = QueryBuilder.buildRaw(sql, pager, "a");
		return this.list(HarborProject.class, sql, params);
	}

	/**
	 * 通过仓库ID获得所有的项目 重载：非分页查询
	 * 
	 * @param id
	 * @return
	 */
	public List<HarborProject> listByHarborId(Long id) throws BoCloudDBException {
		String sql = "select * from harbor_project where harbor_id = :id and is_deleted = false ";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.list(HarborProject.class, sql, params);
	}

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

	/**
	 * 通过harbor_project_id删除
	 * 
	 * @param id
	 * @return
	 * @throws BoCloudDBException
	 */
	public boolean deleteByHarborProjectId(Long id) throws BoCloudDBException {
		String sql = "update harbor_project set is_deleted = true where harbor_project_id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);

		return this.execute(sql, params) > 0;
	}

	/**
	 * 根据id查找
	 * 
	 * @param id
	 * @return
	 * @throws BoCloudDBException
	 */
	public HarborProject query(Long id) throws BoCloudDBException {
		String sql = "select * from harbor_project where id = :id and is_deleted = false";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<HarborProject> list = this.list(HarborProject.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public HarborProject query(Long harborId, String projectName) {
		String sql = "select * from harbor_project a where a.harbor_id = :harborId and a.is_deleted = 0 and a.name = :projectName ";
		Map<String, Object> params = MapTools.simpleMap("harborId", harborId);
		params.put("projectName", projectName);
		List<HarborProject> list = this.list(HarborProject.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
