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.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.DataStoreFile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

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

/**
 * vCenter存储 文件接口
 *
 * @author hufy
 */
@Component("dataStoreFileRepository")
public class DataStoreFileRepository extends BasicGenericDao<DataStoreFile, Long> {

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

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

	public List<DataStoreFile> listByDataStore(Long dataStoreId) throws Exception {
		String sql = "select * from data_store_file a where a.is_deleted = 0 and a.store_id = :storeId";
		Map<String, Object> params = MapTools.simpleMap("storeId", dataStoreId);
		return this.list(DataStoreFile.class, sql, params);
	}

	public Boolean removeByDataStore(Long dataStoreId) throws Exception {
		String sql = "update data_store_file set is_deleted=1  where  store_id = :storeId";
		Map<String, Object> params = MapTools.simpleMap("storeId", dataStoreId);
		return this.execute(sql, params) > 0;
	}

	public Boolean remove(Long id, Long userId) throws Exception {
		String sql = "update data_store_file 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 Boolean removeByHost(Long hostId, Long userId) throws Exception {
		String sql = "update data_store_file set is_deleted = 1 , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and host_id = :hostId";
		Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

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

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from data_store_file 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<DataStoreFile> stores = this.list(DataStoreFile.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();
		for (DataStoreFile store : stores) {
			beans.add(new SimpleBean(store.getId(), store.getName()));
		}
		return beans;
	}

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

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

}
