|
|
@@ -0,0 +1,110 @@
|
|
|
+package com.qingjian.module.dm.clean.impl.cleaner;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DatePattern;
|
|
|
+import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
+import cn.hutool.core.text.StrPool;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.core.util.ReUtil;
|
|
|
+import cn.hutool.core.util.ReflectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.qingjian.common.cache.GlobalCache;
|
|
|
+import com.qingjian.common.model.call.entity.CallRecord;
|
|
|
+import com.qingjian.common.model.dm.dto.TableRuleDTO;
|
|
|
+import com.qingjian.common.model.dm.entity.CleanErrorLog;
|
|
|
+import com.qingjian.module.dm.clean.AbstractDataCleaner;
|
|
|
+import com.qingjian.module.dm.clean.CleanResult;
|
|
|
+import com.qingjian.module.dm.clean.func.Fun;
|
|
|
+import com.qingjian.module.dm.clean.func.FunProcess;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class CallDataCleaner1 extends AbstractDataCleaner<CallRecord> {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CleanResult<CallRecord> doClean(Map<Integer, String> rowData, Integer lineNo) {
|
|
|
+ CleanResult<CallRecord> cleanResult = new CleanResult<>();
|
|
|
+ CallRecord callRecord = new CallRecord();
|
|
|
+ String callDate = "";
|
|
|
+ String callTime = "";
|
|
|
+ // 遍历表格规则列表
|
|
|
+ for (TableRuleDTO rule : tableRules) {
|
|
|
+ // 获取当前规则对应的文件名索引
|
|
|
+ int fileColIndex = rule.getFileColIndex();
|
|
|
+ // 根据行数据和文件名索引获取对应的内容
|
|
|
+ String content = getContent(rowData, fileColIndex, rule.getColumnRule());
|
|
|
+ content = getRegexContent(rule.getFieldNameEn(), content);
|
|
|
+ // 如果获取的内容为空,则将对应字段名的映射值设为 null 并跳过当前规则
|
|
|
+ if (StrUtil.isEmpty(content) && rule.getRequired() == 1) {
|
|
|
+ String errMsg = StrUtil.format("第{}行,第{}列 [{}] 数据为空", lineNo, fileColIndex, rule.getFileColName());
|
|
|
+ CleanErrorLog cleanErrorLog = buildLog(errMsg);
|
|
|
+ cleanResult.setError(cleanErrorLog);
|
|
|
+ cleanResult.setValid(false);
|
|
|
+ callRecord = null;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (StrUtil.isEmpty(content)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ content = content.trim();
|
|
|
+ //去除特殊字符/空白字符
|
|
|
+ String copyContent = ReUtil.replaceAll(content, GlobalCache.SPECIAL_CHARS_PATTERN, "");
|
|
|
+ // 创建一个函数处理对象,并设置异常处理逻辑,当处理过程中出现异常时记录日志
|
|
|
+ String finalContent = content;
|
|
|
+ FunProcess funProcess = FunProcess.create().onException((e) -> log.error("清洗函数字段处理异常: {}", finalContent, e));
|
|
|
+ // 遍历选中的规则视图映射
|
|
|
+ List<Fun> funList = defaultDateFunMap.getOrDefault(fileColIndex, List.of());
|
|
|
+ funProcess.addAll(funList);
|
|
|
+ // 根据字段类型进行不同的处理
|
|
|
+ // 根据字段类型进行不同的处理
|
|
|
+ switch (rule.getFieldType()) {
|
|
|
+ case CALL_DIRECTION -> {
|
|
|
+ String newKey = rule.getTableId() + StrPool.UNDERLINE + copyContent;
|
|
|
+ copyContent = GlobalCache.DIRECTION_CONF_MAP.getOrDefault(newKey, copyContent);
|
|
|
+ }
|
|
|
+ case PHONE ->
|
|
|
+ // 如果字段类型是电话,去除内容中的中国区号
|
|
|
+ copyContent = ReUtil.replaceAll(copyContent, GlobalCache.CHINA_PREFIX_PATTERN, "");
|
|
|
+ default -> // 默认情况,不做处理
|
|
|
+ {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //判断函数是否可以执行
|
|
|
+ if (funProcess.isExecute()) {
|
|
|
+ // 使用函数处理对象处理内容
|
|
|
+ copyContent = funProcess.process(copyContent);
|
|
|
+ }
|
|
|
+ if (StrUtil.isEmpty(content) && rule.getRequired() == 1) {
|
|
|
+ String errMsg = StrUtil.format("第{}行,第{}列 [{}] 数据清洗后为空,该内容是必填!", lineNo, fileColIndex, rule.getFileColName());
|
|
|
+ if (fileColIndex == -1) {
|
|
|
+ errMsg = StrUtil.format("第{}行,[{}] 数据为空", lineNo, rule.getFileColName());
|
|
|
+ }
|
|
|
+ CleanErrorLog cleanErrorLog = buildLog(errMsg);
|
|
|
+ cleanResult.setError(cleanErrorLog);
|
|
|
+ cleanResult.setValid(false);
|
|
|
+ callRecord = null;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if ("callDate".equals(rule.getFieldNameEn())) {
|
|
|
+ callDate = copyContent;
|
|
|
+ }
|
|
|
+ if ("callTime".equals(rule.getFieldNameEn())) {
|
|
|
+ callTime = copyContent;
|
|
|
+ }
|
|
|
+ ReflectUtil.setFieldValue(callRecord, rule.getFieldNameEn(), copyContent);
|
|
|
+ }
|
|
|
+ if (cleanResult.isValid()) {
|
|
|
+ callRecord.setId(IdUtil.getSnowflakeNextId());
|
|
|
+ callRecord.setFileId(fileId);
|
|
|
+ callRecord.setSheetId(sheetId);
|
|
|
+ String transDateTimeStr = callDate + " " + StrUtil.emptyToDefault(callTime, "00:00:00");
|
|
|
+ LocalDateTime transDateTime = LocalDateTimeUtil.parse(transDateTimeStr.trim(), DatePattern.NORM_DATETIME_FORMATTER);
|
|
|
+ callRecord.setCallDateTime(transDateTime);
|
|
|
+ cleanResult.setData(callRecord);
|
|
|
+ }
|
|
|
+ return cleanResult;
|
|
|
+ }
|
|
|
+}
|