package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
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.IDFactory;
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.Vdc;
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;

/**
 * 虚拟数据中心数据处理接口实现
 *
 * @author weiwei
 * @version 1.0
 * @since 2016.12.28
 */
@Component("vdcRepository")
public class VdcRepository extends BasicGenericDao<Vdc, Long> {

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

	@Override
	public void save(Vdc vdc) throws BoCloudDBException {
		vdc.setGuid(IDFactory.instance().uuid());
		super.save(vdc);
	}

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

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from vdc 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<Vdc> vdcs = this.list(Vdc.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();
		for (Vdc vdc : vdcs) {
			beans.add(new SimpleBean(vdc.getId(), vdc.getName()));
		}
		return beans;
	}

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update vdc 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 int count(List<Param> params) throws Exception {
		String sql = "select count(1) from vdc 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 Vdc query(Long id) throws Exception {
		String sql = "select a.* from vdc a where a.id = :id";
		Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
		List<Vdc> list = this.list(Vdc.class, sql, param);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByVendor(Long vendorId, Long userId) throws Exception {
		String sql = "update vdc 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;
	}

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

	public boolean exists(String name, Long vendorId) throws Exception {
		String sql = "select a.* from vdc a where a.is_deleted = 0 and a.name = :name and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		params.put(Common.VENDORID, vendorId);
		return this.list(Vdc.class, sql, params).size() > 0;
	}

	public Vdc getByValue(String vdcValue, Long vendorId) throws Exception {
		String sql = "select a.* from vdc a where a.vdc_val = :vdcValue and a.vendor_id = :vendorId";
		Map<String, Object> param = MapTools.simpleMap("vdcValue", vdcValue);
		param.put("vendorId", vendorId);
		List<Vdc> list = this.list(Vdc.class, sql, param);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
