package com.blueland.render.rest;


import java.util.HashMap;
/**
 * @author 代码自动生成工具
 * @version 1.0
 * @since  
 */
import java.util.List;
import java.util.Map;

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

import com.blueland.core.util.UserUtils;
import com.blueland.model.TPfApplication;
import com.blueland.result.R;
import com.blueland.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.blueland.bo.MenuEntity;

import io.swagger.annotations.Api;

import static com.blueland.constant.ResponseConstant.*;


@Api(value = "menu")
@RestController
@RequestMapping(value = "/menu")
public class MenuRest {
	@Autowired
	private IMenuService iMenuService;

	@Autowired
	private IApplicationService appService;

	@Autowired
	private SysRedisService sysRedisService;
	/** 
	 * 查看父对象
	 **/
	@RequestMapping(value = "/{menuId}/menuId/resource",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public Map<String, Object> viewParent(@PathVariable("menuId") String menuId)  {
		String appId = UserUtils.getTenantId();
		Map < String , Object > result = new HashMap< String , Object>(); 
		List<MenuEntity> menuEntryList = null;
		try {
			menuEntryList = iMenuService.getParent(menuId,appId);
			result.put("resultCode", RESULT_CODE_SUCCESS_CODE);
			result.put("resultMsg", "处理成功");
			result.put("result", menuEntryList);
		} catch (Exception e) {
			result.put("resultCode", RESULT_CODE_ERROR_CODE);
			result.put("resultMsg", "系统内部异常");
			e.printStackTrace();
		}
		return result;
	}
	
	/** 
	 * 查看对象
	 * 根据app_id获取资源
	 **/
	@RequestMapping(value = "/resource",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public Map<String, Object> listMenuAll(@RequestParam Map<String,Object> queryParams,HttpServletRequest request,HttpServletResponse response)  {
		String appId = UserUtils.getTenantId();
		Map < String , Object > result = new HashMap< String , Object>();
		List<MenuEntity> menuEntryList = null;
		
		try {
			if(StringUtils.isBlank(appId)){
				result.put("resultCode", RESULT_CODE_ERROR_CODE);
				result.put("resultMsg", "输入参数异常");
			}else{
				TPfApplication app = appService.getById(appId);
			    if(app==null){
			    	result.put("resultCode", RESULT_CODE_ERROR_CODE);
					result.put("resultMsg", "应用ID错误");
				}else{
					String menuName = null;
					String parentName = null;
					String menuType = null;
					if(queryParams.containsKey("menuName")) {
						menuName = queryParams.get("menuName").toString();
					}
					if(queryParams.containsKey("parentName")) {
						parentName = queryParams.get("parentName").toString();
					}
					if(queryParams.containsKey("menuType")) {
						menuType = queryParams.get("menuType").toString();
					}
					
					menuEntryList = iMenuService.getMenuChildren(appId,menuName,parentName,menuType);
					result.put("resultCode", RESULT_CODE_SUCCESS_CODE);
					result.put("resultMsg", "处理成功");
					result.put("result", menuEntryList);
				}
			}
		} catch (Exception e) {
			result.put("resultCode", RESULT_CODE_ERROR_CODE);
			result.put("resultMsg", "系统内部异常");
			e.printStackTrace();
		}
		return result;
	}

    @ApiOperation(value = "加载租户id:菜单类型type:菜单id对应菜单实体的redis数据")
    @GetMapping("/initMenu")
    public R initMenu(){
		sysRedisService.initMenu();
        return R.ok();
    }

    @ApiOperation(value = "加载租户id:角色id对应List<租户id:菜单类型type:菜单id>的redis数据")
    @GetMapping("/initMenuId")
    public R initMenuId(){
		sysRedisService.initMenuId();
        return R.ok();
    }

}



