package com.hotent.portal.controller;


import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotent.base.enums.ResponseErrorEnums;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.*;
import com.hotent.base.util.StringUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.sys.persistence.manager.SysMethodManager;
import com.hotent.sys.persistence.model.SysMethod;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 系统菜单 控制器类
 * 
 * @company 广州宏天软件股份有限公司
 * @author liyg
 * @email liyg@jee-soft.cn
 * @date 2018-07-02 17:18:55
 */
@RestController
@RequestMapping("/sys/sysMethod/v1")
@Api(tags="系统菜单方法")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
public class SysMethodController extends BaseController<SysMethodManager, SysMethod>{
	@Resource
	SysMethodManager sysMethodManager;
//	@Autowired
//	UserManager userService;

	@RequestMapping(value="listJson", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "菜单资源数据", httpMethod = "POST", notes = "菜单资源数据")
	public PageList<SysMethod> listJson(@ApiParam(name="queryFilter",value="通用查询对象")@RequestBody QueryFilter<SysMethod> queryFilter) throws Exception {
		return sysMethodManager.query(queryFilter);
	}
	
	@RequestMapping(value="getRoleMethods", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取角色已有的后台方法集", httpMethod = "POST", notes = "获取角色已有的后台方法集")
	public PageList<SysMethod> getRoleMethods(@ApiParam(name="roleAlias",value="角色别名") @RequestParam("roleAlias") String roleAlias,@ApiParam(name="queryFilter",value="通用查询对象")@RequestBody QueryFilter queryFilter){
		return sysMethodManager.getRoleMethods(roleAlias,queryFilter);
	}

	/**
	 * 通过菜单和角色 获取 授权的接口
	 * @author: 梁宏锦
	 * @date: 2024/04/09
	 * @param roleAlias: 角色别名 xgly,xtgly
	 * @param menuAlias: 菜单别名 demension
	 * @param page: 页号
	 * @param pageSize: 分页大小
	 * @return: com.hotent.base.query.PageList<java.util.Map<java.lang.String,java.lang.Object>>
	 **/
	@RequestMapping(value="getCheckMethodsByRoleAlias", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "通过菜单和角色 获取 授权的接口", httpMethod = "GET", notes = "通过菜单和角色 获取 授权的接口")
	public CommonResult<PageList<Map<String, Object>>> getCheckMethodsByRoleAlias(
			@ApiParam(name="roleAlias",value="角色别名") @RequestParam(required = false) String roleAlias,
			@ApiParam(name="menuAlias",value="菜单别名") @RequestParam("menuAlias") String menuAlias,
			@ApiParam(name = "page", value = "页号",required = false) @RequestParam(required = false) Integer page,
			@ApiParam(name = "pageSize", value = "分页大小",required = false) @RequestParam(required = false) Integer pageSize) {
		IPage<Map<String, Object>> npage = new Page<Map<String, Object>>(page, pageSize);
		List<String> alias = null;
		if(StringUtil.isNotEmpty(roleAlias)) {
			alias = new ArrayList<>();
			alias = Arrays.asList(roleAlias.split(","));
		}

		PageList<Map<String, Object>> pageList = sysMethodManager.getCheckMethodsByRoleAlias(npage, alias, menuAlias);
		CommonResult<PageList<Map<String, Object>>> result = new CommonResult(ResponseErrorEnums.SUCCESS_OPTION);
		result.setValue(pageList);
		return result;
	}

	/**
	 * 通过菜单/角色/接口别名 获取 授权的角色信息
	 * @author: 梁宏锦
	 * @date: 2024/04/09
	 * @param roleAlias: 角色别名 xgly,xtgly
	 * @param menuAlias: 菜单别名 demension
	 * @param methodAlias: 接口别名
	 * @return: com.hotent.base.query.PageList<java.util.Map<java.lang.String,java.lang.Object>>
	 **/
	@RequestMapping(value="getRoleInfoByMethodAlias", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "通过菜单/角色/接口别名 获取 授权的角色信息", httpMethod = "GET", notes = "通过菜单/角色/接口别名 获取 授权的角色信息")
	public CommonResult<List<Map<String, Object>>> getRoleInfoByMethodAlias(
			@ApiParam(name="roleAlias",value="角色别名") @RequestParam(required = false) String roleAlias,
			@ApiParam(name="menuAlias",value="菜单别名") @RequestParam("menuAlias") String menuAlias,
			@ApiParam(name = "methodAlias", value = "接口别名") @RequestParam("methodAlias") String methodAlias) {
		List<String> alias = null;
		if(StringUtil.isNotEmpty(roleAlias)) {
			alias = new ArrayList<>();
			alias = Arrays.asList(roleAlias.split(","));
		}
		List<Map<String, Object>> list = sysMethodManager.getRoleInfoByMethodAlias(alias, menuAlias, methodAlias);

		CommonResult<List<Map<String, Object>>> result = new CommonResult(ResponseErrorEnums.SUCCESS_OPTION);
		result.setValue(list);
		return result;
	}

	/**
	 * 通过菜单/角色/接口别名 获取 授权的用户信息
	 * @author: 梁宏锦
	 * @date: 2024/04/09
	 * @param roleAlias: 角色别名 xgly,xtgly
	 * @param menuAlias: 菜单别名 demension
	 * @param methodAlias: 接口别名
	 * @param page: 页号
	 * @param pageSize: 分页大小
	 * @return: com.hotent.base.query.PageList<java.util.Map<java.lang.String,java.lang.Object>>
	 **/
	@RequestMapping(value="getUserInfoByMethodAlias", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "通过菜单/角色/接口别名 获取 授权的用户信息", httpMethod = "GET", notes = "通过菜单/角色/接口别名 获取 授权的用户信息")
	public CommonResult<Page<Map<String, Object>>> getUserInfoByMethodAlias(
			@ApiParam(name = "roleAlias",value="角色别名") @RequestParam(required = false) String roleAlias,
			@ApiParam(name = "menuAlias",value="菜单别名") @RequestParam("menuAlias") String menuAlias,
			@ApiParam(name = "methodAlias", value = "接口别名") @RequestParam("methodAlias") String methodAlias,
			@ApiParam(name = "fullName", value = "用户姓名或账号", required = false) @RequestParam(required = false) String fullName,
			@ApiParam(name = "roleCode", value = "角色code", required = false) @RequestParam(required = false) String roleCode,
			@ApiParam(name = "page", value = "页号",required = false) @RequestParam(required = false) Integer page,
			@ApiParam(name = "pageSize", value = "分页大小",required = false) @RequestParam(required = false) Integer pageSize) throws Exception {
		List<String> alias = null;
		if(StringUtil.isNotEmpty(roleAlias)) {
			alias = new ArrayList<>();
			alias = Arrays.asList(roleAlias.split(","));
		}
		List<String> roleList = new ArrayList<>();
		if(StringUtil.isEmpty(roleCode)) {
			List<Map<String, Object>> resultList = sysMethodManager.getRoleInfoByMethodAlias(alias, menuAlias, methodAlias);
			roleList = resultList.stream()
					.map(p -> (String)p.get("CODE_"))
					.collect(Collectors.toList());
		} else {
			roleList.add(roleCode);
		}

		QueryFilter filter = QueryFilter.build().withPage(new PageBean(page, pageSize));
		filter.addFilter("r.CODE_", roleList, QueryOP.IN, FieldRelation.AND, "group_code");
		if (StringUtil.isNotEmpty(fullName)) {
			filter.addFilter("u.fullname_", fullName, QueryOP.LIKE, FieldRelation.OR, "quick");
			filter.addFilter("u.account_", fullName, QueryOP.LIKE, FieldRelation.OR, "quick");
		}
		filter.withSorter(new FieldSort("u.fullname_", Direction.ASC));
		Page<Map<String, Object>> list = (Page<Map<String, Object>>)sysMethodManager.getUserListMapByRoleCode(filter);

		CommonResult<Page<Map<String, Object>>> result = new CommonResult(ResponseErrorEnums.SUCCESS_OPTION);
		result.setValue(list);
		return result;
	}
}
