Przeglądaj źródła

feat:报告支持AI

wangzaijun 5 dni temu
rodzic
commit
a80d5d379e

+ 7 - 6
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/AbstractReportParser.java

@@ -40,13 +40,13 @@ public abstract class AbstractReportParser<T extends ReportData> implements Repo
      * 初始化数据的方法
      */
     protected void init() {
-        List<EmailFieldMappingDO> emailFieldMapping = this.fieldMappingMapper.getEmailFieldMapping(ListUtil.of(3, 4));
-        if (CollUtil.isEmpty(emailFieldMapping)) {
+        List<EmailFieldMappingDO> mapping = this.fieldMappingMapper.getEmailFieldMapping(ListUtil.of(3, 4));
+        if (CollUtil.isEmpty(mapping)) {
             throw new ReportParseException(ReportParseStatus.PARSE_RULE_NO_FUND);
         }
-        for (EmailFieldMappingDO mapping : emailFieldMapping) {
-            String code = mapping.getCode();
-            List<String> names = StrUtil.split(mapping.getName(), ",");
+        for (EmailFieldMappingDO temp : mapping) {
+            String code = temp.getCode();
+            List<String> names = StrUtil.split(temp.getName(), ",");
             for (String name : names) {
                 this.fieldMapper.putIfAbsent(name, code);
             }
@@ -67,7 +67,8 @@ public abstract class AbstractReportParser<T extends ReportData> implements Repo
      * @param fundInfo   报告中基金基本信息
      * @return /
      */
-    protected abstract T parseExtInfoAndSetData(ReportBaseInfoDTO reportInfo, ReportFundInfoDTO fundInfo) throws ReportParseException;
+    protected abstract T parseExtInfoAndSetData(ReportBaseInfoDTO reportInfo,
+                                                ReportFundInfoDTO fundInfo) throws ReportParseException;
 
     /**
      * 绑定基金基本信息

+ 43 - 0
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/ai/AIAnnuallyReportParser.java

@@ -0,0 +1,43 @@
+package com.smppw.modaq.application.components.report.parser.ai;
+
+import com.smppw.modaq.application.components.report.parser.ReportParserConstant;
+import com.smppw.modaq.common.exception.ReportParseException;
+import com.smppw.modaq.domain.dto.report.AnnuallyReportData;
+import com.smppw.modaq.domain.dto.report.ReportBaseInfoDTO;
+import com.smppw.modaq.domain.dto.report.ReportFundInfoDTO;
+import com.smppw.modaq.domain.dto.report.ReportParserParams;
+import com.smppw.modaq.domain.mapper.EmailFieldMappingMapper;
+import org.springframework.stereotype.Component;
+
+@Component(ReportParserConstant.PARSER_AI_ANNUALLY)
+public class AIAnnuallyReportParser extends AbstractAIReportParser<AnnuallyReportData> {
+    public AIAnnuallyReportParser(EmailFieldMappingMapper fieldMappingMapper) {
+        super(fieldMappingMapper);
+    }
+
+    @Override
+    protected boolean isSupportAIParse() {
+        return false;
+    }
+
+    @Override
+    protected String prompt() {
+        return "";
+    }
+
+    @Override
+    protected void handleAiResult(String result) throws ReportParseException {
+
+    }
+
+    @Override
+    protected AnnuallyReportData parseExtInfoAndSetData(ReportBaseInfoDTO reportInfo,
+                                                        ReportFundInfoDTO fundInfo) throws ReportParseException {
+        return null;
+    }
+
+    @Override
+    protected ReportFundInfoDTO buildFundInfo(ReportParserParams params) {
+        return null;
+    }
+}

+ 5 - 0
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/ai/AILetterReportParser.java

@@ -22,6 +22,11 @@ public class AILetterReportParser extends AbstractAIReportParser<LetterReportDat
     }
 
     @Override
+    protected boolean isSupportAIParse() {
+        return true;
+    }
+
+    @Override
     protected void handleAiResult(String result) throws ReportParseException {
         try {
             JSONObject jsonResult = JSONUtil.parseObj(result);

+ 43 - 0
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/ai/AIMonthlyReportParser.java

@@ -0,0 +1,43 @@
+package com.smppw.modaq.application.components.report.parser.ai;
+
+import com.smppw.modaq.application.components.report.parser.ReportParserConstant;
+import com.smppw.modaq.common.exception.ReportParseException;
+import com.smppw.modaq.domain.dto.report.MonthlyReportData;
+import com.smppw.modaq.domain.dto.report.ReportBaseInfoDTO;
+import com.smppw.modaq.domain.dto.report.ReportFundInfoDTO;
+import com.smppw.modaq.domain.dto.report.ReportParserParams;
+import com.smppw.modaq.domain.mapper.EmailFieldMappingMapper;
+import org.springframework.stereotype.Component;
+
+@Component(ReportParserConstant.PARSER_AI_MONTHLY)
+public class AIMonthlyReportParser extends AbstractAIReportParser<MonthlyReportData> {
+    public AIMonthlyReportParser(EmailFieldMappingMapper fieldMappingMapper) {
+        super(fieldMappingMapper);
+    }
+
+    @Override
+    protected boolean isSupportAIParse() {
+        return true;
+    }
+
+    @Override
+    protected String prompt() {
+        return "";
+    }
+
+    @Override
+    protected void handleAiResult(String result) throws ReportParseException {
+
+    }
+
+    @Override
+    protected MonthlyReportData parseExtInfoAndSetData(ReportBaseInfoDTO reportInfo,
+                                                       ReportFundInfoDTO fundInfo) throws ReportParseException {
+        return null;
+    }
+
+    @Override
+    protected ReportFundInfoDTO buildFundInfo(ReportParserParams params) {
+        return null;
+    }
+}

+ 43 - 0
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/ai/AIQuarterlyReportParser.java

@@ -0,0 +1,43 @@
+package com.smppw.modaq.application.components.report.parser.ai;
+
+import com.smppw.modaq.application.components.report.parser.ReportParserConstant;
+import com.smppw.modaq.common.exception.ReportParseException;
+import com.smppw.modaq.domain.dto.report.QuarterlyReportData;
+import com.smppw.modaq.domain.dto.report.ReportBaseInfoDTO;
+import com.smppw.modaq.domain.dto.report.ReportFundInfoDTO;
+import com.smppw.modaq.domain.dto.report.ReportParserParams;
+import com.smppw.modaq.domain.mapper.EmailFieldMappingMapper;
+import org.springframework.stereotype.Component;
+
+@Component(ReportParserConstant.PARSER_AI_QUARTERLY)
+public class AIQuarterlyReportParser extends AbstractAIReportParser<QuarterlyReportData> {
+    public AIQuarterlyReportParser(EmailFieldMappingMapper fieldMappingMapper) {
+        super(fieldMappingMapper);
+    }
+
+    @Override
+    protected boolean isSupportAIParse() {
+        return false;
+    }
+
+    @Override
+    protected String prompt() {
+        return "";
+    }
+
+    @Override
+    protected void handleAiResult(String result) throws ReportParseException {
+
+    }
+
+    @Override
+    protected QuarterlyReportData parseExtInfoAndSetData(ReportBaseInfoDTO reportInfo,
+                                                         ReportFundInfoDTO fundInfo) throws ReportParseException {
+        return null;
+    }
+
+    @Override
+    protected ReportFundInfoDTO buildFundInfo(ReportParserParams params) {
+        return null;
+    }
+}

+ 10 - 0
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/ai/AbstractAIReportParser.java

@@ -33,6 +33,9 @@ public abstract class AbstractAIReportParser<T extends ReportData> extends Abstr
 
     @Override
     public T parse(ReportParserParams params) throws ReportParseException {
+        if (!isSupportAIParse()) {
+            throw new ReportParseException(ReportParseStatus.NO_SUPPORT_AI);
+        }
         // 初始化
         this.init();
         String filename = params.getFilename();
@@ -74,6 +77,13 @@ public abstract class AbstractAIReportParser<T extends ReportData> extends Abstr
     }
 
     /**
+     * 报告是否支持ai工具解析
+     *
+     * @return /
+     */
+    protected abstract boolean isSupportAIParse();
+
+    /**
      * 处理ai解析结果,方便构建结构化对象
      *
      * @param result ai解析结果

+ 9 - 36
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/pdf/AbstractPDReportParser.java

@@ -39,15 +39,6 @@ public abstract class AbstractPDReportParser<T extends ReportData> extends Abstr
      */
     protected List<String> textList;
 
-//    @Value("${email.report.ai-parser-url}")
-//    private String aiParserUrl;
-
-//    protected String aiFileId;
-//
-//    protected String aiParserContent;
-//
-//    protected Boolean aiParse = false;
-
     public AbstractPDReportParser(EmailFieldMappingMapper fieldMappingMapper) {
         super(fieldMappingMapper);
     }
@@ -81,37 +72,22 @@ public abstract class AbstractPDReportParser<T extends ReportData> extends Abstr
                 if (i >= 1 && params.getReportType() == ReportType.LETTER) {
                     break;
                 }
-                Integer rows = tableList.stream().map(Table::getRowCount)
-                        .filter(rowCount -> rowCount >= 1).reduce(0, Integer::sum);
-                if (rows >= 1) {
-                    for (Table table : tableList) {
-                        int rowCount = table.getRowCount();
-                        if (rowCount >= 1) {
-                            tables.add(table);
-                        }
+                for (Table table : tableList) {
+                    int rowCount = table.getRowCount();
+                    if (rowCount >= 1) {
+                        tables.add(table);
                     }
-                } else {
-//                    this.aiParse = true;
-//                    Map<String, Object> paramsMap = MapUtil.newHashMap(4);
-//                    paramsMap.put("filepath", filepath);
-//                    paramsMap.put("file_id", params.getAiFileId());
-//                    String body = null;
-//                    try {
-//                        body = HttpUtil.get(this.aiParserUrl, paramsMap);
-//                        JSONObject jsonObject = JSONUtil.parseObj(body);
-//                        this.aiFileId = MapUtil.getStr(jsonObject, "file_id");
-//                        String content = StrUtil.split(jsonObject.getStr("content"), "```").get(1);
-//                        this.aiParserContent = "{" + StrUtil.subAfter(content, "{", false) + "}";
-//                    } catch (Exception e) {
-//                        this.logger.warn("{} ai解析失败,解析结果{},错误原因:{}", filename, body, ExceptionUtil.stacktraceToString(e));
-//                    }
-                    throw new ReportParseException(ReportParseStatus.NOT_A_FIXED_FORMAT, filename);
                 }
                 i++;
             }
             if (tables.isEmpty()) {
                 throw new ReportParseException(ReportParseStatus.REPORT_IS_SCAN, filename);
             }
+            Integer rows = tables.stream().map(Table::getRowCount)
+                    .filter(rowCount -> rowCount >= 1).reduce(0, Integer::sum);
+            if (rows < 1) {
+                throw new ReportParseException(ReportParseStatus.NOT_A_FIXED_FORMAT, filename);
+            }
             this.initTableInfo(tables);
         }
         T reportData = this.buildReportData(params, filename);
@@ -140,9 +116,6 @@ public abstract class AbstractPDReportParser<T extends ReportData> extends Abstr
         super.init();
         // 先初始化为null
         this.textList = null;
-//        this.aiFileId = null;
-//        this.aiParserContent = null;
-//        this.aiParse = false;
     }
 
     /**

+ 0 - 7
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/pdf/PDLetterReportParser.java

@@ -26,13 +26,6 @@ public class PDLetterReportParser extends AbstractPDReportParser<LetterReportDat
     protected void initTableInfo(List<Table> tables) {
         // 每次重新清空map数据
         this.allInfoMap.clear();
-//        if (CollUtil.isEmpty(tables)) {
-//            if (StrUtil.isNotBlank(this.aiParserContent)) {
-//                JSONObject jsonObject = JSONUtil.parseObj(this.aiParserContent);
-//                this.allInfoMap.putAll(flattenMap(jsonObject, ListUtil.list(false)));
-//            }
-//            return;
-//        }
         for (Table table : tables) {
             int rowCount = table.getRowCount();
             int colCount = table.getColCount();

+ 0 - 5
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/pdf/PDMonthlyReportParser.java

@@ -97,9 +97,4 @@ public class PDMonthlyReportParser extends AbstractPDReportParser<MonthlyReportD
         reportData.setNetReport(dtos);
         return reportData;
     }
-
-    @Override
-    protected void cleaningReportData(MonthlyReportData reportData) {
-        // todo 数据清洗
-    }
 }

+ 0 - 5
mo-daq/src/main/java/com/smppw/modaq/application/components/report/parser/pdf/PDQuarterlyReportParser.java

@@ -187,11 +187,6 @@ public class PDQuarterlyReportParser<T extends QuarterlyReportData> extends Abst
         return t;
     }
 
-    @Override
-    protected void cleaningReportData(T reportData) {
-        // todo 数据清洗
-    }
-
     /**
      * 构建基金行业配置解析数据
      *

+ 1 - 0
mo-daq/src/main/java/com/smppw/modaq/common/enums/ReportParseStatus.java

@@ -2,6 +2,7 @@ package com.smppw.modaq.common.enums;
 
 public enum ReportParseStatus implements StatusCode {
     AI_NOT_FOUND(20009, "AI资源找不到"),
+    NO_SUPPORT_AI(20010, "报告[{}]不支持AI解析"),
     PARSE_FAIL(21000, "定期报告或交易确认单解析错误:{}"),
     NOT_A_REPORT(21001, "[{}]不是定期报告或交易确认单"),
     REPORT_IS_SCAN(21002, "报告[{}]为扫描件"),

+ 2 - 1
mo-daq/src/main/java/com/smppw/modaq/domain/dto/report/LetterReportData.java

@@ -29,7 +29,8 @@ public class LetterReportData extends ReportData {
         if (this.investorInfo == null || fundTransaction == null) {
             return false;
         }
-        return !StrUtil.isBlank(this.investorInfo.getInvestorName()) && !StrUtil.isBlank(this.fundTransaction.getFundName());
+        return !StrUtil.isBlank(this.investorInfo.getInvestorName())
+                && !StrUtil.isBlank(this.fundTransaction.getFundName());
     }
 
     @Override

+ 2 - 1
mo-daq/src/main/java/com/smppw/modaq/domain/dto/report/ReportData.java

@@ -54,7 +54,8 @@ public abstract class ReportData implements Serializable {
         if (this.baseInfo == null || this.fundInfo == null) {
             return false;
         }
-        return !StrUtil.isBlank(this.baseInfo.getReportName()) && !StrUtil.isBlank(this.fundInfo.getFundName());
+        return !StrUtil.isBlank(this.baseInfo.getReportName())
+                && !StrUtil.isBlank(this.fundInfo.getFundName());
     }
 
     @Override