package com.palmnest.dao.estateserve.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.palmnest.dao.estateserve.UserAccountDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.AppFunction;
import com.palmnest.model.estateserve.UserAccountRecode;
import com.palmnest.model.estateserve.VirtualAccount;

@Repository("userAccountDao")
public class UserAccountDaoImpl extends GenericDaoHibernate<UserAccountRecode, Long> implements UserAccountDao {
	public UserAccountDaoImpl() {
		super(UserAccountRecode.class);
	}

	@Override
	public List<UserAccountRecode> getUserAccountRecode(String orgId, String openId) {
		// SQL注释掉的部分    and isOnline='2'
		String sql = "from UserAccountRecode where openId=? and orgId=? ";
		List<UserAccountRecode> list = getSession().createQuery(sql).setString(0, openId).setString(1, orgId).list();
		return list;
	}

	@Override
	public List<UserAccountRecode> getUserAccountByAccount(String orgId, String accountId,
														   String openId) {
		String sql = "from UserAccountRecode where openId=? and accountId=?  and orgId=?";
		List<UserAccountRecode> list = getSession().createQuery(sql).setString(0, openId).setLong(1, Long.parseLong(accountId)).setString(2, orgId).list();
		return list;
	}

	@Override
	public List<UserAccountRecode> getUserAccountByAccountId(Long accountId) {
		String sql = "from UserAccountRecode where accountId=? ";
		List<UserAccountRecode> list = getSession().createQuery(sql).setLong(0, accountId).list();
		return list;
	}

	@Override
	public int updateUserAccountByAccId(Long accountId) throws Exception {
		String sql = " update  tb_user_account  set  isOnline='1' where accountId=? ";
		return getSession().createSQLQuery(sql).setLong(0, accountId).executeUpdate();
	}

	@Override
	public int isHaveAccLogin(String openId, String orgId) throws Exception {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select u.* from tb_user_account u ");
		sql.append(" left join tb_vir_account a on a.id=u.accountId ");
		sql.append(" left join tb_virtualarchitecture v on v.id=a.virId ");
		sql.append(" where u.isOnline='2' and a.isEnable='1' and v.isEnabled='1' ");
		sql.append(" and v.organizationId=? and u.openId=? ");
		List<UserAccountRecode> list = getSession().createSQLQuery(sql.toString()).setInteger(0, Integer.parseInt(orgId)).setString(1, openId).list();
		if (list != null && list.size() > 0) {
			return 1;
		}
		return 0;
	}

}
