From 3cb964dfd34cc34d72385576bc93fd3879865aef Mon Sep 17 00:00:00 2001 From: gongzhenkun <1658878546@qq.com> Date: Fri, 28 Oct 2022 18:26:45 +0800 Subject: [PATCH] =?UTF-8?q?AI=E6=8E=A5=E5=8F=A3=E8=B0=83=E6=95=B4=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E2=80=9DAI=E5=88=86=E6=9E=90=E4=B8=AD?= =?UTF-8?q?=E2=80=9C=E7=9A=84=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/api/AppController.java | 73 +++++++++++-------- .../controller/common/CommonController.java | 4 + 2 files changed, 46 insertions(+), 31 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 4ec562c..551f145 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 @@ -28,7 +28,6 @@ import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import sun.misc.BASE64Encoder; @@ -328,7 +327,8 @@ public class AppController extends BaseController if (tRecordResps.isEmpty()) { return AjaxResult.success(new ArrayList()); } - List respList = tRecordResps.stream().sorted(Comparator.comparing(TRecordResp::getUploadTime).reversed()).collect(Collectors.toList()); + List respList = tRecordResps.stream() + .sorted(Comparator.comparing(TRecordResp::getUploadTime, Comparator.nullsLast(Date::compareTo)).reversed()).collect(Collectors.toList()); return AjaxResult.success(respList); } @@ -416,11 +416,18 @@ public class AppController extends BaseController @PostMapping("/ai/{recordId}") @ApiOperation("ai接口") @Log(title = "调用ai接口", businessType = BusinessType.OTHER) - @Transactional public AjaxResult aiPost(@PathVariable("recordId") Long recordId, @RequestBody AIPostReqEx aiPostReqEx) { AiPostReq aiPostReq = new AiPostReq(); BeanUtils.copyProperties(aiPostReqEx, aiPostReq); + logger.info("update recordId is '{}' status. system time is '{}'", recordId, System.currentTimeMillis()); + TRecord tRecord = new TRecord(); + tRecord.setId(recordId); + // APP跳转页面,需要立刻修改记录状态 + tRecord.setStatus("2"); + tRecord.setAppDealTime(aiPostReqEx.getAppDealTime()); + logger.info("update record status is '{}', record is '{}'",tRecord.getStatus(), tRecord); + tRecordService.updateTRecord(tRecord); //删除同一次预约调用多次ai接口的图片数据 logger.info("aiPost param is {}", aiPostReq); imageService.deleteTImageByThirdId(recordId); @@ -428,16 +435,11 @@ public class AppController extends BaseController ObjectMapper mapper = new ObjectMapper(); String URL = RequestParamsUtil.HTTP + ip + RequestParamsUtil.SEPARATOR + port + url; JSONObject jsonObject = new JSONObject(); - TRecord tRecord = new TRecord(); - tRecord.setId(recordId); - tRecord.setStatus("1"); - tRecord.setAppDealTime(aiPostReqEx.getAppDealTime()); - tRecordService.updateTRecord(tRecord); String result; Date start; Date end; - try - { + Map data = new HashMap<>(); + try { start = new Date(); if (aiPostReq.getUsr() != null) { aiPostReq.getUsr().setTs_submit_ai(start.getTime()); @@ -445,37 +447,39 @@ public class AppController extends BaseController logger.info("ai param is {}", aiPostReq); result = HttpUtils.sendPost(URL, mapper.writeValueAsString(delNullData(aiPostReq))); end = new Date(); + } catch (Exception e) { + // AI接口异常,需要重置当前记录状态 + updateRecordStatus(recordId, "0"); + logger.error("AI result exception, msg is {}", e.getMessage()); + return AjaxResult.error("AI result exception, please try again!", data); } - catch (Exception e) - { - throw new ServiceException(e.getMessage()); - } - if (!StringUtils.isEmpty(result)) - { - JSONObject data = JSONObject.parseObject(result); - Integer code = data.getInteger("code"); - if(code != 200 && code != 204){ - TRecord tRecord1 = new TRecord(); - tRecord1.setId(recordId); - tRecord1.setStatus("0"); - tRecordService.updateTRecord(tRecord1); - throw new ServiceException(data.getString("msg")); + if (StringUtils.isEmpty(result)) { + // AI接口非正常返回,需要重置当前记录状态 + updateRecordStatus(recordId, "0"); + return AjaxResult.error(200,"AI result is null. please try again!"); + } else { + JSONObject resultJB = JSONObject.parseObject(result); + Integer code = resultJB.getInteger("code"); + if (code != 200 && code != 204) { + // AI接口非正常返回,需要重置当前记录状态 + updateRecordStatus(recordId, "0"); + throw new ServiceException(resultJB.getString("msg")); } if (code == 204) { - TRecord tRecord1 = new TRecord(); - tRecord1.setId(recordId); - tRecord1.setStatus("0"); - tRecordService.updateTRecord(tRecord1); - return AjaxResult.error(code, data.getString("msg")); + // AI接口非正常返回,需要重置当前记录状态 + updateRecordStatus(recordId, "0"); + return AjaxResult.error(code, resultJB.getString("msg")); } - jsonObject = removeBase64Image(data,recordId); + // 正常返回,记录状态置为“1” + tRecord.setStatus("1"); + jsonObject = removeBase64Image(resultJB, recordId); tRecord.setUploadTime(start); tRecord.setAiResultReturnTime(end); tRecord.setAiResult(JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue)); tRecord.setAiResult2(JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue)); + logger.info("update record status is '{}', record is '{}'",tRecord.getStatus(), tRecord); tRecordService.updateTRecord(tRecord); } - Map data = new HashMap<>(); data.put("result",jsonObject); TImage tImage = new TImage(); tImage.setThirdId(recordId); @@ -484,6 +488,13 @@ public class AppController extends BaseController return AjaxResult.success(data); } + private void updateRecordStatus(Long recordId, String status) { + TRecord tRecord1 = new TRecord(); + tRecord1.setId(recordId); + tRecord1.setStatus(status); + tRecordService.updateTRecord(tRecord1); + } + @GetMapping("/image/{recordId}") @ApiOperation("获取图片接口") @Log(title = "获取图片接口", businessType = BusinessType.OTHER) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java index 5a8a1c1..e0c856d 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java @@ -4,6 +4,9 @@ import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + +import com.ruoyi.common.annotation.Log; +import com.ruoyi.common.enums.BusinessType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -88,6 +91,7 @@ public class CommonController ajax.put("fileName", fileName); ajax.put("newFileName", FileUtils.getName(fileName)); ajax.put("originalFilename", file.getOriginalFilename()); + log.info("upload result is {}", ajax); return ajax; } catch (Exception e)