package com.bocloud.ims.repository.resource;

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.ObjectStorageItem;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.apache.commons.lang3.StringUtils;

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

/**
 * 对象存储项Repository实现
 *
 * @author hufy
 * @version 1.0
 * @since 2017年8月29日
 */
@Repository("objectStorageItemRepository")
public class ObjectStorageItemRepository extends BasicGenericDao<ObjectStorageItem, Long> {

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

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

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

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

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

	public boolean delete(Long id, Long userId) throws Exception {
		String sql = "update object_storage_item set is_deleted = 1,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 ObjectStorageItem getByName(String name, Long containerId) throws Exception {
		String sql = "select * from object_storage_item where name = :name and is_deleted = 0 and container_id=:containerId";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		paramMap.put("containerId", containerId);
		List<ObjectStorageItem> list = this.list(ObjectStorageItem.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

	public List<ObjectStorageItem> listByContainerId(Long containerId, String name, Long userId) throws Exception {
		String sql = "select * from object_storage_item a where a.is_deleted = 0 and a.container_id = :containerId and a.owner_id=:ownerId";
		Map<String, Object> params = MapTools.simpleMap("containerId", containerId);
		params.put("ownerId", userId);
		if (StringUtils.isNotEmpty(name)) {
			sql = sql + " and a.name like :name";
			params.put("name", name + "%");
		}
		return this.list(ObjectStorageItem.class, sql, params);
	}

	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "delete from object_storage_item where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.execute(sql, params) > 0;
	}

}
