|
@@ -0,0 +1,243 @@
|
|
|
+package com.simuwang.manage.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.read.listener.PageReadListener;
|
|
|
+import com.alibaba.excel.support.ExcelTypeEnum;
|
|
|
+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.StringUtil;
|
|
|
+import com.simuwang.base.mapper.daq.ProductInformationMapper;
|
|
|
+import com.simuwang.base.pojo.dos.FundAliasDO;
|
|
|
+import com.simuwang.base.pojo.dos.ProductInformationDO;
|
|
|
+import com.simuwang.base.pojo.dto.*;
|
|
|
+import com.simuwang.base.pojo.dto.query.ProductPageQuery;
|
|
|
+import com.simuwang.base.pojo.vo.*;
|
|
|
+import com.simuwang.manage.service.ProductService;
|
|
|
+import com.simuwang.shiro.utils.UserUtils;
|
|
|
+import com.smppw.common.pojo.ResultVo;
|
|
|
+import com.smppw.common.pojo.enums.status.ResultCode;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+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.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ProductServiceImpl implements ProductService {
|
|
|
+ @Autowired
|
|
|
+ private ProductInformationMapper productInformationMapper;
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
|
|
|
+ @Value("${email.file.path}")
|
|
|
+ private String path;
|
|
|
+ @Override
|
|
|
+ public MybatisPage<ProductInformationVO> searchProductList(ProductPageQuery productPageQuery) {
|
|
|
+ List<ProductInformationDO> productInformationDOList = productInformationMapper.searchProductList(productPageQuery);
|
|
|
+ List<ProductInformationVO> productInformationVOList = productInformationDOList.stream().map(ProductInformationDO::toVO).collect(Collectors.toList());
|
|
|
+ long total = productInformationMapper.countProductList(productPageQuery);
|
|
|
+ return MybatisPage.of(total,productInformationVOList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResultVo uploadProduct(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);
|
|
|
+ ProductData productData = parseProductFile(file);
|
|
|
+ vo.setData(parseResult(productData));
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.error(e.getMessage(),e);
|
|
|
+ vo.setMsg("文件解析异常");
|
|
|
+ vo.setData(false);
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ProductUploadResult parseResult(ProductData productData) {
|
|
|
+ ProductUploadResult result = new ProductUploadResult();
|
|
|
+ int productStartRow = 8;
|
|
|
+ List<ProductExcelSuccessDataVO> successDataVOList = new ArrayList<>();
|
|
|
+ List<ProductExcelFailDataVO> excelFailDataVOList = new ArrayList<>();
|
|
|
+ List<ProductInformationData> productInformationDataList = productData.getProductInformationDataList();
|
|
|
+ saveProductInformation(productInformationDataList,successDataVOList,excelFailDataVOList,productStartRow);
|
|
|
+ result.setSuccess(successDataVOList);
|
|
|
+ result.setFail(excelFailDataVOList);
|
|
|
+ result.setTotal(successDataVOList.size()+excelFailDataVOList.size());
|
|
|
+ result.setFailCount(excelFailDataVOList.size());
|
|
|
+ result.setSuccessCount(successDataVOList.size());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveProductInformation(List<ProductInformationData> productInformationDataList, List<ProductExcelSuccessDataVO> successDataVOList, List<ProductExcelFailDataVO> excelFailDataVOList, int productStartRow) {
|
|
|
+ for (int idx =0;idx< productInformationDataList.size();idx++) {
|
|
|
+ ProductInformationData productInformationData = productInformationDataList.get(idx);
|
|
|
+ if(StringUtil.isEmpty(productInformationData.getId())){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(!isNumber(productInformationData.getId())){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(StringUtil.isEmpty(productInformationData.getRegisterNumber()) && StringUtil.isEmpty(productInformationData.getProductName())){
|
|
|
+ ProductExcelFailDataVO failDataVO = toExcelFailDataVO(ExcelConst.PRODUCT_SHEET_NAME,ExcelConst.DELETION_NAME_REGISTERENUMBER,idx+productStartRow);
|
|
|
+ excelFailDataVOList.add(failDataVO);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ Date date = DateUtils.parse(productInformationData.getInceptionDate(),DateUtils.YYYY_MM_DD);
|
|
|
+ if(date.after(new Date())){
|
|
|
+ ProductExcelFailDataVO failDataVO = toExcelFailDataVO(ExcelConst.PRODUCT_SHEET_NAME,ExcelConst.ERROR_INCEPTION_DATE,idx+productStartRow);
|
|
|
+ excelFailDataVOList.add(failDataVO);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ ProductExcelFailDataVO failDataVO = toExcelFailDataVO(ExcelConst.PRODUCT_SHEET_NAME,ExcelConst.ERROR_DATE_FORMAT,idx+productStartRow);
|
|
|
+ excelFailDataVOList.add(failDataVO);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ProductInformationDO productInformationDO = toProductDO(productInformationData);
|
|
|
+ ProductInformationDO oldProductInformationDO = productInformationMapper.selectByNameAndRegisterNumber(productInformationDO);
|
|
|
+ if(oldProductInformationDO != null){
|
|
|
+ productInformationDO.setId(oldProductInformationDO.getId());
|
|
|
+ }else{
|
|
|
+ productInformationDO.setCreatetime(new Date());
|
|
|
+ productInformationDO.setCreatorid(UserUtils.getLoginUser().getUserId());
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ productInformationMapper.insertOrUpdate(productInformationDO);
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.error(e.getMessage(),e);
|
|
|
+ ProductExcelFailDataVO failDataVO = toExcelFailDataVO(ExcelConst.PRODUCT_SHEET_NAME,ExcelConst.SAVE_FAIL,idx+productStartRow);
|
|
|
+ excelFailDataVOList.add(failDataVO);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ProductExcelSuccessDataVO successDataVO = new ProductExcelSuccessDataVO();
|
|
|
+ successDataVO.setRowNum(idx+productStartRow);
|
|
|
+ successDataVO.setSheet(ExcelConst.PRODUCT_SHEET_NAME);
|
|
|
+ successDataVO.setParseStatus(ExcelConst.SUCCESS);
|
|
|
+ successDataVOList.add(successDataVO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private ProductInformationDO toProductDO(ProductInformationData productInformationData) {
|
|
|
+ ProductInformationDO productInformationDO = new ProductInformationDO();
|
|
|
+ productInformationDO.setProductName(productInformationData.getProductName());
|
|
|
+ productInformationDO.setRegisterNumber(productInformationData.getRegisterNumber());
|
|
|
+ productInformationDO.setAssetFrequency(productInformationData.getAssetFrequency());
|
|
|
+ productInformationDO.setNavFrequency(productInformationData.getNavFrequency());
|
|
|
+ productInformationDO.setFirstStrategy(productInformationData.getFirstStrategy());
|
|
|
+ productInformationDO.setSecondStrategy(productInformationData.getSecondStrategy());
|
|
|
+ productInformationDO.setThirdStrategy(productInformationData.getThirdStrategy());
|
|
|
+ productInformationDO.setInceptionDate(productInformationData.getInceptionDate());
|
|
|
+ productInformationDO.setInvestmentManager(productInformationData.getInvestmentManager());
|
|
|
+ productInformationDO.setInvestmentStrategyDesc(productInformationData.getInvestmentStrategyDesc());
|
|
|
+ productInformationDO.setReportFrequency(productInformationData.getReportFrequency());
|
|
|
+ productInformationDO.setRemark(productInformationData.getRemark());
|
|
|
+ productInformationDO.setMsLabel(productInformationDO.getMsLabel());
|
|
|
+ productInformationDO.setStartDate(productInformationData.getStartDate());
|
|
|
+ productInformationDO.setLiquidateDate(productInformationData.getLiquidateDate());
|
|
|
+ productInformationDO.setIsStruct(productInformationData.getIsStruct());
|
|
|
+ productInformationDO.setUpdatetime(new Date());
|
|
|
+ productInformationDO.setUpdaterid(UserUtils.getLoginUser().getUserId());
|
|
|
+ productInformationDO.setIsvalid(1);
|
|
|
+ return productInformationDO;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isNumber(String str) {
|
|
|
+ if (StringUtils.isEmpty(str)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if ("%".equals(str)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ str = str.replace(",", "").replace(",", "");
|
|
|
+ //容许没有整数部分 eg:.2
|
|
|
+ String regex = "^-?(\\d+)?(\\.)?\\d+%?$";
|
|
|
+
|
|
|
+ boolean judge = str.matches(regex);
|
|
|
+ // 非常规数值,可能为科学计数法
|
|
|
+ if (!judge) {
|
|
|
+ try {
|
|
|
+ String plainString = new BigDecimal(str).toPlainString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ProductExcelFailDataVO toExcelFailDataVO(String sheetName, String msg,Integer rowNum) {
|
|
|
+ ProductExcelFailDataVO failDataVO = new ProductExcelFailDataVO();
|
|
|
+ failDataVO.setFailReason(msg);
|
|
|
+ failDataVO.setRowNum(rowNum);
|
|
|
+ failDataVO.setSheet(sheetName);
|
|
|
+ return failDataVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ProductData parseProductFile(File file) {
|
|
|
+ // 创建一个 list 存储每行的数据,即 ExcelData 对象
|
|
|
+ List<ProductInformationData> productInformationDataList = new ArrayList<>();
|
|
|
+ // 直接使用 EasyExcel 的 read 方法,同时定义表头的类型,以便将列中数据映射为 ExcelData 对象
|
|
|
+ EasyExcel.read(file, ProductInformationData.class, new PageReadListener<ProductInformationData>(dataList -> {
|
|
|
+ // 并且每行数据,并将其 add 至 list 中
|
|
|
+ for (ProductInformationData excelData : dataList) {
|
|
|
+ if (excelData != null) {
|
|
|
+ productInformationDataList.add(excelData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })).excelType(ExcelTypeEnum.XLSX).sheet(0).headRowNumber(8).doRead();
|
|
|
+
|
|
|
+ List<ProductContractData> productContractDataList = new ArrayList<>();
|
|
|
+ EasyExcel.read(file, ProductContractData.class, new PageReadListener<ProductContractData>(dataList -> {
|
|
|
+ // 并且每行数据,并将其 add 至 list 中
|
|
|
+ for (ProductContractData excelData : dataList) {
|
|
|
+ if (excelData != null) {
|
|
|
+ productContractDataList.add(excelData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })).excelType(ExcelTypeEnum.XLSX).sheet(1).headRowNumber(8).doRead();
|
|
|
+
|
|
|
+ List<ProductDerivativeData> productDerivativeDataList = new ArrayList<>();
|
|
|
+ EasyExcel.read(file, ProductDerivativeData.class, new PageReadListener<ProductDerivativeData>(dataList -> {
|
|
|
+ // 并且每行数据,并将其 add 至 list 中
|
|
|
+ for (ProductDerivativeData excelData : dataList) {
|
|
|
+ if (excelData != null) {
|
|
|
+ productDerivativeDataList.add(excelData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })).excelType(ExcelTypeEnum.XLSX).sheet(2).headRowNumber(8).doRead();
|
|
|
+
|
|
|
+ List<InvestmentManagerData> investmentManagerDataList = new ArrayList<>();
|
|
|
+ EasyExcel.read(file, InvestmentManagerData.class, new PageReadListener<InvestmentManagerData>(dataList -> {
|
|
|
+ // 并且每行数据,并将其 add 至 list 中
|
|
|
+ for (InvestmentManagerData excelData : dataList) {
|
|
|
+ if (excelData != null) {
|
|
|
+ investmentManagerDataList.add(excelData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })).excelType(ExcelTypeEnum.XLSX).sheet(3).headRowNumber(3).doRead();
|
|
|
+ ProductData productData = new ProductData();
|
|
|
+ productData.setProductInformationDataList(productInformationDataList);
|
|
|
+ productData.setProductContractDataList(productContractDataList);
|
|
|
+ productData.setProductDerivativeDataList(productDerivativeDataList);
|
|
|
+ productData.setInvestmentManagerDataList(investmentManagerDataList);
|
|
|
+ return productData;
|
|
|
+ }
|
|
|
+}
|