package com.bocloud.ims.repository.manageone;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.manageone.ManageOneAccount;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * manage one vdc数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年2月27日
 *
 */
@Repository("manageOneAccountRepository")
public class ManageOneAccountRepository extends BasicGenericDao<ManageOneAccount, Long> {

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

	public ManageOneAccount query(Long id) throws Exception {
		String sql = "select a.* from manage_one_account a where a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<ManageOneAccount> list = this.list(ManageOneAccount.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public ManageOneAccount queryByTenantId(Long tenantId, Long vendorId) throws Exception {
		String sql = "select a.* from manage_one_account a where a.tenant_id = :tenantId and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.TENANTID, tenantId);
		params.put(Common.VENDORID, vendorId);
		List<ManageOneAccount> list = this.list(ManageOneAccount.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public boolean removeByTenantId(Long tenantId) {
		String sql = "delete from manage_one_account where tenant_id = :tenantId";
		Map<String, Object> params = MapTools.simpleMap(Common.TENANTID, tenantId);
		return this.execute(sql, params) > 0;
	}

	public List<ManageOneAccount> listByVendorId(Long vendorId) {
		String sql = "select a.* from manage_one_account a where a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(ManageOneAccount.class, sql, paramMap);
	}

	public boolean removeByVendorId(Long vendorId) {
		String sql = "delete from manage_one_account where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public boolean remove(Long id) {
		String sql = "delete from manage_one_account where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.execute(sql, params) > 0;
	}

}
