package com.palmnest.dao.estateserve.impl;

import com.palmnest.dao.estateserve.VirtualAccountDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.estateserve.VirtualAccount;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Repository("virtualAccountDao")
public class VirtualAccountDaoImpl extends GenericDaoHibernate<VirtualAccount, Long> implements VirtualAccountDao {
	public VirtualAccountDaoImpl() {
		super(VirtualAccount.class);
	}

	@Override
	public List<VirtualAccount> getVirAccountByVirId(int virId) {
		String sql = "from VirtualAccount where virId=? and isEnable='1'";
		List<VirtualAccount> list = getSession().createQuery(sql).setInteger(0, virId).list();
		return list;
	}

	@Override
	public List<VirtualAccount> getVirAccountByAccount(String orgId, String account,
													   String password) {
		String sql = "from VirtualAccount where isEnable='1' and virAccount=? and password=? and orgId=?";
		List<VirtualAccount> list = getSession().createQuery(sql).setString(0, account).setString(1, password).setString(2, orgId).list();
		return list;
	}

	@Override
	public List<VirtualAccount> getVirAccountByVirIdIsEnable(int virId) {
		String sql = "from VirtualAccount where virId=? and isEnable='0'";
		List<VirtualAccount> list = getSession().createQuery(sql).setInteger(0, virId).list();
		return list;
	}

	@Override
	public List<VirtualAccount> getVirAccByVirId(int virId) {
		String sql = "from VirtualAccount where virId=?";
		List<VirtualAccount> list = getSession().createQuery(sql).setInteger(0, virId).list();
		return list;
	}
	
	@Override
	public List<VirtualAccount> getVirAccByVirAndOrg(int virId,int orgId) {
		String sql="from VirtualAccount where virId=? and orgId=? and isEnable='1'";
		List<VirtualAccount> list=getSession().createQuery(sql).setInteger(0, virId).setInteger(1, orgId).list();
		return list;
	}

	@Override
	public VirtualAccount getVirtualAccountByName(String virAccount) {
		String sql = "from VirtualAccount where virAccount=? and isEnable=1";
		List<VirtualAccount> list = getSession().createQuery(sql).setString(0, virAccount).list();
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}
}
