package com.palmnest.service.estateserve.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.util.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.palmnest.dao.estateserve.FloorInfoDao;
import com.palmnest.dao.estateserve.RoomInfoDao;
import com.palmnest.dao.estateserve.VirtualArchDao;
import com.palmnest.model.FloorInfo;
import com.palmnest.model.RoomInfo;
import com.palmnest.service.estateserve.RoomInfoManager;
import com.palmnest.service.estateserve.VirtualArchManager;
import com.palmnest.service.impl.GenericManagerImpl;

/**
 * 房间服务接口实现类
 *
 * @author longmeizhen
 */
@Service("roomInfoManager")
public class RoomInfoManagerImpl extends GenericManagerImpl<RoomInfo, Integer> implements RoomInfoManager {

	private Logger LOG = Logger.getLogger(RoomInfoManagerImpl.class);

	private RoomInfoDao roomInfoDao;

	@Autowired
	public void setRoomInfoDao(RoomInfoDao roomInfoDao) {
		this.roomInfoDao = roomInfoDao;
		this.dao = roomInfoDao;
	}

	/**
	 * 根据楼层ID获取房间信息列表
	 *
	 * @param floorId 房间id
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<RoomInfo> getRoomInfoByfloorId(int floorId) throws Exception {
		return roomInfoDao.getRoomInfoByFloorId(floorId);
	}

	/**
	 * 保存或更新房间信息
	 */
	@Override
	public String saveOrUpdateRoomInfo(RoomInfo roomInfo) {
		String isSuccess = "fail";
		if (roomInfo.getId() == null) {
			RoomInfo room = roomInfoDao.save(roomInfo);
			if (room != null) {
				isSuccess = "success";
			}
		} else {
			try {
				roomInfoDao.updateObj(roomInfo);
				isSuccess = "success";
			} catch (Exception e) {
				LOG.error(e.getMessage());
				isSuccess = "fail";
			}
		}
		return isSuccess;
	}

	@Override
	public List<RoomInfo> getRoomListByRoomIds(int floorId, String roomIds)
			throws Exception {
		String floorIdStr = "";
		if (StringUtils.isNotEmpty(roomIds)) {
			String[] str = roomIds.split(",");
			for (int i = 0; i < str.length; i++) {
				floorIdStr += "'" + str[i] + "',";
			}
			roomIds = floorIdStr.substring(0, floorIdStr.length() - 1);
		}
		return roomInfoDao.getRoomListByRoomIds(floorId, roomIds);
	}

	@Override
	public List<RoomInfo> getRoomInfo() throws Exception {
		return roomInfoDao.getRoomInfo();
	}

	@Override
	public List<RoomInfo> getRoomListByName(String name) throws Exception {
		return roomInfoDao.getRoomListByName(name);
	}

	@Override
	public List<RoomInfo> getRoomListByFloorIds(String floorIds)
			throws Exception {
		String[] str = floorIds.split(",");
		String le = "";
		for (int i = 0; i < str.length; i++) {
			le = "'" + str[i] + "',";
		}
		if (StringUtils.isNotBlank(le)) {
			le = le.substring(0, le.length() - 1);
		}

		return roomInfoDao.getRoomListByFloorIds(le);
	}

	@Override
	public List<RoomInfo> getRoomListByRooms(String rooms) throws Exception {
		return roomInfoDao.getRoomListByRooms(rooms);
	}

	@Override
	public List<RoomInfo> getRoomListByRooms(String floorIds, String rooms) throws Exception {
		return roomInfoDao.getRoomListByRooms(floorIds, rooms);
	}

	@Override
	public List<RoomInfo> getRoomListByRoomNos(String roomNos) throws Exception {
		return roomInfoDao.getRoomListByRoomNos(roomNos);
	}
	
	@Override
	public BigDecimal getSumArueByFloorId(int floorId) throws Exception {
		return roomInfoDao.getSumArueByFloorId(floorId);
	}

}
