1
0

ValuationParseService.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. package com.simuwang.daq.service;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import cn.hutool.core.date.DateTime;
  5. import cn.hutool.core.date.StopWatch;
  6. import cn.hutool.core.exceptions.ExceptionUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import com.simuwang.base.common.conts.HoldingType;
  9. import com.simuwang.base.common.util.ValuationBusinessUtils;
  10. import com.simuwang.base.common.util.ValuationDataUtils;
  11. import com.simuwang.base.common.util.ValuationTableParseUtil;
  12. import com.simuwang.base.mapper.*;
  13. import com.simuwang.base.pojo.dos.*;
  14. import com.simuwang.base.pojo.valuation.*;
  15. import com.smppw.utils.BigDecimalUtils;
  16. import com.smppw.utils.DateUtil;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Qualifier;
  21. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  22. import org.springframework.stereotype.Service;
  23. import java.math.BigDecimal;
  24. import java.util.*;
  25. import java.util.concurrent.*;
  26. import java.util.stream.Collectors;
  27. @Service
  28. public class ValuationParseService {
  29. private static final Logger log = LoggerFactory.getLogger(ValuationParseService.class);
  30. public static final int stepSize = 10000;
  31. private final ValuationMarketCodeMapper valuationMarketCodeMapper;
  32. private final FundPositionDetailMapper fundPositionDetailMapper;
  33. private final ValuationTableMapper valuationTableMapper;
  34. private final FundService fundService;
  35. private final PdfValuationRecordMapper pdfValuationRecordMapper;
  36. private final ThreadPoolTaskExecutor executor;
  37. private final ValuationTableAttributeMapper valuationTableAttributeMapper;
  38. public ValuationParseService(ValuationMarketCodeMapper valuationMarketCodeMapper, FundPositionDetailMapper fundPositionDetailMapper,
  39. ValuationTableMapper valuationTableMapper, FundService fundService,
  40. PdfValuationRecordMapper pdfValuationRecordMapper, @Qualifier("valuationExecutor") ThreadPoolTaskExecutor executor,
  41. ValuationTableAttributeMapper valuationTableAttributeMapper) {
  42. this.valuationMarketCodeMapper = valuationMarketCodeMapper;
  43. this.fundPositionDetailMapper = fundPositionDetailMapper;
  44. this.valuationTableMapper = valuationTableMapper;
  45. this.fundService = fundService;
  46. this.pdfValuationRecordMapper = pdfValuationRecordMapper;
  47. this.executor = executor;
  48. this.valuationTableAttributeMapper = valuationTableAttributeMapper;
  49. }
  50. public List<AssetsValuationResult.Record> parseValuationExcel(List<ValuationNeedParseParam> valuationNeedParseParams) {
  51. if (CollUtil.isEmpty(valuationNeedParseParams)) {
  52. return CollUtil.newArrayList();
  53. }
  54. StopWatch stopWatch = new StopWatch();
  55. stopWatch.start("create valuation executor");
  56. stopWatch.stop();
  57. log.info("create valuation executor cost -> {}", stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
  58. List<AssetsValuationResult.Record> records = CollectionUtil.newArrayList();
  59. List<Future<ValuationResult>> futureList = CollectionUtil.newArrayList();
  60. List<String> marketCodeList = valuationMarketCodeMapper.queryMarketCode();
  61. try {
  62. for (ValuationNeedParseParam valuationNeedParseParam : valuationNeedParseParams) {
  63. ParseValuationInfo parseValuationInfo;
  64. AssetsValuationResult.Record record = new AssetsValuationResult.Record();
  65. PreAssetsValuationInfo<PreAssetsValuationBase> preAssetsValuationInfo = ValuationBusinessUtils.importFile(valuationNeedParseParam);
  66. PreAssetsValuationInfo<PreAssetsValuationBase>.Error error = preAssetsValuationInfo.getError();
  67. if (error != null) {
  68. error = preAssetsValuationInfo.getError();
  69. record.setExcelName(error.getExcelName()).setMsg(error.getMsg()).setRowNum(error.getRowNum()).setColumnNum(error.getCellNum())
  70. .setSuccess(0);
  71. records.add(record);
  72. } else {
  73. AssetsValuationExcelInfo<AssetsValuationInfo> excelInfo = processDataV2(preAssetsValuationInfo);
  74. // 邮件解析 -> 需要根据备案编码和基金名称去查询对应的基金id
  75. if (StringUtils.isEmpty(valuationNeedParseParam.getFundId())) {
  76. String headInfo = excelInfo.getExtInfo().getHeadInfo();
  77. if (StringUtils.isNotEmpty(headInfo)) {
  78. AssetsValuationDetails details = excelInfo.getExtInfo();
  79. parseValuationInfo = parseRegisterNumAndFundName(headInfo);
  80. record.setParseValuationInfo(parseValuationInfo);
  81. log.info("表格:{},从表格中解析到的基金名称和备案编码:{}", valuationNeedParseParam.getOriginFileName(), parseValuationInfo.toString());
  82. List<FundInfoDO> fundInfoDOList = fundService.getFundInfoByNamesAndCode(parseValuationInfo.getFundName(), parseValuationInfo.getRegisterNumber());
  83. log.info("表格:{}, 匹配上的基金:{}", valuationNeedParseParam.getOriginFileName(), fundInfoDOList);
  84. if (CollUtil.isEmpty(fundInfoDOList)) {
  85. record.setExcelName(valuationNeedParseParam.getOriginFileName());
  86. record.setMsg("未匹配基金");
  87. record.setDate(details.getValuationDate());
  88. record.setSuccess(0);
  89. records.add(record);
  90. } else {
  91. for (FundInfoDO fundInfoDO : fundInfoDOList) {
  92. String fundId = fundInfoDO.getFundId();
  93. AssetsValuationResult.Record singleFundRecord = new AssetsValuationResult.Record();
  94. singleFundRecord.setExcelName(valuationNeedParseParam.getOriginFileName());
  95. singleFundRecord.setDate(details.getValuationDate());
  96. singleFundRecord.setSuccess(1);
  97. singleFundRecord.setFundId(fundId);
  98. singleFundRecord.setCumulativeNavWithdrawal(String.valueOf(details.getCumulativeNav()));
  99. singleFundRecord.setNav(String.valueOf(details.getNav()));
  100. singleFundRecord.setParseValuationInfo(parseValuationInfo);
  101. String valuationDate = details.getValuationDate();
  102. List<AssetsValuationInfo> data = excelInfo.getData();
  103. if (CollUtil.isNotEmpty(data)) {
  104. ValuationTableDO tableInfo = new ValuationTableDO();
  105. Integer valuationId = trans2UserValuationDoAndWrite(details, 0, fundId, tableInfo, valuationNeedParseParam);
  106. singleFundRecord.setValuationId(valuationId);
  107. Future<ValuationResult> future = executor.submit(() -> {
  108. ValuationResult valuationResult = new ValuationResult();
  109. valuationResult.setFundId(fundId);
  110. List<CmValuationTableAttribute> valuationTableAttributes = ValuationTableParseUtil.getAttrList(data);
  111. List<FundPositionDetailDO> fundPositionDetailDOList = ValuationTableParseUtil.parseDataNew(valuationTableAttributes, tableInfo, marketCodeList);
  112. if (CollectionUtil.isNotEmpty(fundPositionDetailDOList)) {
  113. fundPositionDetailDOList.stream().filter(p -> "202".equals(p.getSecuritiesCode())).findFirst()
  114. .ifPresent(p -> valuationResult.setCumulativeNavWithdrawal(p.getMarketValue() != null ? String.valueOf(p.getMarketValue()) : null));
  115. }
  116. valuationResult.setValuationTableAttributes(valuationTableAttributes);
  117. valuationResult.setList(fundPositionDetailDOList);
  118. valuationResult.setRecord(singleFundRecord);
  119. valuationResult.setValuationDate(valuationDate);
  120. return valuationResult;
  121. });
  122. futureList.add(future);
  123. }
  124. }
  125. }
  126. }
  127. } else {
  128. String fundId = valuationNeedParseParam.getFundId();
  129. AssetsValuationDetails details = excelInfo.getExtInfo();
  130. record.setNav(String.valueOf(details.getNav()));
  131. record.setCumulativeNavWithdrawal(String.valueOf(details.getCumulativeNav()));
  132. record.setExcelName(valuationNeedParseParam.getOriginFileName());
  133. record.setDate(details.getValuationDate());
  134. record.setSuccess(1);
  135. record.setFundId(fundId);
  136. String valuationDate = details.getValuationDate();
  137. List<AssetsValuationInfo> data = excelInfo.getData();
  138. if (CollectionUtil.isNotEmpty(data)) {
  139. ValuationTableDO tableInfo = new ValuationTableDO();
  140. Integer valuationId = trans2UserValuationDoAndWrite(details, 0, fundId, tableInfo, valuationNeedParseParam);
  141. record.setValuationId(valuationId);
  142. Future<ValuationResult> future = executor.submit(() -> {
  143. long startTime = System.currentTimeMillis();
  144. ValuationResult valuationResult = new ValuationResult();
  145. valuationResult.setFundId(fundId);
  146. valuationResult.setUserId(0);
  147. List<FundPositionDetailDO> fundPositionDetailDOList = CollUtil.newArrayList();
  148. try {
  149. List<CmValuationTableAttribute> valuationTableAttributes = ValuationTableParseUtil.getAttrList(data);
  150. valuationResult.setValuationTableAttributes(valuationTableAttributes);
  151. fundPositionDetailDOList = ValuationTableParseUtil.parseDataNew(valuationTableAttributes, tableInfo, marketCodeList);
  152. } catch (Exception e) {
  153. log.info("Exception: {}", ExceptionUtil.stacktraceToString(e));
  154. }
  155. if (CollectionUtil.isNotEmpty(fundPositionDetailDOList)) {
  156. fundPositionDetailDOList.stream().filter(p -> "202".equals(p.getSecuritiesCode())).findFirst()
  157. .ifPresent(p -> valuationResult.setCumulativeNavWithdrawal(p.getMarketValue() != null ? String.valueOf(p.getMarketValue()) : null));
  158. }
  159. valuationResult.setList(fundPositionDetailDOList);
  160. valuationResult.setRecord(record);
  161. valuationResult.setValuationDate(valuationDate);
  162. long endTime = System.currentTimeMillis();
  163. log.info("end parse -> {},spend time -> {}", record.excelName, (endTime - startTime) + "ms");
  164. return valuationResult;
  165. });
  166. futureList.add(future);
  167. }
  168. }
  169. }
  170. }
  171. for (Future<ValuationResult> future : futureList) {
  172. try {
  173. ValuationResult r = future.get(3600, TimeUnit.SECONDS);
  174. String fundId = r.getFundId();
  175. AssetsValuationResult.Record record = r.getRecord();
  176. record.setCumulativeNavWithdrawal(record.getCumulativeNavWithdrawal());
  177. // 获取资产净值和资产份额
  178. BigDecimal assetNet = r.getList().stream().filter(e -> StrUtil.isNotBlank(e.getSecuritiesCode()) && "112".equals(e.getSecuritiesCode()))
  179. .findFirst().map(FundPositionDetailDO::getMarketValue).orElse(null);
  180. BigDecimal assetShare = r.getList().stream().filter(e -> StrUtil.isNotBlank(e.getSecuritiesCode()) && "107".equals(e.getSecuritiesCode()))
  181. .findFirst().map(FundPositionDetailDO::getMarketValue).orElse(null);
  182. record.setAssetNet(assetNet != null ? String.valueOf(assetNet) : null);
  183. record.setAssetShare(assetShare != null ? String.valueOf(assetShare) : null);
  184. records.add(record);
  185. Integer valuationId = record.getValuationId();
  186. List<CmValuationTableAttribute> valuationTableAttributes = r.getValuationTableAttributes();
  187. // 保存估值表原始信息
  188. saveValuationTableAttribute(valuationId, valuationTableAttributes);
  189. // 保存估值表持仓明细信息
  190. saveFundPositionDetail(r.getList(), fundId, r.getValuationDate());
  191. } catch (InterruptedException | ExecutionException | TimeoutException e) {
  192. log.info("valuation excel upload future exec exception: {}", e.getMessage(), e);
  193. }
  194. }
  195. } catch (Exception exception) {
  196. log.error("parse valuation excel error ->{}", ExceptionUtil.stacktraceToString(exception));
  197. } finally {
  198. stopWatch.start("destruction valuation executor");
  199. stopWatch.stop();
  200. log.info("destruction valuation executor cost -> {}", stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
  201. }
  202. //保存PDF估值表记录
  203. savePdfValuationRecord(valuationNeedParseParams, records);
  204. return records;
  205. }
  206. private void saveValuationTableAttribute(Integer valuationId, List<CmValuationTableAttribute> valuationTableAttributes) {
  207. if (valuationId == null || CollUtil.isEmpty(valuationTableAttributes)) {
  208. return;
  209. }
  210. List<ValuationTableAttributeDO> valuationTableAttributeDOList = buildValuationTableAttributeDO(valuationId, valuationTableAttributes);
  211. valuationTableAttributeMapper.deleteByValuationId(valuationId);
  212. if (CollUtil.isNotEmpty(valuationTableAttributeDOList)) {
  213. valuationTableAttributeMapper.batchInsert(valuationTableAttributeDOList);
  214. }
  215. }
  216. private List<ValuationTableAttributeDO> buildValuationTableAttributeDO(Integer valuationId, List<CmValuationTableAttribute> valuationTableAttributes) {
  217. if (CollUtil.isEmpty(valuationTableAttributes)) {
  218. return CollUtil.newArrayList();
  219. }
  220. return valuationTableAttributes.stream().map(e -> {
  221. ValuationTableAttributeDO tableAttributeDO = new ValuationTableAttributeDO();
  222. tableAttributeDO.setValuationId(valuationId);
  223. tableAttributeDO.setSubjectCode(e.getOriginalSubjectCode());
  224. tableAttributeDO.setSubjectName(e.getSubjectName());
  225. tableAttributeDO.setCurrency(e.getCurrency());
  226. tableAttributeDO.setExchangeRate(e.getExchangeRate());
  227. tableAttributeDO.setSecuritiesAmount(e.getSecuritiesAmount());
  228. tableAttributeDO.setUnitCost(e.getUnitCost());
  229. tableAttributeDO.setOriCurrencyCost(e.getOCurrencyCost());
  230. tableAttributeDO.setNetCost(e.getNetCost());
  231. tableAttributeDO.setNetCostRatio(e.getNetCostRatio());
  232. tableAttributeDO.setMarketPrice(e.getMarketPrice());
  233. tableAttributeDO.setOriCurrencyMarketValue(e.getOCurrencyMarketValue());
  234. tableAttributeDO.setMarketValue(e.getMarketValue());
  235. tableAttributeDO.setMarketValueRatio(e.getMarketValueRatio());
  236. tableAttributeDO.setOriCurrencyIncrement(e.getOCurrencyMarketValue());
  237. tableAttributeDO.setIncrement(e.getIncrement());
  238. tableAttributeDO.setHaltInfo(e.getHaltInfo());
  239. tableAttributeDO.setRightsInterestsInfo(e.getRightsInterestsInfo());
  240. tableAttributeDO.setIsvalid(1);
  241. tableAttributeDO.setCreatorId(0);
  242. tableAttributeDO.setUpdaterId(0);
  243. tableAttributeDO.setCreateTime(new Date());
  244. tableAttributeDO.setUpdateTime(new Date());
  245. return tableAttributeDO;
  246. }).collect(Collectors.toList());
  247. }
  248. /**
  249. * 保存PDF估值表记录
  250. *
  251. * @param valuationNeedParseParams 估值表解析参数
  252. * @param records 估值表解析结果
  253. */
  254. private void savePdfValuationRecord(List<ValuationNeedParseParam> valuationNeedParseParams, List<AssetsValuationResult.Record> records) {
  255. if (CollUtil.isEmpty(records)) {
  256. return;
  257. }
  258. List<AssetsValuationResult.Record> pdfRecordList = records.stream().filter(e -> e.getExcelName().endsWith("pdf")).collect(Collectors.toList());
  259. if (CollUtil.isEmpty(pdfRecordList)) {
  260. return;
  261. }
  262. List<PdfValuationRecordDO> pdfValuationRecordDoList = buildCmPdfValuationRecordDo(valuationNeedParseParams, pdfRecordList);
  263. if (CollUtil.isNotEmpty(pdfValuationRecordDoList)) {
  264. pdfValuationRecordMapper.batchInsert(pdfValuationRecordDoList);
  265. }
  266. }
  267. private List<PdfValuationRecordDO> buildCmPdfValuationRecordDo(List<ValuationNeedParseParam> valuationNeedParseParams,
  268. List<AssetsValuationResult.Record> pdfRecordList) {
  269. if (CollUtil.isEmpty(pdfRecordList)) {
  270. return CollUtil.newArrayList();
  271. }
  272. Map<String, ValuationNeedParseParam> needParseParamMap = valuationNeedParseParams.stream()
  273. .collect(Collectors.toMap(ValuationNeedParseParam::getOriginFileName, v -> v));
  274. return pdfRecordList.stream().map(e -> {
  275. PdfValuationRecordDO pdfValuationRecordDo = new PdfValuationRecordDO();
  276. ValuationNeedParseParam parseParam = needParseParamMap.get(e.getExcelName());
  277. if (parseParam != null) {
  278. pdfValuationRecordDo.setUploadDate(new Date());
  279. pdfValuationRecordDo.setExcelUrl(parseParam.getFile().getAbsolutePath());
  280. pdfValuationRecordDo.setPdfUrl(parseParam.getFileUrl());
  281. pdfValuationRecordDo.setFromEmail(parseParam.getFromEmail() == 1 ? 1 : 0);
  282. }
  283. pdfValuationRecordDo.setFundId(e.getFundId());
  284. pdfValuationRecordDo.setFileName(e.getExcelName());
  285. pdfValuationRecordDo.setIsSuccess(e.getSuccess());
  286. pdfValuationRecordDo.setReason(e.getMsg());
  287. pdfValuationRecordDo.setCreatorId(0);
  288. pdfValuationRecordDo.setUpdaterId(0);
  289. pdfValuationRecordDo.setCreateTime(new Date());
  290. pdfValuationRecordDo.setUpdateTime(new Date());
  291. pdfValuationRecordDo.setIsvalid(1);
  292. return pdfValuationRecordDo;
  293. }).collect(Collectors.toList());
  294. }
  295. /**
  296. * 解析备案编码和备案编码
  297. *
  298. * @param headInfo excel第一行和第二行的内容,中间以'@'符号分隔
  299. * @return 备案编码和备案编码
  300. */
  301. public ParseValuationInfo parseRegisterNumAndFundName(String headInfo) {
  302. ParseValuationInfo info = new ParseValuationInfo();
  303. if (StringUtils.isNotEmpty(headInfo)) {
  304. List<String> contentList = Arrays.stream(headInfo.split("@")).collect(Collectors.toList());
  305. for (String content : contentList) {
  306. String registerNumber = content.length() > 6 ? content.substring(0, 6) : null;
  307. if (StrUtil.isNotBlank(registerNumber) && !ValuationDataUtils.hasChinese(registerNumber, false)) {
  308. info.setRegisterNumber(registerNumber);
  309. }
  310. if (content.contains("估值表") || content.contains("专用表")) {
  311. String originRegisterNumber = info.getRegisterNumber();
  312. String originFundName = info.getFundName();
  313. if (content.contains("___")) {
  314. List<String> collect = Arrays.stream(content.split("___")).collect(Collectors.toList());
  315. if (collect.size() == 4 && content.contains("专用表")) {
  316. info.setRegisterNumber(collect.get(1));
  317. info.setFundName(collect.get(2));
  318. } else {
  319. if (CollUtil.isNotEmpty(collect) && StrUtil.isBlank(originRegisterNumber) && !ValuationDataUtils.hasChinese(registerNumber, false)) {
  320. originRegisterNumber = collect.get(0);
  321. info.setRegisterNumber(originRegisterNumber);
  322. }
  323. originFundName = collect.size() == 2 ? collect.get(0) : originFundName;
  324. originFundName = collect.size() > 2 ? collect.get(1) : originFundName;
  325. info.setFundName(originFundName);
  326. }
  327. } else if (content.contains("__")) {
  328. List<String> collect = Arrays.stream(content.split("__")).collect(Collectors.toList());
  329. if (collect.size() == 4 && content.contains("专用表")) {
  330. info.setRegisterNumber(collect.get(1));
  331. info.setFundName(collect.get(2));
  332. } else {
  333. if (CollUtil.isNotEmpty(collect) && StrUtil.isBlank(originRegisterNumber) && !ValuationDataUtils.hasChinese(registerNumber, false)) {
  334. originRegisterNumber = collect.get(0);
  335. info.setRegisterNumber(originRegisterNumber);
  336. }
  337. originFundName = collect.size() == 2 ? collect.get(0) : originFundName;
  338. originFundName = collect.size() > 2 ? collect.get(1) : originFundName;
  339. info.setFundName(originFundName);
  340. }
  341. } else if (content.contains("_")) {
  342. List<String> collect = Arrays.stream(content.split("_")).collect(Collectors.toList());
  343. if (collect.size() == 4 && content.contains("专用表")) {
  344. info.setRegisterNumber(collect.get(1));
  345. info.setFundName(collect.get(2));
  346. } else {
  347. if (CollUtil.isNotEmpty(collect) && StrUtil.isBlank(originRegisterNumber) && !ValuationDataUtils.hasChinese(registerNumber, false)) {
  348. originRegisterNumber = collect.get(0);
  349. info.setRegisterNumber(originRegisterNumber);
  350. }
  351. originFundName = collect.size() == 2 ? collect.get(0) : originFundName;
  352. originFundName = collect.size() > 2 ? collect.get(1) : originFundName;
  353. info.setFundName(originFundName);
  354. }
  355. }
  356. }
  357. }
  358. }
  359. //兼容格式: xxx估值表@基金名称 -> 只能识别到基金名称
  360. if (StrUtil.isBlank(info.getFundName()) && StrUtil.isBlank(info.getRegisterNumber())) {
  361. List<String> contentList = Arrays.stream(headInfo.split("@")).collect(Collectors.toList());
  362. if (CollUtil.isNotEmpty(contentList) && contentList.size() >= 2) {
  363. String fundName = !contentList.get(1).contains("_") ? contentList.get(1) : null;
  364. info.setFundName(fundName);
  365. }
  366. }
  367. // 兼容格式:编码编码+基金名称+日期+估值报表四级 -> SXT974同增FOF稳增1期私募证券投资基金2024年08月02日估值报表四级
  368. if (StrUtil.isBlank(info.getFundName()) && StrUtil.isNotBlank(info.getRegisterNumber())) {
  369. int index = StrUtil.indexOfIgnoreCase(headInfo, "私募证券投资基金");
  370. // 说明找到了
  371. if (index != -1) {
  372. String shortName = headInfo.substring(0, StrUtil.indexOfIgnoreCase(headInfo, "私募证券投资基金"));
  373. String fundName = shortName.replace(info.getRegisterNumber(), "") + "私募证券投资基金";
  374. info.setFundName(fundName);
  375. }
  376. }
  377. return info;
  378. }
  379. public void saveFundPositionDetail(List<FundPositionDetailDO> fundPositionDetails, String fundId, String valuationDate) {
  380. int subBegin = 0;
  381. int subEnd = stepSize;
  382. int insertNum = 0;
  383. int hasStock = 0;
  384. int hasBond = 0;
  385. int hasFuture = 0;
  386. if (CollectionUtil.isNotEmpty(fundPositionDetails)) {
  387. //插入持仓数据 cm_fund_position_detail(记录数较多,得分批)
  388. // 先删除原先的数据 然后写入数据
  389. fundPositionDetailMapper.deleteUnUsed(fundId, valuationDate);
  390. // 将最终结果写入 cm_fund_position_detail
  391. while (subBegin < fundPositionDetails.size()) {
  392. List<FundPositionDetailDO> segment = fundPositionDetails.subList(subBegin, Math.min(subEnd, fundPositionDetails.size()));
  393. for (FundPositionDetailDO fundPositionDetail : segment) {
  394. //实收信托、实收资本对应的编码为107
  395. if (StrUtil.isNotBlank(fundPositionDetail.getSecuritiesName()) && "实收信托".equals(fundPositionDetail.getSecuritiesName().trim())) {
  396. fundPositionDetail.setSubjectCode("107");
  397. fundPositionDetail.setSecuritiesCode("107");
  398. }
  399. //设置创建者id,在拿取最近修改人时有用
  400. fundPositionDetail.setCreatorId(0);
  401. if (fundPositionDetail.getLevel() != null && fundPositionDetail.getLevel() >= 4) {
  402. Integer secType = fundPositionDetail.getSecType();
  403. Integer subType = fundPositionDetail.getSubType();
  404. if (secType != null && subType != null) {
  405. if (secType == HoldingType.Stock.getId() && subType == HoldingType.Stock.getId()) {
  406. hasStock = 1;
  407. }
  408. // 正逆回购不属于债券
  409. String subjectCode = fundPositionDetail.getSubjectCode();
  410. String[] SALE_CODES = {"2202", "1202"};
  411. if (secType == HoldingType.Bond.getId() && !StrUtil.containsAny(subjectCode, SALE_CODES)) {
  412. hasBond = 1;
  413. }
  414. if (secType == HoldingType.Future.getId() || secType == HoldingType.Option.getId()) {
  415. hasFuture = 1;
  416. }
  417. }
  418. }
  419. }
  420. insertNum += fundPositionDetailMapper.insertMulti(segment);
  421. subBegin = subEnd;
  422. subEnd += stepSize;
  423. }
  424. }
  425. // 更新 cm_user_valuation_table
  426. if (insertNum > 0) {
  427. valuationTableMapper.updateUpdateAnalyzed(fundId, valuationDate, hasStock, hasBond, hasFuture);
  428. }
  429. }
  430. public Integer trans2UserValuationDoAndWrite(AssetsValuationDetails details, Integer userId, String fundId,
  431. ValuationTableDO tableInfo, ValuationNeedParseParam valuationNeedParseParam) {
  432. valuationTableMapper.unValid(fundId, details.getValuationDate());
  433. tableInfo.setFundId(fundId);
  434. tableInfo.setValuationDate(DateUtil.StringToDate(details.getValuationDate()));
  435. tableInfo.setIsAnalyzed(0);
  436. tableInfo.setIsvalid(1);
  437. tableInfo.setCreateTime(DateTime.now());
  438. tableInfo.setUpdateTime(DateTime.now());
  439. tableInfo.setCreatorId(userId);
  440. tableInfo.setUpdaterId(userId);
  441. tableInfo.setTotalMarketValue(BigDecimalUtils.toBigDecimal(details.getTotalMarketValue()));
  442. tableInfo.setNetAssetsValue(BigDecimalUtils.toBigDecimal(details.getNetAssetsValue()));
  443. tableInfo.setIncrement(BigDecimalUtils.toBigDecimal(details.getIncrement()));
  444. tableInfo.setTitle(details.getTitle());
  445. tableInfo.setNav(BigDecimalUtils.toBigDecimal(details.getNav()));
  446. tableInfo.setFileUrl(valuationNeedParseParam.getFileUrl());
  447. tableInfo.setHeadInfo(details.getHeadInfo());
  448. tableInfo.setTailInfo(details.getTailInfo());
  449. tableInfo.setTableType(details.getType());
  450. tableInfo.setFromEmail(valuationNeedParseParam.getFromEmail());
  451. tableInfo.setOriginalFile(valuationNeedParseParam.getOriginFileName());
  452. tableInfo.setConvertedFile(valuationNeedParseParam.getFile().getName());
  453. valuationTableMapper.insert(tableInfo);
  454. return tableInfo.getId();
  455. }
  456. public AssetsValuationExcelInfo<AssetsValuationInfo> processDataV2(PreAssetsValuationInfo<PreAssetsValuationBase> info) {
  457. AssetsValuationExcelInfo<AssetsValuationInfo> avInfo = new AssetsValuationExcelInfo<>();
  458. AssetsValuationDetails extInfo = new AssetsValuationDetails();
  459. extInfo.setTailInfo(list2String(info.getFooter()));
  460. extInfo.setHeadInfo(list2String(info.getHeader().getHeaderList()));
  461. extInfo.setOriginalfile(info.getExcelOriginName());
  462. extInfo.setConvertedFile(info.getExcelNewName());
  463. extInfo.setFileUrl(info.getSavePath());
  464. extInfo.setTitle(info.getHeader().getTitle());
  465. extInfo.setType(info.getType().getId());
  466. extInfo.setNav(info.getHeader().getUnitNetValue());
  467. extInfo.setUserId(ValuationDataUtils.string2Int(info.getUserId()));
  468. extInfo.setValuationDate(new Date(info.getHeader().getValuationDate().getTime()));
  469. List<PreAssetsValuationBase> lBases = info.getData();
  470. ValuationDataUtils.removeUnderLine(lBases);
  471. List<AssetsValuationInfo> list = new ArrayList<>(lBases.size());
  472. //现在业务需要保存基金的总资产 总资产 资产净值 直接从解析结果获取
  473. Double totalMarketValue = null;
  474. // 总资产净值
  475. Double netAssetMarketValue = null;
  476. // 基金估值增值
  477. Double increment = null;
  478. for (int i = 0; i < lBases.size(); i++) {
  479. PreAssetsValuationBase d = lBases.get(i);
  480. if (d.getSubjectCode() == null || d.getSubjectCode().isEmpty()) {
  481. continue;
  482. }
  483. String originalSubjectCode = d.getOriginalSubjectCode();
  484. if (ValuationDataUtils.checkMarketValue(originalSubjectCode)) {
  485. totalMarketValue = ValuationDataUtils.string2Double(d.getMarketValue());
  486. }
  487. if (ValuationDataUtils.checkNetAssetsValue(originalSubjectCode)) {
  488. netAssetMarketValue = ValuationDataUtils.string2Double(d.getMarketValue());
  489. increment = ValuationDataUtils.string2Double(d.getValueAdded());
  490. }
  491. AssetsValuationInfo data = new AssetsValuationInfo();
  492. data.setUserId(ValuationDataUtils.string2Int(info.getUserId()));
  493. data.setSecuritiesAmount(ValuationDataUtils.string2Double(d.getAmount()));
  494. data.setHaltInfo(d.getSuspensionInfo());
  495. data.setIncrement(ValuationDataUtils.string2Double(d.getValueAdded()));
  496. data.setMarketValue(ValuationDataUtils.string2Double(d.getMarketValue()));
  497. data.setMarketValueRatio(ValuationDataUtils.string2Double(d.getRatioOfMarketValueToNetWorth()));
  498. data.setNetCost(ValuationDataUtils.string2Double(d.getCost()));
  499. data.setNetCostRatio(ValuationDataUtils.string2Double(d.getRatioOfCostToNetWorth()));
  500. data.setSubjectCode(d.getSubjectCode());
  501. data.setSecuritiesCode((d.getSecurtiesCode()));
  502. // 对于期权,如果以DR、XR开头,则剔除这两个字母
  503. if (data.getSubjectName() != null && (data.getSubjectName().startsWith("DR") || data.getSubjectName().startsWith("XR"))) {
  504. String subjectName = data.getSubjectName().replace("DR", "");
  505. data.setSubjectName(subjectName.replace("XR", ""));
  506. } else {
  507. data.setSubjectName(d.getSubjectName());
  508. }
  509. data.setUnitCost(ValuationDataUtils.string2Double(d.getUnitCost()));
  510. //寻找累计单位净值
  511. if (null == extInfo.getCumulativeNav()) {
  512. if (StringUtils.isNotEmpty(d.getSubjectCode()) && d.getSubjectCode().contains("累计单位净值")) {
  513. if (StringUtils.isNotEmpty(d.getSubjectName()) && ValuationDataUtils.isNumber(d.getSubjectName())) {
  514. extInfo.setCumulativeNav(Double.valueOf(d.getSubjectName()));
  515. }
  516. }
  517. }
  518. switch (d.getType()) {
  519. case GeneralTemplate:
  520. PreAssetsValuationGeneralTemplate general = (PreAssetsValuationGeneralTemplate) d;
  521. data.setOriginalSubjectCode(general.getOriginalSubjectCode());
  522. data.setMarketPrice(ValuationDataUtils.string2Double(general.getMarketPrice()));
  523. data.setCurrency(general.getCurrency());
  524. data.setExchangeRate(general.getExchangeRate());
  525. data.setOriCurrencyCost(ValuationDataUtils.string2Double(general.getOriCurrencyCost()));
  526. data.setOriCurrencyMarketValue(ValuationDataUtils.string2Double(general.getOriCurrencyMarketValue()));
  527. data.setOriCurrencyValueAdded(ValuationDataUtils.string2Double(general.getOriCurrencyValueAdded()));
  528. data.setSubjectLevel(general.getSubjectLevel());
  529. data.setIssuer(general.getIssuer());
  530. data.setVProjectCode(general.getvProjectCode());
  531. data.setVProjectName(general.getvProjectName());
  532. data.setCostRatioAsset(general.getCostRatioAsset());
  533. data.setMvRatioAsset(general.getMvRatioAsset());
  534. data.setSpotPrice(general.getSpotPrice());
  535. data.setRemark(general.getRemark());
  536. data.setRightsInterestsInfo(general.getRightAndInterestInfo());
  537. data.setAssumingCost(general.getAssumingCost());
  538. data.setHoldingState(general.getHoldingState());
  539. if (data.getSubjectName() != null && ValuationDataUtils.isValid(data.getSubjectCode())) {
  540. data.setSubjectCode(data.getSubjectCode().replace(".", ""));
  541. if (ValuationDataUtils.hasChinese(data.getSubjectCode(), false)) {
  542. String code = data.getSubjectCode().replace(data.getSubjectName(), "");
  543. code = ValuationDataUtils.getSubjectCode(code);
  544. if (code.length() >= 4) {
  545. data.setSubjectCode(code);
  546. } else {
  547. data.setSecType(100);
  548. }
  549. }
  550. }
  551. break;
  552. default:
  553. break;
  554. }
  555. list.add(data);
  556. }
  557. extInfo.setTotalMarketValue(totalMarketValue);
  558. extInfo.setNetAssetsValue(netAssetMarketValue);
  559. extInfo.setIncrement(increment);
  560. avInfo.setExtInfo(extInfo);
  561. avInfo.setData(list);
  562. return avInfo;
  563. }
  564. private static String list2String(List<String> list) {
  565. if (CollUtil.isEmpty(list)) {
  566. return null;
  567. }
  568. return String.join("@", list);
  569. }
  570. }