package com.bocloud.ims.repository.resource;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
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.BucketObject;

/**
 * 对象存储数据访问
 * 
 * @date 2021/09/08
 */
@Repository("bucketObjectRepository")
public class BucketObjectRepository extends BasicGenericDao<BucketObject, Long> {

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

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

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from bucket_object a where a.is_deleted = 0 ";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<BucketObject> objects = this.list(BucketObject.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (BucketObject object : objects) {
			beans.add(new SimpleBean(object.getId(), object.getName()));
		}
		return beans;
	}

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

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

	public boolean delete(Long id, Long userId) throws Exception {
		String sql = "update bucket_object set is_deleted = true,mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public BucketObject getByName(String name, Long bucketId, Long parentId) throws Exception {
		String sql = "select * from bucket_object where is_deleted = 0 and name = :name and bucket_id = :bucketId and parent_id = :parentId";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		paramMap.put("bucketId", bucketId);
		paramMap.put("parentId", parentId);
		List<BucketObject> list = this.list(BucketObject.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public BucketObject getByName(String name, Long bucketId, String parentKey, Integer objectIndex) throws Exception {
		String sql = "select * from bucket_object where is_deleted = 0 and name = :name and bucket_id = :bucketId  and object_index = :objectIndex";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		paramMap.put("bucketId", bucketId);
		if (!StringUtils.isEmpty(parentKey)) {
			sql += " and parent_key = :parentKey ";
			paramMap.put("parentKey", parentKey);
		}
		paramMap.put("objectIndex", objectIndex);
		List<BucketObject> list = this.list(BucketObject.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByBucketId(Long bucketId, Long userId) throws Exception {
		String sql = "update bucket_object set is_deleted = 1,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and bucket_id = :bucketId";
		Map<String, Object> params = MapTools.simpleMap("bucketId", bucketId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<BucketObject> listByBucketId(Long bucketId) throws Exception {
		String sql = "select * from bucket_object a where a.is_deleted = 0 and a.bucket_id = :bucketId";
		Map<String, Object> params = MapTools.simpleMap("bucketId", bucketId);
		return this.list(BucketObject.class, sql, params);
	}

	public List<BucketObject> listByBucketId(Long bucketId, Long vendorId) throws Exception {
		String sql = "select a.* from bucket_object a where a.is_deleted = 0 and a.bucket_id = :bucketId and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("bucketId", bucketId);
		params.put("vendorId", vendorId);
		return this.list(BucketObject.class, sql, params);
	}

	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update bucket_object set is_deleted = 1,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<BucketObject> listByTenantId(Long tenantId) throws Exception {
		String sql = "select * from bucket_object a where a.is_deleted = 0 and a.tenant_id = :tenantId";
		Map<String, Object> params = MapTools.simpleMap("tenantId", tenantId);
		return this.list(BucketObject.class, sql, params);
	}

	public List<BucketObject> summary(Date start, Date end) throws Exception {
		String sql = "select * from bucket_object where is_deleted = 0 and gmt_create >= :start and gmt_create <= :end";
		Map<String, Object> params = MapTools.simpleMap("start", start);
		params.put("end", end);
		return this.list(BucketObject.class, sql, params);
	}

	public int sizeSum() throws Exception {
		String sql = "select sum(a.size) from bucket_object a where a.is_deleted = 0 and a.size is not null";
		return this.countQuery(sql, new HashMap<>()).intValue();
	}

	public boolean removeByVid(Long vendorId, String regionId, Long bucketId, Long userId) {
		String sql = "update bucket_object set is_deleted = 1,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		if (!StringUtils.isEmpty(regionId)) {
			sql += " and region_id = :regionId ";
			params.put("regionId", regionId);
		}
		if (null != bucketId && bucketId > 0) {
			sql += " and bucket_id = :bucketId ";
			params.put("bucketId", bucketId);
		}
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

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

	public List<BucketObject> queryByParentId(Long parentId) {
		String sql = "select a.* from bucket_object a where a.is_deleted = 0 and type = 'Folder' and a.parent_id = :parentId";
		Map<String, Object> paramMap = MapTools.simpleMap("parentId", parentId);
		return this.list(BucketObject.class, sql, paramMap);
	}

}
