浏览代码

feat(job): 实现AI内容审核任务及结果处理功能

- 修改AI服务登录地址为新IP端口配置
- 更新投诉记录ID及商品ID字段类型转换逻辑
- 修复查询投诉类型字典信息时的条件错误
- 新增AI审核任务提交后的状态码及返回结果处理
- 实现AI审核结果轮询查询任务处理器
- 添加授权Token拦截器以支持认证请求
- 完善异常处理及日志记录机制
Lhaibo 1 周之前
父节点
当前提交
ace57488e5
共有 1 个文件被更改,包括 95 次插入7 次删除
  1. 95 7
      alien-job/src/main/java/shop/alien/job/second/AiCheckXxlJob.java

+ 95 - 7
alien-job/src/main/java/shop/alien/job/second/AiCheckXxlJob.java

@@ -3,19 +3,24 @@ package shop.alien.job.second;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.xxl.job.core.context.XxlJobHelper;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.client.ClientHttpRequestInterceptor;
 import org.springframework.stereotype.Component;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.http.*;
+import shop.alien.entity.result.R;
 import shop.alien.entity.second.SecondGoodsRecord;
 import shop.alien.entity.second.SecondRiskControlRecord;
 import shop.alien.entity.store.LifeUser;
 import shop.alien.entity.store.LifeUserViolation;
+import shop.alien.entity.store.StoreClockIn;
 import shop.alien.entity.store.StoreDictionary;
 import shop.alien.mapper.LifeUserMapper;
 import shop.alien.mapper.LifeUserViolationMapper;
@@ -97,7 +102,7 @@ public class AiCheckXxlJob {
             HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
             ResponseEntity<String> postForEntity = null;
             try {
-                postForEntity = restTemplate.postForEntity("http://192.168.2.78:9000/ai/user-auth-core/api/v1/auth/login", requestEntity, String.class);
+                postForEntity = restTemplate.postForEntity("http://192.168.2.250:9000/ai/user-auth-core/api/v1/auth/login", requestEntity, String.class);
             } catch (Exception e) {
                 log.error("类:PostMethod 方法:post", e);
             }
@@ -118,11 +123,11 @@ public class AiCheckXxlJob {
                         Map<String, Object> requestBody = new HashMap<>();
 
                         // 设置投诉记录ID
-                        requestBody.put("complaint_id", violation.getId());
+                        requestBody.put("complaint_id", violation.getId().toString());
 
                         // 查询投诉类型字典信息
                         StoreDictionary storeDictionary = storeDictionaryMapper.selectOne(new LambdaQueryWrapper<StoreDictionary>()
-                                .eq(StoreDictionary::getTypeName, violation.getDictId()).eq(StoreDictionary::getTypeName, violation.getDictType()));
+                                .eq(StoreDictionary::getDictId, violation.getDictId()).eq(StoreDictionary::getTypeName, violation.getDictType()));
                         String complaint_type = "";
                         if (storeDictionary != null) {
                             // 设置投诉类型
@@ -135,17 +140,17 @@ public class AiCheckXxlJob {
                         requestBody.put("reporter_info", lifeUserMapper.selectById(violation.getReportingUserId()));
 
                         // 查询被举报的商品记录
-                        SecondGoodsRecord secondGoodsRecord = secondGoodsRecordMapper.selectById(violation.getBusinessId());
+                        SecondGoodsRecord secondGoodsRecord = secondGoodsRecordMapper.selectById(violation.getGoodsId());
                         if (secondGoodsRecord != null) {
                             // 设置被举报人信息
-                            requestBody.put("reported_user_id", secondGoodsRecord.getUserId());
+                            requestBody.put("reported_user_id", secondGoodsRecord.getUserId().toString());
                             requestBody.put("reported_user_type", "");
                             requestBody.put("reported_info", lifeUserMapper.selectById(secondGoodsRecord.getUserId()));
                             requestBody.put("product_name", secondGoodsRecord.getTitle());
                         }
                         // 设置商品相关信息
                         requestBody.put("product_info", secondGoodsRecord);
-                        requestBody.put("product_id", violation.getBusinessId());
+                        requestBody.put("product_id", violation.getGoodsId().toString());
 
                         // 设置投诉文本内容
                         requestBody.put("complaint_text", violation.getOtherReasonContent());
@@ -161,8 +166,22 @@ public class AiCheckXxlJob {
                         HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, aiHeaders);
                         ResponseEntity<String> response = null;
                         try {
-                            response = restTemplate.postForEntity("http://192.168.2.78:9000/ai/auto-review/api/v1/product_complaint_record/submit", request, String.class);
+                            response = restTemplate.postForEntity("http://192.168.2.250:9000/ai/auto-review/api/v1/product_complaint_record/submit", request, String.class);
                             log.info("AI自动审核结果:{}", response.getBody());
+                            if (response.getStatusCodeValue() != 200) {
+                                throw new RuntimeException("AI门店审核接口调用失败 http状态:" + response.getStatusCode());
+                            }
+                            if (StringUtils.isNotEmpty(response.getBody())) {
+                                com.alibaba.fastjson.JSONObject taskObject = com.alibaba.fastjson.JSONObject.parseObject(response.getBody());
+                                if (taskObject.getInteger("code") == 200) {
+                                    com.alibaba.fastjson.JSONObject data = taskObject.getJSONObject("data");
+                                    if (data != null) {
+                                        String taskId = data.getString("task_id");
+                                        violation.setAiTaskId(taskId);
+                                        lifeUserViolationMapper.updateById(violation);
+                                    }
+                                }
+                            }
                         } catch (Exception e) {
                             log.error("AI自动审核请求异常", e);
                         }
@@ -176,6 +195,75 @@ public class AiCheckXxlJob {
         log.info("AI自动审核任务执行完成");
     }
 
+    @XxlJob("aiCheckJobHandlerResult")
+    public void aiCheckJobHandlerResult() {
+        log.info("登录Ai服务获取token..." + loginUrl);
+        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
+        formData.add("username", userName);
+        formData.add("password", passWord);
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
+        ResponseEntity<String> postForEntity = null;
+        try {
+            log.info("请求Ai服务登录接口===================>");
+            postForEntity = restTemplate.postForEntity(loginUrl, requestEntity, String.class);
+        } catch (Exception e) {
+            log.error("请求AI服务登录接口失败", e);
+        }
+        RestTemplate restTemplateWithAuth = new RestTemplate();
+        if (postForEntity != null && postForEntity.getStatusCodeValue() == 200) {
+            log.info("请求Ai服务登录成功 postForEntity.getBody()\t" + postForEntity.getBody());
+            String responseBody = postForEntity.getBody();
+            JSONObject jsonObject = JSONObject.parseObject(responseBody);
+            if (jsonObject != null) {
+                JSONObject dataJson = jsonObject.getJSONObject("data");
+                String accessToken = dataJson.getString("access_token");
+                List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
+                interceptors.add((request, body, execution) -> {
+                    request.getHeaders().set("Authorization", "Bearer " + accessToken);
+                    return execution.execute(request, body);
+                });
+                restTemplateWithAuth.setInterceptors(interceptors);
+            }
+        }
+        // 查询所有待处理的用户违规记录
+        List<LifeUserViolation> lifeUserViolations = lifeUserViolationMapper.selectList(new LambdaQueryWrapper<LifeUserViolation>().eq(LifeUserViolation::getProcessingStatus, "5"));
+
+        // 遍历每条违规记录,组装AI审核请求数据
+        for (LifeUserViolation violation : lifeUserViolations) {
+            // 针对已提交且未删除的动态轮询查询结果
+            try {
+                ResponseEntity<String> response = null;
+                try {
+                    response = restTemplateWithAuth.getForEntity("http://192.168.2.250:9100//api/v1/product_complaint_record/result/" + violation.getAiTaskId(), String.class);
+                    if (response.getStatusCodeValue() != 200) {
+                        log.error("AI内容审核结果获取接口调用失败 http状态:" + response.getStatusCode());
+                    }
+                    JSONObject responseNode = JSONObject.parseObject(response.getBody());
+                    if (responseNode == null) {
+                        log.error("AI接口调用失败,响应内容为空");
+                    }
+                    Integer code = null;
+                    if (responseNode != null) {
+                        code = responseNode.getInteger("code");
+                        if (code == 200) {
+                            JSONObject dataNode = JSONObject.from(responseNode.get("data"));
+
+                        } else {
+                            log.error("AI接口调用失败,错误码: " + code);
+                        }
+                    }
+                } catch (Exception e) {
+                    log.error("调用AI内容审核结果获取接口失败", e);
+                }
+            }catch (RuntimeException ex){
+                XxlJobHelper.handleFail("动态内容审核任务执行失败:" + ex.getMessage());
+            }
+        }
+    }
+
     /**
      * 二手商品风控记录审核任务
      * <p>