package com.palmnest.webapp.controller.backstage;

import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.application.dto.OutTreeDTO;
import com.palmnest.model.AppMenu;
import com.palmnest.service.estateserve.ServeConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @Author HXT
 * @Description 服务配置控制层
 * @Date 2018/8/13 10:52
 **/
@Slf4j
@Controller
@RequestMapping("/admin/serveConfig*")
public class ServeConfigController {


	public static String webUrl = PropertiesKit.readPropertiesValue("callbackWebUrl", "wechat.properties");

	@Autowired
	private ServeConfigService serveConfigService;

	/**
	 * 获取服务包列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/serverList", method = RequestMethod.POST)
	public AppResultDto serverList(HttpServletRequest request) {
		try {
			List<Map<String, Object>> serverList = serveConfigService.getServiceList();
			return AppResultUtil.createSuccesDto(serverList);
		} catch (Exception e) {
			log.error("serverList 接口操作失败", e);
			return AppResultUtil.createErrorDto("100", "获取数据失败!");
		}
	}

	/**
	 * 根据服务id与展示端获取系统功能树
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/functiontree", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public AppResultDto queryFunctionTree(HttpServletRequest request, HttpServletResponse response) {

		try {
			String serverId = request.getParameter("serverId");//服务包id
			String port     = request.getParameter("port");//哪个端 0-PC 1-APP

			//查询指定服务对应端的功能列表
			List<OutTreeDTO> funcTree = serveConfigService.getFunctionTreeById(Integer.parseInt(serverId), Integer.parseInt(port));
			return AppResultUtil.createSuccesDto(funcTree);
		} catch (Exception e) {
			log.error("queryFunctionTree 接口操作失败", e);
			return AppResultUtil.createErrorDto("100", "获取数据失败!");
		}
	}

	/**
	 * 初始化菜单树列表
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/menutree", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public AppResultDto queryMenuTree(HttpServletRequest request, HttpServletResponse response) {

		try {
			String funIds = request.getParameter("funIds");//功能ids
			//查询指定服务对应端的功能列表
			List<OutTreeDTO> menuTree = serveConfigService.getMenuTreeById(funIds);
			return AppResultUtil.createSuccesDto(menuTree);
		} catch (Exception e) {
			log.error("queryMenuTree 接口操作失败", e);
			return AppResultUtil.createErrorDto("100", "获取数据失败!");
		}
	}

	/**
	 * 保存修改服务-功能关系
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveUpdateServiceFunctionRelation", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public AppResultDto saveUpdateServiceFunctionRelation(HttpServletRequest request, HttpServletResponse response) {

		try {
			String   serverId    = request.getParameter("serverId");//服务包id
			String   port        = request.getParameter("port");//哪个端 0-PC 1-APP
			String   functionIds = request.getParameter("funIds");//功能id串
			String[] ids         = functionIds.trim().split(",");

			if (null != ids && ids.length > 0) {
				serveConfigService.saveUpdate(Integer.parseInt(serverId), Integer.parseInt(port), ids);
			}
			return AppResultUtil.createSuccesDto();
		} catch (Exception e) {
			log.error("saveUpdateServiceFunctionRelation 接口操作失败", e);
			return AppResultUtil.createErrorDto("100", "获取数据失败!");
		}
	}

	/**
	 * 根据功能id查询对应菜单
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getMenuByFunid", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public AppResultDto getMenuByFunid(HttpServletRequest request, HttpServletResponse response) {
		try {
			String                    funIds = request.getParameter("funIds");//功能ids
			List<AppMenu>             list   = serveConfigService.getMenuByFunid(funIds);
			List<Map<String, Object>> map    = new ArrayList<Map<String, Object>>();
			for (AppMenu menu : list) {
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("id", menu.getTreeCode());
				m.put("pId", menu.getParentCode());
				m.put("name", menu.getMenuName());
				map.add(m);
			}
			AppResultDto dto = AppResultUtil.createSuccesDto(map);
			return dto;
		} catch (Exception e) {
			log.error("getMenuByFunid 接口获取菜单列表失败", e);
			return AppResultUtil.createErrorDto("100", "获取数据失败!");
		}
	}

	/**
	 * 访问模块菜单配置页面
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/serverPage")
	public ModelAndView serverPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView();

		model.setViewName("admin/service/serverPage");
		return model;
	}
}
