Commit 6cea78a6 authored by linruibiao's avatar linruibiao

接口编写

parent 87ed373b
......@@ -23,6 +23,49 @@ public final class RestTemplateUtils {
private static final RestTemplate REST_TEMPLATE = new RestTemplate();
public static <TResult>TResult get(String url, Map<String, String> headerMap, final Type resultType, Map<String, ?> uriVariables) {
try {
ResponseEntity<String> response = get(url, headerMap, String.class, uriVariables);
if(response.getStatusCodeValue()==200){
if(resultType.equals(String.class)) {
return (TResult)response.getBody();
}
return SlmConstants.gson.fromJson(response.getBody(), resultType);
}
}
catch (Exception e) {
e.printStackTrace();
throw new SysException("出错啦,请联系管理员",e.toString());
}
return null;
}
public static <TResult>TResult post(String url, Map<String, String> headerMap,Object requestBody, final Type resultType, Map<String, ?> uriVariables) {
try {
ResponseEntity<String> response = post(url, headerMap, requestBody,String.class, uriVariables);
if(response.getStatusCodeValue()==200){
if(resultType.equals(String.class)) {
return (TResult)response.getBody();
}
return SlmConstants.gson.fromJson(response.getBody(), resultType);
}
}
catch (Exception e) {
e.printStackTrace();
throw new SysException("出错啦,请联系管理员",e.toString());
}
return null;
}
//#region ----------------------------------GET-------------------------------------------------------
/**
......@@ -76,31 +119,6 @@ public final class RestTemplateUtils {
}
public static <TResult>TResult get(String url, Map<String, String> headerMap, final Type resultType, Map<String, ?> uriVariables) {
try {
ResponseEntity<String> response = get(url, headerMap, String.class, uriVariables);
if(response.getStatusCodeValue()==200){
if(resultType.equals(String.class)) {
return (TResult)response.getBody();
}
return SlmConstants.gson.fromJson(response.getBody(), resultType);
}
}
catch (Exception e) {
e.printStackTrace();
throw new SysException("出错啦,请联系管理员",e.toString());
}
return null;
}
/**
* 带请求头的GET请求调用方式
*
......@@ -212,6 +230,9 @@ public final class RestTemplateUtils {
return post(url, httpHeaders, requestBody, responseType, uriVariables);
}
/**
* 带请求头的POST请求调用方式
*
......
......@@ -60,6 +60,9 @@ public class BaseRestController {
@Value("${dc_client.interfaces.deptsRoles}")
protected String deptsRoles;
@Value("${dc_client.interfaces.businessFiledData}")
protected String businessFiledData;
......@@ -90,7 +93,7 @@ public class BaseRestController {
Map<String, String> map = (Map<String, String>) webRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
// Map<String, Object> map = (Map<String, Object>) webRequest.getAttribute(View.PATH_VARIABLES, RequestAttributes.SCOPE_REQUEST);
Map<String,String> heads = Maps.newHashMap();
heads.put("token",token);
heads.put("X-Token",token);
Map<String,String> uriVariables =Maps.newHashMap();
uriVariables.put("appId",map.get("appId")+"");
String getUrl =dcClientHost+"/{appId}/users/user-info";
......
package com.syc.slm.slmbi.controller;
import com.syc.slm.common.core.util.R;
import com.syc.slm.slmbi.dto.BusinessDTO;
import com.syc.slm.slmbi.service.BusinessConfigService;
import com.syc.slm.slmbi.vo.BusinessConfigVo;
import io.swagger.annotations.Api;
......@@ -8,12 +9,10 @@ import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
* slm-bi
......@@ -46,4 +45,11 @@ public class BusinessConfigController extends BaseRestController{
public R<BusinessConfigVo> selectOneConfig(@PathVariable String appId,@PathVariable String id) {
return call(()->(businessConfigService.selectOneConfig(id)));
}
@PostMapping("/filed")
@ApiOperation("根据配置字段获取字段数据")
@ApiImplicitParam(name = "appId", value = "租户id", required = true,dataTypeClass = String.class)
public R<List<Map<String, Object>>> getBusinessData(@PathVariable String appId,@RequestBody BusinessDTO dto) {
return call(()->(businessConfigService.getBusinessData(dto,businessFiledData,currentUser)));
}
}
......@@ -17,8 +17,10 @@ import java.util.List;
public class BusinessDTO {
@ApiModelProperty(value = "查询表名", required = true)
private String table;
@ApiModelProperty(value = "查询配置的字段", required = true)
private String filed;
@ApiModelProperty(value = "纵轴变量", required = true)
private String y;
@ApiModelProperty(value = "横轴纬度", required = true)
private String x;
@ApiModelProperty(value = "查询配置的时间区间(如果为空不要调用后台)", required = true)
List<BusinessConditionDTO> where;
@ApiModelProperty(value = "统计类型(平均值 avg ,合计值 sum, 最大值 max ,最小值 min,中位数 middle ,原值 original)", required = true)
......
package com.syc.slm.slmbi.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.syc.slm.slmbi.dto.BusinessDTO;
import com.syc.slm.slmbi.entity.BusinessConfig;
import com.syc.slm.slmbi.entity.CurrentUser;
import com.syc.slm.slmbi.vo.BusinessConfigVo;
import java.util.List;
import java.util.Map;
/**
* slm-bi
......@@ -26,4 +29,12 @@ public interface BusinessConfigService extends IService<BusinessConfig> {
*/
BusinessConfigVo selectOneConfig(String id);
/**
* 字段数据
* @param dto
* @param uri
* @param user
* @return
*/
List<Map<String,Object>> getBusinessData(BusinessDTO dto,String uri, CurrentUser user);
}
......@@ -6,11 +6,16 @@ import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.reflect.TypeToken;
import com.syc.slm.common.core.constant.SlmConstants;
import com.syc.slm.common.core.util.R;
import com.syc.slm.common.core.util.RestTemplateUtils;
import com.syc.slm.slmbi.dao.BusinessConfigMapper;
import com.syc.slm.slmbi.dto.BusinessDTO;
import com.syc.slm.slmbi.entity.BaseEntity;
import com.syc.slm.slmbi.entity.BusinessConfig;
import com.syc.slm.slmbi.entity.CurrentUser;
import com.syc.slm.slmbi.exception.SysException;
import com.syc.slm.slmbi.service.BusinessConfigService;
import com.syc.slm.slmbi.vo.BusinessConfigVo;
......@@ -20,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
/**
* slm-bi
......@@ -72,4 +78,32 @@ public class BusinessConfigServiceImpl extends ServiceImpl<BusinessConfigMapper,
}
return ObjectUtils.isNotEmpty(co)?SlmConstants.gson.fromJson(SlmConstants.gson.toJson(co), BusinessConfigVo.class):null;
}
@Override
@Transactional(readOnly = true)
public List<Map<String, Object>> getBusinessData(BusinessDTO dto,String uri, CurrentUser user) {
if(StringUtils.isBlank(dto.getTable())){
throw new SysException("请传入对应的表名");
}
if(StringUtils.isBlank(dto.getStatisticsType())){
throw new SysException("统计类型不能为空");
}
if(StringUtils.isBlank(dto.getX())){
throw new SysException("x轴维度不能为空");
}
if(StringUtils.isBlank(dto.getY())){
throw new SysException("y轴配置字段不能为空");
}
if(CollectionUtils.isEmpty(dto.getWhere())){
throw new SysException("where 条件不能为空");
}
if(dto.getWhere().stream().anyMatch(x -> StringUtils.isBlank(x.getKey()))
|| dto.getWhere().stream().anyMatch(x -> StringUtils.isBlank(x.getValue())) || dto.getWhere().stream().anyMatch(x -> StringUtils.isBlank(x.getQueryType()))) {
throw new SysException("where 条件不能key value queryType 都不能为空");
}
Map<String, String> heads = Maps.newHashMap();
heads.put("X-Token", user.getToken());
R<List<Map<String, Object>>> result = RestTemplateUtils.post(uri, heads,dto, new TypeToken<R<List<Map<String, Object>>>>() {}.getType(), null);
return result.detach();
}
}
......@@ -29,7 +29,7 @@ public class DeptServiceImpl implements DeptService {
log.info("部门查询uri:---------------->"+uri);
log.info("部门查询token:---------------->"+token);
Map<String,String> heads = Maps.newHashMap();
heads.put("token",token);
heads.put("X-Token",token);
Map<String,String> uriVariables =Maps.newHashMap();
uriVariables.put("deptName",deptName);
uriVariables.put("appId",appId);
......
......@@ -416,7 +416,7 @@ public class InstrumentPanelTreeServiceImpl extends ServiceImpl<InstrumentPanelT
private R<Map<String,String>> setDeptAndRole(CurrentUser user,String uri){
Map<String,String> heads = Maps.newHashMap();
heads.put("token",user.getToken());
heads.put("X-Token",user.getToken());
Map<String,String> uriVariables =Maps.newHashMap();
uriVariables.put("appId",user.getAppId());
return RestTemplateUtils.get(uri, heads, new TypeToken<R<Map<String,String>>>() {}.getType(), uriVariables);
......
......@@ -28,7 +28,7 @@ public class PositionServiceImpl implements PositionService {
@Override
public List<PositionVo> selectPositionList(String uri ,String token,String appId,String name) {
Map<String,String> heads = Maps.newHashMap();
heads.put("token",token);
heads.put("X-Token",token);
Map<String,String> uriVariables =Maps.newHashMap();
uriVariables.put("positionName",name);
uriVariables.put("appId",appId);
......
......@@ -27,7 +27,7 @@ public class UserServiceImpl implements UserService {
@Override
public List<UserTreeVo> selectUserList(String appId,String uri,String token,String userName) {
Map<String,String> heads = Maps.newHashMap();
heads.put("token",token);
heads.put("X-Token",token);
Map<String,String> uriVariables =Maps.newHashMap();
uriVariables.put("userName",userName);
uriVariables.put("appId",appId);
......
......@@ -48,7 +48,7 @@ public class VariableServiceImpl implements VariableService {
p.put("current",variableDTO.getCurrent()+"");
p.put("size",variableDTO.getSize()+"");
Map<String,String> heads = Maps.newHashMap();
heads.put("token",token);
heads.put("X-Token",token);
R<Map<String, Object>> result = RestTemplateUtils.get(uri, heads, new TypeToken<R<Map<String, Object>>>() {
}.getType(), p);
......@@ -99,7 +99,7 @@ public class VariableServiceImpl implements VariableService {
p.put("page_num", queryPageDTO.getCurrent() + "");
p.put("page_size", queryPageDTO.getSize() + "");
Map<String, String> heads = Maps.newHashMap();
heads.put("token", token);
heads.put("X-Token", token);
R<Map<String, Object>> result = RestTemplateUtils.get(uri, heads, new TypeToken<R<Map<String, Object>>>() {
}.getType(), p);
Map<String, Object> detach = result.detach();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment