com.itheima.auth.sdk.ops.impl.OrgOperationsImpl Maven / Gradle / Ivy
package com.itheima.auth.sdk.ops.impl;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.itheima.auth.sdk.AuthTemplate;
import com.itheima.auth.sdk.common.Result;
import com.itheima.auth.sdk.config.AuthorityConfig;
import com.itheima.auth.sdk.dto.OrgDTO;
import com.itheima.auth.sdk.dto.OrgTreeDTO;
import com.itheima.auth.sdk.ops.OrgOperations;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author libo
* @Description
* @date 2022/4/27 15:55
**/
public class OrgOperationsImpl implements OrgOperations {
private AuthTemplate authTemplate;
private AuthorityConfig authorityConfig;
public OrgOperationsImpl(AuthTemplate authTemplate) {
this.authTemplate = authTemplate;
this.authorityConfig = authTemplate.getAuthorityConfig();
}
@Override
public Result getOrgById(Long id) {
try {
String url = authorityConfig.getUri() + "/api/authority/org/" + id;
String response = this.authTemplate.getFormHttpApiService().doGet(url, new HashMap<>());
JSONObject jsonObject = JSONUtil.parseObj(response);
if (jsonObject.getInt("code") == 0) {
OrgDTO orgDTO = JSONUtil.toBean(jsonObject.getStr("data"), OrgDTO.class);
return Result.success(orgDTO);
}
return Result.fail(jsonObject.getStr("msg"));
}catch (Exception e) {
return Result.fail(e.getMessage());
}
}
@Override
public Result> getOrgByIds(List ids) {
try {
String param = ids.stream().map(String::valueOf).collect(Collectors.joining(","));
String url = authorityConfig.getUri() + "/api/authority/org/listByIds?ids=" + param;
String response = this.authTemplate.getFormHttpApiService().doGet(url, new HashMap<>());
JSONObject jsonObject = JSONUtil.parseObj(response);
if (jsonObject.getInt("code") == 0) {
List orgDTOList = JSONUtil.toList(jsonObject.getStr("data"), OrgDTO.class);
return Result.success(orgDTOList);
}
return Result.fail(jsonObject.getStr("msg"));
}catch (Exception e) {
return Result.fail(e.getMessage());
}
}
@Override
public Result> getOrgTrees() {
try {
String url = authorityConfig.getUri() + "/api/authority/org/tree";
String response = this.authTemplate.getFormHttpApiService().doGet(url, new HashMap<>());
JSONObject jsonObject = JSONUtil.parseObj(response);
if (jsonObject.getInt("code") == 0) {
List tress = JSONUtil.toList(jsonObject.getStr("data"), OrgTreeDTO.class);
return Result.success(tress);
}
return Result.fail(jsonObject.getStr("msg"));
}catch (Exception e) {
return Result.fail(e.getMessage());
}
}
}