From 2ed5c1be955f715ecb5c698eab22c2e140f04fde Mon Sep 17 00:00:00 2001 From: chenfei <18955928961@163.com> Date: Tue, 16 Aug 2022 16:22:20 +0800 Subject: [PATCH] =?UTF-8?q?ai=E8=AF=B7=E6=B1=82=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/api/AppController.java | 83 ++++++++++++++----- .../ruoyi/common/utils/RequestParamsUtil.java | 2 + .../ruoyi/system/domain/req/AiPostReq.java | 9 ++ .../system/domain/req/Base64ResizedRmgSm.java | 11 +++ .../service/impl/TRecordServiceImpl.java | 6 +- 5 files changed, 85 insertions(+), 26 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AppController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AppController.java index d868607..004bc2f 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AppController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/AppController.java @@ -1,6 +1,8 @@ package com.ruoyi.web.controller.api; import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.core.controller.BaseController; @@ -12,15 +14,13 @@ import com.ruoyi.common.utils.RequestParamsUtil; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.file.FileUtils; import com.ruoyi.common.utils.http.HttpUtils; -import com.ruoyi.system.domain.THospital; -import com.ruoyi.system.domain.TPatient; -import com.ruoyi.system.domain.TPatientHospitalDoctor; -import com.ruoyi.system.domain.TRecord; +import com.ruoyi.system.domain.*; import com.ruoyi.system.domain.req.*; import com.ruoyi.system.domain.resp.TRecordResp; import com.ruoyi.system.service.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -29,8 +29,7 @@ import sun.misc.BASE64Decoder; import javax.annotation.Resource; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.List; +import java.util.*; /** * @author zhangcl @@ -318,12 +317,14 @@ public class AppController extends BaseController @ApiOperation("ai接口") public AjaxResult aiPost(@PathVariable("recordId") Long recordId, @RequestBody AiPostReq aiPostReq) { + saveImage(aiPostReq,recordId); + ObjectMapper mapper = new ObjectMapper(); String URL = RequestParamsUtil.HTTP + ip + RequestParamsUtil.SEPARATOR + port + url; JSONObject jsonObject = new JSONObject(); String result; try { - result = HttpUtils.sendPost(URL, RequestParamsUtil.buildParams(aiPostReq)); + result = HttpUtils.sendPost(URL, mapper.writeValueAsString(aiPostReq)); } catch (Exception e) { @@ -331,7 +332,8 @@ public class AppController extends BaseController } if (!StringUtils.isEmpty(result)) { - jsonObject = noBase64Image(JSONObject.parseObject(result)); + + jsonObject = removeBase64Image(JSONObject.parseObject(result),recordId); TRecord tRecord = new TRecord(); tRecord.setId(recordId); tRecord.setAiResult(jsonObject.toJSONString()); @@ -341,24 +343,54 @@ public class AppController extends BaseController return AjaxResult.success(jsonObject); } + @GetMapping("/image/{recordId}") + @ApiOperation("获取图片接口") + private AjaxResult getAiImage(@PathVariable("recordId") Long recordId){ + if(recordId == null){ + return AjaxResult.error("未获取到参数"); + } + TImage tImage = new TImage(); + tImage.setThirdId(recordId); + return AjaxResult.success(imageService.selectTImageList(tImage)); + } - public JSONObject noBase64Image(JSONObject jsonObject) + private void saveImage(AiPostReq aiPostReq,Long recordId) { - try - { - jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject("shemian").getJSONObject("image_sz").put("v", null); - jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject("yudian").getJSONObject("image_sz_yudian").put("v", null); - jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject("yuban").getJSONObject("image_sz_yuban").put("v", null); - jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject("liewen").getJSONObject("image_sz_liewen").put("v", null); - jsonObject.getJSONObject("data").getJSONObject("shetai").getJSONObject("image_st").put("v", null); - jsonObject.getJSONObject("data").getJSONObject("shexia").getJSONObject("image_sx").put("v", null); - jsonObject.getJSONObject("data").getJSONObject("shexia").getJSONObject("image_sx_vein").put("v", null); + Base64ResizedRmgSm base64_resized_img_sm = aiPostReq.getBase64_resized_img_sm(); + Base64ResizedRmgSm base64_resized_img_sx = aiPostReq.getBase64_resized_img_sx(); + Map smMap = imageKey(base64_resized_img_sm,"base64_resized_img_sm"); + Map sxMap = imageKey(base64_resized_img_sx, "base64_resized_img_sx"); + smMap.putAll(sxMap); + sxMap.forEach((name,value)->{ + TImage tImage = new TImage(); + tImage.setThirdId(recordId); + tImage.setCreateTime(new Date()); + tImage.setName(name); + String path=GenerateImage(value); + tImage.setPath(path); + imageService.insertTImage(tImage); + }); + } + + public Map imageKey(Base64ResizedRmgSm param,String key){ + Map map = new HashMap<>(); + if(!StringUtils.isEmpty(param.getFront())){ + map.put(key+"-front",param.getFront()); } - catch (Exception e) - { - throw new ServiceException("JSON解析错误:" + jsonObject.toJSONString()); + if(!StringUtils.isEmpty(param.getLeft())){ + map.put(key+"-left",param.getLeft()); } - return jsonObject; + if(!StringUtils.isEmpty(param.getFront())){ + map.put(key+"-right",param.getRight()); + } + if(!CollectionUtils.isEmpty(param.getOther())){ + for (int i = 0; i < param.getOther().size(); i++) + { + map.put(key+"-other"+i,param.getOther().get(i)); + } + } + return map; + } public JSONObject removeBase64Image(JSONObject jsonObject,Long recordId) @@ -381,7 +413,12 @@ public class AppController extends BaseController String imgStr=parents.get(i).getJSONObject(imageKey).getString("v"); String path=GenerateImage(imgStr); parents.get(i).getJSONObject(keys[i]).put("v",null); - + TImage tImage = new TImage(); + tImage.setName(imageKey); + tImage.setPath(path); + tImage.setCreateTime(new Date()); + tImage.setThirdId(recordId); + imageService.insertTImage(tImage); //保存图片到数据库,thirdId(recordId),path,name(imageKey) } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RequestParamsUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RequestParamsUtil.java index 990f12e..4869e2a 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RequestParamsUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RequestParamsUtil.java @@ -92,6 +92,8 @@ public class RequestParamsUtil return params; } + + private static String createLinkStringByGet(Map params) throws UnsupportedEncodingException { List keys = new ArrayList<>(params.keySet()); Collections.sort(keys); diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/AiPostReq.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/AiPostReq.java index f4ff025..28b8b1d 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/AiPostReq.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/AiPostReq.java @@ -29,6 +29,15 @@ public class AiPostReq { this.base64_resized_img_sx = base64_resized_img_sx; } + + @Override + public String toString() + { + return "{" + + "base64_resized_img_sm=" + base64_resized_img_sm + + ", base64_resized_img_sx=" + base64_resized_img_sx + + '}'; + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/Base64ResizedRmgSm.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/Base64ResizedRmgSm.java index e83861a..b62fef1 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/Base64ResizedRmgSm.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/req/Base64ResizedRmgSm.java @@ -48,4 +48,15 @@ public class Base64ResizedRmgSm { this.other = other; } + + @Override + public String toString() + { + return "{" + + "front='" + front + '\'' + + ", left='" + left + '\'' + + ", right='" + right + '\'' + + ", other=" + other + + '}'; + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TRecordServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TRecordServiceImpl.java index 72eb33c..6b1b8f4 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TRecordServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TRecordServiceImpl.java @@ -199,9 +199,9 @@ public class TRecordServiceImpl implements ITRecordService Integer value3 ; try { - value1 = jsonObject.getJSONObject("shezhi").getJSONObject(keys0[0]).getInteger(keys0[1]); - value2 = jsonObject.getJSONObject("shetai").getInteger(key1); - value3 = jsonObject.getJSONObject("shexia").getInteger(key2); + value1 = jsonObject.getJSONObject("data").getJSONObject("shezhi").getJSONObject(keys0[0]).getInteger(keys0[1]); + value2 = jsonObject.getJSONObject("data").getJSONObject("shetai").getInteger(key1); + value3 = jsonObject.getJSONObject("data").getJSONObject("shexia").getInteger(key2); }catch (Exception e){ throw new ServiceException("JSON格式错误"); }