package com.palmnest.webapp.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.ElementConstant;
import com.palmnest.application.core.util.FileUploadUtils;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.core.util.Pages;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.model.User;
import com.palmnest.service.estateserve.AppUserManager;
import com.palmnest.service.estateserve.ServicePackBasicsManager;
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.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 服务项基础信息配置控制类
 *
 * @author xiemin
 * @created
 */
@Slf4j
@Controller
@RequestMapping("/admin/servicePackBasics*")
public class ServicePackBasicsController extends BaseFormController {

	@Autowired
	private ServicePackBasicsManager servicePackBasicsManager;
	@Autowired
	private AppUserManager           appUserManager;

	/**
	 * 查询基础信息列表--初始化
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {

		String name              = request.getParameter("name");
		String serviceTypeInfoId = request.getParameter("serviceTypeInfoId");
		String startTime         = request.getParameter("startTime");
		String endTime           = request.getParameter("endTime");
		String pageNo            = request.getParameter("pageNo");

		if (StringUtils.isEmpty(pageNo)) {
			pageNo = "1";
		}

		modelMap.put("name", name);
		modelMap.put("startTime", startTime);
		modelMap.put("endTime", endTime);
		modelMap.put("pageNo", pageNo);
		modelMap.put("serviceTypeInfoId", serviceTypeInfoId);

		return "admin/servicePack/servicePackBasicsList";
	}

	/**
	 * 查询基础信息列表
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/listInfo")
	public String listInfo(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {

		String name      = request.getParameter("name");
		String isDeposit = request.getParameter("isDeposit");
		String belongId  = request.getParameter("belongId");
		String startTime = request.getParameter("startTime");
		String endTime   = request.getParameter("endTime");
		String pageNo    = request.getParameter("pageNo");

		if (StringUtils.isEmpty(pageNo)) {
			pageNo = "1";
		}

		modelMap.put("name", name);
		modelMap.put("isDeposit", isDeposit);
		modelMap.put("startTime", startTime);
		modelMap.put("endTime", endTime);
		modelMap.put("pageNo", pageNo);
		modelMap.put("belongId", belongId);

		Pages<Map<String, String>> page = servicePackBasicsManager.queryServicePackBasicsList(modelMap);
		modelMap.put("page", page);
		modelMap.put("visitPrefix", FileUploadUtils.getVisitPrefix());

		return "admin/servicePack/servicePackBasicsListInfo";
	}

	/**
	 * 加载数据--服务项基础信息
	 *
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "queryServicePackBasics")
	public String queryServicePackBasics(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {

		String id = request.getParameter("id");

		Map<String, String> map = new HashMap<String, String>();

		if (StringUtils.isNotEmpty(id)) {
			//根据场地id查询服务项详细信息
			map = servicePackBasicsManager.findDetails(Integer.valueOf(id));
		}

		modelMap.put("map", map);
		return "admin/servicePack/addServicePackBasics";
	}

	@RequestMapping(value = "addServicePackBasics")
	public String addServicePackBasics(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();

		String id            = request.getParameter("id");
		String name          = request.getParameter("name");
		String isDeposit     = request.getParameter("isDeposit");
		String servicePackId = request.getParameter("servicePackId");
		String deposit       = request.getParameter("deposit");

		Map<String, String> map = new HashMap<String, String>();

		map.put("id", id);
		map.put("name", name);
		map.put("isDeposit", isDeposit);
		map.put("deposit", deposit);
		map.put("servicePackId", servicePackId);

		try {
			User user = getUserManager().getUserByUsername(request.getRemoteUser());
			map.put("adminName", user.getUsername());
			//添加
			json = servicePackBasicsManager.addServicePackBasics(map);
			StringUtil.charSet(json, request, response);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		}

		json.put("code", -1);
		json.put("msg", "系统异常，操作失败");

		StringUtil.charSet(json, request, response);

		return null;
	}

	/**
	 * 删除服务项
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "deleteServicePackBasics")
	public String deleteServicePackBasics(HttpServletRequest request, HttpServletResponse response) {

		JSONObject json = new JSONObject();

		String id = request.getParameter("id");

		try {
			//删除
			json = servicePackBasicsManager.deleteServicePackBasics(id, ElementConstant.YESORNO_0);
			StringUtil.charSet(json, request, response);
			return null;
		} catch (Exception e) {
			log.error(LogExceptionStackUtil.LogExceptionStack(e));
		}

		json.put("code", -1);
		json.put("msg", "系统异常，操作失败");
		StringUtil.charSet(json, request, response);
		return null;
	}

}
