80 lines
2.8 KiB
Java
80 lines
2.8 KiB
Java
|
|
package com.ruoyi.loanpricing.service;
|
|||
|
|
|
|||
|
|
import com.alibaba.fastjson2.JSON;
|
|||
|
|
import com.alibaba.fastjson2.JSONObject;
|
|||
|
|
import com.alibaba.fastjson2.TypeReference;
|
|||
|
|
import com.ruoyi.common.core.domain.entity.SysDictData;
|
|||
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|||
|
|
import com.ruoyi.common.exception.ServiceException;
|
|||
|
|
import com.ruoyi.common.utils.http.HttpUtils;
|
|||
|
|
import com.ruoyi.loanratepricing.domain.RatePricingProperties;
|
|||
|
|
import com.ruoyi.loanratepricing.domain.dto.ModelInvokeDTO;
|
|||
|
|
import com.ruoyi.loanratepricing.domain.entity.ModelOutputFields;
|
|||
|
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
|
import org.springframework.stereotype.Service;
|
|||
|
|
|
|||
|
|
import javax.annotation.Resource;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.Map;
|
|||
|
|
import java.util.Objects;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @Author 吴凯程
|
|||
|
|
* @Date 2025/12/11
|
|||
|
|
**/
|
|||
|
|
@Service
|
|||
|
|
@Slf4j
|
|||
|
|
public class ModelService {
|
|||
|
|
|
|||
|
|
@Resource
|
|||
|
|
private RatePricingProperties ratePricingProperties;
|
|||
|
|
|
|||
|
|
@Resource
|
|||
|
|
private RedisCache redisCache;
|
|||
|
|
|
|||
|
|
public ModelOutputFields invokeModel(ModelInvokeDTO modelInvokeDTO) {
|
|||
|
|
Map<String, String> requestBody = entityToMap(modelInvokeDTO);
|
|||
|
|
JSONObject response = HttpUtils.doPostFormUrlEncoded(ratePricingProperties.getModelUrl(), requestBody, null, JSONObject.class);
|
|||
|
|
log.info("------------------->调用模型返回结果:" + JSON.toJSONString(response));
|
|||
|
|
if(Objects.nonNull(response) && response.containsKey("code") && response.getInteger("code") == 10000){
|
|||
|
|
JSONObject mappingOutputFields = response.getJSONObject("data").getJSONObject("mappingOutputFields");
|
|||
|
|
return JSON.parseObject(mappingOutputFields.toJSONString(), ModelOutputFields.class);
|
|||
|
|
}else{
|
|||
|
|
log.error("------------------->调用模型失败,失败原因为:" + response.getString("message"));
|
|||
|
|
throw new ServiceException("调用模型失败");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 使用FastJSON将实体类转换为Map<String, String>
|
|||
|
|
* @param obj 待转换的实体类对象
|
|||
|
|
* @return 转换后的Map
|
|||
|
|
*/
|
|||
|
|
public static Map<String, String> entityToMap(Object obj) {
|
|||
|
|
if (obj == null) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
// 先转为JSON字符串,再转换为指定类型的Map
|
|||
|
|
String jsonStr = JSON.toJSONString(obj);
|
|||
|
|
return JSON.parseObject(jsonStr, new TypeReference<Map<String, String>>() {});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void replaceIndicatorToVariableName(JSONObject jsonObject) {
|
|||
|
|
List<SysDictData> variableNameList = redisCache.getCacheObject("sys_dict:model_indicator_metric");
|
|||
|
|
variableNameList.forEach(sysDictData -> {
|
|||
|
|
if (jsonObject.containsKey(sysDictData.getDictLabel())) {
|
|||
|
|
jsonObject.put(sysDictData.getDictValue(), jsonObject.get(sysDictData.getDictLabel()));
|
|||
|
|
jsonObject.remove(sysDictData.getDictLabel());
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|