package cn.superdesk.uniorder.dubbo.consumer.sys.role;

import cn.superdesk.uniorder.common.enums.login.SysLoginTypeEnum;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dto.vo.sys.role.ViewMenuTree;
import cn.superdesk.uniorder.dto.vo.sys.role.ViewRoleMenu;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import cn.superdesk.uniorder.dubbo.facade.sys.role.DubboSysMenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 系统角色权限 - 系统菜单
 * @Author: linjinyu 重构
 * @CreateDate: 2021/11/24 下午2:36
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/11/24 下午2:36
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class SysMenuDubboConsumerService {


	@Reference(version = "1.0.0", timeout = 30000, retries = -1)
	private DubboSysMenuService dubboSysMenuService;

	@Resource
	private PermissionDubboCustomerService permissionDubboCustomerService;


	public List<ViewMenuTree> getTreeById(Long id) {

		return dubboSysMenuService.getTreeById(id);
	}

	public List<ViewMenuTree> getTreeByRoleId(Long roleId) {

		return dubboSysMenuService.getTreeByRoleId(roleId);
	}

	public List<ViewMenuTree> getTreeByUser(String token) {

//		Map<String, Object> tokenVo = JwtUtil.getTokenVo(token);
//		if (tokenVo.get("outLogin").toString().equals("1")) {
//			return dubboSysMenuService.getTreeByRoleId(Long.parseLong(tokenVo.get("roleType").toString()));
//		} else {
//			return dubboSysMenuService.getTreeByUserId(Long.parseLong(tokenVo.get("userId").toString()));
//		}

		// mark by linjinyu at 20210312
		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		if (loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.服务之窗.getKey()) == 0) {
			return dubboSysMenuService.getTreeByRoleId(loginSysUser.getRoleType());
		} else {
			return dubboSysMenuService.getTreeByUserId(loginSysUser.getSysUserId());
		}

	}

	public List<ViewRoleMenu> getDataByUser(String token) {

		//String userId = JwtUtil.checkToken(token);

		// change 20191204
//		Map<String, Object> tokenVo = JwtUtil.getTokenVo(token);
//		return dubboSysMenuService.getDataByUser(Long.parseLong(tokenVo.get("roleType").toString()));

		// mark by linjinyu at 20210312
		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);
		return dubboSysMenuService.getDataByUser(loginSysUser.getRoleType());
	}
}
