@ -26,6 +26,7 @@
package com.yxt.anrui.base.biz.basemodelmodprice ;
import cn.hutool.core.bean.BeanUtil ;
import cn.hutool.core.text.StrBuilder ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONObject ;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper ;
@ -64,10 +65,21 @@ import com.yxt.messagecenter.api.message.MessageFeign;
import com.yxt.messagecenter.api.message.MessageFlowVo ;
import com.yxt.messagecenter.api.message.MessageFlowableQuery ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.poi.hssf.usermodel.HSSFWorkbook ;
import org.apache.poi.ss.usermodel.Cell ;
import org.apache.poi.ss.usermodel.Row ;
import org.apache.poi.ss.usermodel.Sheet ;
import org.apache.poi.ss.usermodel.Workbook ;
import org.apache.poi.xssf.usermodel.XSSFWorkbook ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.web.multipart.MultipartFile ;
import java.io.IOException ;
import java.io.InputStream ;
import java.math.BigDecimal ;
import java.text.ParseException ;
import java.text.SimpleDateFormat ;
import java.util.* ;
import java.util.stream.Collectors ;
@ -519,4 +531,205 @@ public class BaseModelModpriceService extends MybatisBaseService<BaseModelModpri
}
return rb . setMsg ( "操作失败!提交的数据不一致!" ) ;
}
public ResultBean < BaseModelModpriceImportReturn > getExcelInfo ( String fileName , MultipartFile file ) throws IOException , ParseException {
ResultBean rb = ResultBean . fireFail ( ) ;
if ( ! fileName . matches ( "^.+\\.(?i)(xls)$" ) & & ! fileName . matches ( "^.+\\.(?i)(xlsx)$" ) ) {
return rb . fail ( ) . setMsg ( "上传文件不正确" ) ;
}
int [ ] resultCell = new int [ ] { 0 , 1 , 2 , 3 , 4 } ;
List < BaseModelModpriceImportVo > resultList = new ArrayList < > ( ) ;
boolean isExcel2003 = true ;
if ( fileName . matches ( "^.+\\.(?i)(xlsx)$" ) ) {
isExcel2003 = false ;
}
InputStream is = file . getInputStream ( ) ;
Workbook wb = null ;
if ( isExcel2003 ) {
wb = new HSSFWorkbook ( is ) ;
} else {
wb = new XSSFWorkbook ( is ) ;
}
Sheet sheet = wb . getSheetAt ( 0 ) ;
BaseModelModpriceImportReturn importReturn = getSheetVal ( sheet , resultCell ) ;
resultList = importReturn . getImportVoList ( ) ;
System . out . println ( "结果是--->" + resultList ) ;
importReturn . setImportVoList ( resultList ) ;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( "yyyy-MM-dd HH:mm" ) ;
String currentTime = simpleDateFormat . format ( System . currentTimeMillis ( ) ) ;
if ( null ! = resultList ) {
int size = resultList . size ( ) ;
importReturn . setCheckResult ( currentTime + " " + "上报成功" + String . valueOf ( size ) + "条记录。" ) ;
return rb . success ( ) . setData ( importReturn ) ;
} else {
return rb . fail ( ) . setData ( importReturn ) ;
}
}
public BaseModelModpriceImportReturn getSheetVal ( Sheet sheet , int [ ] resultCell ) throws ParseException {
BaseModelModpriceImportReturn importReturn = new BaseModelModpriceImportReturn ( ) ;
List < BaseModelModpriceImportVo > importVoList = new ArrayList < > ( ) ;
int [ ] resultIndex = new int [ resultCell . length ] ;
BaseModelModpriceImportVo importVo ;
StringBuilder sb = new StringBuilder ( ) ;
ArrayList < String > rowList = new ArrayList < > ( ) ;
for ( int r = 1 ; r < = sheet . getLastRowNum ( ) ; r + + ) {
Row row = sheet . getRow ( r ) ;
if ( row = = null ) {
continue ;
}
importVo = new BaseModelModpriceImportVo ( ) ;
for ( int i = 0 ; i < row . getPhysicalNumberOfCells ( ) ; i + + ) {
String trim = new String ( ) ;
try {
trim = getCellVal ( row . getCell ( i ) ) . toString ( ) . trim ( ) ;
String temp = getCellVal ( row . getCell ( i ) ) . toString ( ) . trim ( ) ;
for ( int j = 0 ; j < resultCell . length ; j + + ) {
if ( i = = resultCell [ j ] ) {
switch ( i ) {
case 0 :
importVo . setRankNo ( temp ) ;
rowList . add ( temp ) ;
break ;
case 1 :
importVo . setVehicleTypeCode ( temp ) ;
break ;
case 2 :
importVo . setConfigCode ( temp ) ;
break ;
case 3 :
importVo . setGuidedPrice ( temp ) ;
break ;
case 4 :
importVo . setManufactorSettlementPrice ( temp ) ;
break ;
default :
break ;
}
} else {
continue ;
}
}
} catch ( Exception e ) {
if ( StringUtils . isBlank ( trim ) ) {
String word = new String ( ) ;
switch ( i ) {
case 0 :
word = "序号" ;
break ;
case 1 :
word = "车型编码" ;
break ;
case 2 :
word = "配置编码" ;
break ;
case 3 :
word = "调整后全款销售指导价(元)" ;
break ;
case 4 :
word = "调整后贷款销售指导价(元)" ;
break ;
default :
break ;
}
String checkResult = "第" + ( r ) + "行" + word + "为空" ;
sb . append ( checkResult ) . append ( ";" ) ;
continue ;
}
}
}
importVoList . add ( importVo ) ;
}
HashSet < String > hashSet = new HashSet < > ( ) ;
HashMap < String , String > map = new HashMap < > ( ) ;
for ( BaseModelModpriceImportVo modpriceImportVo : importVoList ) {
if ( StringUtils . isNotBlank ( modpriceImportVo . getVehicleTypeCode ( ) )
& & StringUtils . isNotBlank ( modpriceImportVo . getConfigCode ( ) )
& & StringUtils . isNotBlank ( modpriceImportVo . getGuidedPrice ( ) )
& & StringUtils . isNotBlank ( modpriceImportVo . getManufactorSettlementPrice ( ) )
) {
//如果车型编码+配置编码存在,则判断金额是否相等
if ( map . containsKey ( modpriceImportVo . getVehicleTypeCode ( ) + modpriceImportVo . getConfigCode ( ) ) ) {
String guidedAndManuFactor = map . get ( modpriceImportVo . getVehicleTypeCode ( ) + modpriceImportVo . getConfigCode ( ) ) ;
String [ ] split = guidedAndManuFactor . split ( "," ) ;
String oldGuided = split [ 0 ] ;
String oldManuFactor = split [ 1 ] ;
if ( ! oldGuided . equals ( modpriceImportVo . getGuidedPrice ( ) ) | | ! oldManuFactor . equals ( modpriceImportVo . getManufactorSettlementPrice ( ) ) ) {
hashSet . add ( modpriceImportVo . getVehicleTypeCode ( ) + modpriceImportVo . getConfigCode ( ) ) ;
}
} else { //如果车型编码+配置编码不存在,则存入map
map . put ( modpriceImportVo . getVehicleTypeCode ( ) + modpriceImportVo . getConfigCode ( ) , modpriceImportVo . getGuidedPrice ( ) + "," + modpriceImportVo . getManufactorSettlementPrice ( ) ) ;
}
}
}
Iterator < String > iterator = hashSet . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
String next = iterator . next ( ) ;
sb . append ( "第" ) ;
for ( BaseModelModpriceImportVo modpriceImportVo : importVoList ) {
if ( StringUtils . isNotBlank ( modpriceImportVo . getVehicleTypeCode ( ) )
& & StringUtils . isNotBlank ( modpriceImportVo . getConfigCode ( ) )
& & StringUtils . isNotBlank ( modpriceImportVo . getGuidedPrice ( ) )
& & StringUtils . isNotBlank ( modpriceImportVo . getManufactorSettlementPrice ( ) )
) {
if ( ( modpriceImportVo . getVehicleTypeCode ( ) + modpriceImportVo . getConfigCode ( ) ) . equals ( next ) ) {
sb . append ( modpriceImportVo . getRankNo ( ) + "行、" ) ;
}
}
}
sb . delete ( sb . length ( ) - 1 , sb . length ( ) ) ;
sb . append ( "相同车型编码、配置编码对应的销售指导价不同;" ) ;
}
HashSet < String > rowSet = new HashSet < > ( rowList ) ;
if ( rowSet . size ( ) ! = rowList . size ( ) ) {
StringBuilder sbd = new StringBuilder ( ) ;
Set < String > set = new HashSet < > ( ) ;
Set < String > duplicateElements = new HashSet < > ( ) ;
for ( String rankNo : rowList ) {
if ( ! set . add ( rankNo ) ) {
duplicateElements . add ( rankNo ) ;
}
}
for ( String duplicateElement : duplicateElements ) {
sbd . append ( duplicateElement ) . append ( "、" ) ;
}
sbd . delete ( sbd . length ( ) - 1 , sbd . length ( ) ) ;
sb . append ( sbd . toString ( ) + "序号有重复" ) . append ( ";" ) ;
}
importReturn . setImportVoList ( importVoList ) ;
if ( StringUtils . isNotBlank ( sb . toString ( ) ) ) {
sb . delete ( sb . length ( ) - 1 , sb . length ( ) ) ;
importReturn . setCheckResult ( sb . toString ( ) ) ;
importReturn . setImportVoList ( null ) ;
return importReturn ;
}
return importReturn ;
}
public Object getCellVal ( Cell cell ) {
Object obj = null ;
if ( cell ! = null ) {
switch ( cell . getCellTypeEnum ( ) ) {
case BOOLEAN :
obj = cell . getBooleanCellValue ( ) ;
break ;
case ERROR :
obj = cell . getErrorCellValue ( ) ;
break ;
case NUMERIC :
obj = cell . getNumericCellValue ( ) ;
break ;
case STRING :
obj = cell . getStringCellValue ( ) ;
break ;
default :
break ;
}
}
return obj ;
}
}