123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package ieven.server.webapp.domain.alipay;
- import com.mongodb.client.MongoCursor;
- import ieven.server.webapp.domain.ModelIdInput;
- import ieven.server.webapp.domain.data.DataMap;
- import ieven.server.webapp.domain.data.Fields;
- import ieven.server.webapp.domain.data.FieldsService;
- import ieven.server.webapp.domain.file.FileStatus;
- import ieven.server.webapp.domain.file.LogicalFile;
- import ieven.server.webapp.infrastructure.wrapper.Mapped;
- import ieven.server.webapp.util.DataUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.collections4.CollectionUtils;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.bson.Document;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.data.mongodb.core.MongoTemplate;
- import org.springframework.data.mongodb.core.query.Criteria;
- import org.springframework.data.mongodb.core.query.Query;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- import java.util.*;
- @Slf4j
- @Service
- public class AlipayService {
- @Autowired
- private MongoTemplate mongoTemplate;
- @Autowired
- private FieldsService fieldsService;
- @Autowired
- @Lazy
- private AlipayService alipayService;
- private final static String FNAME_PERSONINFO = "提取的人员信息表";
- private final static String FIELDNAME_BANKCARD = "绑定银行卡";
- private final static String EXTRACT_BANKCARDTYPE = "提取银行卡类型";
- private final static String EXTRACT_BANKCARDCODE = "提取银行卡开户行";
- private final static String EXTRACT_BANKCARD = "提取银行卡号";
- private final static List<String> PERSONINFO_HEADERNAMES = Arrays.asList("用户ID", "登录邮箱", "登录手机", "账户名称", "证件类型", "证件号", "绑定手机", "绑定银行卡", "提取银行卡类型", "提取银行卡开户行", "提取银行卡号");
- public Mapped testCursor() {
- Query query = new Query(Criteria.where("fileId").is("618ce033a14aa6502ed010ba"));
- MongoCursor<Document> cursor = mongoTemplate.getCollection("data")
- .find(query.getQueryObject())
- .batchSize(1000)
- .noCursorTimeout(true)
- .cursor();
- Document doc;
- while (cursor.hasNext()) {
- //写法1(建议)
- doc = cursor.next();
- log.info(doc.getString("f4"));
- }
- return null;
- }
- public Mapped extractPersonInfo(ModelIdInput modelIdInput) {
- String modelId = modelIdInput.getModelId();
- //插入文件
- LogicalFile logicalFile = createNewLogicalFile(modelId, FNAME_PERSONINFO);
- String fileId = logicalFile.getId();
- //创建并存储表头
- Fields fields = fieldsService.createHeaderAndSave(PERSONINFO_HEADERNAMES, fileId);
- //根据模型id查询所有的文件
- String key1 = "fieldsReverse." + FIELDNAME_BANKCARD;
- Query queryExist = new Query(Criteria.where(key1).exists(true).and(""));
- List<Fields> fieldsList = mongoTemplate.find(queryExist, Fields.class);
- for (Fields fields1 : fieldsList) {
- //再次过滤出是原始非修改过的文件的
- Query queryIsGenerated = new Query(Criteria.where("id").is(fields1.getFileId()).and("generated").is(Boolean.FALSE));
- LogicalFile checked = mongoTemplate.findOne(queryIsGenerated, LogicalFile.class);
- if (checked != null && checked.getId() != null && checked.getId().equals(fields1.getFileId())) {
- alipayService.extractPersonInfoByFileId(fields1, fields);
- }
- }
- Mapped mapped = Mapped.OK();
- mapped.put("result", logicalFile);
- return mapped;
- }
- //遍历文件里面的所有内容
- @Async
- public void extractPersonInfoByFileId(Fields input, Fields output) {
- LinkedHashMap<String, String> inputReversed = input.getFieldsReverse();
- LinkedHashMap<String, String> outputReversed = output.getFieldsReverse();
- String inputFileId = input.getFileId();
- String outputFileId = output.getFileId();
- String in1 = inputReversed.get("绑定银行卡");
- String out1 = inputReversed.get("绑定银行卡");
- String out2 = outputReversed.get("提取银行卡类型");
- String out3 = outputReversed.get("提取银行卡开户行");
- String out4 = outputReversed.get("提取银行卡号");
- Query query = new Query(Criteria.where("fileId").is(inputFileId));
- query.fields().include();
- MongoCursor<Document> cursor = mongoTemplate.getCollection("data")
- .find(query.getQueryObject())
- .batchSize(1000)
- .noCursorTimeout(true)
- .cursor();
- Document doc;
- List<DataMap> needToSave = new ArrayList<>(1000);
- while (cursor.hasNext()) {
- //写法1(建议)
- doc = cursor.next();
- DataMap newDoc = new DataMap();
- newDoc.put("fileId", outputFileId);
- //其他字段
- for (String headerName : PERSONINFO_HEADERNAMES) {
- String inputAlias = inputReversed.get(headerName);
- if (StringUtils.isNotBlank(inputAlias)) {
- String outputAlias = outputReversed.get(headerName);
- if (StringUtils.isNotBlank(outputAlias)) {
- newDoc.put(outputAlias, doc.get(inputAlias));
- }
- }
- }
- //绑定银行卡字段
- String value1 = doc.getString(in1);
- String[] splited1 = value1.split(";");
- for (String s1 : splited1) {
- if (StringUtils.isNotBlank(s1)) {
- String[] splited2 = s1.split(":");
- if (splited2.length == 3) {
- newDoc.put(out2, splited2[0]);
- newDoc.put(out3, splited2[1]);
- newDoc.put(out4, splited2[2]);
- needToSave.add(newDoc);
- }
- }
- }
- if (needToSave.size() >= 1000) {
- saveLines(new ArrayList<>(needToSave));
- needToSave.clear();
- }
- }
- if (CollectionUtils.isNotEmpty(needToSave)) {
- // saveLines(new ArrayList<>(needToSave));
- saveLines(needToSave);
- }
- }
- /**
- * 创建新的逻辑表,只包含表头
- */
- public LogicalFile createNewLogicalFile(String modelId, String filename) {
- LogicalFile logicalFile = new LogicalFile();
- logicalFile.setFilename(filename);
- logicalFile.setStatus(FileStatus.STATUS_LOADING);
- String current = DataUtils.currentDate();
- logicalFile.setUploadDate(current);
- logicalFile.setModelId(modelId);
- logicalFile.setGenerated(Boolean.TRUE);
- return mongoTemplate.insert(logicalFile);
- }
- @Async
- public void saveLines(List<DataMap> documentList) {
- mongoTemplate.insert(documentList,DataMap.class);
- }
- }
|