FundNavAssetServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package com.simuwang.manage.service.impl;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.read.listener.PageReadListener;
  4. import com.alibaba.excel.support.ExcelTypeEnum;
  5. import com.simuwang.base.common.conts.ExcelConst;
  6. import com.simuwang.base.common.enums.DistributeType;
  7. import com.simuwang.base.common.support.MybatisPage;
  8. import com.simuwang.base.common.util.DateUtils;
  9. import com.simuwang.base.common.util.StringUtil;
  10. import com.simuwang.base.mapper.AssetMapper;
  11. import com.simuwang.base.mapper.FundInfoMapper;
  12. import com.simuwang.base.mapper.FundNavAssetMapper;
  13. import com.simuwang.base.mapper.NavMapper;
  14. import com.simuwang.base.pojo.dos.AssetDO;
  15. import com.simuwang.base.pojo.dos.FundNavAssetDO;
  16. import com.simuwang.base.pojo.dos.NavDO;
  17. import com.simuwang.base.pojo.dto.DistributionExcelData;
  18. import com.simuwang.base.pojo.dto.NavAssetExcelData;
  19. import com.simuwang.base.pojo.dto.query.FundNavAssetPageQuery;
  20. import com.simuwang.base.pojo.vo.*;
  21. import com.simuwang.manage.service.FundNavAssetService;
  22. import com.simuwang.shiro.utils.UserUtils;
  23. import com.smppw.common.pojo.ResultVo;
  24. import com.smppw.common.pojo.enums.status.ResultCode;
  25. import org.apache.commons.io.FileUtils;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.beans.factory.annotation.Value;
  30. import org.springframework.stereotype.Service;
  31. import org.springframework.web.multipart.MultipartFile;
  32. import java.io.File;
  33. import java.io.InputStream;
  34. import java.math.BigDecimal;
  35. import java.util.ArrayList;
  36. import java.util.HashMap;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.stream.Collectors;
  40. /**
  41. * FileName: FundNavAssetServiceImpl
  42. * Author: chenjianhua
  43. * Date: 2024/9/15 16:58
  44. * Description: ${DESCRIPTION}
  45. */
  46. @Service
  47. public class FundNavAssetServiceImpl implements FundNavAssetService {
  48. @Autowired
  49. private FundNavAssetMapper fundNavAssetMapper;
  50. @Autowired
  51. private AssetMapper assetMapper;
  52. @Autowired
  53. private NavMapper navMapper;
  54. @Autowired
  55. private FundInfoMapper fundInfoMapper;
  56. @Value("${email.file.path}")
  57. private String path;
  58. private static final Logger logger = LoggerFactory.getLogger(FundNavAssetServiceImpl.class);
  59. @Override
  60. public MybatisPage<FundNavAssetVO> searchNavAssetList(FundNavAssetPageQuery fundNavAssetPageQuery) {
  61. List<FundNavAssetDO> fundNavAssetDOList = fundNavAssetMapper.searchNavAssetList(fundNavAssetPageQuery);
  62. List<FundNavAssetVO> fundNavAssetVOList = fundNavAssetDOList.stream().map(FundNavAssetDO::toVo).collect(Collectors.toList());
  63. long total = fundNavAssetMapper.countNavAssetList(fundNavAssetPageQuery);
  64. return MybatisPage.of(total,fundNavAssetVOList);
  65. }
  66. @Override
  67. public void saveFundAsset(FundAssetVO fundAssetVO) {
  68. List<AssetDO> assetDOList = new ArrayList<>();
  69. AssetDO assetDO = new AssetDO();
  70. assetDO.setAssetNet(fundAssetVO.getAssetNet());
  71. assetDO.setAssetShare(fundAssetVO.getAssetShare());
  72. assetDO.setPriceDate(DateUtils.parse(fundAssetVO.getPriceDate(),DateUtils.YYYY_MM_DD));
  73. assetDO.setFundId(fundAssetVO.getFundId());
  74. assetDO.setUpdateTime(DateUtils.getNowDate());
  75. assetDO.setCreatorId(UserUtils.getLoginUser().getUserId());
  76. assetDO.setUpdaterId(UserUtils.getLoginUser().getUserId());
  77. assetDO.setIsvalid(1);
  78. AssetDO oldAssetDO = assetMapper.queryFundAsset(assetDO);
  79. if(StringUtil.isNull(oldAssetDO)){
  80. assetDO.setCreateTime(DateUtils.getNowDate());
  81. assetDOList.add(assetDO);
  82. assetMapper.batchInsert(assetDOList);
  83. }else{
  84. oldAssetDO.setAssetShare(assetDO.getAssetShare());
  85. oldAssetDO.setAssetNet(assetDO.getAssetNet());
  86. assetDOList.add(oldAssetDO);
  87. assetMapper.batchUpdate(assetDOList);
  88. }
  89. }
  90. @Override
  91. public void saveFundNav(FundNavVO fundNavVO) {
  92. List<NavDO> navDOList = new ArrayList<>();
  93. NavDO navDO = new NavDO();
  94. navDO.setCumulativeNavWithdrawal(fundNavVO.getCumulativeNavWithdrawal());
  95. navDO.setNav(fundNavVO.getNav());
  96. navDO.setFundId(fundNavVO.getFundId());
  97. navDO.setNav(fundNavVO.getNav());
  98. navDO.setPriceDate(DateUtils.parse(fundNavVO.getPriceDate(),DateUtils.YYYY_MM_DD));
  99. navDO.setIsvalid(1);
  100. navDO.setCreatorId(UserUtils.getLoginUser().getUserId());
  101. navDO.setUpdaterId(UserUtils.getLoginUser().getUserId());
  102. navDO.setUpdateTime(DateUtils.getNowDate());
  103. NavDO oldNav = navMapper.queryFundNav(navDO);
  104. if(StringUtil.isNull(oldNav)){
  105. navDO.setCreateTime(DateUtils.getNowDate());
  106. navDOList.add(navDO);
  107. navMapper.batchInsert(navDOList);
  108. }else{
  109. oldNav.setUpdateTime(DateUtils.getNowDate());
  110. oldNav.setCumulativeNav(navDO.getCumulativeNav());
  111. oldNav.setCumulativeNavWithdrawal(navDO.getCumulativeNavWithdrawal());
  112. oldNav.setNav(navDO.getNav());
  113. navDOList.add(oldNav);
  114. navMapper.batchUpdate(navDOList);
  115. }
  116. }
  117. @Override
  118. public void deleteFundNavAsset(FundNavAssetDelListVO fundNavAssetDelListVO) {
  119. List<FundNavAssetDelVO> delVOList = fundNavAssetDelListVO.getFundNavAssetDelVOList();
  120. Integer userId = UserUtils.getLoginUser().getUserId();
  121. for(FundNavAssetDelVO delVO : delVOList){
  122. navMapper.deleteNav(delVO.getFundId(),delVO.getPriceDate(),userId);
  123. assetMapper.deleteAsset(delVO.getFundId(),delVO.getPriceDate(),userId);
  124. }
  125. }
  126. @Override
  127. public ResultVo uploadNavAsset(MultipartFile excelFile) {
  128. ResultVo vo = new ResultVo(ResultCode.SUCCESS);
  129. File file = null;
  130. try{
  131. InputStream inputStream = excelFile.getInputStream();
  132. file = new File(path+"/upload/"+ System.currentTimeMillis()+"/"+excelFile.getOriginalFilename());
  133. FileUtils.copyInputStreamToFile(inputStream,file);
  134. List<NavAssetExcelData> list = parseDistributionFile(file);
  135. vo.setData(parseResult(list));
  136. }catch (Exception e){
  137. logger.error(e.getMessage(),e);
  138. vo.setMsg("文件解析异常");
  139. vo.setData(false);
  140. return vo;
  141. }
  142. return vo;
  143. }
  144. private Map<String,Object> parseResult(List<NavAssetExcelData> list) {
  145. Map<String,Object> result = new HashMap<>();
  146. int startRow = 3;
  147. List<ExcelNavAssetSuccessDataVO> successDataVOList = new ArrayList<>();
  148. List<ExcelNavAssetFailDataVO> excelFailDataVOList = new ArrayList<>();
  149. for(int dataIdx=1;dataIdx<list.size(); dataIdx++){
  150. NavAssetExcelData excelData = list.get(dataIdx);
  151. try{
  152. if((StringUtil.isEmpty(excelData.getFundName()) && StringUtil.isEmpty(excelData.getFundId()))
  153. || StringUtil.isEmpty(excelData.getPriceDate())){
  154. ExcelNavAssetFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.REQUIRE_FIELD,dataIdx+startRow);
  155. excelFailDataVOList.add(failDataVO);
  156. continue;
  157. }
  158. if(StringUtil.isNotEmpty(excelData.getFundId())){
  159. String fundName = fundInfoMapper.getFundNameByFundId(excelData.getFundId());
  160. if(StringUtil.isEmpty(fundName)){
  161. ExcelNavAssetFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.NOT_MAPPING_FUND,dataIdx+startRow);
  162. excelFailDataVOList.add(failDataVO);
  163. continue;
  164. }
  165. }else{
  166. String fundId = fundInfoMapper.queryFundIdByName(excelData.getFundName());
  167. if(StringUtil.isEmpty(fundId)){
  168. ExcelNavAssetFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.NOT_MAPPING_FUND,dataIdx+startRow);
  169. excelFailDataVOList.add(failDataVO);
  170. continue;
  171. }else{
  172. excelData.setFundId(fundId);
  173. }
  174. }
  175. if(StringUtil.isEmpty(excelData.getNav()) || Double.valueOf(excelData.getNav()) < 0){
  176. ExcelNavAssetFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.ERROR_NAV,dataIdx+startRow);
  177. excelFailDataVOList.add(failDataVO);
  178. continue;
  179. }
  180. if(StringUtil.isEmpty(excelData.getCumulativeNavWithdrawal()) || Double.valueOf(excelData.getCumulativeNavWithdrawal()) < 0){
  181. ExcelNavAssetFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.ERROR_NAV_WITHDRAWAL,dataIdx+startRow);
  182. excelFailDataVOList.add(failDataVO);
  183. continue;
  184. }
  185. //开始处理成功数据
  186. FundNavVO fundNavVO = new FundNavVO();
  187. fundNavVO.setFundId(excelData.getFundId());
  188. fundNavVO.setPriceDate(excelData.getPriceDate());
  189. fundNavVO.setNav(BigDecimal.valueOf(Double.valueOf(excelData.getNav())));
  190. fundNavVO.setCumulativeNavWithdrawal(BigDecimal.valueOf(Double.valueOf(excelData.getCumulativeNavWithdrawal())));
  191. saveFundNav(fundNavVO);
  192. if(StringUtil.isEmpty(excelData.getAssetNet()) && StringUtil.isEmpty(excelData.getAssetShare())){
  193. continue;
  194. }
  195. FundAssetVO fundAssetVO = new FundAssetVO();
  196. fundAssetVO.setFundId(excelData.getFundId());
  197. fundAssetVO.setPriceDate(excelData.getPriceDate());
  198. fundAssetVO.setAssetNet(StringUtil.isEmpty(excelData.getAssetNet())?null:BigDecimal.valueOf(Double.valueOf(excelData.getAssetNet())));
  199. fundAssetVO.setAssetShare(StringUtil.isEmpty(excelData.getAssetShare())?null:BigDecimal.valueOf(Double.valueOf(excelData.getAssetShare())));
  200. saveFundAsset(fundAssetVO);
  201. //处理成功结果
  202. ExcelNavAssetSuccessDataVO excelNavAssetSuccessDataVO = new ExcelNavAssetSuccessDataVO();
  203. excelNavAssetSuccessDataVO.setFundName(excelData.getFundName());
  204. excelNavAssetSuccessDataVO.setPriceDate(excelData.getPriceDate());
  205. excelNavAssetSuccessDataVO.setNav(excelData.getNav());
  206. excelNavAssetSuccessDataVO.setCumulativeNavWithdrawal(excelData.getCumulativeNavWithdrawal());
  207. excelNavAssetSuccessDataVO.setAssetNet(excelData.getAssetNet());
  208. excelNavAssetSuccessDataVO.setAssetShare(excelData.getAssetShare());
  209. successDataVOList.add(excelNavAssetSuccessDataVO);
  210. }catch (Exception e){
  211. logger.error(e.getMessage(),e);
  212. ExcelNavAssetFailDataVO failDataVO = toExcelFailDataVO(excelData,e.getMessage(),dataIdx+startRow);
  213. excelFailDataVOList.add(failDataVO);
  214. }
  215. }
  216. result.put("success",successDataVOList);
  217. result.put("fail",excelFailDataVOList);
  218. return result;
  219. }
  220. private ExcelNavAssetFailDataVO toExcelFailDataVO(NavAssetExcelData excelData, String msg, int runNum) {
  221. ExcelNavAssetFailDataVO failDataVO = new ExcelNavAssetFailDataVO();
  222. failDataVO.setFailReason(msg);
  223. failDataVO.setRowNum(runNum);
  224. failDataVO.setFundName(excelData.getFundName());
  225. failDataVO.setPriceDate(excelData.getPriceDate());
  226. failDataVO.setNav(excelData.getNav());
  227. failDataVO.setCumulativeNavWithdrawal(excelData.getCumulativeNavWithdrawal());
  228. return failDataVO;
  229. }
  230. private List<NavAssetExcelData> parseDistributionFile(File file) {
  231. // 创建一个 list 存储每行的数据,即 ExcelData 对象
  232. List<NavAssetExcelData> list = new ArrayList<>();
  233. // 直接使用 EasyExcel 的 read 方法,同时定义表头的类型,以便将列中数据映射为 ExcelData 对象
  234. EasyExcel.read(file, NavAssetExcelData.class, new PageReadListener<NavAssetExcelData>(dataList -> {
  235. // 并且每行数据,并将其 add 至 list 中
  236. for (NavAssetExcelData excelData : dataList) {
  237. if (excelData != null) {
  238. list.add(excelData);
  239. }
  240. }
  241. })).excelType(ExcelTypeEnum.XLSX).sheet().doRead();
  242. return list;
  243. }
  244. }