package com.cm.common.plugin.oauth.service.role.impl;

import com.alibaba.fastjson.JSONArray;
import com.cm.common.base.AbstractService;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.exception.AccessTokenException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.IApiConsts;
import com.cm.common.plugin.oauth.service.role.IRoleService;
import com.cm.common.plugin.oauth.token.ClientTokenManager;
import com.cm.common.plugin.utils.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2ClientProperties;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: RoleServiceImpl
 * @Description: 角色管理
 * @Author: WangGeng
 * @Date: 2020/1/9 17:13
 * @Version: 1.0
 **/
@Service
public class RoleServiceImpl extends AbstractService implements IRoleService {

    @Autowired
    private ApiPathProperties apiPathProperties;
    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Override
    public JSONArray listZTreeRole(String roleParentId) {
        Map<String, Object> params = new HashMap<>(1);
        params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
        params.put("id", roleParentId);
        String result = restTemplateUtil.doGetFormNormal(String.format(IApiConsts.LIST_ZTREE_ROLE, apiPathProperties.getUserCenter()), params);
        if (result == null) {
            throw new AccessTokenException("认证失败");
        }
        if (result.isEmpty()) {
            throw new SearchException("获取列表失败");
        }
        return JSONArray.parseArray(result);
    }

}
