package com.bocloud.ims.repository.resource;

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

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

/**
 * 对象关系数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("objectRelationRepository")
public class ObjectRelationRepository extends BasicGenericDao<ObjectRelation, Long> {

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

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

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

	public List<ObjectRelation> list(int page, int rows, List<Param> params, Map<String, String> sorter, String ip,
			String platform) throws Exception {
		String sql = "select a.* from object_relation a left join resource b on a.res_id=b.id left join cloud_vendor c on b.vendor_id=c.id "
				+ " where b.is_deleted=0 ";
		if (null != ip) {
			sql += " and b.ip like '%" + ip + "%' ";
		}
		if (null != platform) {
			if (platform.contains("私有平台") || "私有平台".contains(platform)) {
				sql += " and (c.name is null or c.name like '%" + platform + "%') ";
			} else {
				sql += " and c.name like '%" + platform + "%' ";
			}
		}
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(ObjectRelation.class, sql, paramMap);
	}

	public int count(List<Param> params, String ip, String platform) throws Exception {
		String sql = "select count(1) from object_relation a left join resource b on a.res_id=b.id left join cloud_vendor c on b.vendor_id=c.id "
				+ " where b.is_deleted=0 ";
		if (null != ip) {
			sql += " and b.ip like '%" + ip + "%' ";
		}
		if (null != platform) {
			if (platform.contains("私有平台") || "私有平台".contains(platform)) {
				sql += " and (c.name is null or c.name like '%" + platform + "%') ";
			} else {
				sql += " and c.name like '%" + platform + "%' ";
			}
		}
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	public List<ObjectRelation> list() throws Exception {
		String sql = "select * from object_relation";
		return this.list(ObjectRelation.class, sql, new HashMap<>(8));
	}

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

	public List<ObjectRelation> listByTarget(Long target) throws Exception {
		String sql = "select * from object_relation where target = :target";
		Map<String, Object> params = MapTools.simpleMap("target", target);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		List<ObjectRelation> objectRelations = new ArrayList<>();
		for (Object obj : list) {
			objectRelations.add((ObjectRelation) obj);
		}
		return objectRelations;
	}

	public List<ObjectRelation> listByTargetAndShared(Long target, boolean shared) throws Exception {
		String sql = "select * from object_relation where target = :target and shared = :shared";
		Map<String, Object> params = MapTools.simpleMap("target", target);
		params.put("shared", shared);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		List<ObjectRelation> objectRelations = new ArrayList<>();
		for (Object obj : list) {
			objectRelations.add((ObjectRelation) obj);
		}
		return objectRelations;
	}

	public List<ObjectRelation> listByTargetAndCatelog(Long target, String catalog) throws Exception {
		String sql = "select * from object_relation where target = :target and catelog = :catalog";
		Map<String, Object> params = MapTools.simpleMap("target", target);
		params.put("catalog", catalog);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		List<ObjectRelation> objectRelations = new ArrayList<>();
		for (Object obj : list) {
			objectRelations.add((ObjectRelation) obj);
		}
		return objectRelations;
	}

	public List<ObjectRelation> listByTargetAndCategory(Long target, String category) throws Exception {
		String sql = "select * from object_relation where target = :target and category = :category";
		Map<String, Object> params = MapTools.simpleMap("target", target);
		params.put("category", category);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		List<ObjectRelation> objectRelations = new ArrayList<>();
		for (Object obj : list) {
			objectRelations.add((ObjectRelation) obj);
		}
		return objectRelations;
	}

	public List<ObjectRelation> list(Long target, String category, boolean shared, boolean used) throws Exception {
		String sql = "select * from object_relation where target = :target and category = :category and shared = :shared and used = :used";
		Map<String, Object> params = MapTools.simpleMap("target", target);
		params.put("category", category);
		params.put("shared", shared);
		params.put("used", used);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		List<ObjectRelation> objectRelations = new ArrayList<>();
		for (Object obj : list) {
			objectRelations.add((ObjectRelation) obj);
		}
		return objectRelations;
	}

	public ObjectRelation get(Long target, Long resId, String category) throws Exception {
		String sql = "select * from object_relation where target = :target and res_id = :resId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("target", target);
		params.put("resId", resId);
		params.put("category", category);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		ObjectRelation objectRelation = null;
		if (null != list && !list.isEmpty()) {
			objectRelation = (ObjectRelation) list.get(0);
		}
		return objectRelation;
	}

	public ObjectRelation get(Long resId, String category) throws Exception {
		String sql = "select * from object_relation where res_id = :resId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("resId", resId);
		params.put("category", category);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		ObjectRelation objectRelation = null;
		if (null != list && !list.isEmpty()) {
			objectRelation = (ObjectRelation) list.get(0);
		}
		return objectRelation;
	}

	public List<ObjectRelation> listByCategory(String category) throws Exception {
		String sql = "select * from object_relation where category = :category";
		Map<String, Object> params = MapTools.simpleMap("category", category);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		List<ObjectRelation> objectRelations = new ArrayList<>();
		for (Object obj : list) {
			ObjectRelation objectRelation = (ObjectRelation) obj;
			objectRelations.add(objectRelation);
		}
		return objectRelations;
	}

	public List<ObjectRelation> ListByResAndCategory(Long resId, String category) throws Exception {
		String sql = "select * from object_relation where res_id = :resId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("resId", resId);
		params.put("category", category);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		List<ObjectRelation> objectRelations = new ArrayList<>();
		for (Object obj : list) {
			objectRelations.add((ObjectRelation) obj);
		}
		return objectRelations;
	}

	public boolean used(Long target, Long resId, String category, boolean used) throws Exception {
		String sql = "update object_relation set used = :used where target = :target and res_id = :resId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("target", target);
		params.put("resId", resId);
		params.put("category", category);
		params.put("used", used);
		return this.execute(sql, params) > 0;
	}

	public List<ObjectRelation> listByRes(Long resId) throws Exception {
		String sql = "select * from object_relation where res_id = :resId and used is not null";
		Map<String, Object> params = MapTools.simpleMap("resId", resId);
		List<ObjectRelation> list = this.list(ObjectRelation.class, sql, params);
		List<ObjectRelation> objectRelations = new ArrayList<>();
		for (Object obj : list) {
			objectRelations.add((ObjectRelation) obj);
		}
		return objectRelations;
	}

	public boolean deleteByRid(Long resId, String category) throws Exception {
		String sql = "delete from object_relation where res_id = :resId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("resId", resId);
		params.put("category", category);
		return this.execute(sql, params) > 0;
	}

	public boolean resByUsed(Long resId) throws Exception {
		List<ObjectRelation> ors = ListByResAndCategory(resId, Catalog.User.name());
		return ors.size() > 1 ? true : false;
	}

	public boolean updateExpriedTime(Long resId, Date expiredTime, String category, String catalog) throws Exception {
		String sql = "update object_relation a set a.expired_time = :expiredTime where a.res_id = :resId and a.category = :category and a.catalog = :catalog";
		Map<String, Object> params = MapTools.simpleMap("resId", resId);
		params.put("expiredTime", expiredTime);
		params.put("category", category);
		params.put("catalog", catalog);
		return this.execute(sql, params) > 0;
	}
}
