package com.palmnest.service.estateserve.impl;

import com.palmnest.application.dto.input.MeetingOrgDto;
import com.palmnest.dao.estateserve.FloorInfoDao;
import com.palmnest.dao.estateserve.RoomInfoDao;
import com.palmnest.model.FloorInfo;
import com.palmnest.model.RoomInfo;
import com.palmnest.model.property.TbBuild;
import com.palmnest.service.estateserve.FloorInfoManager;
import com.palmnest.service.estateserve.VirtualArchManager;
import com.palmnest.service.impl.GenericManagerImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

/**
 * 楼层服务接口实现类
 *
 * @author longmeizhen
 */
@Slf4j
@Service("floorInfoManager")
public class FloorInfoManagerImpl extends GenericManagerImpl<FloorInfo, Integer> implements FloorInfoManager {


	private FloorInfoDao floorInfoDao;

	@Autowired
	public void setFloorInfoDao(FloorInfoDao floorInfoDao) {
		this.floorInfoDao = floorInfoDao;
		this.dao = floorInfoDao;
	}

	@Autowired
	private RoomInfoDao        roomInfoDao;
	@Autowired
	private VirtualArchManager virtualArchManager;

	/**
	 * 根据楼栋ID获取楼层列表
	 *
	 * @param orgId 楼栋id
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<FloorInfo> getFloorInfoByBuildId(int buildId) throws Exception {
		return floorInfoDao.getFloorInfoByBuildId(buildId);
	}

	@Override
	public void saveOrUpdateFloorInfo(FloorInfo floorInfo) throws Exception {
		if (floorInfo.getId() == null) {
			floorInfoDao.saveObj(floorInfo);
		} else {
			floorInfoDao.updateObj(floorInfo);
		}
	}

	@Override
	public List<FloorInfo> getFloorListByOrgId(int orgId, String floorIds)
			throws Exception {
//		String floorIdStr="";
//		if(StringUtils.isNotEmpty(floorIds)){
//			String [] str=floorIds.split(",");
//			for(int i=0;i<str.length;i++){
//				floorIdStr+="'"+str[i]+"',";
//			}
//			floorIds=floorIdStr.substring(0,floorIdStr.length()-1);
//		}
		return floorInfoDao.getFloorListByOrgId(orgId, floorIds);
	}

	@Override
	public List<FloorInfo> getFloorListByName(String name, String buildId) throws Exception {
		return floorInfoDao.getFloorListByName(name, buildId);
	}

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


	@Override
	public List<Map<String, Object>> getFloorAndRoomByOrgId(int buildId)
			throws Exception {
		List<FloorInfo>           floorList = floorInfoDao.getFloorInfoByBuildId(buildId);
		List<Map<String, Object>> mapList   = new ArrayList<Map<String, Object>>();
		List<String>              objList   = virtualArchManager.getBandRoomId();
		if (floorList != null && floorList.size() > 0) {
			for (int i = 0; i < floorList.size(); i++) {
				Map<String, Object> floorMap  = new HashMap<String, Object>();
				FloorInfo           floorInfo = floorList.get(i);
				if (floorInfo != null) {
					floorMap.put("floorId", floorInfo.getId());
					floorMap.put("floorName", floorInfo.getName());
					List<RoomInfo>            roomList = roomInfoDao.getRoomInfoByFloorId(floorInfo.getId());
					List<Map<String, Object>> list     = new ArrayList<Map<String, Object>>();
					for (int j = 0; j < roomList.size(); j++) {
						Map<String, Object> roomMap  = new HashMap<String, Object>();
						RoomInfo            roomInfo = roomList.get(j);
						roomMap.put("roomName", roomInfo.getName());
						roomMap.put("roomId", roomInfo.getId());
						boolean bool = objList.contains(roomInfo.getId().toString());
						if (bool) {
							roomMap.put("isLess", 1);//如果房间号在list中，代表该房间号已有企业入驻了
						} else {
							roomMap.put("isLess", 0);
						}
						list.add(roomMap);
					}
					floorMap.put("roomList", list);
				}

				mapList.add(floorMap);
			}
		}
		return mapList;
	}

	@Override
	public List<FloorInfo> getFloorAll() {
		return floorInfoDao.getFloorAll();
	}

	@Override
	public List<FloorInfo> getFloorHaveMeeting(String orgId) {
		return floorInfoDao.getFloorHaveMeeting(orgId);
	}

	@Override
	public List<TbBuild> getTbBuildListByOrgaid(String orgId) {

		return floorInfoDao.getTbBuildListByOrgaid(orgId);
	}

	@Override
	public TbBuild getTbBuildById(int id) {
		return floorInfoDao.getTbBuildById(id);
	}

	@Override
	public List<Map<String, Object>> getFloorAndRoomList(String roomIds) {

		DecimalFormat df4 = new DecimalFormat();
		df4.applyPattern("0.00");

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		roomIds = roomIds.replace(",", ";");
		String[] roomIdStr = roomIds.split(";");
		if (roomIdStr != null && roomIdStr.length > 0) {
			for (int j = 0; j < roomIdStr.length; j++) {
				Map<String, Object> map       = new HashMap<String, Object>();
				RoomInfo            roomInfo  = roomInfoDao.get(Integer.parseInt(roomIdStr[j]));
				FloorInfo           floorInfo = floorInfoDao.get(Integer.parseInt(String.valueOf(roomInfo.getFloorId())));
				map.put("floorId", floorInfo.getId().toString());
				map.put("floorName", floorInfo.getName());
				map.put("roomId", roomInfo.getId().toString());
				map.put("roomName", roomInfo.getName());
				if (null != roomInfo.getArea()) {
					map.put("area", df4.format(roomInfo.getArea().setScale(2)));
				} else {
					map.put("area", "");
				}
				list.add(map);
			}
		}
		return list;
	}

	@Override
	public List<MeetingOrgDto> getFloorByStartTime(String startTime,
												   String endTime) throws Exception {
		List<MeetingOrgDto>       orgList = new ArrayList<MeetingOrgDto>();
		Set<Map<String, Object>>  orgSet  = new HashSet<Map<String, Object>>();
		List<Map<String, Object>> mapList = floorInfoDao.getFloorData(startTime, endTime);
		if (mapList != null && mapList.size() > 0) {
			for (int i = 0; i < mapList.size(); i++) {
				Map<String, Object> map    = mapList.get(i);
				Map<String, Object> orgMap = new HashMap<String, Object>();
				orgMap.put("orgId", map.get("orgId") == null ? "" : map.get("orgId").toString());
				orgMap.put("orgName", map.get("orgName") == null ? "" : map.get("orgName").toString());
				orgMap.put("client", map.get("client") == null ? "" : map.get("client").toString());
				orgMap.put("buildId", map.get("buildId") == null ? "" : map.get("buildId").toString());
				orgMap.put("buName", map.get("buName") == null ? "" : map.get("buName").toString());
				orgMap.put("isEnable", map.get("orgEn") == null ? "" : map.get("orgEn").toString());
				orgSet.add(orgMap);
			}
			if (orgSet != null && orgSet.size() > 0) {
				for (Map<String, Object> map : orgSet) {
					String                    buildId   = map.get("buildId") == null ? "" : map.get("buildId").toString();
					List<Map<String, Object>> floorList = new ArrayList<Map<String, Object>>();
					for (int j = 0; j < mapList.size(); j++) {
						Map<String, Object> mapFloor = mapList.get(j);
						String              buId     = mapFloor.get("buildId") == null ? "" : mapFloor.get("buildId").toString();
						if (buildId.equals(buId)) {//如果相同，就增加一条楼层信息
							Map<String, Object> floorMap = new HashMap<String, Object>();
							floorMap.put("floorId", mapFloor.get("floorId") == null ? "" : mapFloor.get("floorId").toString());
							floorMap.put("floorName", mapFloor.get("floorName") == null ? "" : mapFloor.get("floorName").toString() + "楼");
							floorMap.put("isEnable", mapFloor.get("flrEn") == null ? "" : mapFloor.get("flrEn").toString());
							floorList.add(floorMap);
						}
					}
					MeetingOrgDto dto = new MeetingOrgDto();
					dto.setOrgId(map.get("orgId") == null ? "" : map.get("orgId").toString());
					dto.setOrgName(map.get("orgName") == null ? "" : map.get("orgName").toString());
					dto.setClient(map.get("client") == null ? "" : map.get("client").toString());
					dto.setBuildId(map.get("buildId") == null ? "" : map.get("buildId").toString());
					dto.setBuName(map.get("buName") == null ? "" : map.get("buName").toString());
					dto.setIsEnable(map.get("isEnable") == null ? "" : map.get("isEnable").toString());
					dto.setFloorList(floorList);
//					map.put("floorList", floorList);
					orgList.add(dto);
				}
			}
		}
		return orgList;
	}

	@Override
	public List<FloorInfo> getALLFloor(String orgId) {
		return floorInfoDao.getALLFloor(orgId);
	}
}
