package com.palmnest.dao.estateserve.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.palmnest.dao.estateserve.RoomInfoDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.RoomInfo;

/**
 * 房间号DAO实现类
 *
 * @author longmeizhen
 */
@Repository("roomInfoDao")
public class RoomInfoDaoImpl extends GenericDaoHibernate<RoomInfo, Integer> implements RoomInfoDao {


	public RoomInfoDaoImpl() {
		super(RoomInfo.class);
	}

	/**
	 * 根据楼栋
	 * 获取楼层信息集合信息
	 *
	 * @param roleType
	 * @return
	 */
	@Override
	public List<RoomInfo> getRoomInfoByFloorId(int floorId) throws Exception {
		String strHQL = "from RoomInfo where  floorId=? and isEnabled=1 ";
		List<RoomInfo> list = getSession().createQuery(strHQL).setInteger(0, floorId).list();
		return list;
	}
	
	/**
	 * 根据楼栋
	 * 获取楼层信息集合信息
	 *
	 * @param roleType
	 * @return
	 */
	@Override
	public BigDecimal getSumArueByFloorId(int floorId) throws Exception {
		String sql = "SELECT ifnull(sum(area),0) from tb_roomno where floorId = ? and isEnabled=1 and areaRuleType in (1,2,3,4) ";
		return ((BigDecimal) getSession().createSQLQuery(sql).setInteger(0, floorId).uniqueResult());
	}

	/**
	 * 添加房间
	 */
	@Override
	public RoomInfo saveFloorInfo(RoomInfo roomInfo) throws Exception {
		return super.save(roomInfo);
	}


	@Override
	public List<RoomInfo> getRoomListByRoomIds(int floorId, String roomIds)
			throws Exception {
		StringBuilder strHQL = new StringBuilder("from RoomInfo where isEnabled=1 and floorId=? ");
		if (StringUtils.isNotEmpty(roomIds)) {
			strHQL.append(" and id in (" + roomIds + ") ");
		}
		List<RoomInfo> list = getSession().createQuery(strHQL.toString()).setInteger(0, floorId).list();
		return list;
	}

	@Override
	public List<RoomInfo> getRoomInfo() throws Exception {
		String strHQL = "from RoomInfo where   isEnabled=1 ";
		List<RoomInfo> list = getSession().createQuery(strHQL).list();
		return list;
	}

	@Override
	public List<RoomInfo> getRoomListByName(String name) throws Exception {
		String strHQL = "from RoomInfo where  name=? and isEnabled=1 ";
		List<RoomInfo> list = getSession().createQuery(strHQL).setString(0, name).list();
		return list;
	}

	@Override
	public List<RoomInfo> getRoomListByFloorIds(String floorIds)
			throws Exception {
		String strHQL = "from RoomInfo where  floorId in (" + floorIds + ") and isEnabled=1 ";
		List<RoomInfo> list = getSession().createQuery(strHQL).list();
		return list;
	}

	@Override
	public List<RoomInfo> getRoomListByRooms(String rooms) throws Exception {
		String strHQL = "from RoomInfo where name in (" + rooms + ") and isEnabled=1 ";
		List<RoomInfo> list = getSession().createQuery(strHQL).list();
		return list;
	}
	
	@Override
	public List<RoomInfo> getRoomListByRoomNos(String roomNos) throws Exception {
		String strHQL = "from RoomInfo where id in (" + roomNos + ") and isEnabled=1 ";
		List<RoomInfo> list = getSession().createQuery(strHQL).list();
		return list;
	}

	@Override
	public List<RoomInfo> getRoomListByRooms(String floorIds, String rooms) throws Exception {
		String strHQL = "from RoomInfo where floorId in (" + floorIds + ") and name in (" + rooms + ") and isEnabled=1 ";
		List<RoomInfo> list = getSession().createQuery(strHQL).list();
		return list;
	}
}
