package com.zhgd.xmgl.modules.basicdata.controller.admin;

import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.modules.basicdata.dto.MenuTreeDto;
import com.zhgd.xmgl.modules.basicdata.entity.BaseMenu;
import com.zhgd.xmgl.modules.basicdata.service.IBaseMenuService;
import com.zhgd.xmgl.util.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @Title: Controller
 * @Description: 系统菜单
 * @author： pds
 * @date：   2020-10-12
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/baseMenu")
@Slf4j
@Api(tags = "系统菜单")
public class BaseMenuController {
	@Autowired
	private IBaseMenuService baseMenuService;
	
	/**
	  * 列表查询
	 * @return
	 */
	@OperLog(operModul = "系统菜单管理", operType = "列表查询", operDesc = "列表查询系统菜单信息")
	@ApiOperation(value = " 列表查询系统菜单信息", notes = "列表查询系统菜单信息", httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "moduleId", value = "模块ID", paramType = "query", required = true, dataType = "String"),
    })
    @PostMapping(value = "/list")
	public Result<List<BaseMenu>> selectBaseMenuList(@RequestBody Map<String,Object> map) {
		return Result.success(baseMenuService.selectBaseMenuList(map));
	}

//	@OperLog(operModul = "系统菜单管理", operType = "列表查询", operDesc = "列表层级查询模块下的菜单")
//    @ApiOperation(value = " 列表层级查询系统菜单信息", notes = "列表层级查询系统菜单信息", httpMethod="POST")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "moduleId", value = "模块ID", paramType = "query", required = false, dataType = "String"),
//    })
//    @PostMapping(value = "/selectGroupList")
//    public Result<JSONArray> selectGroupList(@RequestBody Map<String,Object> map) {
//        return Result.success(baseMenuService.selectGroupList(map));
//    }

	/**
	  *   添加
	 * @param baseMenu
	 * @return
	 */
	@OperLog(operModul = "系统菜单管理", operType = "新增", operDesc = "新增系统菜单")
	@ApiOperation(value = " 添加系统菜单信息", notes = "添加系统菜单信息" , httpMethod="POST")
	@PostMapping(value = "/add")
	public Result<BaseMenu> add(@RequestBody BaseMenu baseMenu) {
        baseMenuService.saveBaseMenu(baseMenu);
        return Result.ok();
	}
	
	/**
	  *  编辑
	 * @param baseMenu
	 * @return
	 */
	@OperLog(operModul = "系统菜单管理", operType = "修改", operDesc = "编辑系统菜单")
	@ApiOperation(value = "编辑系统菜单信息", notes = "编辑系统菜单信息" , httpMethod="POST")
	@PostMapping(value = "/edit")
	public Result<BaseMenu> edit(@RequestBody BaseMenu baseMenu) {
        baseMenuService.editBaseMenu(baseMenu);
        return Result.ok();
	}
	
	/**
	  *   通过id删除
	 * @param
	 * @return
	 */
	@OperLog(operModul = "系统菜单管理", operType = "删除", operDesc = "删除单个系统菜单")
	@ApiOperation(value = "删除系统菜单信息", notes = "删除系统菜单信息" , httpMethod="POST")
	@ApiImplicitParam(name = "menuId", value = "系统菜单ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/delete")
	public Result<BaseMenu> delete(@RequestBody BaseMenu baseMenuEntity) {
		Result<BaseMenu> result = new Result<BaseMenu>();
		BaseMenu baseMenu = baseMenuService.getById(baseMenuEntity.getMenuId());
		if(baseMenu==null) {
            result.error500(MessageUtil.get("notFindErr"));
		}else {
			boolean ok = baseMenuService.delInfo(baseMenuEntity.getMenuId());
			if(ok) {
                result.successMsg(MessageUtil.get("deleteSucess"));
			}
		}
		
		return result;
	}
	

	
	/**
	  * 通过id查询
	 * @param
	 * @return
	 */
	@OperLog(operModul = "系统菜单管理", operType = "查询", operDesc = "查询单个系统菜单")
	@ApiOperation(value = "通过id查询系统菜单信息", notes = "通过id查询系统菜单信息" , httpMethod="POST")
    @ApiImplicitParam(name = "menuId", value = "系统菜单ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/queryById")
	public Result<BaseMenu> queryById(@RequestBody Map<String,Object> map) {
		Result<BaseMenu> result = new Result<BaseMenu>();
		BaseMenu baseMenu = baseMenuService.getById(MapUtils.getString(map,"menuId"));
		if(baseMenu==null) {
            result.error500(MessageUtil.get("notFindErr"));
		}else {
			result.setResult(baseMenu);
			result.setSuccess(true);
		}
		return result;
	}

    /*@ApiOperation(value = "查询项目默认的菜单列表", notes = "查询项目默认的菜单列表", httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectSn", value = "项目sn", paramType = "query", required = true, dataType = "String"),
    })
    @PostMapping(value = "/getProjectBaseMenuList")
    public Result<List<EntityMap>> getProjectBaseMenuList(@RequestBody Map<String, Object> map) {
        return Result.success(baseMenuService.getProjectBaseMenuList(map));
    }*/

//	@OperLog(operModul = "系统菜单管理", operType = "查询", operDesc = "查询项目默认的模块和菜单列表")
//    @ApiOperation(value = "查询项目默认的模块和菜单列表", notes = "查询项目默认的模块和菜单列表", httpMethod="POST")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "projectSn", value = "项目sn", paramType = "query", required = true, dataType = "String"),
//    })
//    @PostMapping(value = "/getProjectModuleAndMenu")
//    public Result<Map<String, Object>> getProjectModuleAndMenu(@RequestBody Map<String, Object> map) {
//        return Result.success(baseMenuService.getProjectModuleAndMenu(map));
//    }

//	@OperLog(operModul = "系统菜单管理", operType = "查询", operDesc = "查询项目默认的菜单列表并根据模块分组")
//    @ApiOperation(value = "查询项目默认的菜单列表并根据模块分组", notes = "查询项目默认的菜单列表并根据模块分组", httpMethod="POST")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "projectSn", value = "项目sn", paramType = "query", required = true, dataType = "String"),
//    })
//    @PostMapping(value = "/getProjectModuleGroupMenuList")
//    public Result<List<BaseModule>> getProjectModuleGroupMenuList(@RequestBody Map<String, Object> map) {
//        return Result.success(baseMenuService.getProjectModuleGroupMenuList(map));
//    }

    /*@ApiOperation(value = "查询指定系统模块菜单信息", notes = "查询指定系统模块菜单信息", httpMethod="POST")
    @PostMapping(value = "/getDefaultProjectMenu")
    public Result<Map<String,Object>> getDefaultProjectMenu() {
        return Result.success(baseMenuService.getDefaultProjectMenu());
    }*/

	/**
	 * 获取树形模块、菜单全部信息
	 * @return
	 */
	@OperLog(operModul = "系统菜单管理", operType = "查询", operDesc = "获取树形模块、菜单全部信息")
	@ApiOperation(value = "获取树形模块、菜单全部信息", notes = "获取树形模块、菜单全部信息" , httpMethod="GET")
	@GetMapping(value = "/queryAll")
	public Result<List<MenuTreeDto>> queryAll() {
		return Result.success(baseMenuService.treeMenu(0));
	}

	/**
	 * 用户查询自身菜单树形列表
	 * @return
	 */
	@OperLog(operModul = "系统菜单管理", operType = "查询", operDesc = "用户查询自身菜单树形列表")
	@ApiOperation(value = "用户查询自身菜单树形列表", notes = "用户查询自身菜单树形列表" , httpMethod="POST")
	@ApiImplicitParam(name = "moduleId", value = "模块ID", paramType = "query", required = true, dataType = "Long")
	@PostMapping(value = "/queryBySelf")
	public Result<List<MenuTreeDto>> queryByModule(@RequestBody Map<String,Object> map) {
		return Result.success(baseMenuService.myTreeMenu(MapUtils.getLong(map,"moduleId")));
	}

	/**
	 * 获取角色的菜单权限
	 * @return
	 */
	@OperLog(operModul = "系统菜单管理", operType = "查询", operDesc = "获取角色的菜单权限")
	@ApiOperation(value = "获取角色的菜单权限", notes = "获取角色的菜单权限" , httpMethod="POST")
	@ApiImplicitParam(name = "roleId", value = "系统角色表ID", paramType = "query", required = true, dataType = "Long")
	@PostMapping(value = "/queryByRole")
	public Result<Set<Long>> queryByRole(@RequestBody Map<String,Object> map) {
		return Result.success(baseMenuService.roleMenu(MapUtils.getString(map,"roleId")));
	}
}
