Files
loan-pricing/ruoyi-loan-pricing/src/main/java/com/ruoyi/loanpricing/service/ModelService.java

71 lines
2.3 KiB
Java
Raw Normal View History

2026-01-21 09:32:22 +08:00
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;
2026-01-21 15:58:39 +08:00
import com.ruoyi.loanpricing.domain.dto.ModelInvokeDTO;
import com.ruoyi.loanpricing.domain.entity.LoanPricingWorkflow;
import org.springframework.beans.factory.annotation.Value;
2026-01-21 09:32:22 +08:00
import lombok.extern.slf4j.Slf4j;
2026-01-21 15:58:39 +08:00
import org.springframework.scheduling.annotation.EnableAsync;
2026-01-21 09:32:22 +08:00
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Objects;
/**
* @Author 吴凯程
* @Date 2025/12/11
**/
@Service
@Slf4j
2026-01-21 15:58:39 +08:00
@EnableAsync
2026-01-21 09:32:22 +08:00
public class ModelService {
2026-01-21 15:58:39 +08:00
@Value("${model.url}")
private String modelUrl;
2026-01-21 09:32:22 +08:00
2026-01-21 15:58:39 +08:00
public JSONObject invokeModel(ModelInvokeDTO modelInvokeDTO) {
2026-01-21 09:32:22 +08:00
Map<String, String> requestBody = entityToMap(modelInvokeDTO);
2026-01-21 15:58:39 +08:00
JSONObject response = HttpUtils.doPostFormUrlEncoded(modelUrl, requestBody, null, JSONObject.class);
2026-01-21 09:32:22 +08:00
log.info("------------------->调用模型返回结果:" + JSON.toJSONString(response));
if(Objects.nonNull(response) && response.containsKey("code") && response.getInteger("code") == 10000){
JSONObject mappingOutputFields = response.getJSONObject("data").getJSONObject("mappingOutputFields");
2026-01-21 15:58:39 +08:00
// return JSON.parseObject(mappingOutputFields.toJSONString(), ModelOutputFields.class);
return mappingOutputFields;
2026-01-21 09:32:22 +08:00
}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>>() {});
}
}