123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package com.simuwang.manage.service.impl;
- import cn.hutool.core.date.DateUtil;
- import com.alibaba.excel.EasyExcel;
- import com.alibaba.excel.read.listener.PageReadListener;
- import com.alibaba.excel.support.ExcelTypeEnum;
- import com.simuwang.base.common.conts.DateConst;
- import com.simuwang.base.common.conts.ExcelConst;
- import com.simuwang.base.common.enums.DistributeType;
- import com.simuwang.base.common.support.MybatisPage;
- import com.simuwang.base.common.util.DateUtils;
- import com.simuwang.base.common.util.ExcelUtil;
- import com.simuwang.base.common.util.FileUtil;
- import com.simuwang.base.common.util.StringUtil;
- import com.simuwang.base.mapper.DistributionMapper;
- import com.simuwang.base.mapper.FundInfoMapper;
- import com.simuwang.base.mapper.NavMapper;
- import com.simuwang.base.pojo.dos.DistributionDO;
- import com.simuwang.base.pojo.dos.DistributionTablePageDO;
- import com.simuwang.base.pojo.dos.FundInfoDO;
- import com.simuwang.base.pojo.dos.NavDO;
- import com.simuwang.base.pojo.dto.DistributionExcelData;
- import com.simuwang.base.pojo.dto.query.DistributionPageQuery;
- import com.simuwang.base.pojo.vo.*;
- import com.simuwang.manage.service.DistributionService;
- import com.simuwang.shiro.utils.UserUtils;
- import com.smppw.common.pojo.ResultVo;
- import com.smppw.common.pojo.enums.status.ResultCode;
- import com.smppw.common.pojo.enums.status.StatusCode;
- import org.apache.commons.io.FileUtils;
- import org.apache.poi.ss.formula.functions.Na;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.File;
- import java.io.InputStream;
- import java.math.BigDecimal;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * FileName: DistributionServiceImpl
- * Author: chenjianhua
- * Date: 2024/9/15 14:07
- * Description: ${DESCRIPTION}
- */
- @Service
- public class DistributionServiceImpl implements DistributionService {
- private static final Logger logger = LoggerFactory.getLogger(DistributionServiceImpl.class);
- @Autowired
- private DistributionMapper distributionMapper;
- @Autowired
- private NavMapper navMapper;
- @Autowired
- private FundInfoMapper fundInfoMapper;
- @Value("${email.file.path}")
- private String path;
- @Override
- public MybatisPage<DistributionTablePageVO> searchDistributionList(DistributionPageQuery distributionPageQuery) {
- List<DistributionTablePageDO> distributionTablePageDOList = distributionMapper.searchDistributionList(distributionPageQuery);
- List<DistributionTablePageVO> distributionTablePageVOList = distributionTablePageDOList.stream().map(DistributionTablePageDO::toVo).collect(Collectors.toList());
- long total = distributionMapper.countDistributionList(distributionPageQuery);
- return MybatisPage.of(total,distributionTablePageVOList);
- }
- @Override
- public ResultVo saveDistribution(DistributionVO distributionVO) {
- ResultVo vo = new ResultVo(true);
- //判断是否存在当天净值
- NavDO navDO = new NavDO();
- navDO.setFundId(distributionVO.getFundId());
- navDO.setNav(distributionVO.getNav());
- navDO.setCreatorId(UserUtils.getLoginUser().getUserId());
- navDO.setUpdaterId(UserUtils.getLoginUser().getUserId());
- navDO.setCumulativeNavWithdrawal(distributionVO.getCumulativeNavWithdrawal());
- navDO.setPriceDate(DateUtils.parse(distributionVO.getDistributeDate(),DateUtils.YYYY_MM_DD));
- if(StringUtil.isNull(distributionVO.getNav()) || StringUtil.isNull(distributionVO.getCumulativeNavWithdrawal())){
- NavDO oldNav = navMapper.queryFundNav(navDO);
- if(StringUtil.isNull(oldNav)){
- vo.setData(false);
- vo.setMsg("当天不存在净值数据,请补充");
- return vo;
- }
- }
- DistributionDO distributionDO = new DistributionDO();
- distributionDO.setFundId(distributionVO.getFundId());
- distributionDO.setDistribution(distributionVO.getDistribution());
- distributionDO.setDistributeDate(DateUtils.parse(distributionVO.getDistributeDate(),DateUtils.YYYY_MM_DD));
- distributionDO.setDistributeType(distributionVO.getDistributeType());
- distributionDO.setIsvalid(1);
- distributionDO.setCreatorId(UserUtils.getLoginUser().getUserId());
- distributionDO.setUpdaterId(UserUtils.getLoginUser().getUserId());
- distributionDO.setUpdateTime(DateUtils.getNowDate());
- DistributionDO oldDistributionDO = distributionMapper.selectDistributionByDate(distributionVO.getFundId(),DateUtils.parse(distributionVO.getDistributeDate(),DateUtils.YYYY_MM_DD));
- if(!StringUtil.isNull(oldDistributionDO)){
- if(distributionVO.getId() != null && !oldDistributionDO.getId().equals(distributionVO.getId())){
- vo.setData(false);
- vo.setMsg("同一基金在同一天不能已存在分红数据");
- return vo;
- }
- distributionDO.setId(oldDistributionDO.getId());
- distributionMapper.updateDistributionById(distributionDO);
- }else{
- distributionDO.setCreateTime(DateUtils.getNowDate());
- distributionMapper.saveDistribution(distributionDO);
- }
- if(distributionVO.getNav() == null){
- return vo;
- }
- //保存净值
- NavDO oldNav = navMapper.queryFundNav(navDO);
- if(StringUtil.isNull(oldNav)){
- navDO.setUpdateTime(DateUtils.getNowDate());
- navDO.setCreateTime(DateUtils.getNowDate());
- navDO.setIsvalid(1);
- navMapper.saveNav(navDO);
- }else{
- navDO.setId(oldNav.getId());
- navDO.setUpdateTime(DateUtils.getNowDate());
- navMapper.updateNav(navDO);
- }
- return vo;
- }
- @Override
- public void deleteDistribution(IdListVO idListVO) {
- Integer userId = UserUtils.getLoginUser().getUserId();
- distributionMapper.deleteDistribution(idListVO.getIdList(),userId);
- }
- @Override
- public ResultVo uploadDistribution(MultipartFile excelFile) {
- ResultVo vo = new ResultVo(ResultCode.SUCCESS);
- File file = null;
- try{
- InputStream inputStream = excelFile.getInputStream();
- file = new File(path+"/upload/"+ System.currentTimeMillis()+"/"+excelFile.getOriginalFilename());
- FileUtils.copyInputStreamToFile(inputStream,file);
- List<DistributionExcelData> list = parseDistributionFile(file);
- vo.setData(parseResult(list));
- }catch (Exception e){
- logger.error(e.getMessage(),e);
- vo.setMsg("文件解析异常");
- vo.setData(false);
- return vo;
- }
- return vo;
- }
- private DistributionUploadResult parseResult(List<DistributionExcelData> list) {
- DistributionUploadResult result = new DistributionUploadResult();
- int startRow = 3;
- List<ExcelSuccessDataVO> successDataVOList = new ArrayList<>();
- List<ExcelFailDataVO> excelFailDataVOList = new ArrayList<>();
- for(int dataIdx=1;dataIdx<list.size(); dataIdx++){
- DistributionExcelData excelData = list.get(dataIdx);
- try{
- if((StringUtil.isEmpty(excelData.getFundName()) && StringUtil.isEmpty(excelData.getFundId()))
- || StringUtil.isEmpty(excelData.getPriceDate()) || StringUtil.isEmpty(excelData.getNav())
- || StringUtil.isEmpty(excelData.getDistributeType()) || StringUtil.isEmpty(excelData.getDistribution())
- || StringUtil.isEmpty(excelData.getCumulativeNavWithdrawal())){
- ExcelFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.REQUIRE_FIELD,dataIdx+startRow);
- excelFailDataVOList.add(failDataVO);
- continue;
- }
- if(excelData.getDistributeType().trim().equals(ExcelConst.CASH_DIVIDENDS)){
- if(Double.valueOf(excelData.getDistribution()) < 0f){
- ExcelFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.CASH_DIVIDENDS_FAIL,dataIdx+startRow);
- excelFailDataVOList.add(failDataVO);
- continue;
- }
- }else{
- if(Double.valueOf(excelData.getDistribution()) == 0f){
- ExcelFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.DIVIDENDS_SPLIT_FAIL,dataIdx+startRow);
- excelFailDataVOList.add(failDataVO);
- continue;
- }
- }
- if(StringUtil.isNotEmpty(excelData.getFundId())){
- String fundName = fundInfoMapper.getFundNameByFundId(excelData.getFundId());
- if(StringUtil.isEmpty(excelData.getFundName())){
- excelData.setFundName(fundName);
- }
- if(StringUtil.isEmpty(fundName)){
- ExcelFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.NOT_MAPPING_FUND,dataIdx+startRow);
- excelFailDataVOList.add(failDataVO);
- continue;
- }
- }else{
- String fundId = fundInfoMapper.queryFundIdByName(excelData.getFundName());
- if(StringUtil.isEmpty(fundId)){
- ExcelFailDataVO failDataVO = toExcelFailDataVO(excelData,ExcelConst.NOT_MAPPING_FUND,dataIdx+startRow);
- excelFailDataVOList.add(failDataVO);
- continue;
- }else{
- excelData.setFundId(fundId);
- }
- }
- //开始处理成功数据
- DistributionVO distributionVO = new DistributionVO();
- distributionVO.setFundId(excelData.getFundId());
- distributionVO.setDistributeDate(excelData.getPriceDate());
- distributionVO.setDistributeType(DistributeType.getDistributeTypeByInfo(excelData.getDistributeType()).getCode());
- distributionVO.setDistribution(BigDecimal.valueOf(Double.valueOf(excelData.getDistribution())));
- distributionVO.setNav(BigDecimal.valueOf(Double.valueOf(excelData.getNav())));
- distributionVO.setCumulativeNavWithdrawal(BigDecimal.valueOf(Double.valueOf(excelData.getCumulativeNavWithdrawal())));
- ResultVo vo = saveDistribution(distributionVO);
- if((boolean)vo.getData() == true){
- ExcelSuccessDataVO successDataVO = new ExcelSuccessDataVO();
- successDataVO.setDistribution(excelData.getDistribution());
- successDataVO.setFundName(excelData.getFundName());
- successDataVO.setPriceDate(excelData.getPriceDate());
- successDataVO.setDistributeType(excelData.getDistributeType());
- successDataVOList.add(successDataVO);
- }else{
- ExcelFailDataVO failDataVO = toExcelFailDataVO(excelData,vo.getMsg(),dataIdx+startRow);
- excelFailDataVOList.add(failDataVO);
- }
- }catch (Exception e){
- logger.error(e.getMessage(),e);
- ExcelFailDataVO failDataVO = toExcelFailDataVO(excelData,e.getMessage(),dataIdx+startRow);
- excelFailDataVOList.add(failDataVO);
- }
- }
- result.setSuccess(successDataVOList);
- result.setFail(excelFailDataVOList);
- result.setTotal(successDataVOList.size()+excelFailDataVOList.size());
- result.setFailCount(excelFailDataVOList.size());
- result.setSuccessCount(successDataVOList.size());
- return result;
- }
- private ExcelFailDataVO toExcelFailDataVO(DistributionExcelData excelData, String msg,Integer rowNum) {
- ExcelFailDataVO failDataVO = new ExcelFailDataVO();
- failDataVO.setFailReason(msg);
- failDataVO.setRowNum(rowNum);
- failDataVO.setDistribution(excelData.getDistribution());
- failDataVO.setFundName(excelData.getFundName());
- failDataVO.setPriceDate(excelData.getPriceDate());
- failDataVO.setDistributeType(excelData.getDistributeType());
- return failDataVO;
- }
- private List<DistributionExcelData> parseDistributionFile(File file) {
- // 创建一个 list 存储每行的数据,即 ExcelData 对象
- List<DistributionExcelData> list = new ArrayList<>();
- // 直接使用 EasyExcel 的 read 方法,同时定义表头的类型,以便将列中数据映射为 ExcelData 对象
- EasyExcel.read(file, DistributionExcelData.class, new PageReadListener<DistributionExcelData>(dataList -> {
- // 并且每行数据,并将其 add 至 list 中
- for (DistributionExcelData excelData : dataList) {
- if (excelData != null) {
- list.add(excelData);
- }
- }
- })).excelType(ExcelTypeEnum.XLSX).sheet().doRead();
- return list;
- }
- }
|