package com.weichat.web;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.weichat.model.Qiyefuwu;
import com.weichat.service.ServerService;
import com.weichat.util.DateTimeUtils;

/**
 * ÆóÒµ·þÎñController
 * 
 * ÏîÄ¿Ãû³Æ£ºWeiChat ÀàÃû³Æ£ºServerController.java ÀàÃèÊö£ºTODO ´´½¨ÈË£ºÀîË§¿µ ´´½¨Ê±¼ä£ºÏÂÎç8:15:07
 * ÐÞ¸ÄÈË£ºÀîË§¿µ ÐÞ¸ÄÊ±¼ä£ºÏÂÎç8:15:07 ÐÞ¸Ä±¸×¢£º
 * 
 * FreeHuman Soft Team
 * 
 * @version 1.0 Beta
 */
@Controller("serverController")
@RequestMapping(value = "/server")
public class ServerController {
	private static Logger LOGGER = LoggerFactory
			.getLogger(ServerController.class);

	@Resource(name = "serverServiceImpl")
	private ServerService serverService;

	@RequestMapping(value = "/servershow", method = RequestMethod.GET)
	public String serverShow(HttpServletRequest request, ModelMap modelMap) {
		LOGGER.info("Ìø×ªµ½enterprise_update_situationÏÂµÄserverÒ³Ãæ³É¹¦£¡"
				+ DateTimeUtils
						.getNowDateOfStringFormatUsingDateTimeTemplateOne());
		Double id = Double.parseDouble(request.getParameter("id"));
		modelMap.addAttribute("server", serverService.findQiyefuwuById(id));
		modelMap.addAttribute("id", id);
		return "/update/enterprise_update_situation/server";
	}

	/**
	 * ÐÞ¸ÄÆóÒµ·þÎñ
	 * 
	 * @param response
	 * @param dangtuanjianshe
	 * @throws IOException
	 */
	@RequestMapping(value = "/serverupdate", method = RequestMethod.POST)
	public void updateQiyefuwu(HttpServletResponse response,
			HttpServletRequest request, @ModelAttribute Qiyefuwu qiyefuwu)
			throws IOException {
		LOGGER.info("ÆóÒµ·þÎñÐÞ¸Ä!"
				+ DateTimeUtils
						.getNowDateOfStringFormatUsingDateTimeTemplateOne());
		StringBuffer sbResult = new StringBuffer();
		// ÅÐ¶ÏÆóÒµ°²È«ÊÇ·ñ´æÔÚ
		if (serverService.checkQiyefuwu(qiyefuwu.getInfomation().getId())) {// ´æÔÚ
			if (serverService.updateQiyefuwu(qiyefuwu)) {
				sbResult.append("<script>alert('¹§Ï²£¡Êý¾ÝÒÑ³É¹¦ÐÞ¸Ä¡£'); parent.location.href='../company/companylist.jhtml';</script>");
			} else {
				sbResult.append("<script>alert('·Ç³£±§Ç¸£¬ÐÞ¸ÄÊý¾ÝÊ§°Ü£¡ÇëÖØÊÔÄúµÄ²Ù×÷¡£'); parent.location.href='../company/companylist.jhtml'</script>");
			}
		} else {// ²»´æÔÚ
				// µ÷ÓÃÐÂÔö·½·¨
			if (serverService.addServerByEnterpriseService(qiyefuwu, qiyefuwu
					.getInfomation().getId())) {
				sbResult.append("<script>alert('¹§Ï²£¡Êý¾ÝÒÑ³É¹¦Â¼Èë¡£'); parent.location.href='../company/companylist.jhtml';</script>");
			} else {
				sbResult.append("<script>alert('·Ç³£±§Ç¸£¬Â¼ÈëÊý¾ÝÊ§°Ü£¡ÇëÖØÊÔÄúµÄ²Ù×÷¡£'); parent.location.href='../company/companylist.jhtml'</script>");
			}

		}
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.getWriter().write(sbResult.toString());
	}

	/**
	 * Ìí¼ÓÒ»¸öÆóÒµ·þÎñÐÅÏ¢.
	 * 
	 * @param response
	 * @param dangtuanjianshe
	 * @throws IOException
	 */
	@RequestMapping(value = "/serveradd", method = RequestMethod.POST)
	public void addServer(HttpServletRequest request,
			HttpServletResponse response, @ModelAttribute Qiyefuwu qiyefuwu)
			throws IOException {
		StringBuffer sbResult = new StringBuffer();

		if (serverService.addServerByEnterpriseService(
				qiyefuwu,
				Double.valueOf(request.getSession()
						.getAttribute("enterpriseId").toString()))) {
			sbResult.append("<script>alert('¹§Ï²£¡Êý¾ÝÒÑ³É¹¦Â¼Èë¡£Çë¼ÌÐøÌí¼ÓÏà¹ØµÄÐÅÏ¢¡£'); parent.location.reload(true);</script>");
		} else {
			sbResult.append("<script>alert('·Ç³£±§Ç¸£¬Â¼ÈëÊý¾ÝÊ§°Ü£¡ÇëÖØÊÔÄúµÄ²Ù×÷¡£'); history.go(-1);</script>");
		}

		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.getWriter().write(sbResult.toString());
	}
}
