package com.bocloud.ims.repository.resource;

import java.util.Date;
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.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.Bucket;

/**
 * Bucket数据访问
 * 
 * @date 2021/09/08
 */
@Repository("bucketRepository")
public class BucketRepository extends BasicGenericDao<Bucket, Long> {

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

	public List<Bucket> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.*, r.name region_name from bucket a left join region r on a.vendor_id = r.vendor_id and a.region_id = r.region_id where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(Bucket.class, sql);
	}

	public List<Bucket> listByVendorId(Long vendorId) throws Exception {
		String sql = "select * from bucket where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(Bucket.class, sql, params);
	}

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

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update bucket set is_deleted = true,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 Bucket query(Long id) throws Exception {
		String sql = "select a.*, r.name region_name from bucket a left join region r on a.vendor_id = r.vendor_id and a.region_id = r.region_id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Bucket> list = this.list(Bucket.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

	public boolean exists(String name, Long userId) throws Exception {
		String sql = "select count(*) from bucket a where a.is_deleted = 0 and a.name = :name and a.user_id = :userId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.USERID, userId);
		paramMap.put(Common.NAME, name);
		return this.countQuery(sql, paramMap).intValue() > 0;
	}

	public List<Bucket> listByVendorId(Long vendorId, String regionId) {
		String sql = "select a.* from bucket a where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		if (!StringUtils.isEmpty(regionId)) {
			sql += " and a.region_id = :regionId ";
			params.put("regionId", regionId);
		}
		return this.list(Bucket.class, sql, params);
	}

    public Bucket query(String actualName, Long vendorId) {
		String sql = "select a.* from bucket a where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		if (!StringUtils.isEmpty(actualName)) {
			sql += " and a.actual_name = :actualName ";
			params.put("actualName", actualName);
		}
		List<Bucket> list = this.list(Bucket.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);

    }
}
