package com.palmnest.webapp.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.dao.estateserve.VirtualArchDao;
import com.palmnest.model.RoomInfo;
import com.palmnest.model.User;
import com.palmnest.service.estateserve.RoomInfoManager;
import com.palmnest.service.estateserve.VirtualArchManager;
import com.palmnest.webapp.controller.BaseFormController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * 房间号控制器
 *
 * @author longmeizhen
 */
@Slf4j
@Controller
@RequestMapping(value = "/admin/room/*")
public class RoomInfoController extends BaseFormController {

	@Autowired
	private RoomInfoManager    roomInfoManager;
	@Autowired
	private VirtualArchDao     virtualArchDao;
	@Autowired
	private VirtualArchManager virtualArchManager;

	/**
	 * 新增修改房间号信息
	 *
	 * @param roomInfo
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveUpdateRoom")
	public void saveUpdateRoom(@ModelAttribute("roomInfo") RoomInfo roomInfo, HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject json = new JSONObject();
		try {
			User       user     = (User) request.getSession().getAttribute("UserCache");
			Long       floorId  = Long.parseLong(request.getParameter("floorId"));
			String     roomName = request.getParameter("roomName");
			String     roomId   = request.getParameter("roomId");
			BigDecimal area     = roomInfo.getArea();
			if (StringUtils.isNotEmpty(roomId)) {//修改房间
				roomInfo = roomInfoManager.get(Integer.parseInt(roomId));
				if (roomName.equals(roomInfo.getName())) {//如果内容和当前的一样，允许修改
					roomInfo.setModifyTime(new Date());
					roomInfo.setModifier(user.getId());
					roomInfo.setName(roomName);
					roomInfo.setArea(area);
					roomInfo.setFloorId(floorId);
					roomInfo.setIsEnabled(1);
					String isSucces = roomInfoManager.saveOrUpdateRoomInfo(roomInfo);
					if ("success".equals(isSucces)) {
						json.put("code", "200");
						json.put("data", roomInfo);
						json.put("msg", "");
						StringUtil.charSet(json, request, response);
					} else {
						json.put("code", "100");
						json.put("data", "");
						json.put("msg", "");
						StringUtil.charSet(json, request, response);
					}

				} else {//如果不一样，首先判断数据库中会否存在，存在即返回，不存在就允许修改
					List<RoomInfo> list = roomInfoManager.getRoomListByName(roomName);
					if (list != null && list.size() > 0) {
						json.put("result", "fail");
						json.put("message", "房间名不能重复");
						StringUtil.charSet(json, request, response);
					} else {
						roomInfo.setModifyTime(new Date());
						roomInfo.setModifier(user.getId());
						roomInfo.setName(roomName);
						roomInfo.setFloorId(floorId);
						roomInfo.setIsEnabled(1);
						String isSucces = roomInfoManager.saveOrUpdateRoomInfo(roomInfo);
						if ("success".equals(isSucces)) {
							json.put("code", "200");
							json.put("data", roomInfo);
							json.put("msg", "");
							StringUtil.charSet(json, request, response);
						} else {
							json.put("code", "100");
							json.put("data", "");
							json.put("msg", "");
							StringUtil.charSet(json, request, response);
						}
					}
				}
			} else {//保存房间
				roomInfo.setCreateTime(new Date());
				roomInfo.setCreator(user.getId());
				roomInfo.setName(roomName);
				roomInfo.setFloorId(floorId);
				roomInfo.setIsEnabled(1);
				List<RoomInfo> list = roomInfoManager.getRoomListByName(roomName);
				if (list != null && list.size() > 0) {
					json.put("code", "100");
					json.put("data", "");
					json.put("msg", "房间名不能重复");
					StringUtil.charSet(json, request, response);
				} else {
					String isSucces = roomInfoManager.saveOrUpdateRoomInfo(roomInfo);
					if ("success".equals(isSucces)) {
						json.put("code", "200");
						json.put("data", roomInfo);
						json.put("msg", "");
						StringUtil.charSet(json, request, response);
					} else {
						json.put("code", "100");
						json.put("data", "");
						json.put("msg", "");
						StringUtil.charSet(json, request, response);
					}
				}
			}

		} catch (Exception e) {
			json.put("code", "100");
			json.put("data", "");
			json.put("msg", e.getMessage());
			StringUtil.charSet(json, request, response);
			log.error(e.getMessage());
			throw new Exception(e);
		}

	}

	/**
	 * 获取房间号列表
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getRoomList")
	public void getRoomList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject                json     = new JSONObject();
		int                       floorId  = Integer.parseInt(request.getParameter("floorId"));
		List<RoomInfo>            roomList = new ArrayList<RoomInfo>();
		List<String>              objList  = virtualArchManager.getBandRoomId();
		List<Map<String, Object>> list     = new ArrayList<Map<String, Object>>();
		try {
			roomList = roomInfoManager.getRoomInfoByfloorId(floorId);
			for (int i = 0; i < roomList.size(); i++) {
				Map<String, Object> roomMap  = new HashMap<String, Object>();
				RoomInfo            roomInfo = roomList.get(i);
				roomMap.put("roomInfo", roomInfo);
				boolean bool = objList.contains(roomInfo.getId().toString());
				if (bool) {
					roomMap.put("isLess", 1);//如果房间号在list中，代表该房间号已有企业入驻了
				} else {
					roomMap.put("isLess", 0);
				}
				list.add(roomMap);
			}
			json.put("code", "200");
			json.put("msg", "");
			json.put("data", list);
//			json.put("dataRoom", list);
			StringUtil.charSet(json, request, response);
		} catch (Exception e) {
			json.put("code", "100");
			json.put("data", "");
			json.put("msg", e.getMessage());
			StringUtil.charSet(json, request, response);
			log.error(e.getMessage());
			throw new Exception(e);
		}

	}


	/**
	 * 删除楼层及该楼层相应的房间
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteRoom")
	public void deleteRoom(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject json = new JSONObject();
		try {
			int      roomId   = Integer.parseInt(request.getParameter("floorId"));
			RoomInfo roomInfo = roomInfoManager.get(roomId);
			if (roomInfo != null) {
				roomInfoManager.remove(roomInfo);
			}
			json.put("code", "200");
			json.put("msg", "");
			json.put("data", "");
			StringUtil.charSet(json, request, response);
		} catch (Exception e) {
			json.put("code", "100");
			json.put("data", "");
			json.put("msg", e.getMessage());
			StringUtil.charSet(json, request, response);
			log.error(e.getMessage());
			throw new Exception(e);
		}
	}

}
