package com.community.huiju.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.TaSysMenu;
import com.community.huiju.service.ITaSysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * APP端菜单页面 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-03-06
 */
@RestController
@RequestMapping("/")
@Api(value = "APP菜单 API", description = "APP菜单 API")
public class TaSysMenuController extends BaseController {
	
	@Autowired
	private ITaSysMenuService taSysMenuService;
	
	@ApiOperation(value = "获取菜单列表", notes = "获取菜单列表")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
	})
	@RequestMapping(value = "/ta/menu/list",method = RequestMethod.GET)
	public ResponseBean getList(HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = taSysMenuService.getList(userElement);
		return responseBean;
	}
	
	@ApiOperation(value = "启用菜单", notes = "启用菜单")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataTypeClass = Integer.class, name = "id", value = "编号id"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
	})
	@RequestMapping(value = "/ta/menu/enable/{id}",method = RequestMethod.POST)
	public ResponseBean enableMenu(@PathVariable(value = "id") Integer id, HttpSession session) {
		ResponseBean responseBean = new ResponseBean();

		UserElement userElement = getUserElement(session);

		// 判断是否启动了 8 个菜单
		QueryWrapper<TaSysMenu> sysMenuQueryWrapper = new QueryWrapper<>();
		sysMenuQueryWrapper.eq("status", "0");
		sysMenuQueryWrapper.eq("community_id", userElement.getCommunityId());
		int count = taSysMenuService.count(sysMenuQueryWrapper);
		if (count >= 8) {
			responseBean.addError("先停用一个已开启的！");
			return responseBean;
		}

		TaSysMenu sysMenu = new TaSysMenu();
		sysMenu.setId(id);
		sysMenu.setStatus("0");
		boolean stste = taSysMenuService.updateById(sysMenu);
		if (stste){
			responseBean.addSuccess("启用成功");
			return responseBean;
		}
		responseBean.addSuccess("启用失败");
		return responseBean;
	}
	
	@ApiOperation(value = "停用菜单", notes = "停用菜单")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataTypeClass = Integer.class, name = "id", value = "编号id"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
	})
	@RequestMapping(value = "/ta/menu/disable/{id}",method = RequestMethod.POST)
	public ResponseBean disableMenu(@PathVariable(value = "id") Integer id,HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		TaSysMenu sysMenu = new TaSysMenu();
		sysMenu.setId(id);
		sysMenu.setStatus("1");
		boolean stste = taSysMenuService.updateById(sysMenu);
		if (stste){
			responseBean.addSuccess("停用成功");
			return responseBean;
		}
		responseBean.addSuccess("停用失败");
		return responseBean;
	}
}
