package com.palmnest.dao.estateserve.impl;

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

import org.apache.commons.lang.StringUtils;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.palmnest.application.Constants;
import com.palmnest.dao.estateserve.NeigouOrderDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.estateserve.NeigouOrder;

@Repository("neigouOrderDao")
public class NeigouOrderImpl extends GenericDaoHibernate<NeigouOrder, Integer> implements NeigouOrderDao {

	public NeigouOrderImpl() {
		super(NeigouOrder.class);
	}


	@Override
	public void updateLastMessageId(int last_message_id) {
		/*String strHQL="update tb_neigouOrder_info set last_message_id=? where id =1 ";
		Session session=this.getSession();
		session.setFlushMode(FlushMode.MANUAL);
		
		session.createSQLQuery(strHQL).setInteger(0,last_message_id).executeUpdate();
		
		session.flush();//切记不能遗忘  
		session.getTransaction().commit();  
		session.close();  */
		//getSession().createSQLQuery(strSQL).executeUpdate();
		
		/*NeigouOrder neigouOrder= neigouOrderDao.get(1);
		neigouOrder.setLast_message_id(last_message_id);
		neigouOrderDao.save(neigouOrder);*/


	}

	@Override
	public void updateCurrentMessageId(int currentmessageid) {
		this.getSession().createSQLQuery("update NeigouOrder set currentmessageid=? where id =1 ").setInteger(0, currentmessageid).executeUpdate();
	}


	@Override
	public NeigouOrder saveNeigouOrder(NeigouOrder neigouOrder) {
		return super.save(neigouOrder);
	}


	@Override
	public List<Map<String, Object>> getCompanyAndSuperUser(String companyId, String supperAdminId) {

		StringBuffer strSQL = new StringBuffer();
		strSQL.append("select u.id ContactUserId,u.userMobile 'Phone',u.userName 'ContactName',v.id 'Id',v.name 'Name'").append("\n");
		strSQL.append("from tb_virtualarchitecture  v left join tb_user u on u.virtualArchId=v.id").append("\n");
		strSQL.append("where u.roleType=1  #管理员").append("\n");
		strSQL.append("and contractStatus=1 #签约状态（1-已签约").append("\n");

		if (StringUtils.isNotEmpty(companyId)) {
			strSQL.append("and v.id='" + companyId + "'").append("\n");
		}

		if (StringUtils.isNotEmpty(supperAdminId)) {
			strSQL.append("and u.id='" + supperAdminId + "'").append("\n");
		}
		//and v.phone=u.userMobile

		List<Map<String, Object>> list = getJdbcTemplate().queryForList(strSQL.toString());
		return list;
	}

	@Override
	public List<Map<String, Object>> getSuperUser(String companyId, String supperUserIds) {

		StringBuffer strSQL = new StringBuffer();
		strSQL.append("select u.id ContactUserId,u.userMobile 'Phone',u.userName 'ContactName',v.id 'Id',v.name 'Name'").append("\n");
		strSQL.append("from tb_virtualarchitecture  v left join tb_user u on u.virtualArchId=v.id").append("\n");
		strSQL.append("where u.roleType=1  #管理员").append("\n");
		strSQL.append("and contractStatus=1 #签约状态（1-已签约").append("\n");

		if (StringUtils.isNotEmpty(companyId)) {
			strSQL.append("and v.id='" + companyId + "'").append("\n");
		}

		if (StringUtils.isNotEmpty(supperUserIds)) {
			strSQL.append("and u.id in (" + supperUserIds + ")").append("\n");
		}
		//and v.phone=u.userMobile

		List<Map<String, Object>> list = getJdbcTemplate().queryForList(strSQL.toString());
		return list;
	}

}
